1   /*
2    * Copyright 2018 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.client.armeria;
17  
18  import java.util.List;
19  
20  import com.fasterxml.jackson.databind.JsonNode;
21  
22  import com.linecorp.armeria.client.Endpoint;
23  
24  /**
25   * Decodes the content of a file in Central Dogma into a list of {@link Endpoint}s.
26   *
27   * @param <T> the type of the file in Central Dogma
28   */
29  @FunctionalInterface
30  public interface EndpointListDecoder<T> {
31  
32      /**
33       * Default {@link EndpointListDecoder} implementation for {@link JsonNode}.
34       * Retrieved object must be a JSON array (which has format as {@code "[\"segment1\", \"segment2\"]"})
35       * Each segment represents an endpoint whose format is
36       * {@code <host>[:<port_number>[:weight]]}, such as:
37       * <ul>
38       * <li>{@code "foo.com"} - default port number, default weight (1000)</li>
39       * <li>{@code "bar.com:8080} - port number 8080, default weight (1000)</li>
40       * <li>{@code "10.0.2.15:0:500} - default port number, weight 500</li>
41       * <li>{@code "192.168.1.2:8443:700} - port number 8443, weight 700</li>
42       * </ul>
43       * Note that the port number must be specified when you want to specify the weight.
44       */
45      EndpointListDecoder<JsonNode> JSON = new JsonEndpointListDecoder();
46  
47      /**
48       * Default {@link EndpointListDecoder} implementation for {@link String}.
49       * Retrieved object must be a string which is a list of segments separated by a newline character.
50       * Each segment represents an endpoint whose format is
51       * {@code <host>[:<port_number>[:weight]]}, such as:
52       * <ul>
53       * <li>{@code "foo.com"} - default port number, default weight (1000)</li>
54       * <li>{@code "bar.com:8080} - port number 8080, default weight (1000)</li>
55       * <li>{@code "10.0.2.15:0:500} - default port number, weight 500</li>
56       * <li>{@code "192.168.1.2:8443:700} - port number 8443, weight 700</li>
57       * </ul>
58       * Note that the port number must be specified when you want to specify the weight.
59       */
60      EndpointListDecoder<String> TEXT = new TextEndpointListDecoder();
61  
62      /**
63       * Decodes an object into a set of {@link Endpoint}s.
64       *
65       * @param object an object retrieved from Central Dogma.
66       * @return the list of {@link Endpoint}s
67       */
68      List<Endpoint> decode(T object);
69  }