Search in sources :

Example 96 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class ProxyProtocolTest method test_PROXY_GET_v2.

@Test
public void test_PROXY_GET_v2() throws Exception {
    startServer(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                Assert.assertEquals("10.0.0.4", request.getRemoteAddr());
                Assert.assertEquals(33824, request.getRemotePort());
                Assert.assertEquals("10.0.0.4", request.getLocalAddr());
                Assert.assertEquals(8888, request.getLocalPort());
            } catch (Throwable th) {
                th.printStackTrace();
                response.setStatus(500);
            }
            baseRequest.setHandled(true);
        }
    });
    String request1 = "0D0A0D0A000D0A515549540A211100140A0000040A000004842022B82000050000000000";
    SocketChannel channel = SocketChannel.open();
    channel.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
    channel.write(ByteBuffer.wrap(TypeUtil.fromHexString(request1)));
    FuturePromise<Session> promise = new FuturePromise<>();
    client.accept(null, channel, new Session.Listener.Adapter(), promise);
    Session session = promise.get(5, TimeUnit.SECONDS);
    HttpFields fields = new HttpFields();
    String uri = "http://localhost:" + connector.getLocalPort() + "/";
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_2, fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            MetaData.Response response = (MetaData.Response) frame.getMetaData();
            Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 97 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class AbstractHandlerMBean method getObjectContextBasis.

/* ------------------------------------------------------------ */
@Override
public String getObjectContextBasis() {
    if (_managed != null) {
        String basis = null;
        if (_managed instanceof ContextHandler) {
            ContextHandler handler = (ContextHandler) _managed;
            String context = getContextName(handler);
            if (context == null)
                context = handler.getDisplayName();
            if (context != null)
                return context;
        } else if (_managed instanceof AbstractHandler) {
            AbstractHandler handler = (AbstractHandler) _managed;
            Server server = handler.getServer();
            if (server != null) {
                ContextHandler context = AbstractHandlerContainer.findContainerOf(server, ContextHandler.class, handler);
                if (context != null)
                    basis = getContextName(context);
            }
        }
        if (basis != null)
            return basis;
    }
    return super.getObjectContextBasis();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler)

Example 98 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class ConnectionOpenCloseTest method testOpenClose.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            throw new IllegalStateException();
        }
    });
    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());
        Assert.assertTrue(openLatch.await(5, TimeUnit.SECONDS));
        socket.shutdownOutput();
        Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
        String response = IO.toString(socket.getInputStream());
        Assert.assertEquals(0, response.length());
        // Wait some time to see if the callbacks are called too many times
        TimeUnit.MILLISECONDS.sleep(200);
        Assert.assertEquals(2, callbacks.get());
    }
}
Also used : 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) 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)

Example 99 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class ConnectionOpenCloseTest method testSSLOpenRequestClose.

@Slow
@Test
public void testSSLOpenRequestClose() throws Exception {
    SslContextFactory sslContextFactory = new SslContextFactory();
    File keystore = MavenTestingUtils.getTestResourceFile("keystore");
    sslContextFactory.setKeyStoreResource(Resource.newResource(keystore));
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    server.addBean(sslContextFactory);
    server.removeConnector(connector);
    connector = new ServerConnector(server, sslContextFactory);
    server.addConnector(connector);
    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(2);
    final CountDownLatch closeLatch = new CountDownLatch(2);
    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();
        }
    });
    Socket socket = sslContextFactory.getSslContext().getSocketFactory().createSocket("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);
    Assert.assertEquals(200, response.getStatus());
    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(4, 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) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) Socket(java.net.Socket) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 100 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientTest method testRequestWithResponseContentChunked.

private void testRequestWithResponseContentChunked(int length) throws Exception {
    final byte[] chunk1 = new byte[length];
    final byte[] chunk2 = new byte[length];
    Random random = new Random();
    random.nextBytes(chunk1);
    random.nextBytes(chunk2);
    byte[] bytes = new byte[chunk1.length + chunk2.length];
    System.arraycopy(chunk1, 0, bytes, 0, chunk1.length);
    System.arraycopy(chunk2, 0, bytes, chunk1.length, chunk2.length);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            output.flush();
            output.write(chunk2);
        }
    });
    org.eclipse.jetty.client.api.Request request = client.newRequest(newURI());
    FutureResponseListener listener = new FutureResponseListener(request, 2 * length);
    request.timeout(10, TimeUnit.SECONDS).send(listener);
    ContentResponse response = listener.get();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Aggregations

AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)214 HttpServletRequest (javax.servlet.http.HttpServletRequest)211 HttpServletResponse (javax.servlet.http.HttpServletResponse)211 IOException (java.io.IOException)203 ServletException (javax.servlet.ServletException)203 Test (org.junit.Test)183 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 Request (org.eclipse.jetty.server.Request)116 CountDownLatch (java.util.concurrent.CountDownLatch)80 InterruptedIOException (java.io.InterruptedIOException)40 Result (org.eclipse.jetty.client.api.Result)38 InputStream (java.io.InputStream)35 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)32 Response (org.eclipse.jetty.client.api.Response)32 Request (org.eclipse.jetty.client.api.Request)29 OutputStream (java.io.OutputStream)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24