List of usage examples for org.apache.http.client.protocol HttpClientContext TARGET_AUTH_STATE
String TARGET_AUTH_STATE
To view the source code for org.apache.http.client.protocol HttpClientContext TARGET_AUTH_STATE.
Click Source Link
From source file:org.apache.camel.component.http4.PreemptiveAuthInterceptor.java
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); // If no auth scheme avaialble yet, try to initialize it preemptively if (authState.getAuthScheme() == null) { AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth"); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(HttpClientContext.CREDS_PROVIDER); if (authScheme != null) { Credentials creds = credsProvider.getCredentials(AuthScope.ANY); if (creds == null) { throw new HttpException("No credentials for preemptive authentication"); }//from w w w . ja v a 2 s. c o m authState.update(authScheme, creds); } } }
From source file:com.github.avarabyeu.restendpoint.http.PreemptiveAuthInterceptor.java
/** * Adds provided auth scheme to the client if there are no another provided * auth schemes//from ww w. j a v a 2 s. c o m */ @Override public final void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); AuthCache authCache = new BasicAuthCache(); authCache.put(targetHost, new BasicScheme()); context.setAttribute(HttpClientContext.AUTH_CACHE, authCache); } }
From source file:ca.uhn.fhir.rest.client.HttpBasicAuthInterceptor.java
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { Credentials creds = new UsernamePasswordCredentials(myUsername, myPassword); authState.update(new BasicScheme(), creds); }//from w w w .jav a 2 s.co m }
From source file:io.wcm.maven.plugins.contentpackage.AbstractContentPackageMojo.java
/** * Set up http client with credentials/*from w w w . j av a 2s . c o m*/ * @return Http client * @throws MojoExecutionException Mojo execution exception */ protected final CloseableHttpClient getHttpClient() throws MojoExecutionException { try { URI crxUri = new URI(getCrxPackageManagerUrl()); final AuthScope authScope = new AuthScope(crxUri.getHost(), crxUri.getPort()); final Credentials credentials = new UsernamePasswordCredentials(this.userId, this.password); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(authScope, credentials); HttpClientBuilder httpClientBuilder = HttpClients.custom().setDefaultCredentialsProvider(credsProvider) .addInterceptorFirst(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { // enable preemptive authentication AuthState authState = (AuthState) context .getAttribute(HttpClientContext.TARGET_AUTH_STATE); authState.update(new BasicScheme(), credentials); } }); if (this.relaxedSSLCheck) { SSLContext sslContext = new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); httpClientBuilder.setSSLSocketFactory(sslsf); } return httpClientBuilder.build(); } catch (URISyntaxException ex) { throw new MojoExecutionException("Invalid url: " + getCrxPackageManagerUrl(), ex); } catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException ex) { throw new MojoExecutionException("Could not set relaxedSSLCheck", ex); } }
From source file:org.callimachusproject.client.HttpAuthenticator.java
private AuthState getTargetAuthState(final HttpContext context) { AuthState targetAuthState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); if (targetAuthState == null) { targetAuthState = new AuthState(); context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, targetAuthState); }//from ww w. j av a 2 s . com return targetAuthState; }
From source file:com.olacabs.fabric.processors.httpwriter.HttpWriter.java
private void setAuth(AuthConfiguration authConfiguration, HttpClientBuilder builder) throws InitializationException { if (!Strings.isNullOrEmpty(authConfiguration.getUsername())) { Credentials credentials = new UsernamePasswordCredentials(authConfiguration.getUsername(), authConfiguration.getPassword()); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), credentials); builder.addInterceptorFirst((HttpRequestInterceptor) (request, context) -> { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { //log.debug("SETTING CREDS"); //log.info("Preemptive AuthState: {}", authState); authState.update(new BasicScheme(), credentials); }/*w w w.ja va2 s . c o m*/ }); } else { log.error("Username can't be blank for basic auth."); throw new InitializationException("Username blank for basic auth"); } }
From source file:com.gargoylesoftware.htmlunit.HttpWebConnection.java
/** * Creates an <tt>HttpMethod</tt> instance according to the specified parameters. * @param webRequest the request//from w w w .j a va 2 s. c om * @param httpClientBuilder the httpClientBuilder that will be configured * @return the <tt>HttpMethod</tt> instance constructed according to the specified parameters * @throws IOException * @throws URISyntaxException */ @SuppressWarnings("deprecation") private HttpUriRequest makeHttpMethod(final WebRequest webRequest, final HttpClientBuilder httpClientBuilder) throws IOException, URISyntaxException { final String charset = webRequest.getCharset(); // Make sure that the URL is fully encoded. IE actually sends some Unicode chars in request // URLs; because of this we allow some Unicode chars in URLs. However, at this point we're // handing things over the HttpClient, and HttpClient will blow up if we leave these Unicode // chars in the URL. final URL url = UrlUtils.encodeUrl(webRequest.getUrl(), false, charset); // URIUtils.createURI is deprecated but as of httpclient-4.2.1, URIBuilder doesn't work here as it encodes path // what shouldn't happen here URI uri = URIUtils.createURI(url.getProtocol(), url.getHost(), url.getPort(), url.getPath(), escapeQuery(url.getQuery()), null); if (getVirtualHost() != null) { uri = URI.create(getVirtualHost()); } final HttpRequestBase httpMethod = buildHttpMethod(webRequest.getHttpMethod(), uri); setProxy(httpMethod, webRequest); if (!(httpMethod instanceof HttpEntityEnclosingRequest)) { // this is the case for GET as well as TRACE, DELETE, OPTIONS and HEAD if (!webRequest.getRequestParameters().isEmpty()) { final List<NameValuePair> pairs = webRequest.getRequestParameters(); final org.apache.http.NameValuePair[] httpClientPairs = NameValuePair.toHttpClient(pairs); final String query = URLEncodedUtils.format(Arrays.asList(httpClientPairs), charset); uri = URIUtils.createURI(url.getProtocol(), url.getHost(), url.getPort(), url.getPath(), query, null); httpMethod.setURI(uri); } } else { // POST as well as PUT and PATCH final HttpEntityEnclosingRequest method = (HttpEntityEnclosingRequest) httpMethod; if (webRequest.getEncodingType() == FormEncodingType.URL_ENCODED && method instanceof HttpPost) { final HttpPost postMethod = (HttpPost) method; if (webRequest.getRequestBody() == null) { final List<NameValuePair> pairs = webRequest.getRequestParameters(); final org.apache.http.NameValuePair[] httpClientPairs = NameValuePair.toHttpClient(pairs); final String query = URLEncodedUtils.format(Arrays.asList(httpClientPairs), charset); final StringEntity urlEncodedEntity = new StringEntity(query, charset); urlEncodedEntity.setContentType(URLEncodedUtils.CONTENT_TYPE); postMethod.setEntity(urlEncodedEntity); } else { final String body = StringUtils.defaultString(webRequest.getRequestBody()); final StringEntity urlEncodedEntity = new StringEntity(body, charset); urlEncodedEntity.setContentType(URLEncodedUtils.CONTENT_TYPE); postMethod.setEntity(urlEncodedEntity); } } else if (FormEncodingType.MULTIPART == webRequest.getEncodingType()) { final Charset c = getCharset(charset, webRequest.getRequestParameters()); final MultipartEntityBuilder builder = MultipartEntityBuilder.create().setLaxMode(); builder.setCharset(c); for (final NameValuePair pair : webRequest.getRequestParameters()) { if (pair instanceof KeyDataPair) { buildFilePart((KeyDataPair) pair, builder); } else { builder.addTextBody(pair.getName(), pair.getValue(), ContentType.create("text/plain", charset)); } } method.setEntity(builder.build()); } else { // for instance a PUT or PATCH request final String body = webRequest.getRequestBody(); if (body != null) { method.setEntity(new StringEntity(body, charset)); } } } configureHttpProcessorBuilder(httpClientBuilder, webRequest); // Tell the client where to get its credentials from // (it may have changed on the webClient since last call to getHttpClientFor(...)) final CredentialsProvider credentialsProvider = webClient_.getCredentialsProvider(); // if the used url contains credentials, we have to add this final Credentials requestUrlCredentials = webRequest.getUrlCredentials(); if (null != requestUrlCredentials && webClient_.getBrowserVersion().hasFeature(URL_AUTH_CREDENTIALS)) { final URL requestUrl = webRequest.getUrl(); final AuthScope authScope = new AuthScope(requestUrl.getHost(), requestUrl.getPort()); // updating our client to keep the credentials for the next request credentialsProvider.setCredentials(authScope, requestUrlCredentials); httpContext_.removeAttribute(HttpClientContext.TARGET_AUTH_STATE); } // if someone has set credentials to this request, we have to add this final Credentials requestCredentials = webRequest.getCredentials(); if (null != requestCredentials) { final URL requestUrl = webRequest.getUrl(); final AuthScope authScope = new AuthScope(requestUrl.getHost(), requestUrl.getPort()); // updating our client to keep the credentials for the next request credentialsProvider.setCredentials(authScope, requestCredentials); httpContext_.removeAttribute(HttpClientContext.TARGET_AUTH_STATE); } httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); httpContext_.removeAttribute(HttpClientContext.CREDS_PROVIDER); return httpMethod; }
From source file:org.opentravel.schemacompiler.repository.impl.RemoteRepositoryClient.java
/** * Configures the 'Authorization' header on the given HTTP request using the current user ID and * encrypted password that is configured for this repository. */// w w w . ja v a 2 s . c om private HttpClientContext createHttpContext() { HttpClientContext context = HttpClientContext.create(); if ((userId != null) && (encryptedPassword != null)) { AuthState target = new AuthState(); target.update(new BasicScheme(), new UsernamePasswordCredentials(userId, PasswordHelper.decrypt(encryptedPassword))); context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, target); } return context; }
From source file:org.apache.http.impl.client.InternalHttpClient.java
private void setupContext(final HttpClientContext context) { if (context.getAttribute(HttpClientContext.TARGET_AUTH_STATE) == null) { context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthState()); }//from ww w .j av a 2 s .c o m if (context.getAttribute(HttpClientContext.PROXY_AUTH_STATE) == null) { context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, new AuthState()); } if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) { context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry); } if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) { context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); } if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) { context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); } if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) { context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider); } if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) { context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig); } }