Search in sources :

Example 1 with InputStreamContentProvider

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

the class HttpClientStreamTest method testUploadWithConcurrentServerCloseClosesStream.

@Test
public void testUploadWithConcurrentServerCloseClosesStream() throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);
    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);
            AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            serverLatch.countDown();
        }
    });
    final AtomicBoolean commit = new AtomicBoolean();
    final CountDownLatch closeLatch = new CountDownLatch(1);
    InputStream stream = new InputStream() {

        @Override
        public int read() throws IOException {
            if (commit.get()) {
                try {
                    Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
                    connector.stop();
                    return 0;
                } catch (Throwable x) {
                    throw new IOException(x);
                }
            } else {
                return connector.isStopped() ? -1 : 0;
            }
        }

        @Override
        public void close() throws IOException {
            super.close();
            closeLatch.countDown();
        }
    };
    InputStreamContentProvider provider = new InputStreamContentProvider(stream, 1);
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(provider).onRequestCommit(request -> commit.set(true)).send(result -> {
        Assert.assertTrue(result.isFailed());
        completeLatch.countDown();
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) 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) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 2 with InputStreamContentProvider

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

the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccess.

@Slow
@Test
public void testTimeoutIsCancelledOnSuccess() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    final byte[] content = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).content(new InputStreamContentProvider(new ByteArrayInputStream(content))).timeout(2 * timeout, TimeUnit.MILLISECONDS);
    request.send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            Assert.assertArrayEquals(content, getContent());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
    TimeUnit.MILLISECONDS.sleep(2 * timeout);
    Assert.assertNull(request.getAbortCause());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 3 with InputStreamContentProvider

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

the class Usage method testRequestInputStream.

@Test
public void testRequestInputStream() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    InputStream input = new ByteArrayInputStream("content".getBytes(StandardCharsets.UTF_8));
    ContentResponse response = client.newRequest("localhost", 8080).content(new InputStreamContentProvider(input)).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpClient(org.eclipse.jetty.client.HttpClient) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) Test(org.junit.Test)

Example 4 with InputStreamContentProvider

use of org.eclipse.jetty.client.util.InputStreamContentProvider in project camel by apache.

the class DefaultBulkApiClient method createBatch.

@Override
public void createBatch(InputStream batchStream, String jobId, ContentType contentTypeEnum, final BatchInfoResponseCallback callback) {
    final Request post = getRequest(HttpMethod.POST, batchUrl(jobId, null));
    post.content(new InputStreamContentProvider(batchStream));
    post.header(HttpHeader.CONTENT_TYPE, getContentType(contentTypeEnum) + ";charset=" + StringUtil.__UTF8);
    // make the call and parse the result
    doHttpRequest(post, new ClientResponseCallback() {

        @Override
        public void onResponse(InputStream response, SalesforceException ex) {
            BatchInfo value = null;
            try {
                value = unmarshalResponse(response, post, BatchInfo.class);
            } catch (SalesforceException e) {
                ex = e;
            }
            callback.onResponse(value, ex);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) BatchInfo(org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)

Example 5 with InputStreamContentProvider

use of org.eclipse.jetty.client.util.InputStreamContentProvider in project camel by apache.

the class DefaultRestClient method updateSObject.

@Override
public void updateSObject(String sObjectName, String id, InputStream sObject, ResponseCallback callback) {
    final Request patch = getRequest("PATCH", sobjectsUrl(sObjectName + "/" + id));
    // requires authorization token
    setAccessToken(patch);
    // input stream as entity content
    patch.content(new InputStreamContentProvider(sObject));
    patch.header(HttpHeader.CONTENT_TYPE, PayloadFormat.JSON.equals(format) ? APPLICATION_JSON_UTF8 : APPLICATION_XML_UTF8);
    doHttpRequest(patch, new DelegatingClientCallback(callback));
}
Also used : Request(org.eclipse.jetty.client.api.Request) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider)

Aggregations

InputStreamContentProvider (org.eclipse.jetty.client.util.InputStreamContentProvider)15 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Test (org.junit.Test)8 Request (org.eclipse.jetty.client.api.Request)7 InputStream (java.io.InputStream)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)4 IOException (java.io.IOException)3 InterruptedIOException (java.io.InterruptedIOException)3 ServletException (javax.servlet.ServletException)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)3 Result (org.eclipse.jetty.client.api.Result)3 ByteBuffer (java.nio.ByteBuffer)2 AsyncContext (javax.servlet.AsyncContext)2 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)2 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1