List of usage examples for org.apache.http.impl.client HttpClientBuilder setDefaultCredentialsProvider
public final HttpClientBuilder setDefaultCredentialsProvider(final CredentialsProvider credentialsProvider)
From source file:edu.washington.shibboleth.attribute.resolver.dc.rws.HttpDataSource.java
/** * Initializes the connector and prepares it for use. *//*ww w.j a v a 2s . c o m*/ public void initialize() throws IOException { log.info("HttpDataSource: initialize"); SSLConnectionSocketFactory sf = getSocketFactory(); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sf).build(); connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); connectionManager.setMaxTotal(maxConnections); connectionManager.setDefaultMaxPerRoute(maxConnections); /* * Create our client */ // HttpClientBuilder cb = HttpClients.custom().setConnectionManager(connectionManager); HttpClientBuilder cb = HttpClientBuilder.create().setConnectionManager(connectionManager); // requires lib 4.x // cb = cb.setConnectionManagerShared(true); if (username != null && password != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password); credsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials); cb = cb.setDefaultCredentialsProvider(credsProvider); log.info("HttpDataSource: added basic creds "); } httpClient = cb.build(); }
From source file:com.logsniffer.settings.http.HttpSettings.java
/** * Creates a {@link HttpClientBuilder} considering current settings. * /*from w ww.j a va 2 s . c o m*/ * @return a {@link HttpClientBuilder} considering current settings. */ public HttpClientBuilder createHttpClientBuilder() { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (getHttpProxy() != null && StringUtils.isNotBlank(getHttpProxy().getHost())) { httpClientBuilder = httpClientBuilder.setProxy( new HttpHost(getHttpProxy().getHost(), getHttpProxy().getPort(), getHttpProxy().getSchema())); if (StringUtils.isNotBlank(getHttpProxy().getUser())) { Credentials credentials = new UsernamePasswordCredentials(getHttpProxy().getUser(), getHttpProxy().getPassword()); AuthScope authScope = new AuthScope(getHttpProxy().getHost(), getHttpProxy().getPort()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(authScope, credentials); httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credsProvider); httpClientBuilder.useSystemProperties(); } } return httpClientBuilder; }
From source file:org.opennms.opennms.pris.plugins.defaults.source.HttpRequisitionSource.java
@Override public Object dump() throws Exception { Requisition requisition = null;/*w ww. j av a 2 s. com*/ if (getUrl() != null) { HttpClientBuilder builder = HttpClientBuilder.create(); // If username and password was found, inject the credentials if (getUserName() != null && getPassword() != 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(getUserName(), getPassword()); // Inject the credentials provider.setCredentials(scope, credentials); // Set the default credentials provider builder.setDefaultCredentialsProvider(provider); } HttpClient client = builder.build(); HttpGet request = new HttpGet(getUrl()); 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); } else { LOGGER.error("Parameter requisition.url is missing in requisition.properties"); } if (requisition == null) { LOGGER.error("Requisition is null for unknown reasons"); return null; } LOGGER.info("HttpRequisitionSource delivered for requisition '{}'", requisition.getNodes().size()); return requisition; }
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 a v a 2 s . 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 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(); }
From source file:com.crawler.app.fetcher.PageFetcher.java
public PageFetcher(CrawlConfig config) { super(config); RequestConfig requestConfig = RequestConfig.custom().setExpectContinueEnabled(false) .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).setRedirectsEnabled(false) //.setRelativeRedirectsAllowed(true) .setSocketTimeout(config.getSocketTimeout()).setConnectTimeout(config.getConnectionTimeout()) .build();/*w w w .ja v a 2 s .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()); 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:de.tsystems.mms.apm.performancesignature.viewer.rest.model.CustomJenkinsHttpClient.java
private static HttpClientBuilder createHttpClientBuilder(final boolean verifyCertificate, final CustomProxy customProxy) { HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.useSystemProperties(); if (!verifyCertificate) { SSLContextBuilder builder = new SSLContextBuilder(); try {//from w w w .j ava2s.c o m builder.loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }); httpClientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(builder.build())); } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) { LOGGER.severe(ExceptionUtils.getFullStackTrace(e)); } } if (customProxy != null) { Jenkins jenkins = PerfSigUIUtils.getInstance(); if (customProxy.isUseJenkinsProxy() && jenkins.proxy != null) { final ProxyConfiguration proxyConfiguration = jenkins.proxy; if (StringUtils.isNotBlank(proxyConfiguration.name) && proxyConfiguration.port > 0) { httpClientBuilder.setProxy(new HttpHost(proxyConfiguration.name, proxyConfiguration.port)); if (StringUtils.isNotBlank(proxyConfiguration.getUserName())) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials( proxyConfiguration.getUserName(), proxyConfiguration.getUserName()); credsProvider.setCredentials( new AuthScope(proxyConfiguration.name, proxyConfiguration.port), usernamePasswordCredentials); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); httpClientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); } } } else { httpClientBuilder.setProxy(new HttpHost(customProxy.getProxyServer(), customProxy.getProxyPort())); if (StringUtils.isNotBlank(customProxy.getProxyUser()) && StringUtils.isNotBlank(customProxy.getProxyPassword())) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials( customProxy.getProxyUser(), customProxy.getProxyPassword()); credsProvider.setCredentials( new AuthScope(customProxy.getProxyServer(), customProxy.getProxyPort()), usernamePasswordCredentials); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); httpClientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); } } } return httpClientBuilder; }
From source file:net.sf.jasperreports.data.http.HttpDataService.java
protected void setAuthentication(Map<String, Object> parameters, HttpClientBuilder clientBuilder) { String username = getUsername(parameters); if (username != null) { String password = getPassword(parameters); BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); //FIXME proxy authentication? credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password)); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); }/*from w w w . j a v a 2 s.co m*/ }
From source file:de.comlineag.snc.webcrawler.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();/* w w w .jav a 2 s . 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.debug("Exception thrown while trying to register https:", 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); } clientBuilder.addInterceptorLast(new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header contentEncoding = entity.getContentEncoding(); if (contentEncoding != null) { HeaderElement[] codecs = contentEncoding.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } }); httpClient = clientBuilder.build(); if (connectionMonitorThread == null) { connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager); } connectionMonitorThread.start(); }