List of usage examples for org.apache.http.client CredentialsProvider setCredentials
void setCredentials(AuthScope authscope, Credentials credentials);
From source file:it.cloudicaro.disit.kb.rdf.HttpUtil.java
public static void delete(URL url, String user, String passwd) throws Exception { //System.out.println("DELETE "+url); HttpClient client = HttpClients.createDefault(); HttpDelete request = new HttpDelete(url.toURI()); HttpClientContext context = HttpClientContext.create(); if (user != null && passwd != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, passwd)); context.setCredentialsProvider(credsProvider); }/*ww w.ja va 2 s. c o m*/ HttpResponse response = client.execute(request, context); StatusLine s = response.getStatusLine(); int code = s.getStatusCode(); //System.out.println(code); if (code != 204 && code != 404) throw new Exception( "failed access to " + url.toString() + " code: " + code + " " + s.getReasonPhrase()); }
From source file:org.jboss.as.test.clustering.cluster.web.authentication.BasicAuthenticationWebFailoverTestCase.java
private static void setCredentials(CredentialsProvider provider, String user, String password, URL... urls) { for (URL url : urls) { provider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, password)); }//from www. ja v a2 s .c o m }
From source file:eu.esdihumboldt.util.http.ProxyUtil.java
/** * Set-up the given HTTP client to use the given proxy * /*from w w w . j a v a 2 s.com*/ * @param builder the HTTP client builder * @param proxy the proxy * @return the client builder adapted with the proxy settings */ public static HttpClientBuilder applyProxy(HttpClientBuilder builder, Proxy proxy) { init(); // check if proxy shall be used if (proxy != null && proxy.type() == Type.HTTP) { InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); // set the proxy HttpHost proxyHost = new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort()); builder = builder.setProxy(proxyHost); String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$ String password = System.getProperty("http.proxyPassword"); //$NON-NLS-1$ boolean useProxyAuth = user != null && !user.isEmpty(); if (useProxyAuth) { // set the proxy credentials CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(proxyAddress.getHostName(), proxyAddress.getPort()), new UsernamePasswordCredentials(user, password)); builder = builder.setDefaultCredentialsProvider(credsProvider) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); } _log.trace("Set proxy to " + proxyAddress.getHostName() + ":" + //$NON-NLS-1$ //$NON-NLS-2$ proxyAddress.getPort() + ((useProxyAuth) ? (" as user " + user) : (""))); //$NON-NLS-1$ //$NON-NLS-2$ } return builder; }
From source file:it.cloudicaro.disit.kb.rdf.HttpUtil.java
public static String post(URL url, String data, String contentType, String user, String passwd) throws Exception { //System.out.println("POST "+url); HttpClient client = HttpClients.createDefault(); HttpPost request = new HttpPost(url.toURI()); request.setEntity(new StringEntity(data, ContentType.create(contentType, "UTF-8"))); HttpClientContext context = HttpClientContext.create(); if (user != null && passwd != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, passwd)); context.setCredentialsProvider(credsProvider); }/*from w ww .j a v a 2s. co m*/ HttpResponse response = client.execute(request, context); StatusLine s = response.getStatusLine(); int code = s.getStatusCode(); //System.out.println(code); if (code == 204) return ""; if (code != 200) throw new Exception( "failed access to " + url.toString() + " code: " + code + " " + s.getReasonPhrase()); Reader reader = null; try { reader = new InputStreamReader(response.getEntity().getContent()); StringBuilder sb = new StringBuilder(); { int read; char[] cbuf = new char[1024]; while ((read = reader.read(cbuf)) != -1) { sb.append(cbuf, 0, read); } } return sb.toString(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.helger.httpclient.HttpClientHelper.java
@Nonnull public static HttpContext createHttpContext(@Nullable final HttpHost aProxy, @Nullable final Credentials aProxyCredentials) { final HttpClientContext ret = HttpClientContext.create(); if (aProxy != null) { ret.setRequestConfig(RequestConfig.custom().setProxy(aProxy).build()); if (aProxyCredentials != null) { final CredentialsProvider aCredentialsProvider = new BasicCredentialsProvider(); aCredentialsProvider.setCredentials(new AuthScope(aProxy), aProxyCredentials); ret.setCredentialsProvider(aCredentialsProvider); }//from w w w .ja v a 2s. co m } return ret; }
From source file:com.threatconnect.app.playbooks.db.tcapi.ConnectionUtil.java
/** * Adds proxy information to an http client builder * // www . j a v a 2 s .c o m * @param builder * the HttpClientBuilder builder to add the proxy information to * @param proxyHost * the host of the proxy server * @param proxyPort * the port of the proxy server * @param proxyUserName * the username to authenticate with the proxy server (optional) * @param proxyPassword * the password to authenticate with the proxy server (optional) */ public static void addProxy(final HttpClientBuilder builder, final String proxyHost, final Integer proxyPort, final String proxyUserName, final String proxyPassword) { // check to see if the the host or port are null if (proxyHost == null || proxyPort == null) { logger.warn("proxyHost and proxyPort are required to connect to a proxy"); } else { // add the proxy information to the builder builder.setProxy(new HttpHost(proxyHost, proxyPort)); // authentication required if (proxyUserName != null && proxyPassword != null) { // add the credentials to the proxy information Credentials credentials = new UsernamePasswordCredentials(proxyUserName, proxyPassword); AuthScope authScope = new AuthScope(proxyHost, proxyPort); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(authScope, credentials); builder.setDefaultCredentialsProvider(credsProvider); } } }
From source file:it.cloudicaro.disit.kb.rdf.HttpUtil.java
public static String get(URL url, String accept, String user, String passwd) throws Exception { //System.out.println("GET "+url); HttpClient client = HttpClients.createDefault(); HttpGet request = new HttpGet(url.toURI()); if (accept != null) request.addHeader("Accept", accept); HttpClientContext context = HttpClientContext.create(); if (user != null && passwd != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, passwd)); /*// Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth);*/ // Add AuthCache to the execution context context.setCredentialsProvider(credsProvider); }/*from w ww. j a v a2 s . c o m*/ HttpResponse response = client.execute(request, context); StatusLine s = response.getStatusLine(); int code = s.getStatusCode(); //System.out.println(code); if (code != 200) throw new Exception( "failed access to " + url.toString() + " code: " + code + " " + s.getReasonPhrase()); Reader reader = null; try { reader = new InputStreamReader(response.getEntity().getContent()); StringBuilder sb = new StringBuilder(); { int read; char[] cbuf = new char[1024]; while ((read = reader.read(cbuf)) != -1) { sb.append(cbuf, 0, read); } } return sb.toString(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:org.jboss.as.test.integration.domain.AbstractSSLMasterSlaveTestCase.java
private static CloseableHttpClient createHttpClient(URI mgmtURI) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(SLAVE_HOST_USERNAME, SLAVE_HOST_PASSWORD);/*from w ww . j av a 2 s. c o m*/ AuthScope authScope = new AuthScope(mgmtURI.getHost(), mgmtURI.getPort()); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(authScope, credentials); return HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build(); }
From source file:org.jboss.as.test.integration.management.extension.customcontext.testbase.CustomManagementContextTestBase.java
private static CloseableHttpClient createAuthenticatingClient(ManagementClient managementClient) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(managementClient.getMgmtAddress(), 9990), new UsernamePasswordCredentials(Authentication.USERNAME, Authentication.PASSWORD)); return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).setMaxConnPerRoute(10).build(); }
From source file:ro.cosu.vampires.server.rest.controllers.AbstractControllerTest.java
protected static HttpClient getHttpClient() { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(User.admin().id(), User.admin().id())); return HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build(); }