Example usage for org.apache.http.impl.execchain TunnelRefusedException TunnelRefusedException

List of usage examples for org.apache.http.impl.execchain TunnelRefusedException TunnelRefusedException

Introduction

In this page you can find the example usage for org.apache.http.impl.execchain TunnelRefusedException TunnelRefusedException.

Prototype

public TunnelRefusedException(final String message, final HttpResponse response) 

Source Link

Usage

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.
 *//*from w  w  w. j  av a2s  . 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;
}