Search in sources :

Example 1 with Content

use of org.eclipse.jetty.server.HttpInput.Content in project jetty.project by eclipse.

the class GzipHttpInputInterceptor method readFrom.

@Override
public Content readFrom(Content content) {
    _decoder.decodeChunks(content.getByteBuffer());
    final ByteBuffer chunk = _chunk;
    if (chunk == null)
        return null;
    return new Content(chunk) {

        @Override
        public void succeeded() {
            _decoder.release(chunk);
        }
    };
}
Also used : Content(org.eclipse.jetty.server.HttpInput.Content) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Content

use of org.eclipse.jetty.server.HttpInput.Content in project jetty.project by eclipse.

the class AsyncIOServletTest method testAsyncIntercepted.

@Test
public void testAsyncIntercepted() throws Exception {
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.err.println("Service " + request);
            final HttpInput httpInput = ((Request) request).getHttpInput();
            httpInput.addInterceptor(new HttpInput.Interceptor() {

                int state = 0;

                Content saved;

                @Override
                public Content readFrom(Content content) {
                    // System.err.printf("readFrom s=%d saved=%b %s%n",state,saved!=null,content);
                    switch(state) {
                        case 0:
                            // null transform
                            if (content.isEmpty())
                                state++;
                            return null;
                        case 1:
                            {
                                // copy transform
                                if (content.isEmpty()) {
                                    state++;
                                    return content;
                                }
                                ByteBuffer copy = wrap(toArray(content.getByteBuffer()));
                                content.skip(copy.remaining());
                                return new Content(copy);
                            }
                        case 2:
                            // byte by byte
                            if (content.isEmpty()) {
                                state++;
                                return content;
                            }
                            byte[] b = new byte[1];
                            int l = content.get(b, 0, 1);
                            return new Content(wrap(b, 0, l));
                        case 3:
                            {
                                // double vision
                                if (content.isEmpty()) {
                                    if (saved == null) {
                                        state++;
                                        return content;
                                    }
                                    Content copy = saved;
                                    saved = null;
                                    return copy;
                                }
                                byte[] data = toArray(content.getByteBuffer());
                                content.skip(data.length);
                                saved = new Content(wrap(data));
                                return new Content(wrap(data));
                            }
                        default:
                            return null;
                    }
                }
            });
            AsyncContext asyncContext = request.startAsync();
            ServletInputStream input = request.getInputStream();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            input.setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    while (input.isReady()) {
                        int b = input.read();
                        if (b > 0) {
                            // System.err.printf("0x%2x %s %n", b, Character.isISOControl(b)?"?":(""+(char)b));
                            out.write(b);
                        } else if (b < 0)
                            return;
                    }
                }

                @Override
                public void onAllDataRead() throws IOException {
                    response.getOutputStream().write(out.toByteArray());
                    asyncContext.complete();
                }

                @Override
                public void onError(Throwable x) {
                }
            });
        }
    });
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    CountDownLatch clientLatch = new CountDownLatch(1);
    String expected = "S0" + "S1" + "S2" + "S3S3" + "S4" + "S5" + "S6";
    client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(contentProvider).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded()) {
                Response response = result.getResponse();
                assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
                assertThat(getContentAsString(), Matchers.equalTo(expected));
                clientLatch.countDown();
            }
        }
    });
    contentProvider.offer(BufferUtil.toBuffer("S0"));
    contentProvider.flush();
    contentProvider.offer(BufferUtil.toBuffer("S1"));
    contentProvider.flush();
    contentProvider.offer(BufferUtil.toBuffer("S2"));
    contentProvider.flush();
    contentProvider.offer(BufferUtil.toBuffer("S3"));
    contentProvider.flush();
    contentProvider.offer(BufferUtil.toBuffer("S4"));
    contentProvider.flush();
    contentProvider.offer(BufferUtil.toBuffer("S5"));
    contentProvider.flush();
    contentProvider.offer(BufferUtil.toBuffer("S6"));
    contentProvider.close();
    Assert.assertTrue(clientLatch.await(10, TimeUnit.SECONDS));
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) ReadListener(javax.servlet.ReadListener) Result(org.eclipse.jetty.client.api.Result) 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) HttpInput(org.eclipse.jetty.server.HttpInput) ServletInputStream(javax.servlet.ServletInputStream) Content(org.eclipse.jetty.server.HttpInput.Content) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 3 with Content

use of org.eclipse.jetty.server.HttpInput.Content in project jetty.project by eclipse.

the class HttpInputAsyncStateTest method deliver.

private void deliver(Content... content) {
    if (content != null) {
        for (Content c : content) {
            if (c == EOF_CONTENT) {
                _in.eof();
                _eof = true;
            } else if (c == HttpInput.EARLY_EOF_CONTENT) {
                _in.earlyEOF();
                _eof = true;
            } else {
                _in.addContent(c);
                BufferUtil.append(_expected, c.getByteBuffer().slice());
            }
        }
    }
}
Also used : Content(org.eclipse.jetty.server.HttpInput.Content)

Example 4 with Content

use of org.eclipse.jetty.server.HttpInput.Content in project jetty.project by eclipse.

the class AsyncIOServletTest method testAsyncWriteLessThanContentLengthFlushed.

@Test
public void testAsyncWriteLessThanContentLengthFlushed() throws Exception {
    CountDownLatch complete = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentLength(10);
            AsyncContext async = request.startAsync();
            ServletOutputStream out = response.getOutputStream();
            AtomicInteger state = new AtomicInteger(0);
            out.setWriteListener(new WriteListener() {

                @Override
                public void onWritePossible() throws IOException {
                    while (true) {
                        if (!out.isReady())
                            return;
                        switch(state.get()) {
                            case 0:
                                state.incrementAndGet();
                                WriteListener listener = this;
                                new Thread(() -> {
                                    try {
                                        Thread.sleep(50);
                                        listener.onWritePossible();
                                    } catch (Exception e) {
                                    }
                                }).start();
                                return;
                            case 1:
                                state.incrementAndGet();
                                out.flush();
                                break;
                            case 2:
                                state.incrementAndGet();
                                out.write("12345".getBytes());
                                break;
                            case 3:
                                async.complete();
                                complete.countDown();
                                return;
                        }
                    }
                }

                @Override
                public void onError(Throwable t) {
                }
            });
        }
    });
    AtomicBoolean failed = new AtomicBoolean(false);
    CountDownLatch clientLatch = new CountDownLatch(3);
    client.newRequest(newURI()).path(servletPath).onResponseHeaders(response -> {
        if (response.getStatus() == HttpStatus.OK_200)
            clientLatch.countDown();
    }).onResponseContent(new Response.ContentListener() {

        @Override
        public void onContent(Response response, ByteBuffer content) {
        // System.err.println("Content: "+BufferUtil.toDetailString(content));
        }
    }).onResponseFailure(new Response.FailureListener() {

        @Override
        public void onFailure(Response response, Throwable failure) {
            clientLatch.countDown();
        }
    }).send(result -> {
        failed.set(result.isFailed());
        clientLatch.countDown();
        clientLatch.countDown();
        clientLatch.countDown();
    });
    assertTrue(complete.await(10, TimeUnit.SECONDS));
    assertTrue(clientLatch.await(10, TimeUnit.SECONDS));
    assertTrue(failed.get());
}
Also used : Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) Context(org.eclipse.jetty.server.handler.ContextHandler.Context) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) BufferUtil.toArray(org.eclipse.jetty.util.BufferUtil.toArray) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) HttpInput(org.eclipse.jetty.server.HttpInput) Response(org.eclipse.jetty.client.api.Response) HttpServlet(javax.servlet.http.HttpServlet) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) Session(org.eclipse.jetty.http2.api.Session) FuturePromise(org.eclipse.jetty.util.FuturePromise) Content(org.eclipse.jetty.server.HttpInput.Content) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Matchers.is(org.hamcrest.Matchers.is) Queue(java.util.Queue) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) BufferUtil(org.eclipse.jetty.util.BufferUtil) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Result(org.eclipse.jetty.client.api.Result) Handler(org.eclipse.jetty.server.Handler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpChannel(org.eclipse.jetty.server.HttpChannel) ServletInputStream(javax.servlet.ServletInputStream) ByteBuffer.wrap(java.nio.ByteBuffer.wrap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) Deque(java.util.Deque) InterruptedIOException(java.io.InterruptedIOException) AsyncContext(javax.servlet.AsyncContext) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) WriteListener(javax.servlet.WriteListener) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) HttpConnectionOverHTTP2(org.eclipse.jetty.http2.client.http.HttpConnectionOverHTTP2) Assume(org.junit.Assume) Executor(java.util.concurrent.Executor) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) TimeUnit(java.util.concurrent.TimeUnit) HttpMethod(org.eclipse.jetty.http.HttpMethod) Connection(org.eclipse.jetty.io.Connection) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ReadListener(javax.servlet.ReadListener) DispatcherType(javax.servlet.DispatcherType) Destination(org.eclipse.jetty.client.api.Destination) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) ServletException(javax.servlet.ServletException) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteListener(javax.servlet.WriteListener) Test(org.junit.Test)

Aggregations

Content (org.eclipse.jetty.server.HttpInput.Content)4 ByteBuffer (java.nio.ByteBuffer)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 UncheckedIOException (java.io.UncheckedIOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AsyncContext (javax.servlet.AsyncContext)2 ReadListener (javax.servlet.ReadListener)2 ServletException (javax.servlet.ServletException)2 ServletInputStream (javax.servlet.ServletInputStream)2 HttpServlet (javax.servlet.http.HttpServlet)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)2 Response (org.eclipse.jetty.client.api.Response)2 Result (org.eclipse.jetty.client.api.Result)2 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)2 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1