use of org.wso2.carbon.apimgt.impl.proxy.ExtendedProxyRoutePlanner in project carbon-apimgt by wso2.
the class APIUtil method getHttpClient.
/**
* Return a http client instance
*
* @param port - server port
* @param protocol- service endpoint protocol http/https
* @return
*/
public static HttpClient getHttpClient(int port, String protocol) {
APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
String maxTotal = configuration.getFirstProperty(APIConstants.HTTP_CLIENT_MAX_TOTAL);
String defaultMaxPerRoute = configuration.getFirstProperty(APIConstants.HTTP_CLIENT_DEFAULT_MAX_PER_ROUTE);
String proxyEnabled = configuration.getFirstProperty(APIConstants.PROXY_ENABLE);
String proxyHost = configuration.getFirstProperty(APIConstants.PROXY_HOST);
String proxyPort = configuration.getFirstProperty(APIConstants.PROXY_PORT);
String proxyUsername = configuration.getFirstProperty(APIConstants.PROXY_USERNAME);
String proxyPassword = configuration.getFirstProperty(APIConstants.PROXY_PASSWORD);
String nonProxyHosts = configuration.getFirstProperty(APIConstants.NON_PROXY_HOSTS);
String proxyProtocol = configuration.getFirstProperty(APIConstants.PROXY_PROTOCOL);
if (proxyProtocol != null) {
protocol = proxyProtocol;
}
PoolingHttpClientConnectionManager pool = null;
try {
pool = getPoolingHttpClientConnectionManager(protocol);
} catch (APIManagementException e) {
log.error("Error while getting http client connection manager", e);
}
pool.setMaxTotal(Integer.parseInt(maxTotal));
pool.setDefaultMaxPerRoute(Integer.parseInt(defaultMaxPerRoute));
RequestConfig params = RequestConfig.custom().build();
HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(pool).setDefaultRequestConfig(params);
if (Boolean.parseBoolean(proxyEnabled)) {
HttpHost host = new HttpHost(proxyHost, Integer.parseInt(proxyPort), protocol);
DefaultProxyRoutePlanner routePlanner;
if (!StringUtils.isBlank(nonProxyHosts)) {
routePlanner = new ExtendedProxyRoutePlanner(host, configuration);
} else {
routePlanner = new DefaultProxyRoutePlanner(host);
}
clientBuilder = clientBuilder.setRoutePlanner(routePlanner);
if (!StringUtils.isBlank(proxyUsername) && !StringUtils.isBlank(proxyPassword)) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(proxyHost, Integer.parseInt(proxyPort)), new UsernamePasswordCredentials(proxyUsername, proxyPassword));
clientBuilder = clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}
return clientBuilder.build();
}
Aggregations