From c3a783d2ed1e9e19525b7a88a0ef29aab419a436 Mon Sep 17 00:00:00 2001 From: Shiwei Tian Date: Fri, 20 Oct 2017 12:31:37 +0800 Subject: [PATCH] modify bug Issue-ID: HOLMES-71 Change-Id: Ib3a2f5b6614166aabccdbc78566d35d644661ee9 Signed-off-by: Shiwei Tian --- .../org/onap/holmes/common/utils/HttpsUtils.java | 30 ++++++++++++++-------- 1 file changed, 19 insertions(+), 11 deletions(-) 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..72e27db 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 @@ -18,17 +18,15 @@ 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 javax.annotation.PostConstruct; 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.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; @@ -46,16 +44,19 @@ 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.exception.CorrelationException; @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; - static { + + static{ try { sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() { @@ -93,14 +94,25 @@ public class HttpsUtils { public static String get(String url, Map header) throws Exception { HttpResponse httpResponse = null; + CloseableHttpClient httpClient = null; + HttpGet httpGet = null; + String response = ""; try { - CloseableHttpClient httpClient = getHttpClient(); - HttpGet httpGet = getHttpGet(url, header); + httpClient = getHttpClient(); + httpGet = getHttpGet(url, header); httpResponse = getHttpResponse(httpClient, httpGet); + response = getResponseEntity(httpResponse); } catch (Exception e) { throw new CorrelationException("Failed to use get method query data from server"); + } finally { + if (httpGet != null) { + httpGet.releaseConnection(); + } + if (httpResponse != null) { + httpClient.close(); + } } - return getResponseEntity(httpResponse); + return response; } private static HttpPost getHttpPost(String url, Map header, @@ -155,10 +167,6 @@ public class HttpsUtils { httpResponse = httpClient.execute(httpRequest); } catch (Exception e) { throw new CorrelationException("Failed to get data from server"); - } finally { - if (httpClient != null) { - httpClient.close(); - } } return httpResponse; } -- 2.16.6