List of usage examples for org.apache.http.impl.client HttpClientBuilder build
public CloseableHttpClient build()
From source file:com.ksc.http.apache.client.impl.ApacheHttpClientFactory.java
@Override public ConnectionManagerAwareHttpClient create(HttpClientSettings settings) { final HttpClientBuilder builder = HttpClients.custom(); // Note that it is important we register the original connection manager with the // IdleConnectionReaper as it's required for the successful deregistration of managers final HttpClientConnectionManager cm = cmFactory.create(settings); builder.setRequestExecutor(new SdkHttpRequestExecutor()) .setKeepAliveStrategy(buildKeepAliveStrategy(settings)).disableRedirectHandling() .disableAutomaticRetries().setConnectionManager(ClientConnectionManagerFactory.wrap(cm)); // By default http client enables Gzip compression. So we disable it // here./*from ww w . j ava2 s .c o m*/ // Apache HTTP client removes Content-Length, Content-Encoding and // Content-MD5 headers when Gzip compression is enabled. Currently // this doesn't affect S3 or Glacier which exposes these headers. // if (!(settings.useGzip())) { builder.disableContentCompression(); } addProxyConfig(builder, settings); final ConnectionManagerAwareHttpClient httpClient = new SdkHttpClient(builder.build(), cm); if (settings.useReaper()) { IdleConnectionReaper.registerConnectionManager(cm); } return httpClient; }
From source file:it.larusba.neo4j.jdbc.http.driver.CypherExecutor.java
/** * Default constructor.//from www. j a v a 2s .c o m * * @param host Hostname of the Neo4j instance. * @param port HTTP port of the Neo4j instance. * @param properties Properties of the url connection. * @throws SQLException */ public CypherExecutor(String host, Integer port, Properties properties) throws SQLException { // Create the http client builder HttpClientBuilder builder = HttpClients.custom(); // Adding authentication to the http client if needed if (properties.containsKey("user") && properties.containsKey("password")) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials( properties.get("user").toString(), properties.get("password").toString())); builder.setDefaultCredentialsProvider(credsProvider); } // Setting user-agent StringBuilder sb = new StringBuilder(); sb.append("Neo4j JDBC Driver"); if (properties.containsKey("userAgent")) { sb.append(" via "); sb.append(properties.getProperty("userAgent")); } builder.setUserAgent(sb.toString()); // Create the http client this.http = builder.build(); // Create the url endpoint StringBuffer sbEndpoint = new StringBuffer(); sbEndpoint.append("http://").append(host).append(":").append(port).append("/db/data/transaction"); this.transactionUrl = sbEndpoint.toString(); // Setting autocommit this.setAutoCommit(Boolean.valueOf(properties.getProperty("autoCommit", "true"))); }
From source file:ch.cyberduck.core.sds.SDSSession.java
@Override protected SDSApiClient connect(final HostKeyCallback key) throws BackgroundException { final HttpClientBuilder configuration = builder.build(this); switch (SDSProtocol.Authorization.valueOf(host.getProtocol().getAuthorization())) { case oauth:/*from w w w . ja v a 2 s . c o m*/ configuration .setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(authorizationService)); configuration.addInterceptorLast(authorizationService); configuration.addInterceptorLast(new HttpRequestInterceptor() { @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { request.removeHeaders(SDSSession.SDS_AUTH_TOKEN_HEADER); } }); break; default: configuration.setServiceUnavailableRetryStrategy(retryHandler); configuration.addInterceptorLast(retryHandler); break; } final CloseableHttpClient apache = configuration.build(); final SDSApiClient client = new SDSApiClient(apache); client.setBasePath(new HostUrlProvider(false, true).get(host.getProtocol().getScheme(), host.getPort(), null, host.getHostname(), host.getProtocol().getContext())); client.setHttpClient(ClientBuilder.newClient(new ClientConfig().register(new InputStreamProvider()) .register(MultiPartFeature.class).register(new JSON()).register(JacksonFeature.class) .connectorProvider(new HttpComponentsProvider(apache)))); client.setUserAgent(new PreferencesUseragentProvider().get()); return client; }
From source file:leap.webunit.client.THttpClientImpl.java
protected HttpClient createDefaultHttpClient() { HttpClientBuilder cb = HttpClientBuilder.create(); //TODO : small buffer size will cause socket closed when reading response entity? PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(getDefaultRegistry(), this.dnsResolver); //cm.setDefaultConnectionConfig(ConnectionConfig.custom().setBufferSize(1024 * 1024).build()); cb.setConnectionManager(cm);/*from ww w . j a v a 2 s . c o m*/ cb.setDefaultCookieStore(this.cookieStore); cb.setRedirectStrategy(new DefaultRedirectStrategy() { @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { if (!autoRedirect) { return false; } return super.isRedirected(request, response, context); } }); return cb.build(); }
From source file:org.openehealth.ipf.commons.ihe.fhir.SslAwareApacheRestfulClientFactory.java
protected synchronized HttpClient getNativeHttpClient() { if (httpClient == null) { // @formatter:off RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(getConnectTimeout()) .setSocketTimeout(getSocketTimeout()).setConnectionRequestTimeout(getConnectionRequestTimeout()) .setProxy(proxy).setStaleConnectionCheckEnabled(true).build(); HttpClientBuilder builder = HttpClients.custom().useSystemProperties() .setDefaultRequestConfig(defaultRequestConfig).setMaxConnTotal(getPoolMaxTotal()) .setMaxConnPerRoute(getPoolMaxPerRoute()).setConnectionTimeToLive(5, TimeUnit.SECONDS) .disableCookieManagement(); if (securityInformation != null) { securityInformation.configureHttpClientBuilder(builder); }//from ww w. ja va 2s . c o m // Need to authenticate against proxy if (proxy != null && StringUtils.isNotBlank(getProxyUsername()) && StringUtils.isNotBlank(getProxyPassword())) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxy.getHostName(), proxy.getPort()), new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword())); builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); builder.setDefaultCredentialsProvider(credentialsProvider); } httpClient = builder.build(); } return httpClient; }
From source file:com.mirth.connect.connectors.http.HttpDispatcher.java
@Override public Response send(ConnectorProperties connectorProperties, ConnectorMessage connectorMessage) { HttpDispatcherProperties httpDispatcherProperties = (HttpDispatcherProperties) connectorProperties; eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(), getDestinationName(), ConnectionStatusEventType.WRITING)); String responseData = null;//w w w. j a v a 2 s . co m String responseError = null; String responseStatusMessage = null; Status responseStatus = Status.QUEUED; boolean validateResponse = false; CloseableHttpClient client = null; HttpRequestBase httpMethod = null; CloseableHttpResponse httpResponse = null; File tempFile = null; int socketTimeout = NumberUtils.toInt(httpDispatcherProperties.getSocketTimeout(), 30000); try { configuration.configureDispatcher(this, httpDispatcherProperties); long dispatcherId = getDispatcherId(); client = clients.get(dispatcherId); if (client == null) { BasicHttpClientConnectionManager httpClientConnectionManager = new BasicHttpClientConnectionManager( socketFactoryRegistry.build()); httpClientConnectionManager .setSocketConfig(SocketConfig.custom().setSoTimeout(socketTimeout).build()); HttpClientBuilder clientBuilder = HttpClients.custom() .setConnectionManager(httpClientConnectionManager); HttpUtil.configureClientBuilder(clientBuilder); if (httpDispatcherProperties.isUseProxyServer()) { clientBuilder.setRoutePlanner(new DynamicProxyRoutePlanner()); } client = clientBuilder.build(); clients.put(dispatcherId, client); } URI hostURI = new URI(httpDispatcherProperties.getHost()); String host = hostURI.getHost(); String scheme = hostURI.getScheme(); int port = hostURI.getPort(); if (port == -1) { if (scheme.equalsIgnoreCase("https")) { port = 443; } else { port = 80; } } // Parse the content type field first, and then add the charset if needed ContentType contentType = ContentType.parse(httpDispatcherProperties.getContentType()); Charset charset = null; if (contentType.getCharset() == null) { charset = Charset.forName(CharsetUtils.getEncoding(httpDispatcherProperties.getCharset())); } else { charset = contentType.getCharset(); } if (httpDispatcherProperties.isMultipart()) { tempFile = File.createTempFile(UUID.randomUUID().toString(), ".tmp"); } HttpHost target = new HttpHost(host, port, scheme); httpMethod = buildHttpRequest(hostURI, httpDispatcherProperties, connectorMessage, tempFile, contentType, charset); HttpClientContext context = HttpClientContext.create(); // authentication if (httpDispatcherProperties.isUseAuthentication()) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); Credentials credentials = new UsernamePasswordCredentials(httpDispatcherProperties.getUsername(), httpDispatcherProperties.getPassword()); credsProvider.setCredentials(authScope, credentials); AuthCache authCache = new BasicAuthCache(); RegistryBuilder<AuthSchemeProvider> registryBuilder = RegistryBuilder.<AuthSchemeProvider>create(); if (AuthSchemes.DIGEST.equalsIgnoreCase(httpDispatcherProperties.getAuthenticationType())) { logger.debug("using Digest authentication"); registryBuilder.register(AuthSchemes.DIGEST, new DigestSchemeFactory(charset)); if (httpDispatcherProperties.isUsePreemptiveAuthentication()) { processDigestChallenge(authCache, target, credentials, httpMethod, context); } } else { logger.debug("using Basic authentication"); registryBuilder.register(AuthSchemes.BASIC, new BasicSchemeFactory(charset)); if (httpDispatcherProperties.isUsePreemptiveAuthentication()) { authCache.put(target, new BasicScheme()); } } context.setCredentialsProvider(credsProvider); context.setAuthSchemeRegistry(registryBuilder.build()); context.setAuthCache(authCache); logger.debug("using authentication with credentials: " + credentials); } RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(socketTimeout) .setSocketTimeout(socketTimeout).setStaleConnectionCheckEnabled(true).build(); context.setRequestConfig(requestConfig); // Set proxy information if (httpDispatcherProperties.isUseProxyServer()) { context.setAttribute(PROXY_CONTEXT_KEY, new HttpHost(httpDispatcherProperties.getProxyAddress(), Integer.parseInt(httpDispatcherProperties.getProxyPort()))); } // execute the method logger.debug( "executing method: type=" + httpMethod.getMethod() + ", uri=" + httpMethod.getURI().toString()); httpResponse = client.execute(target, httpMethod, context); StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); logger.debug("received status code: " + statusCode); Map<String, List<String>> headers = new HashMap<String, List<String>>(); for (Header header : httpResponse.getAllHeaders()) { List<String> list = headers.get(header.getName()); if (list == null) { list = new ArrayList<String>(); headers.put(header.getName(), list); } list.add(header.getValue()); } connectorMessage.getConnectorMap().put("responseStatusLine", statusLine.toString()); connectorMessage.getConnectorMap().put("responseHeaders", new MessageHeaders(new CaseInsensitiveMap(headers))); ContentType responseContentType = ContentType.get(httpResponse.getEntity()); if (responseContentType == null) { responseContentType = ContentType.TEXT_PLAIN; } Charset responseCharset = charset; if (responseContentType.getCharset() != null) { responseCharset = responseContentType.getCharset(); } final String responseBinaryMimeTypes = httpDispatcherProperties.getResponseBinaryMimeTypes(); BinaryContentTypeResolver binaryContentTypeResolver = new BinaryContentTypeResolver() { @Override public boolean isBinaryContentType(ContentType contentType) { return HttpDispatcher.this.isBinaryContentType(responseBinaryMimeTypes, contentType); } }; /* * First parse out the body of the HTTP response. Depending on the connector settings, * this could end up being a string encoded with the response charset, a byte array * representing the raw response payload, or a MimeMultipart object. */ Object responseBody = ""; // The entity could be null in certain cases such as 204 responses if (httpResponse.getEntity() != null) { // Only parse multipart if XML Body is selected and Parse Multipart is enabled if (httpDispatcherProperties.isResponseXmlBody() && httpDispatcherProperties.isResponseParseMultipart() && responseContentType.getMimeType().startsWith(FileUploadBase.MULTIPART)) { responseBody = new MimeMultipart(new ByteArrayDataSource(httpResponse.getEntity().getContent(), responseContentType.toString())); } else if (binaryContentTypeResolver.isBinaryContentType(responseContentType)) { responseBody = IOUtils.toByteArray(httpResponse.getEntity().getContent()); } else { responseBody = IOUtils.toString(httpResponse.getEntity().getContent(), responseCharset); } } /* * Now that we have the response body, we need to create the actual Response message * data. Depending on the connector settings this could be our custom serialized XML, a * Base64 string encoded from the raw response payload, or a string encoded from the * payload with the request charset. */ if (httpDispatcherProperties.isResponseXmlBody()) { responseData = HttpMessageConverter.httpResponseToXml(statusLine.toString(), headers, responseBody, responseContentType, httpDispatcherProperties.isResponseParseMultipart(), httpDispatcherProperties.isResponseIncludeMetadata(), binaryContentTypeResolver); } else if (responseBody instanceof byte[]) { responseData = new String(Base64Util.encodeBase64((byte[]) responseBody), "US-ASCII"); } else { responseData = (String) responseBody; } validateResponse = httpDispatcherProperties.getDestinationConnectorProperties().isValidateResponse(); if (statusCode < HttpStatus.SC_BAD_REQUEST) { responseStatus = Status.SENT; } else { eventController.dispatchEvent(new ErrorEvent(getChannelId(), getMetaDataId(), connectorMessage.getMessageId(), ErrorEventType.DESTINATION_CONNECTOR, getDestinationName(), connectorProperties.getName(), "Received error response from HTTP server.", null)); responseStatusMessage = ErrorMessageBuilder .buildErrorResponse("Received error response from HTTP server.", null); responseError = ErrorMessageBuilder.buildErrorMessage(connectorProperties.getName(), responseData, null); } } catch (Exception e) { eventController.dispatchEvent(new ErrorEvent(getChannelId(), getMetaDataId(), connectorMessage.getMessageId(), ErrorEventType.DESTINATION_CONNECTOR, getDestinationName(), connectorProperties.getName(), "Error connecting to HTTP server.", e)); responseStatusMessage = ErrorMessageBuilder.buildErrorResponse("Error connecting to HTTP server", e); responseError = ErrorMessageBuilder.buildErrorMessage(connectorProperties.getName(), "Error connecting to HTTP server", e); } finally { try { HttpClientUtils.closeQuietly(httpResponse); // Delete temp files if we created them if (tempFile != null) { tempFile.delete(); tempFile = null; } } finally { eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(), getDestinationName(), ConnectionStatusEventType.IDLE)); } } return new Response(responseStatus, responseData, responseStatusMessage, responseError, validateResponse); }
From source file:org.jasig.cas.util.http.SimpleHttpClientFactoryBean.java
/** * Build a HTTP client based on the current properties. * * @return the built HTTP client// w w w.j a v a2 s. c o m */ private CloseableHttpClient buildHttpClient() { try { final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory(); final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory; final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", plainsf).register("https", sslsf).build(); final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry); connMgmr.setMaxTotal(this.maxPooledConnections); connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute); final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost()); final HttpRoute httpRoute = new HttpRoute(httpHost); connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE); final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.readTimeout) .setConnectTimeout(this.connectionTimeout).setConnectionRequestTimeout(this.connectionTimeout) .setStaleConnectionCheckEnabled(true).setCircularRedirectsAllowed(this.circularRedirectsAllowed) .setRedirectsEnabled(this.redirectsEnabled).setAuthenticationEnabled(this.authenticationEnabled) .build(); final HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connMgmr) .setDefaultRequestConfig(requestConfig).setSSLSocketFactory(sslsf) .setSSLHostnameVerifier(this.hostnameVerifier).setRedirectStrategy(this.redirectionStrategy) .setDefaultCredentialsProvider(this.credentialsProvider).setDefaultCookieStore(this.cookieStore) .setConnectionReuseStrategy(this.connectionReuseStrategy) .setConnectionBackoffStrategy(this.connectionBackoffStrategy) .setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy) .setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy) .setDefaultHeaders(this.defaultHeaders).useSystemProperties(); return builder.build(); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); throw new RuntimeException(e); } }
From source file:com.redhat.red.offliner.Main.java
/** * Sets up components needed for the download process, including the {@link ExecutorCompletionService}, * {@link java.util.concurrent.Executor}, {@link org.apache.http.client.HttpClient}, and {@link ArtifactListReader} * instances. If baseUrls were provided on the command line, it will initialize the "global" baseUrls list to that. * Otherwise it will use {@link Options#DEFAULT_REPO_URL} and {@link Options#CENTRAL_REPO_URL} as the default * baseUrls. If specified, configures the HTTP proxy and username/password for authentication. * @throws MalformedURLException In case an invalid {@link URL} is given as a baseUrl. */// w w w . ja va 2 s .com protected void init() throws MalformedURLException { int threads = opts.getThreads(); executorService = Executors.newFixedThreadPool(threads, (final Runnable r) -> { // executorService = Executors.newCachedThreadPool( ( final Runnable r ) -> { final Thread t = new Thread(r); t.setDaemon(true); return t; }); executor = new ExecutorCompletionService<>(executorService); errors = new ConcurrentHashMap<String, Throwable>(); final PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(); ccm.setMaxTotal(opts.getConnections()); final HttpClientBuilder builder = HttpClients.custom().setConnectionManager(ccm); final String proxy = opts.getProxy(); String proxyHost = proxy; int proxyPort = 8080; if (proxy != null) { final int portSep = proxy.lastIndexOf(':'); if (portSep > -1) { proxyHost = proxy.substring(0, portSep); proxyPort = Integer.parseInt(proxy.substring(portSep + 1)); } final HttpRoutePlanner planner = new DefaultProxyRoutePlanner(new HttpHost(proxyHost, proxyPort)); builder.setRoutePlanner(planner); } client = builder.build(); final CredentialsProvider creds = new BasicCredentialsProvider(); cookieStore = new BasicCookieStore(); baseUrls = opts.getBaseUrls(); if (baseUrls == null) { baseUrls = new ArrayList<>(); } List<String> repoUrls = (baseUrls.isEmpty() ? DEFAULT_URLS : baseUrls); System.out.println("Planning download from:\n " + StringUtils.join(repoUrls, "\n ")); for (String repoUrl : repoUrls) { if (repoUrl != null) { final String user = opts.getUser(); if (user != null) { final URL u = new URL(repoUrl); final AuthScope as = new AuthScope(u.getHost(), UrlUtils.getPort(u)); creds.setCredentials(as, new UsernamePasswordCredentials(user, opts.getPassword())); } } if (proxy != null) { final String proxyUser = opts.getProxyUser(); if (proxyUser != null) { creds.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, opts.getProxyPassword())); } } } artifactListReaders = new ArrayList<>(3); artifactListReaders.add(new FoloReportArtifactListReader()); artifactListReaders.add(new PlaintextArtifactListReader()); artifactListReaders.add(new PomArtifactListReader(opts.getSettingsXml(), opts.getTypeMapping(), creds)); }
From source file:io.github.cidisk.indexcrawler.fetcher.PageFetcher.java
public PageFetcher(CrawlConfig config) { super(config); RequestConfig requestConfig = RequestConfig.custom().setExpectContinueEnabled(false) .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).setRedirectsEnabled(false) .setSocketTimeout(config.getSocketTimeout()).setConnectTimeout(config.getConnectionTimeout()) .build();//from ww w.j ava 2 s . com RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create(); connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE); if (config.isIncludeHttpsPages()) { try { // Fixing: https://code.google.com/p/crawler4j/issues/detail?id=174 // By always trusting the ssl certificate SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(final X509Certificate[] chain, String authType) { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); connRegistryBuilder.register("https", sslsf); } catch (Exception e) { logger.warn("Exception thrown while trying to register https"); logger.debug("Stacktrace", e); } } Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build(); connectionManager = new PoolingHttpClientConnectionManager(connRegistry); connectionManager.setMaxTotal(config.getMaxTotalConnections()); connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost()); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setDefaultRequestConfig(requestConfig); clientBuilder.setConnectionManager(connectionManager); clientBuilder.setUserAgent(config.getUserAgentString()); if (config.getProxyHost() != null) { if (config.getProxyUsername() != null) { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword())); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); } HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort()); clientBuilder.setProxy(proxy); logger.debug("Working through Proxy: {}", proxy.getHostName()); } httpClient = clientBuilder.build(); if (config.getAuthInfos() != null && !config.getAuthInfos().isEmpty()) { doAuthetication(config.getAuthInfos()); } if (connectionMonitorThread == null) { connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager); } connectionMonitorThread.start(); }