Example usage for org.apache.http.client.config RequestConfig isAuthenticationEnabled

List of usage examples for org.apache.http.client.config RequestConfig isAuthenticationEnabled

Introduction

In this page you can find the example usage for org.apache.http.client.config RequestConfig isAuthenticationEnabled.

Prototype

public boolean isAuthenticationEnabled() 

Source Link

Document

Determines whether authentication should be handled automatically.

Usage

From source file:org.callimachusproject.client.AuthenticationClientExecChain.java

public CloseableHttpResponse execute(final HttpRoute route, final HttpRequestWrapper original,
        final HttpClientContext context, final HttpExecutionAware execAware) throws IOException, HttpException {
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(original);
    final RequestConfig config = context.getRequestConfig();

    CloseableHttpResponse response = null;
    while (true) {
        if (execAware != null && execAware.isAborted()) {
            throw new RequestAbortedException("Request aborted");
        }/* ww  w . j  a  v a 2 s.c  o m*/

        this.authenticator.generateAuthResponse(route, request, context);

        response = delegate.execute(route, request, context, execAware);

        if (config.isAuthenticationEnabled()
                && authenticator.needAuthentication(route, request, response, context)) {
            EntityUtils.consume(response.getEntity());
        } else {
            return response;
        }
    }
}

From source file:org.apache.http.impl.execchain.MainClientExec.java

/**
 * Creates a tunnel to the target server.
 * The connection must be established to the (last) proxy.
 * A CONNECT request for tunnelling through the proxy will
 * be created and sent, the response received and checked.
 * This method does <i>not</i> update the connection with
 * information about the tunnel, that is left to the caller.
 *///w  ww.ja va  2s .  co  m
private boolean createTunnelToTarget(final AuthState proxyAuthState, final HttpClientConnection managedConn,
        final HttpRoute route, final HttpRequest request, final HttpClientContext context)
        throws HttpException, IOException {

    final RequestConfig config = context.getRequestConfig();
    final int timeout = config.getConnectTimeout();

    final HttpHost target = route.getTargetHost();
    final HttpHost proxy = route.getProxyHost();
    HttpResponse response;

    final String authority = target.toHostString();
    final HttpRequest connect = new BasicHttpRequest("CONNECT", authority, request.getProtocolVersion());

    this.requestExecutor.preProcess(connect, this.proxyHttpProcessor, context);

    for (;;) {
        if (!managedConn.isOpen()) {
            this.connManager.connect(managedConn, route, timeout > 0 ? timeout : 0, context);
        }

        connect.removeHeaders(AUTH.PROXY_AUTH_RESP);
        this.authenticator.generateAuthResponse(connect, proxyAuthState, context);

        response = this.requestExecutor.execute(connect, managedConn, context);

        final int status = response.getStatusLine().getStatusCode();
        if (status < 200) {
            throw new HttpException("Unexpected response to CONNECT request: " + response.getStatusLine());
        }

        if (config.isAuthenticationEnabled()) {
            if (this.authenticator.isAuthenticationRequested(proxy, response, this.proxyAuthStrategy,
                    proxyAuthState, context)) {
                if (this.authenticator.handleAuthChallenge(proxy, response, this.proxyAuthStrategy,
                        proxyAuthState, context)) {
                    // Retry request
                    if (this.reuseStrategy.keepAlive(response, context)) {
                        this.log.debug("Connection kept alive");
                        // Consume response content
                        final HttpEntity entity = response.getEntity();
                        EntityUtils.consume(entity);
                    } else {
                        managedConn.close();
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }

    final int status = response.getStatusLine().getStatusCode();

    if (status > 299) {

        // Buffer response content
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            response.setEntity(new BufferedHttpEntity(entity));
        }

        managedConn.close();
        throw new TunnelRefusedException("CONNECT refused by proxy: " + response.getStatusLine(), response);
    }

    // How to decide on security of the tunnelled connection?
    // The socket factory knows only about the segment to the proxy.
    // Even if that is secure, the hop to the target may be insecure.
    // Leave it to derived classes, consider insecure by default here.
    return false;
}

From source file:org.apache.http.impl.execchain.MainClientExec.java

private boolean needAuthentication(final AuthState targetAuthState, final AuthState proxyAuthState,
        final HttpRoute route, final HttpResponse response, final HttpClientContext context) {
    final RequestConfig config = context.getRequestConfig();
    if (config.isAuthenticationEnabled()) {
        HttpHost target = context.getTargetHost();
        if (target == null) {
            target = route.getTargetHost();
        }// w w w  . j  a v a2  s .c  o m
        if (target.getPort() < 0) {
            target = new HttpHost(target.getHostName(), route.getTargetHost().getPort(),
                    target.getSchemeName());
        }
        final boolean targetAuthRequested = this.authenticator.isAuthenticationRequested(target, response,
                this.targetAuthStrategy, targetAuthState, context);

        HttpHost proxy = route.getProxyHost();
        // if proxy is not set use target host instead
        if (proxy == null) {
            proxy = route.getTargetHost();
        }
        final boolean proxyAuthRequested = this.authenticator.isAuthenticationRequested(proxy, response,
                this.proxyAuthStrategy, proxyAuthState, context);

        if (targetAuthRequested) {
            return this.authenticator.handleAuthChallenge(target, response, this.targetAuthStrategy,
                    targetAuthState, context);
        }
        if (proxyAuthRequested) {
            return this.authenticator.handleAuthChallenge(proxy, response, this.proxyAuthStrategy,
                    proxyAuthState, context);
        }
    }
    return false;
}

From source file:org.apache.http.impl.nio.client.MainClientExec.java

private boolean handleConnectResponse(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws HttpException {
    final HttpClientContext localContext = state.getLocalContext();
    final RequestConfig config = localContext.getRequestConfig();
    if (config.isAuthenticationEnabled()) {
        final CredentialsProvider credsProvider = localContext.getCredentialsProvider();
        if (credsProvider != null) {
            final HttpRoute route = handler.getRoute();
            final HttpHost proxy = route.getProxyHost();
            final HttpResponse currentResponse = handler.getCurrentResponse();
            final AuthState proxyAuthState = localContext.getProxyAuthState();
            if (this.authenticator.isAuthenticationRequested(proxy, currentResponse, this.proxyAuthStrategy,
                    proxyAuthState, localContext)) {
                return this.authenticator.handleAuthChallenge(proxy, currentResponse, this.proxyAuthStrategy,
                        proxyAuthState, localContext);
            }//from  ww w.j  a v  a 2  s.c o m
        }
    }
    return false;
}

From source file:org.apache.http.impl.nio.client.MainClientExec.java

private boolean handleResponse(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws HttpException {
    final HttpClientContext localContext = state.getLocalContext();
    final RequestConfig config = localContext.getRequestConfig();
    if (config.isAuthenticationEnabled()) {
        if (needAuthentication(state, handler)) {
            // discard previous auth headers
            final HttpRequestWrapper currentRequest = handler.getCurrentRequest();
            final HttpRequest original = currentRequest.getOriginal();
            if (!original.containsHeader(AUTH.WWW_AUTH_RESP)) {
                currentRequest.removeHeaders(AUTH.WWW_AUTH_RESP);
            }//from  w  ww .  j  av  a2 s . com
            if (!original.containsHeader(AUTH.PROXY_AUTH_RESP)) {
                currentRequest.removeHeaders(AUTH.PROXY_AUTH_RESP);
            }
            return true;
        }
    }
    if (config.isRedirectsEnabled()) {
        final HttpRequest currentRequest = handler.getCurrentRequest();
        final HttpResponse currentResponse = handler.getCurrentResponse();
        if (this.redirectStrategy.isRedirected(currentRequest, currentResponse, localContext)) {
            final int maxRedirects = config.getMaxRedirects() >= 0 ? config.getMaxRedirects() : 100;
            if (state.getRedirectCount() >= maxRedirects) {
                throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
            }
            state.incrementRedirectCount();
            final HttpUriRequest redirect = this.redirectStrategy.getRedirect(currentRequest, currentResponse,
                    localContext);
            state.setRedirect(redirect);
            return true;
        }
    }
    return false;
}