List of usage examples for org.apache.http.impl.client BasicCredentialsProvider setCredentials
public void setCredentials(final AuthScope authscope, final Credentials credentials)
From source file:org.springframework.data.solr.server.support.SolrServerUtilTests.java
/** * @see DATASOLR-189/* w w w . j a v a 2 s . com*/ */ @Test public void cloningLBHttpSolrServerShouldCopyCredentialsProviderCorrectly() { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("foo", "bar")); DefaultHttpClient client = new DefaultHttpClient(); client.setCredentialsProvider(credentialsProvider); LBHttpSolrServer lbSolrServer = new LBHttpSolrServer(client, BASE_URL, ALTERNATE_BASE_URL); LBHttpSolrServer cloned = SolrServerUtils.clone(lbSolrServer, CORE_NAME); Assert.assertThat(((AbstractHttpClient) cloned.getHttpClient()).getCredentialsProvider(), IsEqual.<CredentialsProvider>equalTo(credentialsProvider)); }
From source file:org.springframework.data.solr.server.support.SolrServerUtilTests.java
/** * @see DATASOLR-189// w ww . j a v a2 s. c o m */ @Test public void cloningHttpSolrServerShouldCopyCredentialsProviderCorrectly() { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("foo", "bar")); DefaultHttpClient client = new DefaultHttpClient(); client.setCredentialsProvider(credentialsProvider); HttpSolrServer solrServer = new HttpSolrServer(BASE_URL, client); HttpSolrServer cloned = SolrServerUtils.clone(solrServer); Assert.assertThat(((AbstractHttpClient) cloned.getHttpClient()).getCredentialsProvider(), IsEqual.<CredentialsProvider>equalTo(credentialsProvider)); }
From source file:org.springframework.cloud.dataflow.shell.command.ConfigCommands.java
@CliCommand(value = { "dataflow config server" }, help = "Configure the Spring Cloud Data Flow REST server to use") public String target(@CliOption(mandatory = false, key = { "", "uri" }, help = "the location of the Spring Cloud Data Flow REST endpoint", unspecifiedDefaultValue = Target.DEFAULT_TARGET) String targetUriString, @CliOption(mandatory = false, key = { "username" }, help = "the username for authenticated access to the Admin REST endpoint", unspecifiedDefaultValue = Target.DEFAULT_USERNAME) String targetUsername, @CliOption(mandatory = false, key = { "password" }, help = "the password for authenticated access to the Admin REST endpoint (valid only with a username)", specifiedDefaultValue = Target.DEFAULT_SPECIFIED_PASSWORD, unspecifiedDefaultValue = Target.DEFAULT_UNSPECIFIED_PASSWORD) String targetPassword) { if (!StringUtils.isEmpty(targetPassword) && StringUtils.isEmpty(targetUsername)) { return "A password may be specified only together with a username"; }//from ww w .j a va 2 s. co m if (StringUtils.isEmpty(targetPassword) && !StringUtils.isEmpty(targetUsername)) { // read password from the command line targetPassword = userInput.prompt("Password", "", false); } try { this.targetHolder.setTarget(new Target(targetUriString, targetUsername, targetPassword)); if (StringUtils.hasText(targetUsername) && StringUtils.hasText(targetPassword)) { final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials( targetHolder.getTarget().getTargetCredentials().getUsername(), targetHolder.getTarget().getTargetCredentials().getPassword())); final CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(credentialsProvider).build(); final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); this.restTemplate.setRequestFactory(requestFactory); } this.shell.setDataFlowOperations( new DataFlowTemplate(targetHolder.getTarget().getTargetUri(), this.restTemplate)); return (String.format("Successfully targeted %s", targetUriString)); } catch (Exception e) { this.targetHolder.getTarget().setTargetException(e); this.shell.setDataFlowOperations(null); if (e instanceof DataFlowServerException) { String message = String.format("Unable to parse server response: %s - at URI '%s'.", e.getMessage(), targetUriString); if (logger.isDebugEnabled()) { logger.debug(message, e); } else { logger.warn(message); } return message; } else { return (String.format("Unable to contact Data Flow Server at '%s': '%s'.", targetUriString, e.toString())); } } }
From source file:org.hawk.http.HTTPManager.java
private CloseableHttpClient createClient() { final HttpClientBuilder builder = HttpClientBuilder.create(); // Provide username and password if specified if (username != null) { final BasicCredentialsProvider credProvider = new BasicCredentialsProvider(); credProvider.setCredentials(new AuthScope(new HttpHost(repositoryURL.getHost())), new UsernamePasswordCredentials(username, password)); builder.setDefaultCredentialsProvider(credProvider); }//from www. j av a 2s . com // Follow redirects builder.setRedirectStrategy(new LaxRedirectStrategy()); return builder.build(); }
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();//ww w. ja va2s . 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 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:org.springframework.data.solr.server.support.SolrClientUtilTests.java
/** * @see DATASOLR-189//from w w w .j av a 2s . co m */ @Test public void cloningLBHttpSolrClientShouldCopyCredentialsProviderCorrectly() { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("foo", "bar")); DefaultHttpClient client = new DefaultHttpClient(); client.setCredentialsProvider(credentialsProvider); LBHttpSolrClient lbSolrClient = new LBHttpSolrClient(client, BASE_URL, ALTERNATE_BASE_URL); LBHttpSolrClient cloned = SolrClientUtils.clone(lbSolrClient, CORE_NAME); Assert.assertThat(((AbstractHttpClient) cloned.getHttpClient()).getCredentialsProvider(), IsEqual.<CredentialsProvider>equalTo(credentialsProvider)); }
From source file:org.springframework.data.solr.server.support.SolrClientUtilTests.java
/** * @see DATASOLR-189//from w ww . ja v a2 s . c o m */ @Test public void cloningHttpSolrClientShouldCopyCredentialsProviderCorrectly() { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("foo", "bar")); DefaultHttpClient client = new DefaultHttpClient(); client.setCredentialsProvider(credentialsProvider); HttpSolrClient solrClient = new HttpSolrClient(BASE_URL, client); HttpSolrClient cloned = SolrClientUtils.clone(solrClient); Assert.assertThat(((AbstractHttpClient) cloned.getHttpClient()).getCredentialsProvider(), IsEqual.<CredentialsProvider>equalTo(credentialsProvider)); }
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 . j a va 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.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(); }
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 ww w . j av a 2s . c om*/ 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:org.aludratest.service.gui.web.selenium.TAFMSSeleniumResourceService.java
@Override public String acquire() { // prepare a JSON query to the given TAFMS server JSONObject query = new JSONObject(); try {//from w w w.java 2 s . com query.put("resourceType", "selenium"); query.put("niceLevel", configuration.getIntValue("tafms.niceLevel", 0)); String jobName = configuration.getStringValue("tafms.jobName"); if (jobName != null && !"".equals(jobName)) { query.put("jobName", jobName); } } catch (JSONException e) { } // prepare authentication BasicCredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials( configuration.getStringValue("tafms.user"), configuration.getStringValue("tafms.password"))); CloseableHttpClient client = HttpClientBuilder.create() .setConnectionReuseStrategy(new NoConnectionReuseStrategy()).disableConnectionState() .disableAutomaticRetries().setDefaultCredentialsProvider(provider).build(); String message = null; try { boolean wait; // use preemptive authentication to avoid double connection count AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); URL url = new URL(getTafmsUrl()); HttpHost host = new HttpHost(url.getHost(), url.getPort() == -1 ? url.getDefaultPort() : url.getPort(), url.getProtocol()); authCache.put(host, basicAuth); // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache); do { // send a POST request to resource URL HttpPost request = new HttpPost(getTafmsUrl() + "resource"); // attach query as JSON string data request.setEntity(new StringEntity(query.toString(), ContentType.APPLICATION_JSON)); CloseableHttpResponse response = null; // fire request response = client.execute(request, localcontext); try { if (response.getStatusLine() == null) { throw new ClientProtocolException("No HTTP status line transmitted"); } message = extractMessage(response); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { LOG.error("Exception when querying TAFMS server for resource. HTTP Status: " + response.getStatusLine().getStatusCode() + ", message: " + message); return null; } JSONObject object = new JSONObject(message); if (object.has("errorMessage")) { LOG.error("TAFMS server reported an error: " + object.get("errorMessage")); return null; } // continue wait? if (object.has("waiting") && object.getBoolean("waiting")) { wait = true; query.put("requestId", object.getString("requestId")); } else { JSONObject resource = object.optJSONObject("resource"); if (resource == null) { LOG.error("TAFMS server response did not provide a resource. Message was: " + message); return null; } String sUrl = resource.getString("url"); hostResourceIds.put(sUrl, object.getString("requestId")); return sUrl; } } finally { IOUtils.closeQuietly(response); } } while (wait); // should never come here return null; } catch (ClientProtocolException e) { LOG.error("Exception in HTTP transmission", e); return null; } catch (IOException e) { LOG.error("Exception in communication with TAFMS server", e); return null; } catch (JSONException e) { LOG.error("Invalid JSON received from TAFMS server. JSON message was: " + message, e); return null; } finally { IOUtils.closeQuietly(client); } }