Search in sources :

Example 6 with InputStreamContentProvider

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

the class DefaultRestClient method apexCall.

@Override
public void apexCall(String httpMethod, String apexUrl, Map<String, Object> queryParams, InputStream requestDto, ResponseCallback callback) {
    // create APEX call request
    final Request request;
    try {
        request = getRequest(httpMethod, apexCallUrl(apexUrl, queryParams));
        // set request SObject and content type
        if (requestDto != null) {
            request.content(new InputStreamContentProvider(requestDto));
            request.header(HttpHeader.CONTENT_TYPE, PayloadFormat.JSON.equals(format) ? APPLICATION_JSON_UTF8 : APPLICATION_XML_UTF8);
        }
        // requires authorization token
        setAccessToken(request);
        doHttpRequest(request, new DelegatingClientCallback(callback));
    } catch (UnsupportedEncodingException e) {
        String msg = "Unexpected error: " + e.getMessage();
        callback.onResponse(null, new SalesforceException(msg, e));
    } catch (URISyntaxException e) {
        String msg = "Unexpected error: " + e.getMessage();
        callback.onResponse(null, new SalesforceException(msg, e));
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) Request(org.eclipse.jetty.client.api.Request) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) URISyntaxException(java.net.URISyntaxException)

Example 7 with InputStreamContentProvider

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

the class DefaultRestClient method createSObject.

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

Example 8 with InputStreamContentProvider

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

the class AsyncIOServletTest method testWriteListenerFromOtherThread.

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

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            request.getInputStream().setReadListener(new Listener(asyncContext));
        }
    });
    int cores = 4;
    int iterations = 10;
    CountDownLatch latch = new CountDownLatch(cores * iterations);
    Deque<Throwable> failures = new LinkedBlockingDeque<>();
    for (int i = 0; i < cores; ++i) {
        client.getExecutor().execute(() -> {
            for (int j = 0; j < iterations; ++j) {
                try {
                    ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(new InputStreamContentProvider(new ByteArrayInputStream(new byte[16 * 1024]) {

                        @Override
                        public int read(byte[] b, int off, int len) {
                            sleep(5);
                            return super.read(b, off, Math.min(len, 4242));
                        }
                    })).send();
                    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
                    latch.countDown();
                } catch (Throwable x) {
                    failures.offer(x);
                }
            }
        });
    }
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
    Assert.assertTrue(failures.isEmpty());
}
Also used : BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) WriteListener(javax.servlet.WriteListener) ReadListener(javax.servlet.ReadListener) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) 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) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 9 with InputStreamContentProvider

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

the class AsyncRequestContentTest method testInputStream.

@Test
public void testInputStream() throws Exception {
    start(new ConsumeInputHandler());
    InputStreamContentProvider contentProvider = new InputStreamContentProvider(new ByteArrayInputStream(new byte[1]));
    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 : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with InputStreamContentProvider

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

the class AsyncRequestContentTest method testEmptyInputStream.

@Test
public void testEmptyInputStream() throws Exception {
    start(new ConsumeInputHandler());
    InputStreamContentProvider contentProvider = new InputStreamContentProvider(new ByteArrayInputStream(new byte[0]));
    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 : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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