List of usage examples for org.apache.http.impl.client HttpClientBuilder setDefaultCredentialsProvider
public final HttpClientBuilder setDefaultCredentialsProvider(final CredentialsProvider credentialsProvider)
From source file:org.sonatype.nexus.testsuite.NexusHttpsITSupport.java
/** * @return Client that can use preemptive auth and self-signed certificates */// w ww.j a v a 2 s. c o m protected HttpClientBuilder clientBuilder() throws Exception { HttpClientBuilder builder = HttpClients.custom(); builder.setDefaultRequestConfig(requestConfig()); builder.setDefaultCredentialsProvider(credentialsProvider()); builder.setSSLSocketFactory(sslSocketFactory()); return builder; }
From source file:com.sonymobile.tools.gerrit.gerritevents.workers.rest.AbstractRestCommandJob2.java
@Override public String call() throws IOException { String response = ""; ReviewInput reviewInput = createReview(); String reviewEndpoint = resolveEndpointURL(); HttpPost httpPost = createHttpPostEntity(reviewInput, reviewEndpoint); if (httpPost == null) { return response; }/*www . j ava2 s .c o m*/ CredentialsProvider credProvider = new BasicCredentialsProvider(); credProvider.setCredentials(AuthScope.ANY, credentials); HttpHost proxy = null; if (httpProxy != null && !httpProxy.isEmpty()) { try { URL url = new URL(httpProxy); proxy = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); } catch (MalformedURLException e) { logger.error("Could not parse proxy URL, attempting without proxy.", e); if (altLogger != null) { altLogger.print("ERROR Could not parse proxy URL, attempting without proxy. " + e.getMessage()); } } } HttpClientBuilder builder = HttpClients.custom(); builder.setDefaultCredentialsProvider(credProvider); if (proxy != null) { builder.setProxy(proxy); } CloseableHttpClient httpClient = builder.build(); try { CloseableHttpResponse httpResponse = httpClient.execute(httpPost); response = IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8"); if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { logger.error("Gerrit response: {}", httpResponse.getStatusLine().getReasonPhrase()); if (altLogger != null) { altLogger.print("ERROR Gerrit response: " + httpResponse.getStatusLine().getReasonPhrase()); } } } catch (Exception e) { logger.error("Failed to submit result to Gerrit", e); if (altLogger != null) { altLogger.print("ERROR Failed to submit result to Gerrit" + e.toString()); } } return response; }
From source file:com.github.sardine.AuthenticationTest.java
@Test public void testBasicPreemptiveAuth() throws Exception { final HttpClientBuilder client = HttpClientBuilder.create(); final CountDownLatch count = new CountDownLatch(1); client.setDefaultCredentialsProvider(new BasicCredentialsProvider() { @Override/* www . java 2 s . c o m*/ public Credentials getCredentials(AuthScope authscope) { // Set flag that credentials have been used indicating preemptive authentication count.countDown(); return new Credentials() { public Principal getUserPrincipal() { return new BasicUserPrincipal("anonymous"); } public String getPassword() { return "invalid"; } }; } }); SardineImpl sardine = new SardineImpl(client); URI url = URI.create("http://sudo.ch/dav/basic/"); //Send basic authentication header in initial request sardine.enablePreemptiveAuthentication(url.getHost()); try { sardine.list(url.toString()); fail("Expected authorization failure"); } catch (SardineException e) { // Expect Authorization Failed assertEquals(401, e.getStatusCode()); // Make sure credentials have been queried assertEquals("No preemptive authentication attempt", 0, count.getCount()); } }
From source file:org.sonatype.spice.zapper.client.hc4.Hc4ClientBuilder.java
public Hc4Client build() { final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build(); final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry); cm.setMaxTotal(200);/*ww w. j av a 2 s .c o m*/ cm.setDefaultMaxPerRoute(parameters.getMaximumTrackCount()); final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setConnectionManager(cm) .setUserAgent("Zapper/1.0-HC4"); if (proxyServer != null) { httpClientBuilder.setProxy(proxyServer); } if (credentialsProvider != null) { httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } return new Hc4Client(parameters, remoteUrl, httpClientBuilder.build(), preemptiveAuth ? credentialsProvider : null); }
From source file:com.datatorrent.stram.util.WebServicesClient.java
public WebServicesClient(ClientConfig config) { if (SecurityUtils.isHadoopWebSecurityEnabled()) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setConnectionManager(connectionManager); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); httpClientBuilder.setDefaultAuthSchemeRegistry(authRegistry); ApacheHttpClient4Handler httpClientHandler = new ApacheHttpClient4Handler(httpClientBuilder.build(), new BasicCookieStore(), false); client = new Client(httpClientHandler, config); } else {//w w w . j a va2 s. c om client = Client.create(config); } }
From source file:de.shadowhunt.subversion.internal.AbstractHelper.java
public HttpClient getHttpClient(final String username, final HttpRequestInterceptor... interceptors) { final HttpClientBuilder builder = HttpClientBuilder.create(); final CredentialsProvider cp = new BasicCredentialsProvider(); final Credentials credentials = new UsernamePasswordCredentials(username, PASSWORD); cp.setCredentials(AuthScope.ANY, credentials); builder.setDefaultCredentialsProvider(cp); for (HttpRequestInterceptor interceptor : interceptors) { builder.addInterceptorFirst(interceptor); }//from ww w .ja v a 2 s .com builder.setRetryHandler(new SubversionRequestRetryHandler()); return builder.build(); }
From source file:org.wildfly.swarm.jaxrs.SimpleHttp.java
protected Response getUrlContents(String theUrl, boolean useAuth, boolean followRedirects) { StringBuilder content = new StringBuilder(); int code;//from w w w .j a va2 s . c om try { CredentialsProvider provider = new BasicCredentialsProvider(); HttpClientBuilder builder = HttpClientBuilder.create(); if (!followRedirects) { builder.disableRedirectHandling(); } if (useAuth) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "password"); provider.setCredentials(AuthScope.ANY, credentials); builder.setDefaultCredentialsProvider(provider); } HttpClient client = builder.build(); HttpResponse response = client.execute(new HttpGet(theUrl)); code = response.getStatusLine().getStatusCode(); if (null == response.getEntity()) { throw new RuntimeException("No response content present"); } BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String line; while ((line = bufferedReader.readLine()) != null) { content.append(line + "\n"); } bufferedReader.close(); } catch (Exception e) { throw new RuntimeException(e); } return new Response(code, content.toString()); }
From source file:org.fao.geonet.utils.GeonetHttpRequestFactory.java
public ClientHttpResponse execute(HttpUriRequest request, final Credentials credentials, final AuthScope authScope) throws IOException { final Function<HttpClientBuilder, Void> setCredentials = new Function<HttpClientBuilder, Void>() { @Nullable//from w w w . j av a 2 s . c o m @Override public Void apply(@Nonnull HttpClientBuilder input) { final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(authScope, credentials); input.setDefaultCredentialsProvider(credentialsProvider); return null; } }; return execute(request, setCredentials); }
From source file:de.avanux.smartapplianceenabler.appliance.HttpTransactionExecutor.java
/** * Send a HTTP request whose response has to be closed by the caller! * @param url/* w ww. j a v a 2s. c om*/ * @return */ protected CloseableHttpResponse sendHttpRequest(String url, String data, ContentType contentType, String username, String password) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (username != null && password != null) { CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); provider.setCredentials(AuthScope.ANY, credentials); httpClientBuilder.setDefaultCredentialsProvider(provider); } CloseableHttpClient client = httpClientBuilder.build(); logger.debug("Sending HTTP request"); logger.debug("url=" + url); logger.debug("data=" + data); logger.debug("contentType=" + contentType); logger.debug("username=" + username); logger.debug("password=" + password); try { HttpRequestBase request = null; if (data != null) { request = new HttpPost(url); ((HttpPost) request).setEntity(new StringEntity(data, contentType)); } else { request = new HttpGet(url); } CloseableHttpResponse response = client.execute(request); int responseCode = response.getStatusLine().getStatusCode(); logger.debug("Response code is " + responseCode); return response; } catch (IOException e) { logger.error("Error executing HTTP request.", e); return null; } }