List of usage examples for org.apache.http.client AuthCache put
void put(HttpHost host, AuthScheme authScheme);
From source file:com.github.sardine.impl.SardineImpl.java
@Override public void enablePreemptiveAuthentication(String hostname) { AuthCache cache = new BasicAuthCache(); // Generate Basic preemptive scheme object and stick it to the local execution context BasicScheme basicAuth = new BasicScheme(); // Configure HttpClient to authenticate preemptively by prepopulating the authentication data cache. for (String scheme : Arrays.asList("http", "https")) { cache.put(new HttpHost(hostname, -1, scheme), basicAuth); }/* w w w . j a v a2 s.c o m*/ // Add AuthCache to the execution context this.context.setAttribute(HttpClientContext.AUTH_CACHE, cache); }
From source file:org.apache.abdera2.common.protocol.Session.java
public void usePreemptiveAuthentication(String target, String realm) throws URISyntaxException { AuthCache cache = (AuthCache) localContext.getAttribute(ClientContext.AUTH_CACHE); if (cache == null) { String host = AuthScope.ANY_HOST; int port = AuthScope.ANY_PORT; if (target != null) { URI uri = new URI(target); host = uri.getHost();/* w ww . jav a 2 s. com*/ port = uri.getPort(); } BasicScheme basicAuth = new BasicScheme(); HttpHost targetHost = new HttpHost(host, port, basicAuth.getSchemeName()); cache = new BasicAuthCache(); cache.put(targetHost, basicAuth); localContext.setAttribute(ClientContext.AUTH_CACHE, cache); } }
From source file:de.undercouch.gradle.tasks.download.DownloadAction.java
/** * Add authentication information for the given host * @param host the host//from ww w. java2 s .c om * @param credentials the credentials * @param authScheme the scheme for preemptive authentication (should be * <code>null</code> if adding authentication for a proxy server) * @param context the context in which the authentication information * should be saved */ private void addAuthentication(HttpHost host, Credentials credentials, AuthScheme authScheme, HttpClientContext context) { AuthCache authCache = context.getAuthCache(); if (authCache == null) { authCache = new BasicAuthCache(); context.setAuthCache(authCache); } CredentialsProvider credsProvider = context.getCredentialsProvider(); if (credsProvider == null) { credsProvider = new BasicCredentialsProvider(); context.setCredentialsProvider(credsProvider); } credsProvider.setCredentials(new AuthScope(host), credentials); if (authScheme != null) { authCache.put(host, authScheme); } }
From source file:org.bonitasoft.connectors.rest.RESTConnector.java
/** * Set the builder based on the request elements * //from w w w . j av a 2 s.c o m * @param requestConfigurationBuilder The builder to be set * @param authorization The authentication element of the request * @param proxy The proxy element of the request * @param urlHost The URL host of the request * @param urlPort The URL post of the request * @param urlProtocol The URL protocol of the request * @param httpClientBuilder The builder to be set * @return HTTPContext The HTTP context to be set */ private HttpContext setAuthorizations(final Builder requestConfigurationBuilder, final Authorization authorization, final Proxy proxy, final String urlHost, final int urlPort, final String urlProtocol, final HttpClientBuilder httpClientBuilder) { HttpContext httpContext = HttpClientContext.create(); if (authorization != null) { if (authorization instanceof BasicDigestAuthorization) { final BasicDigestAuthorization castAuthorization = (BasicDigestAuthorization) authorization; final List<String> authPrefs = new ArrayList<>(); if (castAuthorization.isBasic()) { authPrefs.add(AuthSchemes.BASIC); } else { authPrefs.add(AuthSchemes.DIGEST); } requestConfigurationBuilder.setTargetPreferredAuthSchemes(authPrefs); final String username = castAuthorization.getUsername(); final String password = new String(castAuthorization.getPassword()); String host = urlHost; if (isStringInputValid(castAuthorization.getHost())) { host = castAuthorization.getHost(); } int port = urlPort; if (castAuthorization.getPort() != null) { port = castAuthorization.getPort(); } String realm = AuthScope.ANY_REALM; if (isStringInputValid(castAuthorization.getRealm())) { realm = castAuthorization.getRealm(); } final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(host, port, realm), new UsernamePasswordCredentials(username, password)); setProxyCrendentials(proxy, credentialsProvider); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); if (castAuthorization.isPreemptive() || proxy != null) { final AuthCache authoriationCache = new BasicAuthCache(); if (castAuthorization.isPreemptive()) { AuthSchemeBase authorizationScheme = null; if (castAuthorization.isBasic()) { authorizationScheme = new BasicScheme(ChallengeState.TARGET); } else { authorizationScheme = new DigestScheme(ChallengeState.TARGET); } authoriationCache.put(new HttpHost(host, port, urlProtocol), authorizationScheme); } if (proxy != null) { final BasicScheme basicScheme = new BasicScheme(ChallengeState.PROXY); authoriationCache.put(new HttpHost(proxy.getHost(), proxy.getPort()), basicScheme); } final HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authoriationCache); httpContext = localContext; } } } else if (proxy != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); setProxyCrendentials(proxy, credentialsProvider); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); // Make it preemptive if (proxy.hasCredentials()) { final AuthCache authoriationCache = new BasicAuthCache(); final BasicScheme basicScheme = new BasicScheme(ChallengeState.PROXY); authoriationCache.put(new HttpHost(proxy.getHost(), proxy.getPort()), basicScheme); final HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authoriationCache); httpContext = localContext; } } return httpContext; }
From source file:org.eclipse.rdf4j.http.client.SPARQLProtocolSession.java
protected void setUsernameAndPasswordForUrl(String username, String password, String url) { if (username != null && password != null) { logger.debug("Setting username '{}' and password for server at {}.", username, url); java.net.URI requestURI = java.net.URI.create(url); String host = requestURI.getHost(); int port = requestURI.getPort(); AuthScope scope = new AuthScope(host, port); UsernamePasswordCredentials cred = new UsernamePasswordCredentials(username, password); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(scope, cred); httpContext.setCredentialsProvider(credsProvider); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); HttpHost httpHost = new HttpHost(requestURI.getHost(), requestURI.getPort(), requestURI.getScheme()); authCache.put(httpHost, basicAuth); httpContext.setAuthCache(authCache); } else {//from w w w . ja v a2 s .c om httpContext.removeAttribute(HttpClientContext.AUTH_CACHE); httpContext.removeAttribute(HttpClientContext.CREDS_PROVIDER); } }
From source file:com.liferay.petra.json.web.service.client.BaseJSONWebServiceClientImpl.java
protected String execute(HttpRequestBase httpRequestBase) throws JSONWebServiceInvocationException, JSONWebServiceTransportException { signRequest(httpRequestBase);/* w w w .j a v a 2 s . c o m*/ HttpHost httpHost = new HttpHost(_hostName, _hostPort, _protocol); try { if (_closeableHttpAsyncClient == null) { afterPropertiesSet(); } Future<HttpResponse> future = null; if (!isNull(_login) && !isNull(_password)) { HttpClientContext httpClientContext = HttpClientContext.create(); AuthCache authCache = new BasicAuthCache(); AuthScheme authScheme = null; if (!isNull(_proxyHostName)) { authScheme = new BasicScheme(ChallengeState.PROXY); } else { authScheme = new BasicScheme(ChallengeState.TARGET); } authCache.put(httpHost, authScheme); httpClientContext.setAttribute(ClientContext.AUTH_CACHE, authCache); future = _closeableHttpAsyncClient.execute(httpHost, httpRequestBase, httpClientContext, null); } else { future = _closeableHttpAsyncClient.execute(httpHost, httpRequestBase, null); } HttpResponse httpResponse = future.get(); StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (_logger.isTraceEnabled()) { _logger.trace("Server returned status " + statusCode); } HttpEntity httpEntity = httpResponse.getEntity(); if ((statusCode == HttpServletResponse.SC_NO_CONTENT) || (((httpEntity == null) || (httpEntity.getContentLength() == 0)) && _isStatus2XX(statusCode))) { return null; } String content = EntityUtils.toString(httpEntity, _CHARSET); if ((httpEntity.getContentType() != null) && _isApplicationJSONContentType(httpEntity)) { content = updateJSON(content); } if (_isStatus2XX(statusCode)) { return content; } else if ((statusCode == HttpServletResponse.SC_BAD_REQUEST) || (statusCode == HttpServletResponse.SC_FORBIDDEN) || (statusCode == HttpServletResponse.SC_METHOD_NOT_ALLOWED) || (statusCode == HttpServletResponse.SC_NOT_ACCEPTABLE) || (statusCode == HttpServletResponse.SC_NOT_FOUND)) { throw new JSONWebServiceInvocationException(content, statusCode); } else if (statusCode == HttpServletResponse.SC_UNAUTHORIZED) { throw new JSONWebServiceTransportException.AuthenticationFailure( "Not authorized to access JSON web service"); } throw new JSONWebServiceTransportException.CommunicationFailure("Server returned status " + statusCode, statusCode); } catch (ExecutionException ee) { throw new JSONWebServiceTransportException.CommunicationFailure("Unable to transmit request", ee); } catch (InterruptedException ie) { throw new JSONWebServiceTransportException.CommunicationFailure("Unable to transmit request", ie); } catch (IOException ioe) { throw new JSONWebServiceTransportException.CommunicationFailure("Unable to transmit request", ioe); } finally { httpRequestBase.releaseConnection(); } }