Search in sources :

Example 11 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project XobotOS by xamarin.

the class ConnPoolByRoute method freeEntry.

// getEntry
// non-javadoc, see base class AbstractConnPool
@Override
public void freeEntry(BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit) {
    HttpRoute route = entry.getPlannedRoute();
    if (log.isDebugEnabled()) {
        log.debug("Freeing connection" + " [" + route + "][" + entry.getState() + "]");
    }
    poolLock.lock();
    try {
        if (isShutDown) {
            // the pool is shut down, release the
            // connection's resources and get out of here
            closeConnection(entry.getConnection());
            return;
        }
        // no longer issued, we keep a hard reference now
        issuedConnections.remove(entry.getWeakRef());
        RouteSpecificPool rospl = getRoutePool(route, true);
        if (reusable) {
            rospl.freeEntry(entry);
            freeConnections.add(entry);
            idleConnHandler.add(entry.getConnection(), validDuration, timeUnit);
        } else {
            rospl.dropEntry();
            numConnections--;
        }
        notifyWaitingThread(rospl);
    } finally {
        poolLock.unlock();
    }
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute)

Example 12 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project platform_external_apache-http by android.

the class ConnPoolByRoute method deleteEntry.

/**
     * Deletes a given pool entry.
     * This closes the pooled connection and removes all references,
     * so that it can be GCed.
     * 
     * <p><b>Note:</b> Does not remove the entry from the freeConnections list.
     * It is assumed that the caller has already handled this step.</p>
     * <!-- @@@ is that a good idea? or rather fix it? -->
     * 
     * @param entry         the pool entry for the connection to delete
     */
protected void deleteEntry(BasicPoolEntry entry) {
    HttpRoute route = entry.getPlannedRoute();
    if (log.isDebugEnabled()) {
        log.debug("Deleting connection" + " [" + route + "][" + entry.getState() + "]");
    }
    poolLock.lock();
    try {
        closeConnection(entry.getConnection());
        RouteSpecificPool rospl = getRoutePool(route, true);
        rospl.deleteEntry(entry);
        numConnections--;
        if (rospl.isUnused()) {
            routeToPool.remove(route);
        }
        // not idle, but dead
        idleConnHandler.remove(entry.getConnection());
    } finally {
        poolLock.unlock();
    }
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute)

Example 13 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project platform_external_apache-http by android.

the class DefaultRequestDirector method establishRoute.

/**
     * Establishes the target route.
     *
     * @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 {
    //@@@ how to handle CONNECT requests for tunnelling?
    //@@@ refuse to send external CONNECT via director? special handling?
    //@@@ should the request parameters already be used below?
    //@@@ probably yes, but they're not linked yet
    //@@@ will linking above cause problems with linking in reqExec?
    //@@@ probably not, because the parent is replaced
    //@@@ just make sure we don't link parameters to themselves
    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)
                    // the hop to establish
                    final int hop = fact.getHopCount() - 1;
                    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);
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) BasicRouteDirector(org.apache.http.conn.routing.BasicRouteDirector) HttpRouteDirector(org.apache.http.conn.routing.HttpRouteDirector)

Example 14 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project platform_external_apache-http by android.

the class DefaultRequestDirector method handleResponse.

/**
     * Analyzes a response to check need for a followup.
     *
     * @param roureq    the request and route. 
     * @param response  the response to analayze
     * @param context   the context used for the current request execution
     *
     * @return  the followup request and route if there is a followup, or
     *          <code>null</code> if the response should be returned as is
     *
     * @throws HttpException    in case of a problem
     * @throws IOException      in case of an IO problem
     */
protected RoutedRequest handleResponse(RoutedRequest roureq, HttpResponse response, HttpContext context) throws HttpException, IOException {
    HttpRoute route = roureq.getRoute();
    HttpHost proxy = route.getProxyHost();
    RequestWrapper request = roureq.getRequest();
    HttpParams params = request.getParams();
    if (HttpClientParams.isRedirecting(params) && this.redirectHandler.isRedirectRequested(response, context)) {
        if (redirectCount >= maxRedirects) {
            throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
        }
        redirectCount++;
        URI uri = this.redirectHandler.getLocationURI(response, context);
        HttpHost newTarget = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        HttpGet redirect = new HttpGet(uri);
        HttpRequest orig = request.getOriginal();
        redirect.setHeaders(orig.getAllHeaders());
        RequestWrapper wrapper = new RequestWrapper(redirect);
        wrapper.setParams(params);
        HttpRoute newRoute = determineRoute(newTarget, wrapper, context);
        RoutedRequest newRequest = new RoutedRequest(wrapper, newRoute);
        if (this.log.isDebugEnabled()) {
            this.log.debug("Redirecting to '" + uri + "' via " + newRoute);
        }
        return newRequest;
    }
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
        if (this.targetAuthHandler.isAuthenticationRequested(response, context)) {
            HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
            if (target == null) {
                target = route.getTargetHost();
            }
            this.log.debug("Target requested authentication");
            Map<String, Header> challenges = this.targetAuthHandler.getChallenges(response, context);
            try {
                processChallenges(challenges, this.targetAuthState, this.targetAuthHandler, response, context);
            } catch (AuthenticationException ex) {
                if (this.log.isWarnEnabled()) {
                    this.log.warn("Authentication error: " + ex.getMessage());
                    return null;
                }
            }
            updateAuthState(this.targetAuthState, target, credsProvider);
            if (this.targetAuthState.getCredentials() != null) {
                // Re-try the same request via the same route
                return roureq;
            } else {
                return null;
            }
        } else {
            // Reset target auth scope
            this.targetAuthState.setAuthScope(null);
        }
        if (this.proxyAuthHandler.isAuthenticationRequested(response, context)) {
            this.log.debug("Proxy requested authentication");
            Map<String, Header> challenges = this.proxyAuthHandler.getChallenges(response, context);
            try {
                processChallenges(challenges, this.proxyAuthState, this.proxyAuthHandler, response, context);
            } catch (AuthenticationException ex) {
                if (this.log.isWarnEnabled()) {
                    this.log.warn("Authentication error: " + ex.getMessage());
                    return null;
                }
            }
            updateAuthState(this.proxyAuthState, proxy, credsProvider);
            if (this.proxyAuthState.getCredentials() != null) {
                // Re-try the same request via the same route
                return roureq;
            } else {
                return null;
            }
        } else {
            // Reset proxy auth scope
            this.proxyAuthState.setAuthScope(null);
        }
    }
    return null;
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) AbortableHttpRequest(org.apache.http.client.methods.AbortableHttpRequest) AuthenticationException(org.apache.http.auth.AuthenticationException) HttpGet(org.apache.http.client.methods.HttpGet) CredentialsProvider(org.apache.http.client.CredentialsProvider) URI(java.net.URI) HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) HttpHost(org.apache.http.HttpHost) RedirectException(org.apache.http.client.RedirectException)

Example 15 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project platform_external_apache-http by android.

the class AbstractConnPool method handleReference.

// non-javadoc, see interface RefQueueHandler
// BEGIN android-changed
public void handleReference(Reference ref) {
    // END android-changed
    poolLock.lock();
    try {
        if (ref instanceof BasicPoolEntryRef) {
            // check if the GCed pool entry was still in use
            //@@@ find a way to detect this without lookup
            //@@@ flag in the BasicPoolEntryRef, to be reset when freed?
            final boolean lost = issuedConnections.remove(ref);
            if (lost) {
                final HttpRoute route = ((BasicPoolEntryRef) ref).getRoute();
                if (log.isDebugEnabled()) {
                    log.debug("Connection garbage collected. " + route);
                }
                handleLostEntry(route);
            }
        }
    } finally {
        poolLock.unlock();
    }
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute)

Aggregations

HttpRoute (org.apache.http.conn.routing.HttpRoute)33 HttpHost (org.apache.http.HttpHost)17 HttpRequest (org.apache.http.HttpRequest)9 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)8 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)8 InetAddress (java.net.InetAddress)7 IOException (java.io.IOException)6 HttpException (org.apache.http.HttpException)6 Scheme (org.apache.http.conn.scheme.Scheme)6 HttpResponse (org.apache.http.HttpResponse)5 AuthenticationException (org.apache.http.auth.AuthenticationException)5 RedirectException (org.apache.http.client.RedirectException)5 ClientConnectionRequest (org.apache.http.conn.ClientConnectionRequest)5 HttpParams (org.apache.http.params.HttpParams)5 InterruptedIOException (java.io.InterruptedIOException)4 URI (java.net.URI)4 Header (org.apache.http.Header)4 HttpEntity (org.apache.http.HttpEntity)4 CredentialsProvider (org.apache.http.client.CredentialsProvider)4 NonRepeatableRequestException (org.apache.http.client.NonRepeatableRequestException)4