Package org.apache.http.nio

Examples of org.apache.http.nio.NHttpClientConnection


        public void completed(final CPoolEntry entry) {
            Asserts.check(entry.getConnection() != null, "Pool entry with no connection");
            if (log.isDebugEnabled()) {
                log.debug("Connection leased: " + format(entry) + formatStats(entry.getRoute()));
            }
            final NHttpClientConnection managedConn = CPoolProxy.newProxy(entry);
            if (!this.future.completed(managedConn)) {
                pool.release(entry, true);
            }
        }
View Full Code Here


            return false;
        }
    }

    public boolean isStale() {
        final NHttpClientConnection conn = getConnection();
        if (conn != null) {
            return conn.isStale() || !conn.isOpen();
        } else {
            return true;
        }
    }
View Full Code Here

        } else if (method.equals(IS_OPEN_METHOD)) {
            return Boolean.valueOf(isOpen());
        } else if (method.equals(IS_STALE_METHOD)) {
            return Boolean.valueOf(isStale());
        } else {
            final NHttpClientConnection conn = getConnection();
            if (conn == null) {
                if (method.getDeclaringClass().equals(IOControl.class)) {
                    // Ignore IOControl operations on closed connections
                    return null;
                } else {
View Full Code Here

                }
            } finally {
                close();
            }
        } else {
            NHttpClientConnection localConn = this.managedConn.get();
            if (localConn != null && !localConn.isOpen()) {
                releaseConnection();
                localConn = null;
            }
            if (localConn != null) {
                localConn.requestOutput();
            } else {
                requestConnection();
            }
        }
    }
View Full Code Here

            close();
        }
    }

    public void releaseConnection() {
        final NHttpClientConnection localConn = this.managedConn.getAndSet(null);
        if (localConn != null) {
            if (this.log.isDebugEnabled()) {
                this.log.debug("[exchange: " + this.state.getId() + "] releasing connection");
            }
            localConn.getContext().removeAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER);
            if (this.state.isReusable()) {
                this.connmgr.releaseConnection(localConn,
                        this.localContext.getUserToken(),
                        this.state.getValidDuration(), TimeUnit.MILLISECONDS);
            } else {
                try {
                    localConn.close();
                    if (this.log.isDebugEnabled()) {
                        this.log.debug("[exchange: " + this.state.getId() + "] connection discarded");
                    }
                } catch (final IOException ex) {
                    if (this.log.isDebugEnabled()) {
View Full Code Here

    public void abortConnection() {
        discardConnection();
    }

    private void discardConnection() {
        final NHttpClientConnection localConn = this.managedConn.getAndSet(null);
        if (localConn != null) {
            try {
                localConn.shutdown();
                if (this.log.isDebugEnabled()) {
                    this.log.debug("[exchange: " + this.state.getId() + "] connection aborted");
                }
            } catch (final IOException ex) {
                if (this.log.isDebugEnabled()) {
View Full Code Here

    public HttpRequest generateRequest(
            final InternalState state,
            final InternalConnManager connManager) throws IOException, HttpException {
        final HttpClientContext localContext = state.getLocalContext();
        final HttpRoute route = state.getRoute();
        final NHttpClientConnection managedConn = connManager.getConnection();
        if (!state.isRouteEstablished() && state.getRouteTracker() == null) {
            state.setRouteEstablished(this.connmgr.isRouteComplete(managedConn));
            if (!state.isRouteEstablished()) {
                this.log.debug("Start connection routing");
                state.setRouteTracker(new RouteTracker(route));
            } else {
                this.log.debug("Connection route already established");
            }
        }

        if (!state.isRouteEstablished()) {
            final RouteTracker routeTracker = state.getRouteTracker();
            int step;
            loop:
            do {
                final HttpRoute fact = routeTracker.toRoute();
                step = this.routeDirector.nextStep(route, fact);
                switch (step) {
                case HttpRouteDirector.CONNECT_TARGET:
                    this.connmgr.startRoute(managedConn, route, localContext);
                    routeTracker.connectTarget(route.isSecure());
                    break;
                case HttpRouteDirector.CONNECT_PROXY:
                    this.connmgr.startRoute(managedConn, route, localContext);
                    final HttpHost proxy  = route.getProxyHost();
                    routeTracker.connectProxy(proxy, false);
                    break;
                case HttpRouteDirector.TUNNEL_TARGET:
                    if (this.log.isDebugEnabled()) {
                        this.log.debug("[exchange: " + state.getId() + "] Tunnel required");
                    }
                    final HttpRequest connect = createConnectRequest(route, state);
                    state.setCurrentRequest(HttpRequestWrapper.wrap(connect));
                    break loop;
                case HttpRouteDirector.TUNNEL_PROXY:
                    throw new HttpException("Proxy chains are not supported");
                case HttpRouteDirector.LAYER_PROTOCOL:
                    this.connmgr.upgrade(managedConn, route, localContext);
                    routeTracker.layerProtocol(route.isSecure());
                    break;
                case HttpRouteDirector.UNREACHABLE:
                    throw new HttpException("Unable to establish route: " +
                            "planned = " + route + "; current = " + fact);
                case HttpRouteDirector.COMPLETE:
                    this.connmgr.routeComplete(managedConn, route, localContext);
                    state.setRouteEstablished(true);
                    state.setRouteTracker(null);
                    this.log.debug("Connection route established");
                    break;
                default:
                    throw new IllegalStateException("Unknown step indicator "
                            + step + " from RouteDirector.");
                }
            } while (step > HttpRouteDirector.COMPLETE);
        }

        HttpRequestWrapper currentRequest = state.getCurrentRequest();
        if (currentRequest == null) {
            currentRequest = state.getMainRequest();
            state.setCurrentRequest(currentRequest);
        }

        if (state.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);
            }
        }

        localContext.setAttribute(HttpCoreContext.HTTP_CONNECTION, managedConn);
        final RequestConfig config = localContext.getRequestConfig();
        if (config.getSocketTimeout() > 0) {
            managedConn.setSocketTimeout(config.getSocketTimeout());
        }
        return currentRequest;
    }
View Full Code Here

                state.setCurrentResponse(null);
                return;
            }
        }

        final NHttpClientConnection managedConn = connManager.getConnection();
        if (managedConn.isOpen() && this.connReuseStrategy.keepAlive(currentResponse, localContext)) {
            final long validDuration = this.keepaliveStrategy.getKeepAliveDuration(
                    currentResponse, localContext);
            if (this.log.isDebugEnabled()) {
                final String s;
                if (validDuration > 0) {
                    s = "for " + validDuration + " " + TimeUnit.MILLISECONDS;
                } else {
                    s = "indefinitely";
                }
                this.log.debug("[exchange: " + state.getId() + "] Connection can be kept alive " + s);
            }
            state.setValidDuration(validDuration);
            state.setReusable();
        } else {
            if (this.log.isDebugEnabled()) {
                if (managedConn.isOpen()) {
                    this.log.debug("[exchange: " + state.getId() + "] Connection cannot be kept alive");
                }
            }
            state.setNonReusable();
            connManager.releaseConnection();
View Full Code Here

    public HttpRequest generateRequest(
            final InternalState state,
            final InternalConnManager connManager) throws IOException, HttpException {
        final HttpClientContext localContext = state.getLocalContext();
        final HttpRoute route = state.getRoute();
        final NHttpClientConnection localConn = connManager.getConnection();
        if (!this.connmgr.isRouteComplete(localConn)) {
            this.connmgr.startRoute(localConn, route, localContext);
            this.connmgr.routeComplete(localConn, route, localContext);
        }
        localContext.setAttribute(HttpCoreContext.HTTP_CONNECTION, localConn);
        final RequestConfig config = localContext.getRequestConfig();
        if (config.getSocketTimeout() > 0) {
            localConn.setSocketTimeout(config.getSocketTimeout());
        }
        return state.getCurrentRequest();
    }
View Full Code Here

        final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
        poolentry.markRouteComplete();
        callaback.completed(poolentry);

        Assert.assertTrue(future.isDone());
        final NHttpClientConnection managedConn = future.get();
        Assert.assertTrue(Proxy.isProxyClass(managedConn.getClass()));
        Mockito.verify(connCallback).completed(Mockito.<NHttpClientConnection>any());

        Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);
        connman.releaseConnection(managedConn, "new state", 5, TimeUnit.SECONDS);
View Full Code Here

TOP

Related Classes of org.apache.http.nio.NHttpClientConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.