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  
17  package com.linecorp.centraldogma.client;
18  
19  import static com.google.common.base.MoreObjects.firstNonNull;
20  import static com.google.common.base.Preconditions.checkArgument;
21  import static java.util.Objects.requireNonNull;
22  
23  import java.util.Set;
24  
25  import javax.annotation.Nullable;
26  
27  import com.fasterxml.jackson.annotation.JsonCreator;
28  import com.fasterxml.jackson.annotation.JsonProperty;
29  import com.fasterxml.jackson.core.JsonProcessingException;
30  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
31  import com.google.common.collect.ImmutableSet;
32  
33  import com.linecorp.centraldogma.internal.Jackson;
34  
35  final class ClientProfile {
36  
37      private final String name;
38      private final int priority;
39      private final Set<Entry> hosts;
40  
41      @JsonCreator
42      ClientProfile(@JsonProperty(value = "name", required = true) String name,
43                    @JsonProperty("priority") @Nullable Integer priority,
44                    @JsonProperty("hosts") @JsonDeserialize(contentAs = Entry.class) @Nullable Set<Entry> hosts) {
45          this.name = requireNonNull(name, "name");
46          checkArgument(!name.isEmpty(), "name is empty.");
47          this.priority = firstNonNull(priority, 0);
48          this.hosts = ImmutableSet.copyOf(firstNonNull(hosts, ImmutableSet.of()));
49      }
50  
51      @JsonProperty
52      String name() {
53          return name;
54      }
55  
56      @JsonProperty
57      int priority() {
58          return priority;
59      }
60  
61      @JsonProperty
62      Set<Entry> hosts() {
63          return hosts;
64      }
65  
66      @Override
67      public String toString() {
68          try {
69              return Jackson.writeValueAsPrettyString(this);
70          } catch (JsonProcessingException e) {
71              // Should never reach here.
72              throw new Error(e);
73          }
74      }
75  
76      static final class Entry {
77          private final String host;
78          private final String protocol;
79          private final int port;
80  
81          @JsonCreator
82          Entry(@JsonProperty(value = "host", required = true) String host,
83                @JsonProperty(value = "protocol", required = true) String protocol,
84                @JsonProperty(value = "port", required = true) Integer port) {
85              this.host = requireNonNull(host, "host");
86              checkArgument(!host.isEmpty(), "hostname is empty.");
87              this.protocol = requireNonNull(protocol, "protocol");
88              checkArgument(!protocol.isEmpty(), "protocol is empty.");
89              this.port = requireNonNull(port, "port");
90              checkArgument(port > 0 && port < 65536, "port: %s (expected: 1..65535)", port);
91          }
92  
93          @JsonProperty
94          String host() {
95              return host;
96          }
97  
98          @JsonProperty
99          String protocol() {
100             return protocol;
101         }
102 
103         @JsonProperty
104         int port() {
105             return port;
106         }
107 
108         @Override
109         public String toString() {
110             try {
111                 return Jackson.writeValueAsPrettyString(this);
112             } catch (JsonProcessingException e) {
113                 // Should never reach here.
114                 throw new Error(e);
115             }
116         }
117     }
118 }