List of usage examples for org.apache.http.impl.client HttpClientBuilder setDefaultCredentialsProvider
public final HttpClientBuilder setDefaultCredentialsProvider(final CredentialsProvider credentialsProvider)
From source file:de.codecentric.elasticsearch.plugin.kerberosrealm.AbstractUnitTest.java
protected final CloseableHttpClient getHttpClient(final boolean useSpnego) throws Exception { final CredentialsProvider credsProvider = new BasicCredentialsProvider(); final HttpClientBuilder hcb = HttpClients.custom(); if (useSpnego) { //SPNEGO/Kerberos setup log.debug("SPNEGO activated"); final AuthSchemeProvider nsf = new SPNegoSchemeFactory(true);// new NegotiateSchemeProvider(); final Credentials jaasCreds = new JaasCredentials(); credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.SPNEGO), jaasCreds); credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.NTLM), new NTCredentials("Guest", "Guest", "Guest", "Guest")); final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.SPNEGO, nsf).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).build(); hcb.setDefaultAuthSchemeRegistry(authSchemeRegistry); }/*from w w w . ja v a 2 s. c o m*/ hcb.setDefaultCredentialsProvider(credsProvider); hcb.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(10 * 1000).build()); final CloseableHttpClient httpClient = hcb.build(); return httpClient; }
From source file:eu.europa.ec.markt.dss.validation102853.https.CommonDataLoader.java
/** * Define the Credentials//from w w w . jav a2s.c o m * * @param uri * @return {@code HttpClientBuilder} */ private HttpClientBuilder configCredentials(final URI uri) throws DSSException { HttpClientBuilder httpClientBuilder = HttpClients.custom(); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); for (final Map.Entry<HttpHost, UsernamePasswordCredentials> entry : authenticationMap.entrySet()) { final HttpHost httpHost = entry.getKey(); final UsernamePasswordCredentials usernamePasswordCredentials = entry.getValue(); final AuthScope authscope = new AuthScope(httpHost.getHostName(), httpHost.getPort()); credentialsProvider.setCredentials(authscope, usernamePasswordCredentials); } httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); httpClientBuilder = configureProxy(httpClientBuilder, credentialsProvider, uri); return httpClientBuilder; }
From source file:microsoft.exchange.webservices.data.HttpClientWebRequest.java
/** * Prepare asynchronous connection.// www. ja v a 2s. c o m * * @throws microsoft.exchange.webservices.data.EWSHttpException throws EWSHttpException */ public void prepareAsyncConnection() throws EWSHttpException { try { //ssl config HttpClientBuilder builder = HttpClients.custom(); builder.setConnectionManager(this.httpClientConnMng); builder.setSchemePortResolver(new DefaultSchemePortResolver()); EwsSSLProtocolSocketFactory factory = EwsSSLProtocolSocketFactory.build(trustManger); builder.setSSLSocketFactory(factory); builder.setSslcontext(factory.getContext()); //create the cookie store if (cookieStore == null) { cookieStore = new BasicCookieStore(); } builder.setDefaultCookieStore(cookieStore); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(getUserName(), getPassword(), "", getDomain())); builder.setDefaultCredentialsProvider(credsProvider); //fix socket config SocketConfig sc = SocketConfig.custom().setSoTimeout(getTimeout()).build(); builder.setDefaultSocketConfig(sc); RequestConfig.Builder rcBuilder = RequestConfig.custom(); rcBuilder.setConnectionRequestTimeout(getTimeout()); rcBuilder.setConnectTimeout(getTimeout()); rcBuilder.setSocketTimeout(getTimeout()); // fix issue #144 + #160: if we used NTCredentials from above: these are NT credentials ArrayList<String> authPrefs = new ArrayList<String>(); authPrefs.add(AuthSchemes.NTLM); rcBuilder.setTargetPreferredAuthSchemes(authPrefs); // builder.setDefaultRequestConfig(rcBuilder.build()); //HttpClientParams.setRedirecting(client.getParams(), isAllowAutoRedirect()); by default it follows redirects //create the client and execute requests client = builder.build(); httpPostReq = new HttpPost(getUrl().toString()); response = client.execute(httpPostReq); } catch (IOException e) { client = null; httpPostReq = null; throw new EWSHttpException("Unable to open connection to " + this.getUrl()); } catch (Exception e) { client = null; httpPostReq = null; e.printStackTrace(); throw new EWSHttpException("SSL problem " + this.getUrl()); } }
From source file:com.hp.mqm.clt.RestClient.java
public RestClient(Settings settings) { this.settings = settings; HttpClientBuilder httpClientBuilder = HttpClients.custom(); cookieStore = new BasicCookieStore(); httpClientBuilder.setDefaultCookieStore(cookieStore); RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD) .setSocketTimeout(DEFAULT_SO_TIMEOUT).setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT); // proxy setting if (StringUtils.isNotEmpty(settings.getProxyHost())) { HttpHost proxy = new HttpHost(settings.getProxyHost(), settings.getProxyPort()); requestConfigBuilder.setProxy(proxy); if (settings.getProxyUser() != null) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(settings.getProxyHost(), settings.getProxyPort()), new UsernamePasswordCredentials(settings.getProxyUser(), settings.getProxyPassword())); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); }/*from w w w. j av a2 s. com*/ } httpClient = httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()).build(); }
From source file:com.jaeksoft.searchlib.crawler.web.spider.HttpAbstract.java
public HttpAbstract(String userAgent, boolean bFollowRedirect, ProxyHandler proxyHandler) { HttpClientBuilder builder = HttpClients.custom(); redirectStrategy = new DefaultRedirectStrategy(); if (userAgent != null) { userAgent = userAgent.trim();/*w w w . j a va 2 s . c om*/ if (userAgent.length() > 0) builder.setUserAgent(userAgent); else userAgent = null; } if (!bFollowRedirect) builder.disableRedirectHandling(); this.proxyHandler = proxyHandler; Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.NTLM, new NTLMSchemeFactory()) .register(AuthSchemes.BASIC, new BasicSchemeFactory()) .register(AuthSchemes.DIGEST, new DigestSchemeFactory()) .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()) .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build(); credentialsProvider = new BasicCredentialsProvider(); builder.setDefaultCredentialsProvider(credentialsProvider); cookieStore = new BasicCookieStore(); builder.setDefaultCookieStore(cookieStore); builder.setDefaultCredentialsProvider(credentialsProvider); builder.setDefaultAuthSchemeRegistry(authSchemeRegistry); httpClient = builder.build(); }
From source file:eu.itesla_project.histodb.client.impl.HistoDbHttpClientImpl.java
private synchronized CloseableHttpClient getHttpclient(HistoDbConfig config) { if (httpClient == null) { try {// w w w . j av a2 s. co m ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory(); TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] certs, String authType) { } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); LayeredConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", plainsf).register("https", sslsf).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(r); cm.setDefaultMaxPerRoute(10); cm.setMaxTotal(20); HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(cm); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( new AuthScope(new HttpHost(config.getConnectionParameters().getHost(), config.getConnectionParameters().getPort())), new UsernamePasswordCredentials(config.getConnectionParameters().getUserName(), config.getConnectionParameters().getPassword())); if (config.getProxyParameters() != null) { HttpHost proxy = new HttpHost(config.getProxyParameters().getHost(), config.getProxyParameters().getPort()); credentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials( config.getProxyParameters().getUserName(), config.getProxyParameters().getPassword())); httpClientBuilder.setProxy(proxy); } httpClient = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).build(); } catch (KeyManagementException | NoSuchAlgorithmException e) { throw new RuntimeException(e); } } return httpClient; }
From source file:microsoft.exchange.webservices.data.HttpClientWebRequest.java
/** * Prepare connection/*from w ww .jav a 2s . c o m*/ * * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception */ @Override public void prepareConnection() throws EWSHttpException { try { HttpClientBuilder builder = HttpClients.custom(); builder.setConnectionManager(this.httpClientConnMng); //create the cookie store if (cookieStore == null) { cookieStore = new BasicCookieStore(); } builder.setDefaultCookieStore(cookieStore); if (getProxy() != null) { HttpHost proxy = new HttpHost(getProxy().getHost(), getProxy().getPort()); builder.setProxy(proxy); if (HttpProxyCredentials.isProxySet()) { NTCredentials cred = new NTCredentials(HttpProxyCredentials.getUserName(), HttpProxyCredentials.getPassword(), "", HttpProxyCredentials.getDomain()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(proxy), cred); builder.setDefaultCredentialsProvider(credsProvider); } } if (getUserName() != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(getUserName(), getPassword(), "", getDomain())); builder.setDefaultCredentialsProvider(credsProvider); } //fix socket config SocketConfig sc = SocketConfig.custom().setSoTimeout(getTimeout()).build(); builder.setDefaultSocketConfig(sc); RequestConfig.Builder rcBuilder = RequestConfig.custom(); rcBuilder.setAuthenticationEnabled(true); rcBuilder.setConnectionRequestTimeout(getTimeout()); rcBuilder.setConnectTimeout(getTimeout()); rcBuilder.setRedirectsEnabled(isAllowAutoRedirect()); rcBuilder.setSocketTimeout(getTimeout()); // fix issue #144 + #160: if we used NTCredentials from above: these are NT credentials if (getUserName() != null) { ArrayList<String> authPrefs = new ArrayList<String>(); authPrefs.add(AuthSchemes.NTLM); rcBuilder.setTargetPreferredAuthSchemes(authPrefs); } // builder.setDefaultRequestConfig(rcBuilder.build()); httpPostReq = new HttpPost(getUrl().toString()); httpPostReq.addHeader("Content-type", getContentType()); //httpPostReq.setDoAuthentication(true); httpPostReq.addHeader("User-Agent", getUserAgent()); httpPostReq.addHeader("Accept", getAccept()); httpPostReq.addHeader("Keep-Alive", "300"); httpPostReq.addHeader("Connection", "Keep-Alive"); if (isAcceptGzipEncoding()) { httpPostReq.addHeader("Accept-Encoding", "gzip,deflate"); } if (getHeaders().size() > 0) { for (Map.Entry<String, String> httpHeader : getHeaders().entrySet()) { httpPostReq.addHeader(httpHeader.getKey(), httpHeader.getValue()); } } //create the client client = builder.build(); } catch (Exception er) { er.printStackTrace(); } }
From source file:org.glassfish.jersey.apache.connector.ApacheConnector.java
/** * Create the new Apache HTTP Client connector. * * @param client JAX-RS client instance for which the connector is being created. * @param config client configuration.// w w w . j av a 2s. co m */ ApacheConnector(final Client client, final Configuration config) { final Object connectionManager = config.getProperties().get(ApacheClientProperties.CONNECTION_MANAGER); if (connectionManager != null) { if (!(connectionManager instanceof HttpClientConnectionManager)) { LOGGER.log(Level.WARNING, LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(ApacheClientProperties.CONNECTION_MANAGER, connectionManager.getClass().getName(), HttpClientConnectionManager.class.getName())); } } Object reqConfig = config.getProperties().get(ApacheClientProperties.REQUEST_CONFIG); if (reqConfig != null) { if (!(reqConfig instanceof RequestConfig)) { LOGGER.log(Level.WARNING, LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(ApacheClientProperties.REQUEST_CONFIG, reqConfig.getClass().getName(), RequestConfig.class.getName())); reqConfig = null; } } final SSLContext sslContext = client.getSslContext(); final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(getConnectionManager(client, config, sslContext)); clientBuilder.setConnectionManagerShared(PropertiesHelper.getValue(config.getProperties(), ApacheClientProperties.CONNECTION_MANAGER_SHARED, false, null)); clientBuilder.setSslcontext(sslContext); final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); final Object credentialsProvider = config.getProperty(ApacheClientProperties.CREDENTIALS_PROVIDER); if (credentialsProvider != null && (credentialsProvider instanceof CredentialsProvider)) { clientBuilder.setDefaultCredentialsProvider((CredentialsProvider) credentialsProvider); } final Object retryHandler = config.getProperties().get(ApacheClientProperties.RETRY_HANDLER); if (retryHandler != null && (retryHandler instanceof HttpRequestRetryHandler)) { clientBuilder.setRetryHandler((HttpRequestRetryHandler) retryHandler); } final Object proxyUri; proxyUri = config.getProperty(ClientProperties.PROXY_URI); if (proxyUri != null) { final URI u = getProxyUri(proxyUri); final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme()); final String userName; userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); if (userName != null) { final String password; password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); if (password != null) { final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(u.getHost(), u.getPort()), new UsernamePasswordCredentials(userName, password)); clientBuilder.setDefaultCredentialsProvider(credsProvider); } } clientBuilder.setProxy(proxy); } final Boolean preemptiveBasicAuthProperty = (Boolean) config.getProperties() .get(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION); this.preemptiveBasicAuth = (preemptiveBasicAuthProperty != null) ? preemptiveBasicAuthProperty : false; final boolean ignoreCookies = PropertiesHelper.isProperty(config.getProperties(), ApacheClientProperties.DISABLE_COOKIES); if (reqConfig != null) { final RequestConfig.Builder reqConfigBuilder = RequestConfig.copy((RequestConfig) reqConfig); if (ignoreCookies) { reqConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES); } requestConfig = reqConfigBuilder.build(); } else { if (ignoreCookies) { requestConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES); } requestConfig = requestConfigBuilder.build(); } if (requestConfig.getCookieSpec() == null || !requestConfig.getCookieSpec().equals(CookieSpecs.IGNORE_COOKIES)) { this.cookieStore = new BasicCookieStore(); clientBuilder.setDefaultCookieStore(cookieStore); } else { this.cookieStore = null; } clientBuilder.setDefaultRequestConfig(requestConfig); this.client = clientBuilder.build(); }
From source file:org.jenkinsci.plugins.newrelicnotifier.api.NewRelicClientImpl.java
private CloseableHttpClient getHttpClient(URI url) { HttpClientBuilder builder = HttpClientBuilder.create(); ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy; if (proxyConfig != null) { Proxy proxy = proxyConfig.createProxy(url.getHost()); if (proxy != null && proxy.type() == Proxy.Type.HTTP) { SocketAddress addr = proxy.address(); if (addr != null && addr instanceof InetSocketAddress) { InetSocketAddress proxyAddr = (InetSocketAddress) addr; HttpHost proxyHost = new HttpHost(proxyAddr.getAddress().getHostAddress(), proxyAddr.getPort()); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost); builder = builder.setRoutePlanner(routePlanner); String proxyUser = proxyConfig.getUserName(); if (proxyUser != null) { String proxyPass = proxyConfig.getPassword(); CredentialsProvider cred = new BasicCredentialsProvider(); cred.setCredentials(new AuthScope(proxyHost), new UsernamePasswordCredentials(proxyUser, proxyPass)); builder = builder.setDefaultCredentialsProvider(cred) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); }/*ww w . j av a2 s .c o m*/ } } } return builder.build(); }