From 96a6ea5cab6575b7e04d6736cee6906298065a7b Mon Sep 17 00:00:00 2001 From: Shiwei Tian Date: Sun, 8 Apr 2018 17:34:12 +0800 Subject: fix https bug Issue-ID: HOLMES-104 Change-Id: I9146e6f89d9e72744eadd46f28b2ad10eefcfb40 Signed-off-by: Shiwei Tian --- .../java/org/onap/holmes/common/aai/AaiQuery.java | 14 ++++++++- .../org/onap/holmes/common/dmaap/Publisher.java | 16 +++++++++- .../org/onap/holmes/common/utils/HttpsUtils.java | 34 ++++------------------ 3 files changed, 34 insertions(+), 30 deletions(-) (limited to 'holmes-actions/src/main') diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java index ef96476..b3f3f3a 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java @@ -13,12 +13,14 @@ */ package org.onap.holmes.common.aai; +import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.inject.Inject; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpResponse; +import org.apache.http.impl.client.CloseableHttpClient; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.aai.config.AaiConfig; import org.onap.holmes.common.aai.entity.VmEntity; @@ -139,11 +141,21 @@ public class AaiQuery { private String getResponse(String url) throws CorrelationException { String response; + CloseableHttpClient httpClient = null; try { - HttpResponse httpResponse = HttpsUtils.get(url, getHeaders()); + httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + HttpResponse httpResponse = HttpsUtils.get(url, getHeaders(), httpClient); response = HttpsUtils.extractResponseEntity(httpResponse); } catch (Exception e) { throw new CorrelationException("Failed to get data from aai", e); + } finally { + if (httpClient != null) { + try { + httpClient.close(); + } catch (IOException e) { + log.warn("Failed to close http client!"); + } + } } return response; } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java index ad5109b..adddd65 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java @@ -15,6 +15,9 @@ */ package org.onap.holmes.common.dmaap; +import java.io.IOException; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.impl.client.CloseableHttpClient; import org.onap.holmes.common.dmaap.entity.PolicyMsg; import org.onap.holmes.common.exception.CorrelationException; import com.alibaba.fastjson.JSON; @@ -31,6 +34,7 @@ import org.onap.holmes.common.utils.HttpsUtils; @Getter @Setter @Service +@Slf4j public class Publisher { private String topic; @@ -50,10 +54,20 @@ public class Publisher { HashMap headers = new HashMap<>(); headers.put("Accept", MediaType.APPLICATION_JSON); headers.put("Content-Type", MediaType.APPLICATION_JSON); + CloseableHttpClient httpClient = null; try { - httpResponse = HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content, "utf-8")); + httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + httpResponse = HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content, "utf-8"), httpClient); } catch (Exception e) { throw new CorrelationException("Failed to connect to DCAE.", e); + } finally { + if (httpClient != null) { + try { + httpClient.close(); + } catch (IOException e) { + log.warn("Failed to close http client!"); + } + } } return checkStatus(httpResponse); } 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..29a1c91 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) { @@ -218,7 +196,7 @@ public class HttpsUtils { return httpResponse; } - private static CloseableHttpClient getHttpClient(int timeout) throws Exception { + public static CloseableHttpClient getHttpClient(int timeout) { RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(timeout) .setConnectTimeout(timeout) -- cgit 1.2.3-korg