Search in sources :

Example 41 with Request

use of org.eclipse.jetty.server.Request in project jetty.project by eclipse.

the class UnixSocketServer method main.

public static void main(String... args) throws Exception {
    Server server = new Server();
    HttpConnectionFactory http = new HttpConnectionFactory();
    ProxyConnectionFactory proxy = new ProxyConnectionFactory(http.getProtocol());
    UnixSocketConnector connector = new UnixSocketConnector(server, proxy, http);
    server.addConnector(connector);
    server.setHandler(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        protected void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setStatus(200);
            response.getWriter().write("Hello World\r\n");
            response.getWriter().write("remote=" + request.getRemoteAddr() + ":" + request.getRemotePort() + "\r\n");
            response.getWriter().write("local =" + request.getLocalAddr() + ":" + request.getLocalPort() + "\r\n");
        }
    });
    server.start();
    server.join();
}
Also used : Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) 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) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ProxyConnectionFactory(org.eclipse.jetty.server.ProxyConnectionFactory)

Example 42 with Request

use of org.eclipse.jetty.server.Request 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)

Example 43 with Request

use of org.eclipse.jetty.server.Request in project jetty.project by eclipse.

the class HttpClientTest method testResponseWithContentCompleteListenerInvokedOnce.

@Test
public void testResponseWithContentCompleteListenerInvokedOnce() throws Exception {
    start(new EmptyServerHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            super.handle(target, baseRequest, request, response);
            response.getWriter().write("Jetty");
        }
    });
    AtomicInteger completes = new AtomicInteger();
    client.newRequest(newURI()).send(result -> completes.incrementAndGet());
    sleep(1000);
    Assert.assertEquals(1, completes.get());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 44 with Request

use of org.eclipse.jetty.server.Request in project jetty.project by eclipse.

the class ServerTimeoutsTest method testDelayedDispatchRequestWithDelayedFirstContentIdleTimeoutFires.

@Test
public void testDelayedDispatchRequestWithDelayedFirstContentIdleTimeoutFires() throws Exception {
    httpConfig.setDelayDispatchUntilContent(true);
    CountDownLatch handlerLatch = new CountDownLatch(1);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            handlerLatch.countDown();
        }
    });
    long idleTimeout = 2500;
    setServerIdleTimeout(idleTimeout);
    CountDownLatch resultLatch = new CountDownLatch(1);
    client.POST(newURI()).content(new DeferredContentProvider()).send(result -> {
        if (result.isFailed())
            resultLatch.countDown();
    });
    // We did not send the content, the request was not
    // dispatched, the server should have idle timed out.
    Assert.assertFalse(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
    Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 45 with Request

use of org.eclipse.jetty.server.Request in project jetty.project by eclipse.

the class ServerTimeoutsTest method testBlockingTimeoutWithSlowRead.

@Test
public void testBlockingTimeoutWithSlowRead() throws Exception {
    long idleTimeout = 2500;
    long blockingTimeout = 2 * idleTimeout;
    httpConfig.setBlockingTimeout(blockingTimeout);
    CountDownLatch handlerLatch = new CountDownLatch(1);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                ServletInputStream input = request.getInputStream();
                while (true) {
                    int read = input.read();
                    if (read < 0)
                        break;
                }
            } catch (IOException x) {
                handlerLatch.countDown();
                throw x;
            }
        }
    });
    setServerIdleTimeout(idleTimeout);
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        DeferredContentProvider contentProvider = new DeferredContentProvider();
        CountDownLatch resultLatch = new CountDownLatch(1);
        client.newRequest(newURI()).content(contentProvider).send(result -> {
            if (result.getResponse().getStatus() == HttpStatus.INTERNAL_SERVER_ERROR_500)
                resultLatch.countDown();
        });
        // The writes should be slow but not trigger the idle timeout.
        long period = idleTimeout / 2;
        long writes = 2 * (blockingTimeout / period);
        for (long i = 0; i < writes; ++i) {
            contentProvider.offer(ByteBuffer.allocate(1));
            Thread.sleep(period);
        }
        contentProvider.close();
        // Blocking read should timeout.
        Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
        Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
    }
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) 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) ServletInputStream(javax.servlet.ServletInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Aggregations

Request (org.eclipse.jetty.server.Request)248 HttpServletRequest (javax.servlet.http.HttpServletRequest)223 HttpServletResponse (javax.servlet.http.HttpServletResponse)200 Test (org.junit.Test)182 IOException (java.io.IOException)151 ServletException (javax.servlet.ServletException)137 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)129 CountDownLatch (java.util.concurrent.CountDownLatch)65 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)64 InterruptedIOException (java.io.InterruptedIOException)45 InputStream (java.io.InputStream)36 AtomicReference (java.util.concurrent.atomic.AtomicReference)34 Server (org.eclipse.jetty.server.Server)29 Response (org.eclipse.jetty.client.api.Response)27 Result (org.eclipse.jetty.client.api.Result)27 ByteArrayInputStream (java.io.ByteArrayInputStream)26 ServletInputStream (javax.servlet.ServletInputStream)23 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)23 ServletOutputStream (javax.servlet.ServletOutputStream)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21