Search in sources :

Example 31 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class AsyncRequestContentTest method testEmptyDeferredContent.

@Test
public void testEmptyDeferredContent() throws Exception {
    start(new ConsumeInputHandler());
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    CountDownLatch latch = new CountDownLatch(1);
    client.POST(newURI()).content(contentProvider).send(result -> {
        if (result.isSucceeded() && result.getResponse().getStatus() == HttpStatus.OK_200)
            latch.countDown();
    });
    contentProvider.close();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 32 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class HttpClientStreamTest method testUploadWithDeferredContentProviderFailsMultipleOffers.

@Test
public void testUploadWithDeferredContentProviderFailsMultipleOffers() throws Exception {
    start(new EmptyServerHandler());
    final CountDownLatch failLatch = new CountDownLatch(2);
    final Callback callback = new Callback() {

        @Override
        public void failed(Throwable x) {
            failLatch.countDown();
        }
    };
    final CountDownLatch completeLatch = new CountDownLatch(1);
    final DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).onRequestBegin(request -> {
        content.offer(ByteBuffer.wrap(new byte[256]), callback);
        content.offer(ByteBuffer.wrap(new byte[256]), callback);
        request.abort(new Exception("explicitly_thrown_by_test"));
    }).send(result -> {
        if (result.isFailed())
            completeLatch.countDown();
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(failLatch.await(5, TimeUnit.SECONDS));
    // Make sure that adding more content results in the callback to be failed.
    final CountDownLatch latch = new CountDownLatch(1);
    content.offer(ByteBuffer.wrap(new byte[128]), new Callback() {

        @Override
        public void failed(Throwable x) {
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) BufferUtil(org.eclipse.jetty.util.BufferUtil) Result(org.eclipse.jetty.client.api.Result) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) MavenTestingUtils(org.eclipse.jetty.toolchain.test.MavenTestingUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Paths(java.nio.file.Paths) DispatcherType(javax.servlet.DispatcherType) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) InputStream(java.io.InputStream) Callback(org.eclipse.jetty.util.Callback) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ServletException(javax.servlet.ServletException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 33 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class HttpClientStreamTest method testUploadWithDeferredContentProviderFromInputStream.

@Slow
@Test
public void testUploadWithDeferredContentProviderFromInputStream() 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);
            IO.copy(request.getInputStream(), new ByteArrayOutputStream());
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    try (DeferredContentProvider content = new DeferredContentProvider()) {
        client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(result -> {
            if (result.isSucceeded() && result.getResponse().getStatus() == 200)
                latch.countDown();
        });
        // Make sure we provide the content *after* the request has been "sent".
        Thread.sleep(1000);
        try (ByteArrayInputStream input = new ByteArrayInputStream(new byte[1024])) {
            byte[] buffer = new byte[200];
            int read;
            while ((read = input.read(buffer)) >= 0) content.offer(ByteBuffer.wrap(buffer, 0, read));
        }
    }
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ByteArrayInputStream(java.io.ByteArrayInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 34 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class HttpClientContinueTest method test_Expect100Continue_WithConcurrentDeferredContent_Respond100Continue.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and echo the content
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    final DeferredContentProvider content = new DeferredContentProvider();
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).onRequestHeaders(request -> {
        content.offer(ByteBuffer.wrap(data));
        content.close();
    }).content(content).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertArrayEquals(data, getContent());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.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) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 35 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class HttpClientContinueTest method test_Expect100Continue_WithDeferredContent_Respond100Continue.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and echo the content
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] chunk1 = new byte[] { 0, 1, 2, 3 };
    final byte[] chunk2 = new byte[] { 4, 5, 6, 7 };
    final byte[] data = new byte[chunk1.length + chunk2.length];
    System.arraycopy(chunk1, 0, data, 0, chunk1.length);
    System.arraycopy(chunk2, 0, data, chunk1.length, chunk2.length);
    final CountDownLatch latch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(content).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertArrayEquals(data, getContent());
            latch.countDown();
        }
    });
    Thread.sleep(1000);
    content.offer(ByteBuffer.wrap(chunk1));
    Thread.sleep(1000);
    content.offer(ByteBuffer.wrap(chunk2));
    content.close();
    Assert.assertTrue(latch.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) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Aggregations

DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)46 Test (org.junit.Test)41 CountDownLatch (java.util.concurrent.CountDownLatch)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 HttpServletResponse (javax.servlet.http.HttpServletResponse)34 IOException (java.io.IOException)33 ServletException (javax.servlet.ServletException)28 ServletInputStream (javax.servlet.ServletInputStream)18 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)18 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)17 Request (org.eclipse.jetty.server.Request)16 Result (org.eclipse.jetty.client.api.Result)15 InterruptedIOException (java.io.InterruptedIOException)14 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)12 AsyncContext (javax.servlet.AsyncContext)11 Request (org.eclipse.jetty.client.api.Request)11 ByteBuffer (java.nio.ByteBuffer)10 Response (org.eclipse.jetty.client.api.Response)9 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 ReadListener (javax.servlet.ReadListener)7