Search in sources :

Example 11 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project sling by apache.

the class PreemptiveAuthInterceptor method process.

public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        Credentials creds = credsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 12 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project tomee by apache.

the class AuthBeanTest method get.

private String get(final String user, final String password) {
    final BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
    basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
    final CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(basicCredentialsProvider).build();
    final HttpHost httpHost = new HttpHost(webapp.getHost(), webapp.getPort(), webapp.getProtocol());
    final AuthCache authCache = new BasicAuthCache();
    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(httpHost, basicAuth);
    final HttpClientContext context = HttpClientContext.create();
    context.setAuthCache(authCache);
    final HttpGet get = new HttpGet(webapp.toExternalForm() + "servlet");
    CloseableHttpResponse response = null;
    try {
        response = client.execute(httpHost, get, context);
        return response.getStatusLine().getStatusCode() + " " + EntityUtils.toString(response.getEntity());
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    } finally {
        try {
            IO.close(response);
        } catch (final IOException e) {
        // no-op
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 13 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project elasticsearch by elastic.

the class RestClient method setHosts.

/**
     * Replaces the hosts that the client communicates with.
     * @see HttpHost
     */
public synchronized void setHosts(HttpHost... hosts) {
    if (hosts == null || hosts.length == 0) {
        throw new IllegalArgumentException("hosts must not be null nor empty");
    }
    Set<HttpHost> httpHosts = new HashSet<>();
    AuthCache authCache = new BasicAuthCache();
    for (HttpHost host : hosts) {
        Objects.requireNonNull(host, "host cannot be null");
        httpHosts.add(host);
        authCache.put(host, new BasicScheme());
    }
    this.hostTuple = new HostTuple<>(Collections.unmodifiableSet(httpHosts), authCache);
    this.blacklist.clear();
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpHost(org.apache.http.HttpHost) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HashSet(java.util.HashSet)

Example 14 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project SmartAndroidSource by jaychou2012.

the class PreemtiveAuthorizationHttpRequestInterceptor method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 15 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project camel by apache.

the class HttpProducer method executeMethod.

/**
     * Strategy when executing the method (calling the remote server).
     *
     * @param httpRequest the http Request to execute
     * @return the response
     * @throws IOException can be thrown
     */
protected HttpResponse executeMethod(HttpUriRequest httpRequest) throws IOException {
    HttpContext localContext = new BasicHttpContext();
    if (getEndpoint().isAuthenticationPreemptive()) {
        BasicScheme basicAuth = new BasicScheme();
        localContext.setAttribute("preemptive-auth", basicAuth);
    }
    if (httpContext != null) {
        localContext = new BasicHttpContext(httpContext);
    }
    return httpClient.execute(httpRequest, localContext);
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext)

Aggregations

BasicScheme (org.apache.http.impl.auth.BasicScheme)19 HttpHost (org.apache.http.HttpHost)13 AuthCache (org.apache.http.client.AuthCache)12 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)12 AuthScope (org.apache.http.auth.AuthScope)11 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)10 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)10 CredentialsProvider (org.apache.http.client.CredentialsProvider)9 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)9 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 Credentials (org.apache.http.auth.Credentials)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 IOException (java.io.IOException)3 AuthState (org.apache.http.auth.AuthState)3 HttpGet (org.apache.http.client.methods.HttpGet)3 URI (java.net.URI)2 URL (java.net.URL)2 Header (org.apache.http.Header)2 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)2 HttpContext (org.apache.http.protocol.HttpContext)2