public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { s_logger.info("Creating new HTTP connection pool and client"); final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration(); final BasicCookieStore cookieStore = new BasicCookieStore(); final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); connManager.setDefaultMaxPerRoute(MAX_ALLOCATED_CONNECTIONS_PER_ROUTE); connManager.setMaxTotal(MAX_ALLOCATED_CONNECTIONS); final RequestConfig requestConfig = RequestConfig.custom() .setCookieSpec(CookieSpecs.DEFAULT) .setMaxRedirects(maxRedirects) .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT) .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT) .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT) .build(); return HttpClientBuilder.create() .setConnectionManager(connManager) .setRedirectStrategy(new LaxRedirectStrategy()) .setDefaultRequestConfig(requestConfig) .setDefaultCookieStore(cookieStore) .setRetryHandler(new StandardHttpRequestRetryHandler()) .build(); }
@Override public void customize(final HttpClientPlan plan) { checkNotNull(plan); plan.setUserAgentBase(userAgentGenerator.generate()); plan.getClient().setKeepAliveStrategy(new NexusConnectionKeepAliveStrategy(keepAliveDuration.toMillis())); plan.getClient().setRetryHandler(new StandardHttpRequestRetryHandler(2, false)); plan.getConnection().setBufferSize(bufferSize.toBytesI()); plan.getRequest().setConnectionRequestTimeout(connectionRequestTimeout.toMillisI()); plan.getRequest().setCookieSpec(CookieSpecs.IGNORE_COOKIES); plan.getRequest().setExpectContinueEnabled(false); int requestTimeoutMillis = requestTimeout.toMillisI(); plan.getSocket().setSoTimeout(requestTimeoutMillis); plan.getRequest().setConnectTimeout(requestTimeoutMillis); plan.getRequest().setSocketTimeout(requestTimeoutMillis); }
private static HttpClient buildClient(final String fedoraUsername, final String fedoraPassword, final String repositoryURL) { final PoolingClientConnectionManager connMann = new PoolingClientConnectionManager(); connMann.setMaxTotal(MAX_VALUE); connMann.setDefaultMaxPerRoute(MAX_VALUE); final DefaultHttpClient httpClient = new DefaultHttpClient(connMann); httpClient.setRedirectStrategy(new DefaultRedirectStrategy()); httpClient.setHttpRequestRetryHandler(new StandardHttpRequestRetryHandler(0, false)); // If the Fedora instance requires authentication, set it up here if (!isBlank(fedoraUsername) && !isBlank(fedoraPassword)) { LOGGER.debug("Adding BASIC credentials to client for repo requests."); final URI fedoraUri = URI.create(repositoryURL); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(fedoraUri.getHost(), fedoraUri.getPort()), new UsernamePasswordCredentials(fedoraUsername, fedoraPassword)); httpClient.setCredentialsProvider(credsProvider); } return httpClient; }
private static CloseableHttpClient createHttpClient(String host, int port, String username, String password) { SSLContext sslContext = org.apache.http.ssl.SSLContexts.createDefault(); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslConnectionSocketFactory) .build(); BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(host, port, MANAGEMENT_REALM, AuthSchemes.DIGEST), new UsernamePasswordCredentials(username, password)); return HttpClientBuilder.create() .setConnectionManager(new PoolingHttpClientConnectionManager(registry)) .setRetryHandler(new StandardHttpRequestRetryHandler(5, true)) .setDefaultCredentialsProvider(credentialsProvider) .build(); }
private static CloseableHttpClient createHttpClient(String host, int port, String username, String password) { try { SSLContext sslContext = SSLContexts.createDefault(); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sslConnectionSocketFactory) .register("http", PlainConnectionSocketFactory.getSocketFactory()) .build(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(host, port, MANAGEMENT_REALM, AuthSchemes.DIGEST), new UsernamePasswordCredentials(username, password)); PoolingHttpClientConnectionManager connectionPool = new PoolingHttpClientConnectionManager(registry); HttpClientBuilder.create().setConnectionManager(connectionPool).build(); return HttpClientBuilder.create() .setConnectionManager(connectionPool) .setRetryHandler(new StandardHttpRequestRetryHandler(5, true)) .setDefaultCredentialsProvider(credsProvider).build(); } catch (Exception e) { throw new RuntimeException(e); } }
public HttpEndpoint(URI endpoint, Config cfg, HttpClientContextFactory clientContextFactory) { if (endpoint == null) { throw new IllegalArgumentException("Endpoint is required"); } if (cfg == null) { cfg = new ConfigurationBuilder().build(); } CacheConfig cacheConfig = CacheConfig.custom() .setMaxCacheEntries(cfg.getMaxCacheEntries()) .setMaxObjectSize(cfg.getMaxCacheObjectSize()) .build(); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(1000 * cfg.getConnectTimeOutSeconds()) .setSocketTimeout(1000 * cfg.getSocketTimeOutSeconds()) .build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(cfg.getMaxConections()); this.endpoint = endpoint; this.httpClient = CachingHttpClients.custom() .setCacheConfig(cacheConfig) .setDefaultRequestConfig(requestConfig) .setRetryHandler(new StandardHttpRequestRetryHandler()) .setConnectionManager(cm) .build(); this.clientContextFactory = clientContextFactory; initPingThread(cfg.getPingSeconds()); }
/** * Apply connection configuration to plan. */ private void apply(final ConnectionConfiguration connection, final HttpClientPlan plan) { if (connection.getTimeout() != null) { int timeout = connection.getTimeout().toMillisI(); plan.getSocket().setSoTimeout(timeout); plan.getRequest().setConnectTimeout(timeout); plan.getRequest().setSocketTimeout(timeout); } if (connection.getMaximumRetries() != null) { plan.getClient().setRetryHandler(new StandardHttpRequestRetryHandler(connection.getMaximumRetries(), false)); } if (connection.getUserAgentSuffix() != null) { checkState(plan.getUserAgentBase() != null, "Default User-Agent not set"); plan.setUserAgentSuffix(connection.getUserAgentSuffix()); } if (Boolean.TRUE.equals(connection.getUseTrustStore())) { plan.getAttributes().put(SSLContextSelector.USE_TRUST_STORE, Boolean.TRUE); } if (Boolean.TRUE.equals(connection.getEnableCircularRedirects())) { plan.getRequest().setCircularRedirectsAllowed(true); } if (Boolean.TRUE.equals(connection.getEnableCookies())) { plan.getRequest().setCookieSpec(CookieSpecs.DEFAULT); } }
public void start() { this.cookieStore = new BasicCookieStore(); this.builder = HttpClientBuilder.create() .setMaxConnTotal(1) .setRetryHandler(new StandardHttpRequestRetryHandler(0, false)) .setMaxConnPerRoute(1) .setDefaultCookieStore(cookieStore); }
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration(); final BasicCookieStore cookieStore = new BasicCookieStore(); return HttpClientBuilder.create() .setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry)) .setRedirectStrategy(new LaxRedirectStrategy()) .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setMaxRedirects(maxRedirects).build()) .setDefaultCookieStore(cookieStore) .setRetryHandler(new StandardHttpRequestRetryHandler()) .build(); }
protected HttpRequestRetryHandler createHttpRequestRetryHandler() { return new StandardHttpRequestRetryHandler(); }
private CloseableHttpClient build(final Consumer<HttpClientBuilder> customizeBuilder) throws IOException { LOGGER.debug("HTTP {} {}", request, url); if (httpClient != null) { LOGGER.debug("Existing HttpClient re-used"); return httpClient; } if (sslsf == null) { sslsf = createSSLConnectionSocketFactory(null, null, false); } HttpClientBuilder builder = HttpClients.custom() .setRetryHandler(new StandardHttpRequestRetryHandler(3, true) { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { } return super.retryRequest(exception, executionCount, context); } }).setSSLSocketFactory(sslsf); if (username != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(url.getHost(), url.getPort() == -1 ? url.getDefaultPort() : url.getPort()), new UsernamePasswordCredentials(username, password)); builder.setDefaultCredentialsProvider(credsProvider); } if (disableRedirect) { builder.disableRedirectHandling(); } addPreemptiveAuthorizationHeaders(); if (customizeBuilder != null) { customizeBuilder.accept(builder); } if (!cookies.isEmpty()) { final CookieStore cookieStore = new BasicCookieStore(); cookies.entrySet().stream().map(x -> new BasicClientCookie(x.getKey(), x.getValue())).forEach(cookieStore::addCookie); builder.setDefaultCookieStore(cookieStore); } return builder.build(); }
/** * Returns a HTTP client doing Basic Auth if needed. * @param url The URL to browse. * @param user The user. Maybe null. * @param password The password. Maybe null. * @return The HTTP client. */ public static CloseableHttpClient getClient( URL url, String user, String password ) { int timeout = DEFAULT_TIMEOUT; ApplicationSettings set = Config .getInstance() .getApplicationSettings(); String ts = set.getApplicationSetting("requestTimeout_s"); if (ts != null) { try { timeout = S_TO_MS * Integer.parseInt(ts); } catch (NumberFormatException nfe) { log.log(Level.SEVERE, nfe.getMessage()); } } // Use JVM proxy settings. SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault()); RequestConfig requestConfig = RequestConfig. custom(). setSocketTimeout(timeout).build(); HttpClientBuilder builder = HttpClientBuilder.create() .setDefaultRequestConfig(requestConfig); builder.setRetryHandler(new StandardHttpRequestRetryHandler(1, true)); builder.setRoutePlanner(routePlanner); builder.setUserAgent(getUserAgent()); if (user != null && password != null) { UsernamePasswordCredentials defaultCreds = new UsernamePasswordCredentials(user, password); BasicCredentialsProvider credsProv = new BasicCredentialsProvider(); credsProv.setCredentials( new AuthScope(url.getHost(), url.getPort()), defaultCreds); HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProv); BasicAuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); HttpHost target = new HttpHost(url.getHost(), url.getPort()); authCache.put(target, basicAuth); context.setAuthCache(authCache); builder.setDefaultCredentialsProvider(credsProv); } return builder.build(); }