diff options
author | tang peng <tang.peng5@zte.com.cn> | 2019-03-30 07:47:12 +0000 |
---|---|---|
committer | tangpeng <tang.peng5@zte.com.cn> | 2019-03-30 07:50:39 +0000 |
commit | 58401075a66f7c5963848a7349067f995fe4f947 (patch) | |
tree | 56955166fa769a996d0f2723ee52d94cf1a35083 /holmes-actions/src/main/java/org | |
parent | 555a3d9b10af05f2dc64b2a230f80fc436432a7a (diff) |
Make HTTP/s Optional
Change-Id: Ia09126b06ad43f05582ae0f3dd8692df223953fe
Issue-ID: HOLMES-207
Signed-off-by: tang peng <tang.peng5@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/main/java/org')
3 files changed, 37 insertions, 20 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 919f329..b11cafc 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 @@ -123,7 +123,7 @@ public class AaiQuery { CloseableHttpClient httpClient = null; HttpGet httpGet = new HttpGet(url); try { - httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + httpClient = HttpsUtils.getHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient); response = HttpsUtils.extractResponseEntity(httpResponse); } catch (Exception 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 09bb013..d95853d 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,12 +17,10 @@ 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;
-import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import java.util.HashMap;
import javax.ws.rs.core.MediaType;
@@ -61,7 +59,7 @@ public class Publisher { CloseableHttpClient httpClient = null;
HttpPost httpPost = new HttpPost(url);
try {
- httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
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);
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 2df4d55..2aa5695 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 @@ -1,11 +1,11 @@ /** * Copyright 2017 ZTE Corporation. - * + * <p> * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at - * + * <p> * http://www.apache.org/licenses/LICENSE-2.0 - * + * <p> * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; + import lombok.extern.slf4j.Slf4j; import org.apache.http.Consts; import org.apache.http.HttpEntity; @@ -43,12 +44,14 @@ import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; 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.config.MicroServiceConfig; import org.onap.holmes.common.exception.CorrelationException; @Slf4j @@ -61,7 +64,7 @@ public class HttpsUtils { private static SSLContextBuilder sslContextBuilder = null; public static final int DEFUALT_TIMEOUT = 30000; - static{ + static { try { sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() { @@ -89,12 +92,12 @@ public class HttpsUtils { } public static HttpResponse post(HttpPost httpPost, Map<String, String> header, Map<String, String> param, - HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException { + HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException { return getPostAndPutResponse(httpPost, header, param, entity, httpClient); } public static HttpResponse put(HttpPut httpPut, Map<String, String> header, Map<String, String> param, - HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException { + HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException { return getPostAndPutResponse(httpPut, header, param, entity, httpClient); } @@ -124,8 +127,8 @@ public class HttpsUtils { } private static HttpResponse getPostAndPutResponse(HttpEntityEnclosingRequestBase requestBase, - Map<String, String> header, Map<String, String> param, HttpEntity entity, - CloseableHttpClient httpClient) throws CorrelationException { + Map<String, String> header, Map<String, String> param, HttpEntity entity, + CloseableHttpClient httpClient) throws CorrelationException { try { addHeaders(header, requestBase); addParams(param, requestBase); @@ -139,7 +142,7 @@ public class HttpsUtils { } private static HttpResponse getGetAndDeleteResponse(HttpRequestBase requestBase, - Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException { + Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException { try { addHeaders(header, requestBase); return executeRequest(httpClient, requestBase); @@ -169,23 +172,39 @@ public class HttpsUtils { try { httpResponse = httpClient.execute(httpRequest); } catch (Exception e) { - throw new CorrelationException("Failed to get data from server" ,e); + throw new CorrelationException("Failed to get data from server", e); } return httpResponse; } - public static CloseableHttpClient getHttpClient(int timeout) { + public static CloseableHttpClient getConditionalHttpsClient(int timeout) { + HttpClientBuilder builder = getHttpClientBuilder(timeout); + if (isHttpsEnabled()) { + builder.setSSLSocketFactory(sslConnectionSocketFactory); + } + + return builder.build(); + } + + public static CloseableHttpClient getHttpsClient(int timeout) { + HttpClientBuilder builder = getHttpClientBuilder(timeout); + return builder.setSSLSocketFactory(sslConnectionSocketFactory).build(); + } + + private static HttpClientBuilder getHttpClientBuilder(int timeout) { RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(timeout) .setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout) .build(); - CloseableHttpClient httpClient = HttpClients.custom() + + return HttpClients.custom() .setDefaultRequestConfig(defaultRequestConfig) - .setSSLSocketFactory(sslConnectionSocketFactory) .setConnectionManager(connectionManager) - .setConnectionManagerShared(true) - .build(); - return httpClient; + .setConnectionManagerShared(true); + } + + public static boolean isHttpsEnabled() { + return Boolean.valueOf(MicroServiceConfig.getEnv("ENABLE_ENCRYPT")); } } |