Java Code Examples for org.apache.http.impl.client.HttpClientBuilder#setProxyAuthenticationStrategy()
The following examples show how to use
org.apache.http.impl.client.HttpClientBuilder#setProxyAuthenticationStrategy() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PGPKeysServerClient.java From pgpverify-maven-plugin with Apache License 2.0 | 7 votes |
protected HttpClientBuilder setupProxy(HttpClientBuilder clientBuilder) { if (this.proxy == null) { return clientBuilder; } if (proxy.getUsername() != null && !proxy.getUsername().isEmpty() && proxy.getPassword() != null && !proxy.getPassword().isEmpty()) { clientBuilder.setProxyAuthenticationStrategy(ProxyAuthenticationStrategy.INSTANCE); BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider(); AuthScope proxyAuthScope = new AuthScope(proxy.getHost(), proxy.getPort()); UsernamePasswordCredentials proxyAuthentication = new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()); basicCredentialsProvider.setCredentials(proxyAuthScope, proxyAuthentication); clientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider); } return clientBuilder; }
Example 2
Source File: HttpClientAdapter.java From uyuni with GNU General Public License v2.0 | 4 votes |
/** * Initialize an {@link HttpClient} for performing requests. Proxy settings will * be read from the configuration and applied transparently. */ public HttpClientAdapter() { Optional<SSLConnectionSocketFactory> sslSocketFactory = Optional.empty(); try { SSLContext sslContext = SSLContext.getDefault(); sslSocketFactory = Optional.of(new SSLConnectionSocketFactory( sslContext, new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"}, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier())); } catch (NoSuchAlgorithmException e) { log.warn("No such algorithm. Using default context", e); } HttpClientBuilder clientBuilder = HttpClientBuilder.create(); sslSocketFactory.ifPresent(sf -> clientBuilder.setSSLSocketFactory(sf)); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); Builder requestConfigBuilder = RequestConfig.custom() .setConnectTimeout(Config.get().getInt(HTTP_CONNECTION_TIMEOUT, 5) * TO_MILLISECONDS) .setSocketTimeout(Config.get().getInt(HTTP_SOCKET_TIMEOUT, 5 * 60) * TO_MILLISECONDS) .setCookieSpec(CookieSpecs.IGNORE_COOKIES); // Store the proxy settings String proxyHostname = ConfigDefaults.get().getProxyHost(); if (!StringUtils.isBlank(proxyHostname)) { int proxyPort = ConfigDefaults.get().getProxyPort(); proxyHost = new HttpHost(proxyHostname, proxyPort); clientBuilder.setProxy(proxyHost); String proxyUsername = ConfigDefaults.get().getProxyUsername(); String proxyPassword = ConfigDefaults.get().getProxyPassword(); if (!StringUtils.isBlank(proxyUsername) && !StringUtils.isBlank(proxyPassword)) { Credentials proxyCredentials = new UsernamePasswordCredentials( proxyUsername, proxyPassword); credentialsProvider.setCredentials(new AuthScope(proxyHostname, proxyPort), proxyCredentials); } // Explicitly exclude the NTLM authentication scheme requestConfigBuilder = requestConfigBuilder.setProxyPreferredAuthSchemes( Arrays.asList(AuthSchemes.DIGEST, AuthSchemes.BASIC)); clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); clientBuilder.setRoutePlanner(new CustomProxyRoutePlanner(proxyHost)); } // Read proxy exceptions from the "no_proxy" config option String noProxy = Config.get().getString(NO_PROXY); if (!StringUtils.isBlank(noProxy)) { for (String domain : Arrays.asList(noProxy.split(","))) { noProxyDomains.add(domain.toLowerCase().trim()); } } requestConfig = requestConfigBuilder.build(); clientBuilder.setMaxConnPerRoute(Config.get().getInt(MAX_CONNCECTIONS, 1)); clientBuilder.setMaxConnTotal(Config.get().getInt(MAX_CONNCECTIONS, 1)); httpClient = clientBuilder.build(); }
Example 3
Source File: HttpConnectionPoolBuilder.java From cyberduck with GNU General Public License v3.0 | 4 votes |
/** * @param proxy Proxy configuration * @param listener Log listener * @param prompt Prompt for proxy credentials * @return Builder for HTTP client */ public HttpClientBuilder build(final Proxy proxy, final TranscriptListener listener, final LoginCallback prompt) { final HttpClientBuilder configuration = HttpClients.custom(); // Use HTTP Connect proxy implementation provided here instead of // relying on internal proxy support in socket factory switch(proxy.getType()) { case HTTP: case HTTPS: final HttpHost h = new HttpHost(proxy.getHostname(), proxy.getPort(), Scheme.http.name()); if(log.isInfoEnabled()) { log.info(String.format("Setup proxy %s", h)); } configuration.setProxy(h); configuration.setProxyAuthenticationStrategy(new CallbackProxyAuthenticationStrategy(ProxyCredentialsStoreFactory.get(), host, prompt)); break; } configuration.setUserAgent(new PreferencesUseragentProvider().get()); final int timeout = preferences.getInteger("connection.timeout.seconds") * 1000; configuration.setDefaultSocketConfig(SocketConfig.custom() .setTcpNoDelay(true) .setSoTimeout(timeout) .build()); configuration.setDefaultRequestConfig(this.createRequestConfig(timeout)); configuration.setDefaultConnectionConfig(ConnectionConfig.custom() .setBufferSize(preferences.getInteger("http.socket.buffer")) .setCharset(Charset.forName(host.getEncoding())) .build()); if(preferences.getBoolean("http.connections.reuse")) { configuration.setConnectionReuseStrategy(new DefaultClientConnectionReuseStrategy()); } else { configuration.setConnectionReuseStrategy(new NoConnectionReuseStrategy()); } configuration.setRetryHandler(new ExtendedHttpRequestRetryHandler(preferences.getInteger("http.connections.retry"))); configuration.setServiceUnavailableRetryStrategy(new DisabledServiceUnavailableRetryStrategy()); if(!preferences.getBoolean("http.compression.enable")) { configuration.disableContentCompression(); } configuration.setRequestExecutor(new LoggingHttpRequestExecutor(listener)); // Always register HTTP for possible use with proxy. Contains a number of protocol properties such as the // default port and the socket factory to be used to create the java.net.Socket instances for the given protocol configuration.setConnectionManager(this.createConnectionManager(this.createRegistry())); configuration.setDefaultAuthSchemeRegistry(RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.BASIC, new BasicSchemeFactory( Charset.forName(preferences.getProperty("http.credentials.charset")))) .register(AuthSchemes.DIGEST, new DigestSchemeFactory( Charset.forName(preferences.getProperty("http.credentials.charset")))) .register(AuthSchemes.NTLM, preferences.getBoolean("webdav.ntlm.windows.authentication.enable") && WinHttpClients.isWinAuthAvailable() ? new BackportWindowsNTLMSchemeFactory(null) : new NTLMSchemeFactory()) .register(AuthSchemes.SPNEGO, preferences.getBoolean("webdav.ntlm.windows.authentication.enable") && WinHttpClients.isWinAuthAvailable() ? new BackportWindowsNegotiateSchemeFactory(null) : new SPNegoSchemeFactory()) .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build()); return configuration; }