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=48ed0ae246ffab64610b210112fdbb8c3845f2e3;hb=77a032f176183287746858d8eb57a6062286d327;hp=8b88fd2002931c25bd1e39ae53f596425d270e29;hpb=814861d9a0b4d093c482e3338453771423dcd7f0;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 8b88fd2..48ed0ae 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 @@ -55,10 +55,10 @@ import org.onap.holmes.common.exception.CorrelationException; 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{ try { @@ -84,16 +84,10 @@ 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 post(String url, Map header, Map param, - HttpEntity entity, int timeout) throws CorrelationException { + HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException { HttpResponse response; HttpPost httpPost = new HttpPost(url); try { - CloseableHttpClient httpClient = getHttpClient(timeout); addHeaders(header, httpPost); addParams(param, httpPost); if (entity != null) { @@ -107,16 +101,10 @@ public class HttpsUtils { } 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(String url, Map header, Map param, - HttpEntity entity, int timeout) throws CorrelationException { + HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException { HttpResponse response; HttpPut httpPut = new HttpPut(url); try { - CloseableHttpClient httpClient = getHttpClient(timeout); addHeaders(header, httpPut); addParams(param, httpPut); if (entity != null) { @@ -129,15 +117,10 @@ public class HttpsUtils { 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 { + public static HttpResponse get(String url, Map header, CloseableHttpClient httpClient) throws CorrelationException { HttpResponse response; HttpGet httpGet = new HttpGet(url); try { - CloseableHttpClient httpClient = getHttpClient(timeout); addHeaders(header, httpGet); response = executeRequest(httpClient, httpGet); } catch (Exception e) { @@ -146,15 +129,10 @@ public class HttpsUtils { 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 { + public static HttpResponse delete(String url, Map header, CloseableHttpClient httpClient) throws CorrelationException { HttpResponse response; HttpDelete httpDelete = new HttpDelete(url); try { - CloseableHttpClient httpClient = getHttpClient(timeout); addHeaders(header, httpDelete); response = executeRequest(httpClient, httpDelete); } catch (Exception e) { @@ -205,20 +183,12 @@ public class HttpsUtils { try { httpResponse = httpClient.execute(httpRequest); } catch (Exception e) { - e.printStackTrace(); throw new CorrelationException("Failed to get data from server" ,e); - } finally { - if (httpRequest != null) { - httpRequest.releaseConnection(); - } - if (httpClient != null) { - httpClient.close(); - } } return httpResponse; } - private static CloseableHttpClient getHttpClient(int timeout) throws Exception { + public static CloseableHttpClient getHttpClient(int timeout) { RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(timeout) .setConnectTimeout(timeout)