Search in sources :

Example 46 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class AsyncContextListenersTest method testListenerAddedFromListener.

@SuppressWarnings("Duplicates")
@Test
public void testListenerAddedFromListener() throws Exception {
    final AtomicReference<CountDownLatch> completes = new AtomicReference<>(new CountDownLatch(1));
    String path = "/path";
    prepare(path, new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync(request, response);
            asyncContext.addListener(new AsyncListener() {

                @Override
                public void onStartAsync(AsyncEvent event) throws IOException {
                    // This method should not be invoked because we add the
                    // listener *after* having called startAsync(), but we
                    // add a listener to be sure it's not called (it will
                    // screw up the completes count and test will fail).
                    event.getAsyncContext().addListener(this);
                }

                @Override
                public void onComplete(AsyncEvent event) throws IOException {
                    completes.get().countDown();
                }

                @Override
                public void onTimeout(AsyncEvent event) throws IOException {
                }

                @Override
                public void onError(AsyncEvent event) throws IOException {
                }
            });
            asyncContext.complete();
        }
    });
    try (Socket socket = new Socket("localhost", _connector.getLocalPort())) {
        OutputStream output = socket.getOutputStream();
        String request = "" + "GET " + path + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        HttpTester.Input input = HttpTester.from(socket.getInputStream());
        HttpTester.Response response = HttpTester.parseResponse(input);
        Assert.assertEquals(200, response.getStatus());
        completes.get().await(10, TimeUnit.SECONDS);
        // Send a second request
        completes.set(new CountDownLatch(1));
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = HttpTester.parseResponse(input);
        Assert.assertEquals(200, response.getStatus());
        completes.get().await(10, TimeUnit.SECONDS);
    }
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncEvent(javax.servlet.AsyncEvent) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AsyncListener(javax.servlet.AsyncListener) Socket(java.net.Socket) Test(org.junit.Test)

Example 47 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class AsyncListenerTest method test_StartAsync_Throw_OnError.

private void test_StartAsync_Throw_OnError(IOConsumer<AsyncEvent> consumer) throws Exception {
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/ctx");
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            asyncContext.addListener(new AsyncListenerAdapter() {

                @Override
                public void onError(AsyncEvent event) throws IOException {
                    consumer.accept(event);
                }
            });
            throw new QuietServletException(new TestRuntimeException());
        }
    }), "/path/*");
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setStatus(HttpStatus.OK_200);
        }
    }), "/dispatch/*");
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getOutputStream().print("CUSTOM");
        }
    }), "/error/*");
    startServer(context);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) QuietServletException(org.eclipse.jetty.server.QuietServletException) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Example 48 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class AsyncListenerTest method test_StartAsync_OnTimeout.

private void test_StartAsync_OnTimeout(long timeout, IOConsumer<AsyncEvent> consumer) throws Exception {
    ServletContextHandler context = new ServletContextHandler();
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(timeout);
            asyncContext.addListener(new AsyncListenerAdapter() {

                @Override
                public void onTimeout(AsyncEvent event) throws IOException {
                    consumer.accept(event);
                }
            });
        }
    }), "/*");
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setStatus(HttpStatus.OK_200);
        }
    }), "/dispatch/*");
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getOutputStream().print("CUSTOM");
        }
    }), "/error/*");
    startServer(context);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Example 49 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class HTTP2Test method testCustomResponseCode.

@Test
public void testCustomResponseCode() throws Exception {
    final int status = 475;
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setStatus(status);
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    final 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(status, response.getStatus());
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 50 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class HTTP2Test method testHostHeader.

@Test
public void testHostHeader() throws Exception {
    final String host = "fooBar";
    final int port = 1313;
    final String authority = host + ":" + port;
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Assert.assertEquals(host, request.getServerName());
            Assert.assertEquals(port, request.getServerPort());
            Assert.assertEquals(authority, request.getHeader("Host"));
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HostPortHttpField hostHeader = new HostPortHttpField(authority);
    MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, hostHeader, servletPath, HttpVersion.HTTP_2, new HttpFields());
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    final 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(200, response.getStatus());
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpServlet(javax.servlet.http.HttpServlet) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

HttpServlet (javax.servlet.http.HttpServlet)173 HttpServletRequest (javax.servlet.http.HttpServletRequest)152 HttpServletResponse (javax.servlet.http.HttpServletResponse)152 IOException (java.io.IOException)130 ServletException (javax.servlet.ServletException)128 Test (org.junit.Test)117 CountDownLatch (java.util.concurrent.CountDownLatch)68 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)61 InterruptedIOException (java.io.InterruptedIOException)55 ServletOutputStream (javax.servlet.ServletOutputStream)36 AsyncContext (javax.servlet.AsyncContext)34 HttpFields (org.eclipse.jetty.http.HttpFields)32 MetaData (org.eclipse.jetty.http.MetaData)32 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)32 ServletInputStream (javax.servlet.ServletInputStream)31 Session (org.eclipse.jetty.http2.api.Session)30 Stream (org.eclipse.jetty.http2.api.Stream)27 Response (org.eclipse.jetty.client.api.Response)26 HttpContentResponse (org.eclipse.jetty.client.HttpContentResponse)24 HashMap (java.util.HashMap)22