Fixed MSB Registration Failure
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / JerseyClient.java
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java
new file mode 100644 (file)
index 0000000..2d770ad
--- /dev/null
@@ -0,0 +1,75 @@
+/**
+ * Copyright 2020 ZTE Corporation.
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.onap.holmes.common.utils;
+
+import org.glassfish.jersey.client.ClientConfig;
+import org.jvnet.hk2.annotations.Service;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.PostConstruct;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
+
+@Service
+public class JerseyClient {
+    private static Logger logger = LoggerFactory.getLogger(JerseyClient.class);
+    public static final String PROTOCOL_HTTP = "http";
+    public static final String PROTOCOL_HTTPS = "https";
+    private SSLContext sslcontext = null;
+
+    @PostConstruct
+    private void init() {
+        try {
+            sslcontext = SSLContext.getInstance("TLS");
+            sslcontext.init(null, new TrustManager[]{new X509TrustManager() {
+                public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
+                }
+
+                public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
+                }
+
+                public X509Certificate[] getAcceptedIssuers() {
+                    return new X509Certificate[0];
+                }
+            }}, new java.security.SecureRandom());
+        } catch (NoSuchAlgorithmException | KeyManagementException e) {
+            logger.error("Failed to initialize the SSLContext instance!", e);
+        }
+    }
+
+    public Client httpClient() {
+        return ClientBuilder.newClient(new ClientConfig());
+    }
+
+    public Client httpsClient() {
+        return ClientBuilder.newBuilder()
+                .sslContext(sslcontext)
+                .hostnameVerifier((s1, s2) -> true)
+                .build();
+    }
+
+    public Client client(boolean isHttps) {
+        return isHttps ? httpsClient() : httpClient();
+    }
+}