Search in sources :

Example 1 with ExtendedProxyRoutePlanner

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();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) ExtendedProxyRoutePlanner(org.wso2.carbon.apimgt.impl.proxy.ExtendedProxyRoutePlanner) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope)

Aggregations

HttpHost (org.apache.http.HttpHost)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 DefaultProxyRoutePlanner (org.apache.http.impl.conn.DefaultProxyRoutePlanner)1 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)1 ExtendedProxyRoutePlanner (org.wso2.carbon.apimgt.impl.proxy.ExtendedProxyRoutePlanner)1