Example usage for org.apache.http.conn.routing HttpRouteDirector UNREACHABLE

List of usage examples for org.apache.http.conn.routing HttpRouteDirector UNREACHABLE

Introduction

In this page you can find the example usage for org.apache.http.conn.routing HttpRouteDirector UNREACHABLE.

Prototype

int UNREACHABLE

To view the source code for org.apache.http.conn.routing HttpRouteDirector UNREACHABLE.

Click Source Link

Document

Indicates that the route can not be established at all.

Usage

From source file:com.grendelscan.commons.http.apache_overrides.client.CustomClientRequestDirector.java

/**
 * Establishes the target route./*w  w  w  .ja  va 2 s . co  m*/
 * 
 * @param route
 *            the route to establish
 * @param context
 *            the context for the request execution
 * 
 * @throws HttpException
 *             in case of a problem
 * @throws IOException
 *             in case of an IO problem
 */
private void establishRoute(HttpRoute route, HttpContext context) throws HttpException, IOException {

    HttpRouteDirector rowdy = new BasicRouteDirector();
    int step;
    do {
        HttpRoute fact = managedConn.getRoute();
        step = rowdy.nextStep(route, fact);

        switch (step) {

        case HttpRouteDirector.CONNECT_TARGET:
        case HttpRouteDirector.CONNECT_PROXY:
            managedConn.open(route, context, params);
            break;

        case HttpRouteDirector.TUNNEL_TARGET: {
            boolean secure = createTunnelToTarget(route, context);
            LOGGER.debug("Tunnel to target created.");
            managedConn.tunnelTarget(secure, params);
        }
            break;

        case HttpRouteDirector.TUNNEL_PROXY: {
            throw new NotImplementedException("Proxy chaining not supported");
        }

        case HttpRouteDirector.LAYER_PROTOCOL:
            managedConn.layerProtocol(context, params);
            break;

        case HttpRouteDirector.UNREACHABLE:
            throw new IllegalStateException(
                    "Unable to establish route." + "\nplanned = " + route + "\ncurrent = " + fact);

        case HttpRouteDirector.COMPLETE:
            // do nothing
            break;

        default:
            throw new IllegalStateException("Unknown step indicator " + step + " from RouteDirector.");
        } // switch

    } while (step > HttpRouteDirector.COMPLETE);

}

From source file:org.robolectric.shadows.httpclient.DefaultRequestDirector.java

/**
 * Establishes the target route.//  w  w w. j a v a2 s. com
 *
 * @param route     the route to establish
 * @param context   the context for the request execution
 *
 * @throws HttpException    in case of a problem
 * @throws IOException      in case of an IO problem
 */
protected void establishRoute(HttpRoute route, HttpContext context) throws HttpException, IOException {

    HttpRouteDirector rowdy = new BasicRouteDirector();
    int step;
    do {
        HttpRoute fact = managedConn.getRoute();
        step = rowdy.nextStep(route, fact);

        switch (step) {

        case HttpRouteDirector.CONNECT_TARGET:
        case HttpRouteDirector.CONNECT_PROXY:
            managedConn.open(route, context, this.params);
            break;

        case HttpRouteDirector.TUNNEL_TARGET: {
            boolean secure = createTunnelToTarget(route, context);
            this.log.debug("Tunnel to target created.");
            managedConn.tunnelTarget(secure, this.params);
        }
            break;

        case HttpRouteDirector.TUNNEL_PROXY: {
            // The most simple example for this case is a proxy chain
            // of two proxies, where P1 must be tunnelled to P2.
            // route: Source -> P1 -> P2 -> Target (3 hops)
            // fact:  Source -> P1 -> Target       (2 hops)
            final int hop = fact.getHopCount() - 1; // the hop to establish
            boolean secure = createTunnelToProxy(route, hop, context);
            this.log.debug("Tunnel to proxy created.");
            managedConn.tunnelProxy(route.getHopTarget(hop), secure, this.params);
        }
            break;

        case HttpRouteDirector.LAYER_PROTOCOL:
            managedConn.layerProtocol(context, this.params);
            break;

        case HttpRouteDirector.UNREACHABLE:
            throw new IllegalStateException(
                    "Unable to establish route." + "\nplanned = " + route + "\ncurrent = " + fact);

        case HttpRouteDirector.COMPLETE:
            // do nothing
            break;

        default:
            throw new IllegalStateException("Unknown step indicator " + step + " from RouteDirector.");
        } // switch

    } while (step > HttpRouteDirector.COMPLETE);

}

From source file:org.vietspider.net.apache.DefaultRequestDirector.java

/**
 * Establishes the target route./*from  ww  w.j a  va  2s. co  m*/
 *
 * @param route     the route to establish
 * @param context   the context for the request execution
 *
 * @throws HttpException    in case of a problem
 * @throws IOException      in case of an IO problem
 */
protected void establishRoute(HttpRoute route, HttpContext context) throws HttpException, IOException {

    HttpRouteDirector rowdy = new BasicRouteDirector();
    int step;
    do {
        HttpRoute fact = managedConn.getRoute();
        step = rowdy.nextStep(route, fact);

        switch (step) {

        case HttpRouteDirector.CONNECT_TARGET:
        case HttpRouteDirector.CONNECT_PROXY:
            managedConn.open(route, context, this.params);
            break;

        case HttpRouteDirector.TUNNEL_TARGET: {
            boolean secure = createTunnelToTarget(route, context);
            this.log.debug("Tunnel to target created.");
            managedConn.tunnelTarget(secure, this.params);
        }
            break;

        case HttpRouteDirector.TUNNEL_PROXY: {
            // The most simple example for this case is a proxy chain
            // of two proxies, where P1 must be tunnelled to P2.
            // route: Source -> P1 -> P2 -> Target (3 hops)
            // fact:  Source -> P1 -> Target       (2 hops)
            final int hop = fact.getHopCount() - 1; // the hop to establish
            boolean secure = createTunnelToProxy(route, hop, context);
            this.log.debug("Tunnel to proxy created.");
            managedConn.tunnelProxy(route.getHopTarget(hop), secure, this.params);
        }
            break;

        case HttpRouteDirector.LAYER_PROTOCOL:
            managedConn.layerProtocol(context, this.params);
            break;

        case HttpRouteDirector.UNREACHABLE:
            throw new HttpException(
                    "Unable to establish route: " + "planned = " + route + "; current = " + fact);
        case HttpRouteDirector.COMPLETE:
            // do nothing
            break;
        default:
            throw new IllegalStateException("Unknown step indicator " + step + " from RouteDirector.");
        }

    } while (step > HttpRouteDirector.COMPLETE);

}

From source file:org.apache.http.impl.client.DefaultRequestDirector.java

/**
 * Establishes the target route.// w  w w .jav  a2  s.c  om
 *
 * @param route     the route to establish
 * @param context   the context for the request execution
 *
 * @throws HttpException    in case of a problem
 * @throws IOException      in case of an IO problem
 */
protected void establishRoute(final HttpRoute route, final HttpContext context)
        throws HttpException, IOException {

    final HttpRouteDirector rowdy = new BasicRouteDirector();
    int step;
    do {
        final HttpRoute fact = managedConn.getRoute();
        step = rowdy.nextStep(route, fact);

        switch (step) {

        case HttpRouteDirector.CONNECT_TARGET:
        case HttpRouteDirector.CONNECT_PROXY:
            managedConn.open(route, context, this.params);
            break;

        case HttpRouteDirector.TUNNEL_TARGET: {
            final boolean secure = createTunnelToTarget(route, context);
            this.log.debug("Tunnel to target created.");
            managedConn.tunnelTarget(secure, this.params);
        }
            break;

        case HttpRouteDirector.TUNNEL_PROXY: {
            // The most simple example for this case is a proxy chain
            // of two proxies, where P1 must be tunnelled to P2.
            // route: Source -> P1 -> P2 -> Target (3 hops)
            // fact:  Source -> P1 -> Target       (2 hops)
            final int hop = fact.getHopCount() - 1; // the hop to establish
            final boolean secure = createTunnelToProxy(route, hop, context);
            this.log.debug("Tunnel to proxy created.");
            managedConn.tunnelProxy(route.getHopTarget(hop), secure, this.params);
        }
            break;

        case HttpRouteDirector.LAYER_PROTOCOL:
            managedConn.layerProtocol(context, this.params);
            break;

        case HttpRouteDirector.UNREACHABLE:
            throw new HttpException(
                    "Unable to establish route: " + "planned = " + route + "; current = " + fact);
        case HttpRouteDirector.COMPLETE:
            // do nothing
            break;
        default:
            throw new IllegalStateException("Unknown step indicator " + step + " from RouteDirector.");
        }

    } while (step > HttpRouteDirector.COMPLETE);

}

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

/**
 * Establishes the target route./*from   w  ww.j a  v  a  2  s .c om*/
 */
void establishRoute(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 RouteTracker tracker = new RouteTracker(route);
    int step;
    do {
        final HttpRoute fact = tracker.toRoute();
        step = this.routeDirector.nextStep(route, fact);

        switch (step) {

        case HttpRouteDirector.CONNECT_TARGET:
            this.connManager.connect(managedConn, route, timeout > 0 ? timeout : 0, context);
            tracker.connectTarget(route.isSecure());
            break;
        case HttpRouteDirector.CONNECT_PROXY:
            this.connManager.connect(managedConn, route, timeout > 0 ? timeout : 0, context);
            final HttpHost proxy = route.getProxyHost();
            tracker.connectProxy(proxy, false);
            break;
        case HttpRouteDirector.TUNNEL_TARGET: {
            final boolean secure = createTunnelToTarget(proxyAuthState, managedConn, route, request, context);
            this.log.debug("Tunnel to target created.");
            tracker.tunnelTarget(secure);
        }
            break;

        case HttpRouteDirector.TUNNEL_PROXY: {
            // The most simple example for this case is a proxy chain
            // of two proxies, where P1 must be tunnelled to P2.
            // route: Source -> P1 -> P2 -> Target (3 hops)
            // fact:  Source -> P1 -> Target       (2 hops)
            final int hop = fact.getHopCount() - 1; // the hop to establish
            final boolean secure = createTunnelToProxy(route, hop, context);
            this.log.debug("Tunnel to proxy created.");
            tracker.tunnelProxy(route.getHopTarget(hop), secure);
        }
            break;

        case HttpRouteDirector.LAYER_PROTOCOL:
            this.connManager.upgrade(managedConn, route, context);
            tracker.layerProtocol(route.isSecure());
            break;

        case HttpRouteDirector.UNREACHABLE:
            throw new HttpException(
                    "Unable to establish route: " + "planned = " + route + "; current = " + fact);
        case HttpRouteDirector.COMPLETE:
            this.connManager.routeComplete(managedConn, route, context);
            break;
        default:
            throw new IllegalStateException("Unknown step indicator " + step + " from RouteDirector.");
        }

    } while (step > HttpRouteDirector.COMPLETE);
}

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

@Override
public synchronized HttpRequest generateRequest() throws IOException, HttpException {
    final HttpRoute route = this.mainRequest.getRoute();
    if (!this.routeEstablished) {
        int step;
        do {//  w w w .ja v  a2  s. c o m
            final HttpRoute fact = this.managedConn.getRoute();
            step = this.routeDirector.nextStep(route, fact);
            switch (step) {
            case HttpRouteDirector.CONNECT_TARGET:
            case HttpRouteDirector.CONNECT_PROXY:
                break;
            case HttpRouteDirector.TUNNEL_TARGET:
                if (this.log.isDebugEnabled()) {
                    this.log.debug("[exchange: " + this.id + "] Tunnel required");
                }
                final HttpRequest connect = createConnectRequest(route);
                this.currentRequest = wrapRequest(connect);
                this.currentRequest.setParams(this.params);
                break;
            case HttpRouteDirector.TUNNEL_PROXY:
                throw new HttpException("Proxy chains are not supported");
            case HttpRouteDirector.LAYER_PROTOCOL:
                managedConn.layerProtocol(this.localContext, this.params);
                break;
            case HttpRouteDirector.UNREACHABLE:
                throw new HttpException(
                        "Unable to establish route: " + "planned = " + route + "; current = " + fact);
            case HttpRouteDirector.COMPLETE:
                this.routeEstablished = true;
                break;
            default:
                throw new IllegalStateException("Unknown step indicator " + step + " from RouteDirector.");
            }
        } while (step > HttpRouteDirector.COMPLETE && this.currentRequest == null);
    }

    HttpHost target = (HttpHost) this.params.getParameter(ClientPNames.VIRTUAL_HOST);
    if (target == null) {
        target = route.getTargetHost();
    }
    final HttpHost proxy = route.getProxyHost();
    this.localContext.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
    this.localContext.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy);
    this.localContext.setAttribute(ExecutionContext.HTTP_CONNECTION, this.managedConn);
    this.localContext.setAttribute(ClientContext.ROUTE, route);

    if (this.currentRequest == null) {
        this.currentRequest = this.mainRequest.getRequest();

        final String userinfo = this.currentRequest.getURI().getUserInfo();
        if (userinfo != null) {
            this.targetAuthState.update(new BasicScheme(), new UsernamePasswordCredentials(userinfo));
        }

        // Re-write request URI if needed
        rewriteRequestURI(this.currentRequest, route);
    }
    // Reset headers on the request wrapper
    this.currentRequest.resetHeaders();

    this.currentRequest.incrementExecCount();
    if (this.currentRequest.getExecCount() > 1 && !this.requestProducer.isRepeatable()
            && this.requestContentProduced) {
        throw new NonRepeatableRequestException(
                "Cannot retry request " + "with a non-repeatable request entity.");
    }
    this.execCount++;
    if (this.log.isDebugEnabled()) {
        this.log.debug("[exchange: " + this.id + "] Attempt " + this.execCount + " to execute request");
    }
    return this.currentRequest;
}

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

@Override
public HttpRequest generateRequest(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws IOException, HttpException {

    final HttpRoute route = handler.getRoute();

    handler.verifytRoute();//from  ww  w .ja  v a 2  s.c  om

    if (!handler.isRouteEstablished()) {
        int step;
        loop: do {
            final HttpRoute fact = handler.getActualRoute();
            step = this.routeDirector.nextStep(route, fact);
            switch (step) {
            case HttpRouteDirector.CONNECT_TARGET:
                handler.onRouteToTarget();
                break;
            case HttpRouteDirector.CONNECT_PROXY:
                handler.onRouteToProxy();
                break;
            case HttpRouteDirector.TUNNEL_TARGET:
                if (this.log.isDebugEnabled()) {
                    this.log.debug("[exchange: " + state.getId() + "] Tunnel required");
                }
                final HttpRequest connect = createConnectRequest(route, state);
                handler.setCurrentRequest(HttpRequestWrapper.wrap(connect));
                break loop;
            case HttpRouteDirector.TUNNEL_PROXY:
                throw new HttpException("Proxy chains are not supported");
            case HttpRouteDirector.LAYER_PROTOCOL:
                handler.onRouteUpgrade();
                break;
            case HttpRouteDirector.UNREACHABLE:
                throw new HttpException(
                        "Unable to establish route: " + "planned = " + route + "; current = " + fact);
            case HttpRouteDirector.COMPLETE:
                handler.onRouteComplete();
                this.log.debug("Connection route established");
                break;
            default:
                throw new IllegalStateException("Unknown step indicator " + step + " from RouteDirector.");
            }
        } while (step > HttpRouteDirector.COMPLETE);
    }

    final HttpClientContext localContext = state.getLocalContext();
    HttpRequestWrapper currentRequest = handler.getCurrentRequest();
    if (currentRequest == null) {
        currentRequest = state.getMainRequest();
        handler.setCurrentRequest(currentRequest);
    }

    if (handler.isRouteEstablished()) {
        state.incrementExecCount();
        if (state.getExecCount() > 1) {
            final HttpAsyncRequestProducer requestProducer = state.getRequestProducer();
            if (!requestProducer.isRepeatable() && state.isRequestContentProduced()) {
                throw new NonRepeatableRequestException(
                        "Cannot retry request " + "with a non-repeatable request entity.");
            }
            requestProducer.resetRequest();
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("[exchange: " + state.getId() + "] Attempt " + state.getExecCount()
                    + " to execute request");
        }

        if (!currentRequest.containsHeader(AUTH.WWW_AUTH_RESP)) {
            final AuthState targetAuthState = localContext.getTargetAuthState();
            if (this.log.isDebugEnabled()) {
                this.log.debug("Target auth state: " + targetAuthState.getState());
            }
            this.authenticator.generateAuthResponse(currentRequest, targetAuthState, localContext);
        }
        if (!currentRequest.containsHeader(AUTH.PROXY_AUTH_RESP) && !route.isTunnelled()) {
            final AuthState proxyAuthState = localContext.getProxyAuthState();
            if (this.log.isDebugEnabled()) {
                this.log.debug("Proxy auth state: " + proxyAuthState.getState());
            }
            this.authenticator.generateAuthResponse(currentRequest, proxyAuthState, localContext);
        }
    } else {
        if (!currentRequest.containsHeader(AUTH.PROXY_AUTH_RESP)) {
            final AuthState proxyAuthState = localContext.getProxyAuthState();
            if (this.log.isDebugEnabled()) {
                this.log.debug("Proxy auth state: " + proxyAuthState.getState());
            }
            this.authenticator.generateAuthResponse(currentRequest, proxyAuthState, localContext);
        }
    }

    final NHttpClientConnection managedConn = handler.getConnection();
    localContext.setAttribute(HttpCoreContext.HTTP_CONNECTION, managedConn);
    final RequestConfig config = localContext.getRequestConfig();
    if (config.getSocketTimeout() > 0) {
        managedConn.setSocketTimeout(config.getSocketTimeout());
    }
    return currentRequest;
}