List of usage examples for org.apache.http.impl.client HttpClientBuilder setDefaultCredentialsProvider
public final HttpClientBuilder setDefaultCredentialsProvider(final CredentialsProvider credentialsProvider)
From source file:crawler.PageFetcher.java
public PageFetcher(CrawlConfig config) { super(config); RequestConfig requestConfig = RequestConfig.custom().setExpectContinueEnabled(false) .setCookieSpec(config.getCookiePolicy()).setRedirectsEnabled(false) .setSocketTimeout(config.getSocketTimeout()).setConnectTimeout(config.getConnectionTimeout()) .build();//from w ww . j a va2s . co m 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 SniSSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); 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 SniPoolingHttpClientConnectionManager(connRegistry); connectionManager.setMaxTotal(config.getMaxTotalConnections()); connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost()); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setDefaultRequestConfig(requestConfig); clientBuilder.setConnectionManager(connectionManager); clientBuilder.setUserAgent(config.getUserAgentString()); clientBuilder.setDefaultHeaders(config.getDefaultHeaders()); 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(); }
From source file:com.jive.myco.seyren.core.util.graphite.GraphiteHttpClient.java
private HttpClient createHttpClient() { HttpClientBuilder clientBuilder = HttpClientBuilder.create().setConnectionManager(createConnectionManager()) .setDefaultRequestConfig(RequestConfig.custom() .setConnectionRequestTimeout(graphiteConnectionRequestTimeout) .setConnectTimeout(graphiteConnectTimeout).setSocketTimeout(graphiteSocketTimeout).build()); // Set auth header for graphite if username and password are provided if (!StringUtils.isEmpty(graphiteUsername) && !StringUtils.isEmpty(graphitePassword)) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(graphiteUsername, graphitePassword)); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); context.setAttribute("preemptive-auth", new BasicScheme()); clientBuilder.addInterceptorFirst(new PreemptiveAuth()); }/*from www .j a v a 2 s. c o m*/ return clientBuilder.build(); }
From source file:net.siegmar.japtproxy.fetcher.HttpClientConfigurer.java
protected void configureProxy(final HttpClientBuilder httpClientBuilder, final String proxy) throws InitializationException { final URL proxyUrl; try {//from w ww . j a va2 s . com proxyUrl = new URL(proxy); } catch (final MalformedURLException e) { throw new InitializationException("Invalid proxy url", e); } final String proxyHost = proxyUrl.getHost(); final int proxyPort = proxyUrl.getPort() != -1 ? proxyUrl.getPort() : proxyUrl.getDefaultPort(); LOG.info("Set proxy server to '{}:{}'", proxyHost, proxyPort); httpClientBuilder.setRoutePlanner(new DefaultProxyRoutePlanner(new HttpHost(proxyHost, proxyPort))); final String userInfo = proxyUrl.getUserInfo(); if (userInfo != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), buildCredentials(userInfo)); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }
From source file:crawler.java.edu.uci.ics.crawler4j.fetcher.PageFetcher.java
public PageFetcher(CrawlConfig config) { super(config); RequestConfig requestConfig = RequestConfig.custom().setExpectContinueEnabled(false) .setCookieSpec(CookieSpecs.DEFAULT).setRedirectsEnabled(false) .setSocketTimeout(config.getSocketTimeout()).setConnectTimeout(config.getConnectionTimeout()) .build();//from www.j a v a 2s. c o m 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()); clientBuilder.setDefaultHeaders(config.getDefaultHeaders()); 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(); }
From source file:com.liferay.sync.engine.session.Session.java
public Session(URL url, String login, String password, boolean trustSelfSigned, int maxConnections) { if (maxConnections == Integer.MAX_VALUE) { _executorService = Executors.newCachedThreadPool(); } else {/*from ww w . j a v a 2 s .c o m*/ _executorService = Executors.newFixedThreadPool(maxConnections); } HttpClientBuilder httpClientBuilder = createHttpClientBuilder(trustSelfSigned, maxConnections); httpClientBuilder.setConnectionManager(_getHttpClientConnectionManager(trustSelfSigned)); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); _httpHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); credentialsProvider.setCredentials(new AuthScope(_httpHost), new UsernamePasswordCredentials(login, password)); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); _httpClient = httpClientBuilder.build(); _oAuthEnabled = false; }
From source file:net.yacy.cora.protocol.http.HTTPClient.java
private static HttpClientBuilder initClientBuilder() { final HttpClientBuilder builder = HttpClientBuilder.create(); builder.setConnectionManager(CONNECTION_MANAGER); builder.setDefaultRequestConfig(dfltReqConf); // UserAgent//from ww w . j av a2 s .c om builder.setUserAgent(ClientIdentification.yacyInternetCrawlerAgent.userAgent); // remove retries; we expect connections to fail; therefore we should not retry //builder.disableAutomaticRetries(); // disable the cookiestore, cause this may cause segfaults and is not needed builder.setDefaultCookieStore(null); builder.disableCookieManagement(); // add custom keep alive strategy builder.setKeepAliveStrategy(customKeepAliveStrategy()); // ask for gzip builder.addInterceptorLast(new GzipRequestInterceptor()); // uncompress gzip builder.addInterceptorLast(new GzipResponseInterceptor()); // Proxy builder.setRoutePlanner(ProxySettings.RoutePlanner); builder.setDefaultCredentialsProvider(ProxySettings.CredsProvider); return builder; }
From source file:com.brsanthu.googleanalytics.GoogleAnalytics.java
protected CloseableHttpClient createHttpClient(GoogleAnalyticsConfig config) { PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config)); HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connManager); if (isNotEmpty(config.getUserAgent())) { builder.setUserAgent(config.getUserAgent()); }//from w w w . j a va 2 s . co m if (isNotEmpty(config.getProxyHost())) { builder.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort())); if (isNotEmpty(config.getProxyUserName())) { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword())); builder.setDefaultCredentialsProvider(credentialsProvider); } } return builder.build(); }
From source file:org.opennms.opennms.pris.plugins.defaults.source.HttpRequisitionMergeSource.java
private Requisition getRequisition(String url, String userName, String password) { Requisition requisition = null;/*from w w w . ja va2 s. c o m*/ if (url != null) { try { HttpClientBuilder builder = HttpClientBuilder.create(); // If username and password was found, inject the credentials if (userName != null && password != null) { CredentialsProvider provider = new BasicCredentialsProvider(); // Create the authentication scope AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); // Create credential pair UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password); // Inject the credentials provider.setCredentials(scope, credentials); // Set the default credentials provider builder.setDefaultCredentialsProvider(provider); } HttpClient client = builder.build(); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); try { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); JAXBContext jaxbContext = JAXBContext.newInstance(Requisition.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); requisition = (Requisition) jaxbUnmarshaller.unmarshal(bufferedReader); } catch (JAXBException e) { LOGGER.error("The response did not contain a valid requisition as xml.", e); } LOGGER.debug("Got Requisition {}", requisition); } catch (IOException ex) { LOGGER.error("Requesting requisition from {} failed", url, ex); return null; } } else { LOGGER.error("Parameter requisition.url is missing in requisition.properties"); return null; } return requisition; }
From source file:jp.co.ctc_g.jse.core.rest.springmvc.client.ProxyClientHttpRequestFactory.java
/** * ???????HttpClient????????/*from w w w. j a v a 2 s . c o m*/ * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { Assert.notNull(proxyHost, "(proxyHost)???"); Assert.notNull(proxyPort, "??(proxyPort)???"); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(maxTotal); connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute); HttpClientBuilder builder = HttpClients.custom(); builder.setConnectionManager(connectionManager); if (authentication) { Assert.notNull(username, "??true???????(username)???"); Assert.notNull(password, "??true?????(password)???"); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner( new HttpHost(proxyHost, Integer.parseInt(proxyPort))); builder.setRoutePlanner(routePlanner); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(proxyHost, Integer.parseInt(proxyPort)), new UsernamePasswordCredentials(username, password)); builder.setDefaultCredentialsProvider(credsProvider); } builder.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(readTimeout).build()); CloseableHttpClient client = builder.build(); setHttpClient(client); }
From source file:org.piwigo.remotesync.api.client.WSClient.java
protected CloseableHttpClient getHttpClient() throws Exception { if (httpClient == null) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (clientConfiguration.getUsesProxy()) { String proxyUrl = clientConfiguration.getProxyUrl(); int proxyPort = clientConfiguration.getProxyPort(); String proxyUsername = clientConfiguration.getProxyUsername(); String proxyPassword = clientConfiguration.getProxyPassword(); if (proxyUsername != null && proxyUsername.length() > 0 && proxyPassword != null && proxyPassword.length() > 0) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword)); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); }//from w w w .j a v a 2 s . c om HttpHost proxy = new HttpHost(proxyUrl, proxyPort); requestConfig = RequestConfig.custom().setProxy(proxy).build(); } if (clientConfiguration.getTrustSSLCertificates()) { SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.loadTrustMaterial(null, new TrustSSLCertificatesStrategy()); httpClientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContextBuilder.build())); } httpClient = httpClientBuilder.build(); } return httpClient; }