Search in sources :

Example 71 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project openolat by klemens.

the class HttpClientFactory method getHttpClientInstance.

/**
 * @param host For basic authentication
 * @param port For basic authentication
 * @param user For basic authentication
 * @param password For basic authentication
 * @param redirect If redirect is allowed
 * @return CloseableHttpClient
 */
public static CloseableHttpClient getHttpClientInstance(String host, int port, String user, String password, boolean redirect) {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketRegistry);
    SocketConfig.Builder socketConfigBuilder = SocketConfig.copy(SocketConfig.DEFAULT);
    socketConfigBuilder.setSoTimeout(10000);
    cm.setDefaultSocketConfig(socketConfigBuilder.build());
    HttpClientBuilder clientBuilder = HttpClientBuilder.create().setConnectionManager(cm).setMaxConnTotal(10).setDefaultCookieStore(new BasicCookieStore());
    if (redirect) {
        clientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
    } else {
        clientBuilder.setRedirectStrategy(new NoRedirectStrategy());
    }
    if (user != null && user.length() > 0) {
        CredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials(user, password));
        clientBuilder.setDefaultCredentialsProvider(provider);
    }
    return clientBuilder.build();
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) SocketConfig(org.apache.http.config.SocketConfig) AuthScope(org.apache.http.auth.AuthScope) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 72 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project Openfire by igniterealtime.

the class FaviconServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    // Create a pool of HTTP connections to use to get the favicons
    client = HttpClientBuilder.create().setConnectionManager(new PoolingHttpClientConnectionManager()).setRedirectStrategy(new LaxRedirectStrategy()).build();
    // Load the default favicon to use when no favicon was found of a remote host
    try {
        defaultBytes = Files.readAllBytes(Paths.get(JiveGlobals.getHomeDirectory(), "plugins/admin/webapp/images/server_16x16.gif"));
    } catch (final IOException e) {
        LOGGER.warn("Unable to retrieve default favicon", e);
    }
    // Initialize caches.
    missesCache = CacheFactory.createCache("Favicon Misses");
    hitsCache = CacheFactory.createCache("Favicon Hits");
}
Also used : IOException(java.io.IOException) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 73 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project oxTrust by GluuFederation.

the class UmaPermissionService method init.

public void init(@Observes @ApplicationInitialized(ApplicationScoped.class) ApplicationInitializedEvent init) {
    try {
        if (this.umaMetadata != null) {
            if (appConfiguration.isRptConnectionPoolUseConnectionPooling()) {
                // For more information about PoolingHttpClientConnectionManager, please see:
                // http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html?org/apache/http/impl/conn/PoolingHttpClientConnectionManager.html
                log.debug("##### Initializing custom ClientExecutor...");
                PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
                connectionManager.setMaxTotal(appConfiguration.getRptConnectionPoolMaxTotal());
                connectionManager.setDefaultMaxPerRoute(appConfiguration.getRptConnectionPoolDefaultMaxPerRoute());
                connectionManager.setValidateAfterInactivity(appConfiguration.getRptConnectionPoolValidateAfterInactivity() * 1000);
                CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build()).setKeepAliveStrategy(connectionKeepAliveStrategy).setConnectionManager(connectionManager).build();
                ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine(client);
                engine.setFollowRedirects(true);
                this.clientHttpEngine = engine;
                log.info("##### Initializing custom ClientExecutor DONE");
                this.permissionService = UmaClientFactory.instance().createPermissionService(this.umaMetadata, clientHttpEngine);
                this.rptStatusService = UmaClientFactory.instance().createRptStatusService(this.umaMetadata, clientHttpEngine);
            } else {
                this.permissionService = UmaClientFactory.instance().createPermissionService(this.umaMetadata);
                this.rptStatusService = UmaClientFactory.instance().createRptStatusService(this.umaMetadata);
            }
        }
    } catch (Exception ex) {
        log.error("Failed to initialize UmaPermissionService", ex);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ApacheHttpClient43Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine) MalformedURLException(java.net.MalformedURLException) OxIntializationException(org.gluu.exception.OxIntializationException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 74 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project oxTrust by GluuFederation.

the class ShibbolethReloadService method create.

@PostConstruct
public void create() {
    try {
        log.info(">>>>> Initializing ShibbolethReloadService");
        SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial(new TrustSelfSignedStrategy()).build();
        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
        SSLConnectionSocketFactory sslConnSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslConnSocketFactory).register("http", new PlainConnectionSocketFactory()).build();
        PoolingHttpClientConnectionManager poolingHttpConnMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        poolingHttpConnMgr.setMaxTotal(HTTP_DEFAULT_MAX_TOTAL_CONNECTIONS);
        poolingHttpConnMgr.setDefaultMaxPerRoute(HTTP_DEFAULT_MAX_CONN_PER_ROUTE);
        this.connectionManager = poolingHttpConnMgr;
        log.info(">>>> ShibbolethReloadService Initialization complete");
    } catch (Exception e) {
        this.connectionManager = null;
        log.info("Could not initialize ShibbolethReloadService", e);
    }
}
Also used : PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLContext(javax.net.ssl.SSLContext) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) IOException(java.io.IOException) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) PostConstruct(javax.annotation.PostConstruct)

Example 75 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project metacat by Netflix.

the class DruidHttpClientConfig method httpClient.

/**
 * Http client.
 *
 * @param connectorContext connector context
 * @return HttpClient
 */
@Bean
public HttpClient httpClient(final ConnectorContext connectorContext) {
    final int timeout = (int) TimeUtil.toTime(connectorContext.getConfiguration().getOrDefault(DruidConfigConstants.HTTP_TIMEOUT, "5s"), TimeUnit.SECONDS, TimeUnit.MILLISECONDS);
    final int poolsize = Integer.parseInt(connectorContext.getConfiguration().getOrDefault(DruidConfigConstants.POOL_SIZE, "10"));
    final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).setMaxRedirects(3).build();
    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(poolsize);
    return HttpClientBuilder.create().setDefaultRequestConfig(config).setConnectionManager(connectionManager).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Bean(org.springframework.context.annotation.Bean)

Aggregations

PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)180 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)63 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)62 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)54 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)52 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 SSLContext (javax.net.ssl.SSLContext)36 RequestConfig (org.apache.http.client.config.RequestConfig)31 IOException (java.io.IOException)29 Test (org.junit.Test)27 HttpHost (org.apache.http.HttpHost)21 HttpGet (org.apache.http.client.methods.HttpGet)18 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)17 AuthScope (org.apache.http.auth.AuthScope)16 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)16 HttpResponse (org.apache.http.HttpResponse)15 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14 HostnameVerifier (javax.net.ssl.HostnameVerifier)13 HttpClient (org.apache.http.client.HttpClient)12