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);
69 }).first().isPresent()) {
70 builder.register(JacksonJaxbJsonProvider.class);
72 final Client client = builder.register(new ObjectMapperContextResolver(mapper)).build();
75 this.healthClient = new HealthClient(client.target(url).path("v1").path("health"));
79 // agentClient.ping();
83 * Creates a new client given a complete URL.
85 * @deprecated Use {@link Consul.Builder}
87 * @param url The Consul API URL.
88 * @param builder The JAX-RS client builder instance.
89 * @return A new client.
92 public static Consul newClient(String url, ClientBuilder builder, ObjectMapper mapper) {
93 return new Consul(url, builder, mapper);
97 * Creates a new client given a host and a port.
99 * @deprecated Use {@link Consul.Builder}
101 * @param host The Consul API hostname or IP.
102 * @param port The Consul port.
103 * @param builder The JAX-RS client builder instance.
104 * @return A new client.
107 public static Consul newClient(String host, int port, ClientBuilder builder, ObjectMapper mapper) {
109 return new Consul(new URL("http", host, port, "").toString(), builder, mapper);
110 } catch (MalformedURLException e) {
111 throw new ConsulException("Bad Consul URL", e);
116 * Creates a new client given a host and a port.
118 * @deprecated Use {@link Consul.Builder}
120 * @param host The Consul API hostname or IP.
121 * @param port The Consul port.
122 * @return A new client.
125 public static Consul newClient(String host, int port) {
126 return newClient(host, port, ClientBuilder.newBuilder(), Jackson.MAPPER);
130 * Creates a new client given a host and a port.
132 * @deprecated Use {@link Consul.Builder}
134 * @return A new client.
137 public static Consul newClient() {
138 return newClient(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT);
143 public HealthClient healthClient() {
148 * Creates a new {@link Builder} object.
150 * @return A new Consul builder.
152 public static Builder builder() {
153 return new Builder();
157 * Builder for {@link Consul} client objects.
159 public static class Builder {
161 private Optional<SSLContext> sslContext = Optional.absent();
162 private ObjectMapper objectMapper = Jackson.MAPPER;
163 private ClientBuilder clientBuilder = ClientBuilder.newBuilder();
167 url = new URL("http", "localhost", 8500, "");
168 } catch (MalformedURLException e) {
169 throw new RuntimeException(e);
174 * Constructs a new builder.
181 * Sets the URL from a {@link URL} object.
183 * @param url The Consul agent URL.
184 * @return The builder.
186 public Builder withUrl(URL url) {
193 * Sets the URL from a {@link HostAndPort} object.
195 * @param hostAndPort The Consul agent host and port.
196 * @return The builder.
198 public Builder withHostAndPort(HostAndPort hostAndPort) {
200 this.url = new URL("http", hostAndPort.getHostText(), hostAndPort.getPort(), "");
201 } catch (MalformedURLException e) {
202 throw new RuntimeException(e);
209 * Sets the URL from a string.
211 * @param url The Consul agent URL.
212 * @return The builder.
214 public Builder withUrl(String url) {
216 this.url = new URL(url);
217 } catch (MalformedURLException e) {
218 throw new RuntimeException(e);
225 * Sets the {@link SSLContext} for the client.
227 * @param sslContext The SSL context for HTTPS agents.
228 * @return The builder.
230 public Builder withSslContext(SSLContext sslContext) {
231 this.sslContext = Optional.of(sslContext);
237 * Sets the {@link ObjectMapper} for the client.
239 * @param objectMapper The {@link ObjectMapper} to use.
240 * @return The builder.
242 public Builder withObjectMapper(ObjectMapper objectMapper) {
243 this.objectMapper = objectMapper;
245 objectMapper.registerModule(new GuavaModule());
251 * Sets the JAX-RS {@link ClientBuilder} to use.
253 * @param clientBuilder The JAX-RS builder.
254 * @return This builder.
256 public Builder withClientBuilder(ClientBuilder clientBuilder) {
257 this.clientBuilder = clientBuilder;
263 * Constructs a new {@link Consul} client.
265 * @return A new Consul client.
267 public Consul build() {
268 if (this.sslContext.isPresent()) {
269 this.clientBuilder.sslContext(this.sslContext.get());
272 return new Consul(this.url.toExternalForm(), this.clientBuilder, this.objectMapper);