Search in sources :

Example 1 with AbstractHandler

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

the class HttpClientTest method testPOSTWithParameters.

@Test
public void testPOSTWithParameters() throws Exception {
    final String paramName = "a";
    final String paramValue = "€";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            String value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/plain");
                response.getOutputStream().print(value);
            }
        }
    });
    ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).param(paramName, paramValue).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 2 with AbstractHandler

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

the class HttpClientTest method testEarlyEOF.

@Test
public void testEarlyEOF() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Promise some content, then flush the headers, then fail to send the content.
            response.setContentLength(16);
            response.flushBuffer();
            throw new NullPointerException("Explicitly thrown by test");
        }
    });
    try (StacklessLogging stackless = new StacklessLogging(org.eclipse.jetty.server.HttpChannel.class)) {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(60, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
    // Expected.
    }
}
Also used : 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) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with AbstractHandler

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

the class HttpClientTest method testPOSTWithParametersWithContent.

@Test
public void testPOSTWithParametersWithContent() throws Exception {
    final byte[] content = { 0, 1, 2, 3 };
    final String paramName = "a";
    final String paramValue = "€";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            String value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/octet-stream");
                IO.copy(request.getInputStream(), response.getOutputStream());
            }
        }
    });
    for (int i = 0; i < 256; ++i) {
        ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort() + "/?b=1").param(paramName, paramValue).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertNotNull(response);
        Assert.assertEquals(200, response.getStatus());
        Assert.assertArrayEquals(content, response.getContent());
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

Example 4 with AbstractHandler

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

the class ServletContextHandlerTest method testFallThrough.

@Test
public void testFallThrough() throws Exception {
    HandlerList list = new HandlerList();
    _server.setHandler(list);
    ServletContextHandler root = new ServletContextHandler(list, "/", ServletContextHandler.SESSIONS);
    ServletHandler servlet = root.getServletHandler();
    servlet.setEnsureDefaultServlet(false);
    servlet.addServletWithMapping(HelloServlet.class, "/hello/*");
    list.addHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.sendError(404, "Fell Through");
        }
    });
    _server.start();
    String response = _connector.getResponses("GET /hello HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("200 OK"));
    response = _connector.getResponses("GET /other HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("404 Fell Through"));
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 5 with AbstractHandler

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

the class HttpClientTest method testDownloadWithInputStreamResponseListener.

@Test
public void testDownloadWithInputStreamResponseListener() throws Exception {
    String content = "hello world";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.getOutputStream().print(content);
        }
    });
    CountDownLatch latch = new CountDownLatch(1);
    InputStreamResponseListener listener = new InputStreamResponseListener();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).onResponseSuccess(response -> latch.countDown()).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    // Response cannot succeed until we read the content.
    Assert.assertFalse(latch.await(500, TimeUnit.MILLISECONDS));
    InputStream input = listener.getInputStream();
    Assert.assertEquals(content, IO.toString(input));
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : IntStream(java.util.stream.IntStream) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) Random(java.util.Random) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletOutputStream(javax.servlet.ServletOutputStream) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Assume(org.junit.Assume) HttpStatus(org.eclipse.jetty.http.HttpStatus) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) EnumSet(java.util.EnumSet) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) IO(org.eclipse.jetty.util.IO) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) HttpMethod(org.eclipse.jetty.http.HttpMethod) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) InputStream(java.io.InputStream) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) 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) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

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