2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.http.client.internal;
24 import com.fasterxml.jackson.annotation.JsonIgnore;
26 import java.security.KeyManagementException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.SecureRandom;
29 import java.security.cert.CertificateException;
30 import java.security.cert.X509Certificate;
32 import java.util.Map.Entry;
34 import javax.net.ssl.SSLContext;
35 import javax.net.ssl.TrustManager;
36 import javax.net.ssl.X509TrustManager;
37 import javax.ws.rs.client.Client;
38 import javax.ws.rs.client.ClientBuilder;
39 import javax.ws.rs.client.Entity;
40 import javax.ws.rs.client.Invocation.Builder;
41 import javax.ws.rs.core.Response;
43 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
44 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
45 import org.onap.policy.common.endpoints.http.client.HttpClient;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class JerseyClient implements HttpClient {
54 private static Logger logger = LoggerFactory.getLogger(JerseyClient.class);
56 protected final String name;
57 protected final boolean https;
58 protected final boolean selfSignedCerts;
59 protected final String hostname;
60 protected final int port;
61 protected final String basePath;
62 protected final String userName;
63 protected final String password;
65 protected final Client client;
66 protected final String baseUrl;
68 protected boolean alive = true;
73 * <p>name the name https is it https or not selfSignedCerts are there self signed certs hostname
74 * the hostname port port being used basePath base context userName user password password
76 * @param busTopicParams Input parameters object
77 * @throws KeyManagementException key exception
78 * @throws NoSuchAlgorithmException no algorithm exception
80 public JerseyClient(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException {
84 if (busTopicParams.isClientNameInvalid()) {
85 throw new IllegalArgumentException("Name must be provided");
88 if (busTopicParams.isHostnameInvalid()) {
89 throw new IllegalArgumentException("Hostname must be provided");
92 if (busTopicParams.isPortInvalid()) {
93 throw new IllegalArgumentException("Invalid Port provided: " + busTopicParams.getPort());
96 this.name = busTopicParams.getClientName();
97 this.https = busTopicParams.isUseHttps();
98 this.hostname = busTopicParams.getHostname();
99 this.port = busTopicParams.getPort();
100 this.basePath = busTopicParams.getBasePath();
101 this.userName = busTopicParams.getUserName();
102 this.password = busTopicParams.getPassword();
103 this.selfSignedCerts = busTopicParams.isAllowSelfSignedCerts();
105 StringBuilder tmpBaseUrl = new StringBuilder();
107 tmpBaseUrl.append("https://");
108 ClientBuilder clientBuilder;
109 SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
110 if (this.selfSignedCerts) {
111 sslContext.init(null, new TrustManager[] {new X509TrustManager() {
113 public void checkClientTrusted(X509Certificate[] chain, String authType)
114 throws CertificateException {
119 public void checkServerTrusted(X509Certificate[] chain, String authType)
120 throws CertificateException {
125 public X509Certificate[] getAcceptedIssuers() {
126 return new X509Certificate[0];
129 } }, new SecureRandom());
131 ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier((host, session) -> true);
133 sslContext.init(null, null, null);
134 clientBuilder = ClientBuilder.newBuilder().sslContext(sslContext);
136 this.client = clientBuilder.build();
138 tmpBaseUrl.append("http://");
139 this.client = ClientBuilder.newClient();
142 if (this.userName != null && !this.userName.isEmpty() && this.password != null && !this.password.isEmpty()) {
143 HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(userName, password);
144 this.client.register(authFeature);
147 this.baseUrl = tmpBaseUrl.append(this.hostname).append(":").append(this.port).append("/")
148 .append((this.basePath == null) ? "" : this.basePath).toString();
152 public Response get(String path) {
153 if (path != null && !path.isEmpty()) {
154 return this.client.target(this.baseUrl).path(path).request().get();
156 return this.client.target(this.baseUrl).request().get();
161 public Response get() {
162 return this.client.target(this.baseUrl).request().get();
166 public Response put(String path, Entity<?> entity, Map<String, Object> headers) {
167 Builder builder = this.client.target(this.baseUrl).path(path).request();
168 for (Entry<String, Object> header : headers.entrySet()) {
169 builder.header(header.getKey(), header.getValue());
171 return builder.put(entity);
175 public boolean start() {
180 public boolean stop() {
185 public void shutdown() {
186 synchronized (this) {
192 } catch (Exception e) {
193 logger.warn("{}: cannot close because of {}", this, e.getMessage(), e);
198 public synchronized boolean isAlive() {
203 public String getName() {
208 public boolean isHttps() {
213 public boolean isSelfSignedCerts() {
214 return selfSignedCerts;
218 public String getHostname() {
223 public int getPort() {
228 public String getBasePath() {
233 public String getUserName() {
239 public String getPassword() {
244 public String getBaseUrl() {
249 public String toString() {
250 StringBuilder builder = new StringBuilder();
251 builder.append("JerseyClient [name=");
252 builder.append(name);
253 builder.append(", https=");
254 builder.append(https);
255 builder.append(", selfSignedCerts=");
256 builder.append(selfSignedCerts);
257 builder.append(", hostname=");
258 builder.append(hostname);
259 builder.append(", port=");
260 builder.append(port);
261 builder.append(", basePath=");
262 builder.append(basePath);
263 builder.append(", userName=");
264 builder.append(userName);
265 builder.append(", password=");
266 builder.append(password);
267 builder.append(", client=");
268 builder.append(client);
269 builder.append(", baseUrl=");
270 builder.append(baseUrl);
271 builder.append(", alive=");
272 builder.append(alive);
274 return builder.toString();