2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.msb.sdclient.wrapper.consul;
16 import java.net.MalformedURLException;
19 import javax.net.ssl.SSLContext;
20 import javax.ws.rs.client.Client;
21 import javax.ws.rs.client.ClientBuilder;
23 import org.onap.msb.sdclient.wrapper.consul.util.Jackson;
24 import org.onap.msb.sdclient.wrapper.consul.util.ObjectMapperContextResolver;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.fasterxml.jackson.datatype.guava.GuavaModule;
28 import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
29 import com.google.common.base.Optional;
30 import com.google.common.base.Predicate;
31 import com.google.common.collect.FluentIterable;
32 import com.google.common.net.HostAndPort;
35 * Client for interacting with the Consul HTTP API.
42 * Default Consul HTTP API host.
44 public static final String DEFAULT_HTTP_HOST = "localhost";
47 * Default Consul HTTP API port.
49 public static final int DEFAULT_HTTP_PORT = 8500;
53 private final HealthClient healthClient;
57 * Private constructor.
59 * @param url The full URL of a running Consul instance.
60 * @param builder JAX-RS client builder instance.
62 private Consul(String url, ClientBuilder builder, ObjectMapper mapper) {
64 if (!FluentIterable.from(builder.getConfiguration().getClasses()).filter(new Predicate<Class<?>>() {
66 public boolean apply(final Class<?> clazz) {
67 return JacksonJaxbJsonProvider.class.isAssignableFrom(clazz);
71 }).first().isPresent()) {
72 builder.register(JacksonJaxbJsonProvider.class);
74 final Client client = builder.register(new ObjectMapperContextResolver(mapper)).build();
77 this.healthClient = new HealthClient(client.target(url).path("v1").path("health"));
81 // agentClient.ping();
85 * Creates a new client given a complete URL.
87 * @deprecated Use {@link Consul.Builder}
89 * @param url The Consul API URL.
90 * @param builder The JAX-RS client builder instance.
91 * @return A new client.
94 public static Consul newClient(String url, ClientBuilder builder, ObjectMapper mapper) {
95 return new Consul(url, builder, mapper);
99 * Creates a new client given a host and a port.
101 * @deprecated Use {@link Consul.Builder}
103 * @param host The Consul API hostname or IP.
104 * @param port The Consul port.
105 * @param builder The JAX-RS client builder instance.
106 * @return A new client.
109 public static Consul newClient(String host, int port, ClientBuilder builder, ObjectMapper mapper) {
111 return new Consul(new URL("http", host, port, "").toString(), builder, mapper);
112 } catch (MalformedURLException e) {
113 throw new ConsulException("Bad Consul URL", e);
118 * Creates a new client given a host and a port.
120 * @deprecated Use {@link Consul.Builder}
122 * @param host The Consul API hostname or IP.
123 * @param port The Consul port.
124 * @return A new client.
127 public static Consul newClient(String host, int port) {
128 return newClient(host, port, ClientBuilder.newBuilder(), Jackson.MAPPER);
132 * Creates a new client given a host and a port.
134 * @deprecated Use {@link Consul.Builder}
136 * @return A new client.
139 public static Consul newClient() {
140 return newClient(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT);
145 public HealthClient healthClient() {
150 * Creates a new {@link Builder} object.
152 * @return A new Consul builder.
154 public static Builder builder() {
155 return new Builder();
159 * Builder for {@link Consul} client objects.
161 public static class Builder {
163 private Optional<SSLContext> sslContext = Optional.absent();
164 private ObjectMapper objectMapper = Jackson.MAPPER;
165 private ClientBuilder clientBuilder = ClientBuilder.newBuilder();
169 url = new URL("http", "localhost", 8500, "");
170 } catch (MalformedURLException e) {
171 throw new RuntimeException(e);
176 * Constructs a new builder.
183 * Sets the URL from a {@link URL} object.
185 * @param url The Consul agent URL.
186 * @return The builder.
188 public Builder withUrl(URL url) {
195 * Sets the URL from a {@link HostAndPort} object.
197 * @param hostAndPort The Consul agent host and port.
198 * @return The builder.
200 public Builder withHostAndPort(HostAndPort hostAndPort) {
202 this.url = new URL("http", hostAndPort.getHost(), hostAndPort.getPort(), "");
203 } catch (MalformedURLException e) {
204 throw new RuntimeException(e);
211 * Sets the URL from a string.
213 * @param url The Consul agent URL.
214 * @return The builder.
216 public Builder withUrl(String url) {
218 this.url = new URL(url);
219 } catch (MalformedURLException e) {
220 throw new RuntimeException(e);
227 * Sets the {@link SSLContext} for the client.
229 * @param sslContext The SSL context for HTTPS agents.
230 * @return The builder.
232 public Builder withSslContext(SSLContext sslContext) {
233 this.sslContext = Optional.of(sslContext);
239 * Sets the {@link ObjectMapper} for the client.
241 * @param objectMapper The {@link ObjectMapper} to use.
242 * @return The builder.
244 public Builder withObjectMapper(ObjectMapper objectMapper) {
245 this.objectMapper = objectMapper;
247 objectMapper.registerModule(new GuavaModule());
253 * Sets the JAX-RS {@link ClientBuilder} to use.
255 * @param clientBuilder The JAX-RS builder.
256 * @return This builder.
258 public Builder withClientBuilder(ClientBuilder clientBuilder) {
259 this.clientBuilder = clientBuilder;
265 * Constructs a new {@link Consul} client.
267 * @return A new Consul client.
269 public Consul build() {
270 if (this.sslContext.isPresent()) {
271 this.clientBuilder.sslContext(this.sslContext.get());
274 return new Consul(this.url.toExternalForm(), this.clientBuilder, this.objectMapper);