Package org.apache.http

Examples of org.apache.http.HttpClientConnection


        final HttpHost target = start();
        final HttpRoute route = new HttpRoute(target, null, false);
        final HttpContext context = new BasicHttpContext();

        final HttpClientConnection conn = getConnection(this.connManager, route);
        this.connManager.connect(conn, route, 0, context);
        this.connManager.routeComplete(conn, route, context);

        Assert.assertEquals(1, this.connManager.getTotalStats().getLeased());
        Assert.assertEquals(1, this.connManager.getStats(route).getLeased());
View Full Code Here


        final HttpHost target = start();
        final HttpRoute route = new HttpRoute(target, null, false);
        final HttpContext context = new BasicHttpContext();

        final HttpClientConnection conn = getConnection(this.connManager, route);
        this.connManager.connect(conn, route, 0, context);
        this.connManager.routeComplete(conn, route, context);

        Assert.assertEquals(1, this.connManager.getTotalStats().getLeased());
        Assert.assertEquals(1, this.connManager.getStats(route).getLeased());
View Full Code Here

        final HttpContext context = new BasicHttpContext();

        final HttpRequest request =
            new BasicHttpRequest("GET", uri, HttpVersion.HTTP_1_1);

        HttpClientConnection conn = getConnection(this.connManager, route);
        this.connManager.connect(conn, route, 0, context);
        this.connManager.routeComplete(conn, route, context);

        context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
        context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, target);

        final HttpProcessor httpProcessor = new ImmutableHttpProcessor(
                new HttpRequestInterceptor[] { new RequestContent(), new RequestConnControl() });

        final HttpRequestExecutor exec = new HttpRequestExecutor();
        exec.preProcess(request, httpProcessor, context);
        final HttpResponse response = exec.execute(request, conn, context);

        Assert.assertEquals("wrong status in first response",
                     HttpStatus.SC_OK,
                     response.getStatusLine().getStatusCode());

        // check that there are no connections available
        try {
            // this should fail quickly, connection has not been released
            getConnection(this.connManager, route, 100L, TimeUnit.MILLISECONDS);
            Assert.fail("ConnectionPoolTimeoutException should have been thrown");
        } catch (final ConnectionPoolTimeoutException e) {
            // expected
        }

        // abort the connection
        Assert.assertTrue(conn instanceof HttpClientConnection);
        conn.shutdown();
        this.connManager.releaseConnection(conn, null, -1, null);

        // the connection is expected to be released back to the manager
        conn = getConnection(this.connManager, route, 5L, TimeUnit.SECONDS);
        Assert.assertFalse("connection should have been closed", conn.isOpen());

        this.connManager.releaseConnection(conn, null, -1, null);
        this.connManager.shutdown();
    }
View Full Code Here

        final HttpHost target = start();
        final HttpRoute route = new HttpRoute(target, null, false);
        final HttpContext context = new BasicHttpContext();

        final HttpClientConnection conn = getConnection(this.connManager, route);

        final AtomicReference<Throwable> throwRef = new AtomicReference<Throwable>();
        final Thread abortingThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    stallingSocketFactory.waitForState();
                    conn.shutdown();
                    connManager.releaseConnection(conn, null, -1, null);
                    connectLatch.countDown();
                } catch (final Throwable e) {
                    throwRef.set(e);
                }
            }
        });
        abortingThread.start();

        try {
            this.connManager.connect(conn, route, 0, context);
            this.connManager.routeComplete(conn, route, context);
            Assert.fail("expected SocketException");
        } catch(final SocketException expected) {}

        abortingThread.join(5000);
        if(throwRef.get() != null) {
            throw new RuntimeException(throwRef.get());
        }

        Assert.assertFalse(conn.isOpen());

        // the connection is expected to be released back to the manager
        final HttpClientConnection conn2 = getConnection(this.connManager, route, 5L, TimeUnit.SECONDS);
        Assert.assertFalse("connection should have been closed", conn2.isOpen());

        this.connManager.releaseConnection(conn2, null, -1, null);
        this.connManager.shutdown();
    }
View Full Code Here

        final HttpHost target = start();
        final HttpRoute route = new HttpRoute(target, null, false);
        final HttpContext context = new BasicHttpContext();

        final HttpClientConnection conn = getConnection(this.connManager, route);

        final AtomicReference<Throwable> throwRef = new AtomicReference<Throwable>();
        final Thread abortingThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    stallingSocketFactory.waitForState();
                    conn.shutdown();
                    connManager.releaseConnection(conn, null, -1, null);
                    connectLatch.countDown();
                } catch (final Throwable e) {
                    throwRef.set(e);
                }
            }
        });
        abortingThread.start();

        try {
            this.connManager.connect(conn, route, 0, context);
            this.connManager.routeComplete(conn, route, context);
            Assert.fail("IOException expected");
        } catch(final IOException expected) {
        }

        abortingThread.join(5000);
        if(throwRef.get() != null) {
            throw new RuntimeException(throwRef.get());
        }

        Assert.assertFalse(conn.isOpen());

        // the connection is expected to be released back to the manager
        final HttpClientConnection conn2 = getConnection(this.connManager, route, 5L, TimeUnit.SECONDS);
        Assert.assertFalse("connection should have been closed", conn2.isOpen());

        this.connManager.releaseConnection(conn2, null, -1, null);
        this.connManager.shutdown();
    }
View Full Code Here

        final HttpHost target = start();
        final HttpRoute route = new HttpRoute(target, null, false);
        final HttpContext context = new BasicHttpContext();

        final HttpClientConnection conn = getConnection(this.connManager, route);

        final AtomicReference<Throwable> throwRef = new AtomicReference<Throwable>();
        final Thread abortingThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    stallingSocketFactory.waitForState();
                    conn.shutdown();
                    connManager.releaseConnection(conn, null, -1, null);
                    connectLatch.countDown();
                } catch (final Throwable e) {
                    throwRef.set(e);
                }
            }
        });
        abortingThread.start();

        try {
            this.connManager.connect(conn, route, 0, context);
            this.connManager.routeComplete(conn, route, context);
            Assert.fail("IOException expected");
        } catch(final IOException expected) {
        }

        abortingThread.join(5000);
        if(throwRef.get() != null) {
            throw new RuntimeException(throwRef.get());
        }

        Assert.assertFalse(conn.isOpen());

        // the connection is expected to be released back to the manager
        final HttpClientConnection conn2 = getConnection(this.connManager, route, 5L, TimeUnit.SECONDS);
        Assert.assertFalse("connection should have been closed", conn2.isOpen());

        this.connManager.releaseConnection(conn2, null, -1, null);
        this.connManager.shutdown();
    }
View Full Code Here

        HttpPost request = new HttpPost("/echo/");
        request.setHeader("Host", target.getHostName());
        request.setEntity(new StringEntity(message, charset));

        HttpClientConnection conn = connectTo(target);

        httpContext.setAttribute(
                ExecutionContext.HTTP_CONNECTION, conn);
        httpContext.setAttribute(
                ExecutionContext.HTTP_TARGET_HOST, target);
        httpContext.setAttribute(
                ExecutionContext.HTTP_REQUEST, request);

        HttpParamsLinker.link(request, defaultParams);
        httpExecutor.preProcess
            (request, httpProcessor, httpContext);
        HttpResponse response = httpExecutor.execute
            (request, conn, httpContext);
        HttpParamsLinker.link(response, defaultParams);
        httpExecutor.postProcess
            (response, httpProcessor, httpContext);

        assertEquals("wrong status in response", HttpStatus.SC_OK,
                     response.getStatusLine().getStatusCode());

        String received = EntityUtils.toString(response.getEntity());
        conn.close();

        assertEquals("wrong echo", message, received);
    }
View Full Code Here

            if (sizes[i] < 0)
                uri += "/";

            HttpGet request = new HttpGet(uri);

            HttpClientConnection conn = connectTo(target);
           
            httpContext.setAttribute(
                    ExecutionContext.HTTP_CONNECTION, conn);
            httpContext.setAttribute(
                    ExecutionContext.HTTP_TARGET_HOST, target);
            httpContext.setAttribute(
                    ExecutionContext.HTTP_REQUEST, request);

            HttpParamsLinker.link(request, defaultParams);
            httpExecutor.preProcess
                (request, httpProcessor, httpContext);
            HttpResponse response = httpExecutor.execute
                (request, conn, httpContext);
            HttpParamsLinker.link(response, defaultParams);
            httpExecutor.postProcess
                (response, httpProcessor, httpContext);

            assertEquals("(" + sizes[i] + ") wrong status in response",
                         HttpStatus.SC_OK,
                         response.getStatusLine().getStatusCode());

            byte[] data = EntityUtils.toByteArray(response.getEntity());
            if (sizes[i] >= 0)
                assertEquals("(" + sizes[i] + ") wrong length of response",
                             sizes[i], data.length);
            conn.close();
        }
    }
View Full Code Here

        httpexecutor.addInterceptor(new RequestConnControl());
        httpexecutor.addInterceptor(new RequestUserAgent());
        httpexecutor.addInterceptor(new RequestExpectContinue());
       
        HttpHost host = new HttpHost("localhost", 8080);
        HttpClientConnection conn = new DefaultHttpClientConnection(host);
        try {
           
            HttpEntity[] requestBodies = {
                    new StringEntity(
                            "This is the first test request", "UTF-8"),
                    new ByteArrayEntity(
                            "This is the second test request".getBytes("UTF-8")),
                    new InputStreamEntity(
                            new ByteArrayInputStream(
                                    "This is the third test request (will be chunked)"
                                    .getBytes("UTF-8")), -1)
            };
           
            ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
           
            for (int i = 0; i < requestBodies.length; i++) {
                HttpPost request = new HttpPost("/servlets-examples/servlet/RequestInfoExample");
                request.setEntity(requestBodies[i]);
                System.out.println(">> Request URI: " + request.getRequestLine().getUri());
                HttpResponse response = httpexecutor.execute(request, conn);
                System.out.println("<< Response: " + response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
                System.out.println("==============");
                if (!connStrategy.keepAlive(response)) {
                    conn.close();
                } else {
                    System.out.println("Connection kept alive...");
                }
            }
        } finally {
            conn.close();
        }       
    }
View Full Code Here

        httpexecutor.addInterceptor(new RequestConnControl());
        httpexecutor.addInterceptor(new RequestUserAgent());
        httpexecutor.addInterceptor(new RequestExpectContinue());
       
        HttpHost host = new HttpHost("localhost", 8080);
        HttpClientConnection conn = new DefaultHttpClientConnection(host);
        try {
           
            String[] targets = {
                    "/",
                    "/servlets-examples/servlet/RequestInfoExample",
                    "/somewhere%20in%20pampa"};
           
            ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
           
            for (int i = 0; i < targets.length; i++) {
                HttpGet request = new HttpGet(targets[i]);
                System.out.println(">> Request URI: " + request.getRequestLine().getUri());
                HttpResponse response = httpexecutor.execute(request, conn);
                System.out.println("<< Response: " + response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
                System.out.println("==============");
                if (!connStrategy.keepAlive(response)) {
                    conn.close();
                } else {
                    System.out.println("Connection kept alive...");
                }
            }
        } finally {
            conn.close();
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.http.HttpClientConnection

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.