2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.msb.sdclient.wrapper.consul;
18 import java.net.MalformedURLException;
21 import javax.net.ssl.SSLContext;
22 import javax.ws.rs.client.Client;
23 import javax.ws.rs.client.ClientBuilder;
25 import org.onap.msb.sdclient.wrapper.consul.util.Jackson;
26 import org.onap.msb.sdclient.wrapper.consul.util.ObjectMapperContextResolver;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.datatype.guava.GuavaModule;
30 import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
31 import com.google.common.base.Optional;
32 import com.google.common.base.Predicate;
33 import com.google.common.collect.FluentIterable;
34 import com.google.common.net.HostAndPort;
37 * Client for interacting with the Consul HTTP API.
44 * Default Consul HTTP API host.
46 public static final String DEFAULT_HTTP_HOST = "localhost";
49 * Default Consul HTTP API port.
51 public static final int DEFAULT_HTTP_PORT = 8500;
57 private final HealthClient healthClient;
61 * Private constructor.
63 * @param url The full URL of a running Consul instance.
64 * @param builder JAX-RS client builder instance.
66 private Consul(String url, ClientBuilder builder, ObjectMapper mapper) {
68 if (!FluentIterable.from(builder.getConfiguration().getClasses())
69 .filter(new Predicate<Class<?>>() {
71 public boolean apply(final Class<?> clazz) {
72 return JacksonJaxbJsonProvider.class.isAssignableFrom(clazz);
74 }).first().isPresent()) {
75 builder.register(JacksonJaxbJsonProvider.class);
77 final Client client = builder
78 .register(new ObjectMapperContextResolver(mapper))
82 this.healthClient = new HealthClient(client.target(url).path("v1").path("health"));
86 // agentClient.ping();
90 * Creates a new client given a complete URL.
92 * @deprecated Use {@link Consul.Builder}
94 * @param url The Consul API URL.
95 * @param builder The JAX-RS client builder instance.
96 * @return A new client.
99 public static Consul newClient(String url, ClientBuilder builder, ObjectMapper mapper) {
100 return new Consul(url, builder, mapper);
104 * Creates a new client given a host and a port.
106 * @deprecated Use {@link Consul.Builder}
108 * @param host The Consul API hostname or IP.
109 * @param port The Consul port.
110 * @param builder The JAX-RS client builder instance.
111 * @return A new client.
114 public static Consul newClient(String host, int port, ClientBuilder builder, ObjectMapper mapper) {
116 return new Consul(new URL("http", host, port, "").toString(), builder, mapper);
117 } catch (MalformedURLException e) {
118 throw new ConsulException("Bad Consul URL", e);
123 * Creates a new client given a host and a port.
125 * @deprecated Use {@link Consul.Builder}
127 * @param host The Consul API hostname or IP.
128 * @param port The Consul port.
129 * @return A new client.
132 public static Consul newClient(String host, int port) {
133 return newClient(host, port, ClientBuilder.newBuilder(), Jackson.MAPPER);
137 * Creates a new client given a host and a port.
139 * @deprecated Use {@link Consul.Builder}
141 * @return A new client.
144 public static Consul newClient() {
145 return newClient(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT);
150 public HealthClient healthClient() {
154 * Creates a new {@link Builder} object.
156 * @return A new Consul builder.
158 public static Builder builder() {
159 return new Builder();
163 * Builder for {@link Consul} client objects.
165 public static class Builder {
167 private Optional<SSLContext> sslContext = Optional.absent();
168 private ObjectMapper objectMapper = Jackson.MAPPER;
169 private ClientBuilder clientBuilder = ClientBuilder.newBuilder();
173 url = new URL("http", "localhost", 8500, "");
174 } catch (MalformedURLException e) {
175 throw new RuntimeException(e);
180 * Constructs a new builder.
187 * Sets the URL from a {@link URL} object.
189 * @param url The Consul agent URL.
190 * @return The builder.
192 public Builder withUrl(URL url) {
199 * Sets the URL from a {@link HostAndPort} object.
201 * @param hostAndPort The Consul agent host and port.
202 * @return The builder.
204 public Builder withHostAndPort(HostAndPort hostAndPort) {
206 this.url = new URL("http", hostAndPort.getHostText(), hostAndPort.getPort(), "");
207 } catch (MalformedURLException e) {
208 throw new RuntimeException(e);
215 * Sets the URL from a string.
217 * @param url The Consul agent URL.
218 * @return The builder.
220 public Builder withUrl(String url) {
222 this.url = new URL(url);
223 } catch (MalformedURLException e) {
224 throw new RuntimeException(e);
231 * Sets the {@link SSLContext} for the client.
233 * @param sslContext The SSL context for HTTPS agents.
234 * @return The builder.
236 public Builder withSslContext(SSLContext sslContext) {
237 this.sslContext = Optional.of(sslContext);
243 * Sets the {@link ObjectMapper} for the client.
245 * @param objectMapper The {@link ObjectMapper} to use.
246 * @return The builder.
248 public Builder withObjectMapper(ObjectMapper objectMapper) {
249 this.objectMapper = objectMapper;
251 objectMapper.registerModule(new GuavaModule());
257 * Sets the JAX-RS {@link ClientBuilder} to use.
259 * @param clientBuilder The JAX-RS builder.
260 * @return This builder.
262 public Builder withClientBuilder(ClientBuilder clientBuilder) {
263 this.clientBuilder = clientBuilder;
269 * Constructs a new {@link Consul} client.
271 * @return A new Consul client.
273 public Consul build() {
274 if (this.sslContext.isPresent()) {
275 this.clientBuilder.sslContext(this.sslContext.get());
278 return new Consul(this.url.toExternalForm(), this.clientBuilder, this.objectMapper);