From 3356fb82b20ebe7209522e4e009dd6aea5ed1802 Mon Sep 17 00:00:00 2001 From: Shiwei Tian Date: Mon, 16 Apr 2018 16:28:18 +0800 Subject: fix https timeout get connection Issue-ID: HOLMES-104 Change-Id: Ib79bb3dea470fd922f2e8fc906f33d8d238cd62e Signed-off-by: Shiwei Tian --- .../java/org/onap/holmes/common/aai/AaiQuery.java | 6 +- .../org/onap/holmes/common/dmaap/Publisher.java | 6 +- .../org/onap/holmes/common/utils/HttpsUtils.java | 84 +++++++++------------- 3 files changed, 45 insertions(+), 51 deletions(-) (limited to 'holmes-actions/src/main/java/org/onap') 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 b3f3f3a..a13c627 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 @@ -20,6 +20,8 @@ import java.util.Map; import javax.inject.Inject; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.impl.client.CloseableHttpClient; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.aai.config.AaiConfig; @@ -142,13 +144,15 @@ public class AaiQuery { private String getResponse(String url) throws CorrelationException { String response; CloseableHttpClient httpClient = null; + HttpGet httpGet = new HttpGet(url); try { httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); - HttpResponse httpResponse = HttpsUtils.get(url, getHeaders(), httpClient); + HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient); response = HttpsUtils.extractResponseEntity(httpResponse); } catch (Exception e) { throw new CorrelationException("Failed to get data from aai", e); } finally { + httpGet.releaseConnection(); if (httpClient != null) { try { httpClient.close(); 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 adddd65..b3a9214 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 @@ -17,6 +17,8 @@ package org.onap.holmes.common.dmaap; import java.io.IOException; import lombok.extern.slf4j.Slf4j; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.onap.holmes.common.dmaap.entity.PolicyMsg; import org.onap.holmes.common.exception.CorrelationException; @@ -55,12 +57,14 @@ public class Publisher { headers.put("Accept", MediaType.APPLICATION_JSON); headers.put("Content-Type", MediaType.APPLICATION_JSON); CloseableHttpClient httpClient = null; + HttpPost httpPost = new HttpPost(url); try { httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); - httpResponse = HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content, "utf-8"), httpClient); + httpResponse = HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content, "utf-8"), httpClient); } catch (Exception e) { throw new CorrelationException("Failed to connect to DCAE.", e); } finally { + httpPost.releaseConnection(); if (httpClient != null) { try { httpClient.close(); 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 48ed0ae..2df4d55 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,6 +18,7 @@ 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; @@ -83,62 +84,22 @@ public class HttpsUtils { } } - public static HttpResponse post(String url, Map header, Map param, - HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException { - HttpResponse response; - HttpPost httpPost = new HttpPost(url); - try { - 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 get(HttpGet httpGet, Map header, CloseableHttpClient httpClient) throws CorrelationException { + return getGetAndDeleteResponse(httpGet, header, httpClient); } - public static HttpResponse put(String url, Map header, Map param, + public static HttpResponse post(HttpPost httpPost, Map header, Map param, HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException { - HttpResponse response; - HttpPut httpPut = new HttpPut(url); - try { - 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; + return getPostAndPutResponse(httpPost, header, param, entity, httpClient); } - public static HttpResponse get(String url, Map header, CloseableHttpClient httpClient) throws CorrelationException { - HttpResponse response; - HttpGet httpGet = new HttpGet(url); - try { - 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 put(HttpPut httpPut, Map header, Map param, + HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException { + return getPostAndPutResponse(httpPut, header, param, entity, httpClient); } - public static HttpResponse delete(String url, Map header, CloseableHttpClient httpClient) throws CorrelationException { - HttpResponse response; - HttpDelete httpDelete = new HttpDelete(url); - try { - 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) { @@ -162,6 +123,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 = ""; -- cgit 1.2.3-korg