Search in sources :

Example 1 with Connection

use of org.eclipse.jetty.io.Connection in project druid by druid-io.

the class JettyMonitoringConnectionFactory method newConnection.

@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
    final Connection connection = connectionFactory.newConnection(connector, endPoint);
    connection.addListener(new Connection.Listener() {

        @Override
        public void onOpened(Connection connection) {
            activeConns.incrementAndGet();
        }

        @Override
        public void onClosed(Connection connection) {
            activeConns.decrementAndGet();
        }
    });
    return connection;
}
Also used : Connection(org.eclipse.jetty.io.Connection)

Example 2 with Connection

use of org.eclipse.jetty.io.Connection in project jetty.project by eclipse.

the class LocalConnector method accept.

@Override
protected void accept(int acceptorID) throws IOException, InterruptedException {
    if (LOG.isDebugEnabled())
        LOG.debug("accepting {}", acceptorID);
    LocalEndPoint endPoint = _connects.take();
    endPoint.onOpen();
    onEndPointOpened(endPoint);
    Connection connection = getDefaultConnectionFactory().newConnection(this, endPoint);
    endPoint.setConnection(connection);
    connection.onOpen();
}
Also used : Connection(org.eclipse.jetty.io.Connection)

Example 3 with Connection

use of org.eclipse.jetty.io.Connection in project jetty.project by eclipse.

the class SslConnectionFactory method newConnection.

@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
    SSLEngine engine = _sslContextFactory.newSSLEngine(endPoint.getRemoteAddress());
    engine.setUseClientMode(false);
    SslConnection sslConnection = newSslConnection(connector, endPoint, engine);
    sslConnection.setRenegotiationAllowed(_sslContextFactory.isRenegotiationAllowed());
    configure(sslConnection, connector, endPoint);
    ConnectionFactory next = connector.getConnectionFactory(_nextProtocol);
    EndPoint decryptedEndPoint = sslConnection.getDecryptedEndPoint();
    Connection connection = next.newConnection(connector, decryptedEndPoint);
    decryptedEndPoint.setConnection(connection);
    return sslConnection;
}
Also used : SslConnection(org.eclipse.jetty.io.ssl.SslConnection) SSLEngine(javax.net.ssl.SSLEngine) Connection(org.eclipse.jetty.io.Connection) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 4 with Connection

use of org.eclipse.jetty.io.Connection in project jetty.project by eclipse.

the class AsyncIOServletTest method testAsyncReadIdleTimeout.

@Test
public void testAsyncReadIdleTimeout() throws Exception {
    int status = 567;
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            assertScope();
            AsyncContext asyncContext = request.startAsync(request, response);
            asyncContext.setTimeout(0);
            ServletInputStream inputStream = request.getInputStream();
            inputStream.setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    assertScope();
                    while (inputStream.isReady() && !inputStream.isFinished()) inputStream.read();
                }

                @Override
                public void onAllDataRead() throws IOException {
                    assertScope();
                }

                @Override
                public void onError(Throwable t) {
                    assertScope();
                    response.setStatus(status);
                    // Do not put Connection: close header here, the test
                    // verifies that the server closes no matter what.
                    asyncContext.complete();
                }
            });
        }
    });
    connector.setIdleTimeout(1000);
    CountDownLatch closeLatch = new CountDownLatch(1);
    connector.addBean(new Connection.Listener() {

        @Override
        public void onOpened(Connection connection) {
        }

        @Override
        public void onClosed(Connection connection) {
            closeLatch.countDown();
        }
    });
    String data = "0123456789";
    DeferredContentProvider content = new DeferredContentProvider();
    content.offer(ByteBuffer.wrap(data.getBytes(StandardCharsets.UTF_8)));
    CountDownLatch responseLatch = new CountDownLatch(1);
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(content).onResponseSuccess(r -> responseLatch.countDown()).timeout(5, TimeUnit.SECONDS).send(result -> {
        assertEquals(status, result.getResponse().getStatus());
        clientLatch.countDown();
    });
    assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
    assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
    content.close();
    assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) Connection(org.eclipse.jetty.io.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ReadListener(javax.servlet.ReadListener) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Test(org.junit.Test)

Example 5 with Connection

use of org.eclipse.jetty.io.Connection in project jetty.project by eclipse.

the class ConnectionOpenCloseTest method testOpenRequestClose.

@Slow
@Test
public void testOpenRequestClose() throws Exception {
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
        }
    });
    server.start();
    final AtomicInteger callbacks = new AtomicInteger();
    final CountDownLatch openLatch = new CountDownLatch(1);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    connector.addBean(new Connection.Listener.Adapter() {

        @Override
        public void onOpened(Connection connection) {
            callbacks.incrementAndGet();
            openLatch.countDown();
        }

        @Override
        public void onClosed(Connection connection) {
            callbacks.incrementAndGet();
            closeLatch.countDown();
        }
    });
    try (Socket socket = new Socket("localhost", connector.getLocalPort())) {
        socket.setSoTimeout((int) connector.getIdleTimeout());
        OutputStream output = socket.getOutputStream();
        output.write(("GET / HTTP/1.1\r\n" + "Host: localhost:" + connector.getLocalPort() + "\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream inputStream = socket.getInputStream();
        HttpTester.Response response = HttpTester.parseResponse(inputStream);
        assertThat("Status Code", response.getStatus(), is(200));
        Assert.assertEquals(-1, inputStream.read());
        socket.close();
        Assert.assertTrue(openLatch.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
        // Wait some time to see if the callbacks are called too many times
        TimeUnit.SECONDS.sleep(1);
        Assert.assertEquals(2, callbacks.get());
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) Connection(org.eclipse.jetty.io.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Socket(java.net.Socket) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Aggregations

Connection (org.eclipse.jetty.io.Connection)18 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 IOException (java.io.IOException)7 Socket (java.net.Socket)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ServletException (javax.servlet.ServletException)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 AbstractConnection (org.eclipse.jetty.io.AbstractConnection)4 EndPoint (org.eclipse.jetty.io.EndPoint)3 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)3 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)3 Timer (com.codahale.metrics.Timer)2 InputStream (java.io.InputStream)2 InterruptedIOException (java.io.InterruptedIOException)2 OutputStream (java.io.OutputStream)2 UncheckedIOException (java.io.UncheckedIOException)2 AsyncContext (javax.servlet.AsyncContext)2 ReadListener (javax.servlet.ReadListener)2