From 5d83c833212b68ed0537def331abb3edb5de79ea Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 5 Mar 2020 17:02:13 +0530 Subject: [PATCH] Create HTTP client based on hostname verification property --- .../request/interceptor/util/HandlerUtil.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java index a17e3dcb70..f35c0700ee 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java @@ -27,6 +27,7 @@ import org.apache.http.Consts; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; @@ -53,7 +54,7 @@ public class HandlerUtil { * @throws IOException IO exception returns if error occurs when executing the httpMethod */ public static ProxyResponse execute(HttpRequestBase httpRequest) throws IOException { - try (CloseableHttpClient client = HttpClients.createDefault()) { + try (CloseableHttpClient client = getHttpClient()) { HttpResponse response = client.execute(httpRequest); ProxyResponse proxyResponse = new ProxyResponse(); @@ -217,4 +218,18 @@ public class HandlerUtil { return gatewayPort; } + /** + * Retrieve Http client based on hostname verification. + * @return {@link CloseableHttpClient} http client + */ + public static CloseableHttpClient getHttpClient() { + boolean isIgnoreHostnameVerification = Boolean.parseBoolean(System. + getProperty("org.wso2.ignoreHostnameVerification")); + if (isIgnoreHostnameVerification) { + return HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build(); + } else { + return HttpClients.createDefault(); + } + } + }