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=619728f21ddd45497da236d1dbdae4e21a36e5c2;hp=091b25c0214e6e3d9f61ca3a0811737c745b0d15;hpb=095ad14dcf2c7e836924e9e4911c5237a94661ce;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 091b25c..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,25 +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.HashMap;
-import java.util.List;
-import java.util.Map;
import lombok.extern.slf4j.Slf4j;
-import org.apache.http.Consts;
-import org.apache.http.HeaderIterator;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.NameValuePair;
-import org.apache.http.ParseException;
+import org.apache.http.*;
+import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-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;
@@ -41,20 +27,33 @@ 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 SSLConnectionSocketFactory sslConnectionSocketFactory = null;
private static PoolingHttpClientConnectionManager connectionManager = null;
private static SSLContextBuilder sslContextBuilder = null;
+ public static final int DEFUALT_TIMEOUT = 30000;
+
static {
try {
sslContextBuilder = new SSLContextBuilder();
@@ -65,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())
@@ -74,43 +73,29 @@ public class HttpsUtils {
connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(200);
} catch (Exception e) {
- log.error("Failed to initialize the ssl builder: " + e.getMessage());
+ log.error("Failed to initialize the ssl builder: " + e.getMessage(), e);
}
}
- public static String post(String url, Map header, Map param,
- HttpEntity entity) throws Exception {
- HttpResponse httpResponse = null;
- try {
- CloseableHttpClient httpClient = getHttpClient();
- HttpPost httpPost = getHttpPost(url, header, param, entity);
- httpResponse = getHttpResponse(httpClient, httpPost);
- } catch (Exception e) {
- throw new CorrelationException("Failed to use post method query data from server");
- }
- return getResponseEntity(httpResponse);
+ public static HttpResponse get(HttpGet httpGet, Map header, CloseableHttpClient httpClient) throws CorrelationException {
+ return getGetAndDeleteResponse(httpGet, header, httpClient);
}
- public static String get(String url, Map header) throws Exception {
- HttpResponse httpResponse = null;
- try {
- CloseableHttpClient httpClient = getHttpClient();
- HttpGet httpGet = getHttpGet(url, header);
- httpResponse = getHttpResponse(httpClient, httpGet);
- } catch (Exception e) {
- throw new CorrelationException("Failed to use get method query data from server");
- }
- return getResponseEntity(httpResponse);
+ public static HttpResponse post(HttpPost httpPost, Map header, Map param,
+ HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
+ return getPostAndPutResponse(httpPost, header, param, entity, httpClient);
}
- private static HttpPost getHttpPost(String url, Map header,
- Map param, HttpEntity entity) {
- HttpPost httpPost = new HttpPost(url);
- if (!header.isEmpty()) {
- for (Map.Entry entry : header.entrySet()) {
- httpPost.addHeader(entry.getKey(), entry.getValue());
- }
- }
+ 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 delete(HttpDelete httpDelete, Map header, CloseableHttpClient httpClient) throws CorrelationException {
+ return getGetAndDeleteResponse(httpDelete, header, httpClient);
+ }
+
+ private static void addParams(Map param, HttpEntityEnclosingRequestBase requestBase) {
if (!param.isEmpty()) {
List formparams = new ArrayList<>();
for (Map.Entry entry : param.entrySet()) {
@@ -118,57 +103,98 @@ public class HttpsUtils {
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams,
Consts.UTF_8);
- httpPost.setEntity(urlEncodedFormEntity);
+ requestBase.setEntity(urlEncodedFormEntity);
}
- if (entity != null) {
- httpPost.setEntity(entity);
- }
- return httpPost;
}
- private static HttpGet getHttpGet(String url, Map header) {
- HttpGet httpGet = new HttpGet(url);
+ private static HttpRequestBase addHeaders(Map header, HttpRequestBase httpRequestBase) {
if (!header.isEmpty()) {
for (Map.Entry entry : header.entrySet()) {
- httpGet.addHeader(entry.getKey(), entry.getValue());
+ httpRequestBase.addHeader(entry.getKey(), entry.getValue());
}
}
- return httpGet;
+ 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);
+ }
}
- private static String getResponseEntity(HttpResponse httpResponse) throws IOException {
+ public static String extractResponseEntity(HttpResponse httpResponse)
+ throws CorrelationException, IOException {
String result = "";
if (httpResponse != null) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = httpResponse.getEntity();
result = EntityUtils.toString(resEntity);
+ } else {
+ throw new CorrelationException("Get an error status from server : " + statusCode);
}
}
return result;
}
- private static HttpResponse getHttpResponse(CloseableHttpClient httpClient, HttpRequestBase httpRequest)
- throws Exception {
- HttpResponse httpResponse = null;
+ private static HttpResponse executeRequest(CloseableHttpClient httpClient, HttpRequestBase httpRequest)
+ throws CorrelationException, IOException {
+ HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(httpRequest);
} catch (Exception e) {
- throw new CorrelationException("Failed to get data from server");
- } finally {
- if (httpClient != null) {
- httpClient.close();
- }
+ throw new CorrelationException("Failed to get data from server", e);
}
return httpResponse;
}
- private static CloseableHttpClient getHttpClient() throws Exception {
- CloseableHttpClient httpClient = HttpClients.custom()
- .setSSLSocketFactory(sslConnectionSocketFactory)
- .setConnectionManager(connectionManager)
- .setConnectionManagerShared(true)
+ 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();
- return httpClient;
+
+ return HttpClients.custom()
+ .setDefaultRequestConfig(defaultRequestConfig)
+ .setConnectionManager(connectionManager)
+ .setConnectionManagerShared(true);
+ }
+
+ public static boolean isHttpsEnabled() {
+ return Boolean.valueOf(MicroServiceConfig.getEnv("ENABLE_ENCRYPT"));
}
}