diff options
Diffstat (limited to 'holmes-actions/src/main/java/org')
3 files changed, 123 insertions, 63 deletions
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 b3005d5..ef96476 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 @@ -18,6 +18,7 @@ import java.util.List; import java.util.Map; import javax.inject.Inject; import lombok.extern.slf4j.Slf4j; +import org.apache.http.HttpResponse; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.aai.config.AaiConfig; import org.onap.holmes.common.aai.entity.VmEntity; @@ -137,9 +138,10 @@ public class AaiQuery { } private String getResponse(String url) throws CorrelationException { - String response = ""; + String response; try { - response = HttpsUtils.get(url, getHeaders()); + HttpResponse httpResponse = HttpsUtils.get(url, getHeaders()); + response = HttpsUtils.extractResponseEntity(httpResponse); } catch (Exception e) { throw new CorrelationException("Failed to get data from aai", e); } 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 7201f36..ad5109b 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,19 +15,18 @@ */
package org.onap.holmes.common.dmaap;
+import org.onap.holmes.common.dmaap.entity.PolicyMsg;
+import org.onap.holmes.common.exception.CorrelationException;
import com.alibaba.fastjson.JSON;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.WebTarget;
+import java.util.HashMap;
import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
import lombok.Getter;
import lombok.Setter;
+import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
+import org.apache.http.entity.StringEntity;
import org.jvnet.hk2.annotations.Service;
-import org.onap.holmes.common.dmaap.entity.PolicyMsg;
-import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.HttpsUtils;
@Getter
@Setter
@@ -40,20 +39,26 @@ public class Publisher { private String authExpDate;
public boolean publish(PolicyMsg msg) throws CorrelationException {
- Client client = ClientBuilder.newClient();
- String content = JSON.toJSONString(msg);
- WebTarget webTarget = client.target(url);
- Response response = null;
+ String content;
+ try {
+ content = JSON.toJSONString(msg);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to convert the message object to a json string.",
+ e);
+ }
+ HttpResponse httpResponse;
+ HashMap<String, String> headers = new HashMap<>();
+ headers.put("Accept", MediaType.APPLICATION_JSON);
+ headers.put("Content-Type", MediaType.APPLICATION_JSON);
try {
- response = webTarget.request(MediaType.APPLICATION_JSON)
- .post(Entity.entity(content, MediaType.APPLICATION_JSON));
+ httpResponse = HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content, "utf-8"));
} catch (Exception e) {
throw new CorrelationException("Failed to connect to DCAE.", e);
}
- return checkStatus(response);
+ return checkStatus(httpResponse);
}
- private boolean checkStatus(Response response) {
- return response.getStatus() == HttpStatus.SC_OK;
+ private boolean checkStatus(HttpResponse httpResponse) {
+ return (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) ? true : false;
}
}
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 3d3c827..41db955 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 @@ -26,9 +26,13 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; @@ -51,6 +55,7 @@ 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; @@ -78,50 +83,87 @@ public class HttpsUtils { } } - public static String post(String url, Map<String, String> header, Map<String, String> param, - HttpEntity entity) throws Exception { - HttpResponse httpResponse = null; + public static HttpResponse post(String url, Map<String, String> header, Map<String, String> param, + HttpEntity entity) throws CorrelationException { + return post(url, header, param, entity, DEFUALT_TIMEOUT); + } + + public static HttpResponse post(String url, Map<String, String> header, Map<String, String> param, + HttpEntity entity, int timeout) throws CorrelationException { + HttpResponse response; + HttpPost httpPost = new HttpPost(url); try { - CloseableHttpClient httpClient = getHttpClient(); - HttpPost httpPost = getHttpPost(url, header, param, entity); - httpResponse = getHttpResponse(httpClient, httpPost); + CloseableHttpClient httpClient = getHttpClient(timeout); + addHeaders(header, httpPost); + addParams(param, httpPost); + if (entity != null) { + httpPost.setEntity(entity); + } + response = executeRequest(httpClient, httpPost); } catch (Exception e) { - throw new CorrelationException("Failed to use post method query data from server"); + throw new CorrelationException("Failed to query data from server through POST method!"); } - return getResponseEntity(httpResponse); + return response; } - public static String get(String url, Map<String, String> header) throws Exception { - HttpResponse httpResponse = null; - CloseableHttpClient httpClient = null; - HttpGet httpGet = null; - String response = ""; + public static HttpResponse put(String url, Map<String, String> header, Map<String, String> param, + HttpEntity entity) throws CorrelationException { + return put(url, header, param, entity, DEFUALT_TIMEOUT); + } + + public static HttpResponse put(String url, Map<String, String> header, Map<String, String> param, + HttpEntity entity, int timeout) throws CorrelationException { + HttpResponse response; + HttpPut httpPut = new HttpPut(url); try { - 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(); + CloseableHttpClient httpClient = getHttpClient(timeout); + 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; } - private static HttpPost getHttpPost(String url, Map<String, String> header, - Map<String, String> param, HttpEntity entity) { - HttpPost httpPost = new HttpPost(url); - if (!header.isEmpty()) { - for (Map.Entry<String, String> entry : header.entrySet()) { - httpPost.addHeader(entry.getKey(), entry.getValue()); - } + public static HttpResponse get(String url, Map<String, String> header) throws CorrelationException { + return get(url, header, DEFUALT_TIMEOUT); + } + + public static HttpResponse get(String url, Map<String, String> header, int timeout) throws CorrelationException { + HttpResponse response; + HttpGet httpGet = new HttpGet(url); + try { + CloseableHttpClient httpClient = getHttpClient(timeout); + 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 delete(String url, Map<String, String> header) throws CorrelationException { + return delete(url, header, DEFUALT_TIMEOUT); + } + + public static HttpResponse delete(String url, Map<String, String> header, int timeout) throws CorrelationException { + HttpResponse response; + HttpDelete httpDelete = new HttpDelete(url); + try { + CloseableHttpClient httpClient = getHttpClient(timeout); + addHeaders(header, httpDelete); + response = executeRequest(httpClient, httpDelete); + } catch (Exception e) { + throw new CorrelationException("Failed to query data from server through DELETE method!"); } + return response; + } + + private static void addParams(Map<String, String> param, HttpEntityEnclosingRequestBase requestBase) { if (!param.isEmpty()) { List<NameValuePair> formparams = new ArrayList<>(); for (Map.Entry<String, String> entry : param.entrySet()) { @@ -129,49 +171,60 @@ public class HttpsUtils { } UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); - httpPost.setEntity(urlEncodedFormEntity); - } - if (entity != null) { - httpPost.setEntity(entity); + requestBase.setEntity(urlEncodedFormEntity); } - return httpPost; } - private static HttpGet getHttpGet(String url, Map<String, String> header) { - HttpGet httpGet = new HttpGet(url); + private static HttpRequestBase addHeaders(Map<String, String> header, HttpRequestBase httpRequestBase) { if (!header.isEmpty()) { for (Map.Entry<String, String> entry : header.entrySet()) { - httpGet.addHeader(entry.getKey(), entry.getValue()); + httpRequestBase.addHeader(entry.getKey(), entry.getValue()); } } - return httpGet; + return httpRequestBase; } - 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 a error status from server : " + statusCode); } } return result; } - private static HttpResponse getHttpResponse(CloseableHttpClient httpClient, HttpRequestBase httpRequest) + private static HttpResponse executeRequest(CloseableHttpClient httpClient, HttpRequestBase httpRequest) throws Exception { - HttpResponse httpResponse = null; + HttpResponse httpResponse; try { httpResponse = httpClient.execute(httpRequest); } catch (Exception e) { throw new CorrelationException("Failed to get data from server"); + } finally { + if (httpRequest != null) { + httpRequest.releaseConnection(); + } + if (httpClient != null) { + httpClient.close(); + } } return httpResponse; } - private static CloseableHttpClient getHttpClient() throws Exception { + private static CloseableHttpClient getHttpClient(int timeout) throws Exception { + RequestConfig defaultRequestConfig = RequestConfig.custom() + .setSocketTimeout(timeout) + .setConnectTimeout(timeout) + .setConnectionRequestTimeout(timeout) + .build(); CloseableHttpClient httpClient = HttpClients.custom() + .setDefaultRequestConfig(defaultRequestConfig) .setSSLSocketFactory(sslConnectionSocketFactory) .setConnectionManager(connectionManager) .setConnectionManagerShared(true) |