aboutsummaryrefslogtreecommitdiffstats
path: root/msb-core/apiroute/apiroute-service/src/main/java/org/openo/msb/wrapper/consul/Consul.java
blob: 67c9ef3cc669c68da99d42773ef48e7f26bdc0ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
 * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
* Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openo.msb.wrapper.consul;

import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.SSLContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

import org.openo.msb.wrapper.consul.util.Jackson;
import org.openo.msb.wrapper.consul.util.ObjectMapperContextResolver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.net.HostAndPort;

/**
 * Client for interacting with the Consul HTTP API.
 *
 * @author rfast
 */
public class Consul {

    /**
     * Default Consul HTTP API host.
     */
    public static final String DEFAULT_HTTP_HOST = "localhost";

    /**
     * Default Consul HTTP API port.
     */
    public static final int DEFAULT_HTTP_PORT = 8500;




    private final CatalogClient catalogClient;
    
    private final HealthClient healthClient;


    /**
     * Private constructor.
     *
     * @param url     The full URL of a running Consul instance.
     * @param builder JAX-RS client builder instance.
     */
    private Consul(String url, ClientBuilder builder, ObjectMapper mapper) {

        if (!FluentIterable.from(builder.getConfiguration().getClasses())
                      .filter(new Predicate<Class<?>>() {
                        @Override
                        public boolean apply(final Class<?> clazz) {
                            return JacksonJaxbJsonProvider.class.isAssignableFrom(clazz);
                        }
                    }).first().isPresent()) {
            builder.register(JacksonJaxbJsonProvider.class);
        }
        final Client client = builder
                .register(new ObjectMapperContextResolver(mapper))
                .build();

     
        if(url.endsWith("8500")){
            this.catalogClient = new CatalogClient(client.target(url).path("v1").path("catalog"));
            this.healthClient = new HealthClient(client.target(url).path("v1").path("health"));
            }
            else{
                this.catalogClient = new CatalogClient(client.target(url).path("api").path("catalog").path("v1"));
                this.healthClient = new HealthClient(client.target(url).path("api").path("health").path("v1"));   
            }
       

//        agentClient.ping();
    }

    /**
     * Creates a new client given a complete URL.
     *
     * @deprecated Use {@link Consul.Builder}
     *
     * @param url     The Consul API URL.
     * @param builder The JAX-RS client builder instance.
     * @return A new client.
     */
    @Deprecated
    public static Consul newClient(String url, ClientBuilder builder, ObjectMapper mapper) {
        return new Consul(url, builder, mapper);
    }

    /**
     * Creates a new client given a host and a port.
     *
     * @deprecated Use {@link Consul.Builder}
     *
     * @param host    The Consul API hostname or IP.
     * @param port    The Consul port.
     * @param builder The JAX-RS client builder instance.
     * @return A new client.
     */
    @Deprecated
    public static Consul newClient(String host, int port, ClientBuilder builder, ObjectMapper mapper) {
        try {
            return new Consul(new URL("http", host, port, "").toString(), builder, mapper);
        } catch (MalformedURLException e) {
            throw new ConsulException("Bad Consul URL", e);
        }
    }

    /**
     * Creates a new client given a host and a port.
     *
     * @deprecated Use {@link Consul.Builder}
     *
     * @param host The Consul API hostname or IP.
     * @param port The Consul port.
     * @return A new client.
     */
    @Deprecated
    public static Consul newClient(String host, int port) {
        return newClient(host, port, ClientBuilder.newBuilder(), Jackson.MAPPER);
    }

    /**
     * Creates a new client given a host and a port.
     *
     * @deprecated Use {@link Consul.Builder}
     *
     * @return A new client.
     */
    @Deprecated
    public static Consul newClient() {
        return newClient(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT);
    }

   
    /**
     * Get the Catalog HTTP client.
     * <p>
     * /v1/catalog
     *
     * @return The Catalog HTTP client.
     */
    public CatalogClient catalogClient() {
        return catalogClient;
    }
    
    public HealthClient healthClient() {
        return healthClient;
    }
    /**
     * Creates a new {@link Builder} object.
     *
     * @return A new Consul builder.
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * Builder for {@link Consul} client objects.
     */
    public static class Builder {
        private URL url;
        private Optional<SSLContext> sslContext = Optional.absent();
        private ObjectMapper objectMapper = Jackson.MAPPER;
        private ClientBuilder clientBuilder = ClientBuilder.newBuilder();

        {
            try {
                url = new URL("http", "localhost", 8500, "");
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }

        /**
         * Constructs a new builder.
         */
        Builder() {

        }

        /**
         * Sets the URL from a {@link URL} object.
         *
         * @param url The Consul agent URL.
         * @return The builder.
         */
        public Builder withUrl(URL url) {
            this.url = url;

            return this;
        }

        /**
         * Sets the URL from a {@link HostAndPort} object.
         *
         * @param hostAndPort The Consul agent host and port.
         * @return The builder.
         */
        public Builder withHostAndPort(HostAndPort hostAndPort) {
            try {
                this.url = new URL("http", hostAndPort.getHostText(), hostAndPort.getPort(), "");
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }

            return this;
        }

        /**
         * Sets the URL from a string.
         *
         * @param url The Consul agent URL.
         * @return The builder.
         */
        public Builder withUrl(String url) {
            try {
                this.url = new URL(url);
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }

            return this;
        }

        /**
         * Sets the {@link SSLContext} for the client.
         *
         * @param sslContext The SSL context for HTTPS agents.
         * @return The builder.
         */
        public Builder withSslContext(SSLContext sslContext) {
            this.sslContext = Optional.of(sslContext);

            return this;
        }

        /**
         * Sets the {@link ObjectMapper} for the client.
         *
         * @param objectMapper The {@link ObjectMapper} to use.
         * @return The builder.
         */
        public Builder withObjectMapper(ObjectMapper objectMapper) {
            this.objectMapper = objectMapper;

            objectMapper.registerModule(new GuavaModule());

            return this;
        }

        /**
         * Sets the JAX-RS {@link ClientBuilder} to use.
         *
         * @param clientBuilder The JAX-RS builder.
         * @return This builder.
         */
        public Builder withClientBuilder(ClientBuilder clientBuilder) {
            this.clientBuilder = clientBuilder;

            return this;
        }

        /**
         * Constructs a new {@link Consul} client.
         *
         * @return A new Consul client.
         */
        public Consul build() {
            if (this.sslContext.isPresent()) {
                this.clientBuilder.sslContext(this.sslContext.get());
            }

            return new Consul(this.url.toExternalForm(), this.clientBuilder, this.objectMapper);
        }
    }
}