X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=holmes-actions%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Fcommon%2Futils%2FHttpsUtils.java;h=8b4be573e366088eb3be5bb1f6354bd59bf8c71e;hb=babb845c53219cb3a17f302c3f6bac5a20132c48;hp=a2c4852c292875e1a785fd847092ac7e5dc2c865;hpb=b1105dd8f4fa2cf21cc4eb4a6f825685a185173b;p=holmes%2Fcommon.git
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
index a2c4852..8b4be57 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
@@ -1,11 +1,11 @@
/**
* Copyright 2017 ZTE Corporation.
- *
+ *
* 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
@@ -14,26 +14,11 @@
package org.onap.holmes.common.utils;
-import java.io.IOException;
-import java.security.cert.CertificateException;
-import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
import lombok.extern.slf4j.Slf4j;
-import org.apache.http.Consts;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.NameValuePair;
+import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpDelete;
-import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpPut;
-import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.client.methods.*;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
@@ -42,25 +27,34 @@ import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.jvnet.hk2.annotations.Service;
+import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;
+import java.io.IOException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
@Slf4j
@Service
public class HttpsUtils {
private static final String HTTP = "http";
private static final String HTTPS = "https";
- private static final int DEFUALT_TIMEOUT = 30000;
private static SSLConnectionSocketFactory sslConnectionSocketFactory = null;
private static PoolingHttpClientConnectionManager connectionManager = null;
private static SSLContextBuilder sslContextBuilder = null;
+ public static final int DEFUALT_TIMEOUT = 30000;
- static{
+ static {
try {
sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() {
@@ -70,7 +64,7 @@ public class HttpsUtils {
}
});
sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(),
- new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null,
+ new String[]{"SSLv3", "TLSv1", "TLSv1.2"}, null,
NoopHostnameVerifier.INSTANCE);
Registry registry = RegistryBuilder.create()
.register(HTTP, new PlainConnectionSocketFactory())
@@ -83,84 +77,22 @@ public class HttpsUtils {
}
}
- public static HttpResponse post(String url, Map header, Map param,
- HttpEntity entity) throws CorrelationException {
- return post(url, header, param, entity, DEFUALT_TIMEOUT);
+ public static HttpResponse get(HttpGet httpGet, Map header, CloseableHttpClient httpClient) throws CorrelationException {
+ return getGetAndDeleteResponse(httpGet, header, httpClient);
}
- public static HttpResponse post(String url, Map header, Map param,
- HttpEntity entity, int timeout) throws CorrelationException {
- HttpResponse response;
- HttpPost httpPost = new HttpPost(url);
- try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
- addHeaders(header, httpPost);
- addParams(param, httpPost);
- if (entity != null) {
- httpPost.setEntity(entity);
- }
- response = executeRequest(httpClient, httpPost);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through POST method!");
- }
- return response;
+ public static HttpResponse post(HttpPost httpPost, Map header, Map param,
+ HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
+ return getPostAndPutResponse(httpPost, header, param, entity, httpClient);
}
- public static HttpResponse put(String url, Map header, Map param,
- HttpEntity entity) throws CorrelationException {
- return put(url, header, param, entity, DEFUALT_TIMEOUT);
+ public static HttpResponse put(HttpPut httpPut, Map header, Map param,
+ HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
+ return getPostAndPutResponse(httpPut, header, param, entity, httpClient);
}
- public static HttpResponse put(String url, Map header, Map param,
- HttpEntity entity, int timeout) throws CorrelationException {
- HttpResponse response;
- HttpPut httpPut = new HttpPut(url);
- try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
- addHeaders(header, httpPut);
- addParams(param, httpPut);
- if (entity != null) {
- httpPut.setEntity(entity);
- }
- response = executeRequest(httpClient, httpPut);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through PUT method!");
- }
- return response;
- }
-
- public static HttpResponse get(String url, Map header) throws CorrelationException {
- return get(url, header, DEFUALT_TIMEOUT);
- }
-
- public static HttpResponse get(String url, Map header, int timeout) throws CorrelationException {
- HttpResponse response;
- HttpGet httpGet = new HttpGet(url);
- try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
- addHeaders(header, httpGet);
- response = executeRequest(httpClient, httpGet);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through GET method!");
- }
- return response;
- }
-
- public static HttpResponse delete(String url, Map header) throws CorrelationException {
- return delete(url, header, DEFUALT_TIMEOUT);
- }
-
- public static HttpResponse delete(String url, Map header, int timeout) throws CorrelationException {
- HttpResponse response;
- HttpDelete httpDelete = new HttpDelete(url);
- try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
- addHeaders(header, httpDelete);
- response = executeRequest(httpClient, httpDelete);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through DELETE method!");
- }
- return response;
+ public static HttpResponse delete(HttpDelete httpDelete, Map header, CloseableHttpClient httpClient) throws CorrelationException {
+ return getGetAndDeleteResponse(httpDelete, header, httpClient);
}
private static void addParams(Map param, HttpEntityEnclosingRequestBase requestBase) {
@@ -184,6 +116,31 @@ public class HttpsUtils {
return httpRequestBase;
}
+ private static HttpResponse getPostAndPutResponse(HttpEntityEnclosingRequestBase requestBase,
+ Map header, Map param, HttpEntity entity,
+ CloseableHttpClient httpClient) throws CorrelationException {
+ try {
+ addHeaders(header, requestBase);
+ addParams(param, requestBase);
+ if (entity != null) {
+ requestBase.setEntity(entity);
+ }
+ return executeRequest(httpClient, requestBase);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to connect to server", e);
+ }
+ }
+
+ private static HttpResponse getGetAndDeleteResponse(HttpRequestBase requestBase,
+ Map header, CloseableHttpClient httpClient) throws CorrelationException {
+ try {
+ addHeaders(header, requestBase);
+ return executeRequest(httpClient, requestBase);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to connect to server", e);
+ }
+ }
+
public static String extractResponseEntity(HttpResponse httpResponse)
throws CorrelationException, IOException {
String result = "";
@@ -193,7 +150,7 @@ public class HttpsUtils {
HttpEntity resEntity = httpResponse.getEntity();
result = EntityUtils.toString(resEntity);
} else {
- throw new CorrelationException("Get a error status from server : " + statusCode);
+ throw new CorrelationException("Get an error status from server : " + statusCode);
}
}
return result;
@@ -205,30 +162,39 @@ public class HttpsUtils {
try {
httpResponse = httpClient.execute(httpRequest);
} catch (Exception e) {
- throw new CorrelationException("Failed to get data from server");
- } finally {
- if (httpRequest != null) {
- httpRequest.releaseConnection();
- }
- if (httpClient != null) {
- httpClient.close();
- }
+ throw new CorrelationException("Failed to get data from server", e);
}
return httpResponse;
}
- private static CloseableHttpClient getHttpClient(int timeout) throws Exception {
+ public static CloseableHttpClient getConditionalHttpsClient(int timeout) {
+ HttpClientBuilder builder = getHttpClientBuilder(timeout);
+ if (isHttpsEnabled()) {
+ builder.setSSLSocketFactory(sslConnectionSocketFactory);
+ }
+
+ return builder.build();
+ }
+
+ public static CloseableHttpClient getHttpsClient(int timeout) {
+ HttpClientBuilder builder = getHttpClientBuilder(timeout);
+ return builder.setSSLSocketFactory(sslConnectionSocketFactory).build();
+ }
+
+ private static HttpClientBuilder getHttpClientBuilder(int timeout) {
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(timeout)
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.build();
- CloseableHttpClient httpClient = HttpClients.custom()
+
+ return HttpClients.custom()
.setDefaultRequestConfig(defaultRequestConfig)
- .setSSLSocketFactory(sslConnectionSocketFactory)
.setConnectionManager(connectionManager)
- .setConnectionManagerShared(true)
- .build();
- return httpClient;
+ .setConnectionManagerShared(true);
+ }
+
+ public static boolean isHttpsEnabled() {
+ return Boolean.valueOf(MicroServiceConfig.getEnv("ENABLE_ENCRYPT"));
}
}