Create HTTP client based on hostname verification property

feature/appm-store/pbac
Saad Sahibjan 5 years ago
parent aad275978f
commit 5d83c83321

@ -27,6 +27,7 @@ import org.apache.http.Consts;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; 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 * @throws IOException IO exception returns if error occurs when executing the httpMethod
*/ */
public static ProxyResponse execute(HttpRequestBase httpRequest) throws IOException { public static ProxyResponse execute(HttpRequestBase httpRequest) throws IOException {
try (CloseableHttpClient client = HttpClients.createDefault()) { try (CloseableHttpClient client = getHttpClient()) {
HttpResponse response = client.execute(httpRequest); HttpResponse response = client.execute(httpRequest);
ProxyResponse proxyResponse = new ProxyResponse(); ProxyResponse proxyResponse = new ProxyResponse();
@ -217,4 +218,18 @@ public class HandlerUtil {
return gatewayPort; 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();
}
}
} }

Loading…
Cancel
Save