Search in sources :

Example 91 with AbstractHandler

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

the class JettyPathTest method testParameterDecoding.

@Test
public void testParameterDecoding() throws Exception {
    Server server = run(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().print(request.getParameter("hi"));
            baseRequest.setHandled(true);
        }
    });
    try {
        for (char c = 0; c < 1024; c++) {
            String queryParam = String.valueOf(c);
            String url = Url.httpBuilder("localhost", "/").port(8080).addQueryParam("hi", queryParam).toString();
            try (InputStream stream = new URL(url).openStream()) {
                byte[] bytes = ByteStreams.toByteArray(stream);
                assertEquals("char c = " + c, queryParam, new String(bytes));
            }
        }
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) URL(java.net.URL) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

Example 92 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project async-http-client by AsyncHttpClient.

the class AsyncStreamLifecycleTest method configureHandler.

@Override
public AbstractHandler configureHandler() throws Exception {
    return new AbstractHandler() {

        public void handle(String s, Request request, HttpServletRequest req, final HttpServletResponse resp) throws IOException, ServletException {
            resp.setContentType("text/plain;charset=utf-8");
            resp.setStatus(200);
            final Continuation continuation = ContinuationSupport.getContinuation(req);
            continuation.suspend();
            final PrintWriter writer = resp.getWriter();
            executorService.submit(new Runnable() {

                public void run() {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        logger.error("Failed to sleep for 100 ms.", e);
                    }
                    logger.info("Delivering part1.");
                    writer.write("part1");
                    writer.flush();
                }
            });
            executorService.submit(new Runnable() {

                public void run() {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        logger.error("Failed to sleep for 200 ms.", e);
                    }
                    logger.info("Delivering part2.");
                    writer.write("part2");
                    writer.flush();
                    continuation.complete();
                }
            });
            request.setHandled(true);
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Continuation(org.eclipse.jetty.continuation.Continuation) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) PrintWriter(java.io.PrintWriter)

Example 93 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project async-http-client by AsyncHttpClient.

the class BasicHttpTest method postUnboundedInputStreamAsBodyStream.

@Test
public void postUnboundedInputStreamAsBodyStream() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders h = new DefaultHttpHeaders();
            h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
            server.enqueue(new AbstractHandler() {

                EchoHandler chain = new EchoHandler();

                @Override
                public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
                    assertEquals(request.getHeader(TRANSFER_ENCODING.toString()), HttpHeaderValues.CHUNKED.toString());
                    assertNull(request.getHeader(CONTENT_LENGTH.toString()));
                    chain.handle(target, request, httpServletRequest, httpServletResponse);
                }
            });
            server.enqueueEcho();
            client.preparePost(getTargetUrl()).setHeaders(h).setBody(new ByteArrayInputStream("{}".getBytes(StandardCharsets.ISO_8859_1))).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    assertEquals(response.getStatusCode(), 200);
                    assertEquals(response.getResponseBody(), "{}");
                    return response;
                }
            }).get(TIMEOUT, SECONDS);
        });
    });
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) EchoHandler(org.asynchttpclient.testserver.HttpServer.EchoHandler) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 94 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project async-http-client by AsyncHttpClient.

the class NonAsciiContentLengthTest method setUpGlobal.

@BeforeClass(alwaysRun = true)
public void setUpGlobal() throws Exception {
    server = new Server();
    ServerConnector connector = addHttpConnector(server);
    server.setHandler(new AbstractHandler() {

        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // Can only handle bodies of up to 1024 bytes.
            int MAX_BODY_SIZE = 1024;
            byte[] b = new byte[MAX_BODY_SIZE];
            int offset = 0;
            int numBytesRead;
            try (ServletInputStream is = request.getInputStream()) {
                while ((numBytesRead = is.read(b, offset, MAX_BODY_SIZE - offset)) != -1) {
                    offset += numBytesRead;
                }
            }
            assertEquals(request.getContentLength(), offset);
            response.setStatus(200);
            response.setCharacterEncoding(request.getCharacterEncoding());
            response.setContentLength(request.getContentLength());
            try (ServletOutputStream os = response.getOutputStream()) {
                os.write(b, 0, offset);
            }
        }
    });
    server.start();
    port1 = connector.getLocalPort();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) Server(org.eclipse.jetty.server.Server) ServletOutputStream(javax.servlet.ServletOutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) BeforeClass(org.testng.annotations.BeforeClass)

Example 95 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project async-http-client by AsyncHttpClient.

the class RedirectTest method setUpGlobal.

@BeforeClass
@Override
public void setUpGlobal() throws Exception {
    server = new Server();
    ServerConnector connector1 = addHttpConnector(server);
    ServerConnector connector2 = addHttpConnector(server);
    HandlerList list = new HandlerList();
    list.addHandler(new AbstractHandler() {

        @Override
        public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
            if (request.getLocalPort() == port2) {
                httpServletResponse.sendRedirect(getTargetUrl());
            }
        }
    });
    list.addHandler(getWebSocketHandler());
    server.setHandler(list);
    server.start();
    port1 = connector1.getLocalPort();
    port2 = connector2.getLocalPort();
    logger.info("Local HTTP server started successfully");
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Server(org.eclipse.jetty.server.Server) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)219 HttpServletRequest (javax.servlet.http.HttpServletRequest)216 HttpServletResponse (javax.servlet.http.HttpServletResponse)216 IOException (java.io.IOException)203 ServletException (javax.servlet.ServletException)203 Test (org.junit.Test)188 Request (org.eclipse.jetty.server.Request)121 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 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