Search in sources :

Example 11 with InputStreamContentProvider

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

the class HttpClientStreamTest method testInputStreamContentProviderThrowingWhileReading.

@Test(expected = ExecutionException.class)
public void testInputStreamContentProviderThrowingWhileReading() throws Exception {
    start(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        public void doNonErrorHandle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] data = new byte[] { 0, 1, 2, 3 };
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(new InputStreamContentProvider(new InputStream() {

        private int index = 0;

        @Override
        public int read() throws IOException {
            // Will eventually throw ArrayIndexOutOfBounds
            return data[index++];
        }
    }, data.length / 2)).timeout(5, TimeUnit.SECONDS).send();
}
Also used : Request(org.eclipse.jetty.server.Request) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

Example 12 with InputStreamContentProvider

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

the class HttpClientStreamTest method testUploadWithConnectFailureClosesStream.

@Test
public void testUploadWithConnectFailureClosesStream() throws Exception {
    start(new EmptyServerHandler());
    final CountDownLatch closeLatch = new CountDownLatch(1);
    InputStream stream = new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)) {

        @Override
        public void close() throws IOException {
            super.close();
            closeLatch.countDown();
        }
    };
    InputStreamContentProvider content = new InputStreamContentProvider(stream);
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("0.0.0.1", connector.getLocalPort()).scheme(getScheme()).content(content).send(result -> {
        Assert.assertTrue(result.isFailed());
        completeLatch.countDown();
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 13 with InputStreamContentProvider

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

the class AbstractClientBase method doHttpRequest.

protected void doHttpRequest(final Request request, final ClientResponseCallback callback) {
    // Highly memory inefficient,
    // but buffer the request content to allow it to be replayed for authentication retries
    final ContentProvider content = request.getContent();
    if (content instanceof InputStreamContentProvider) {
        final List<ByteBuffer> buffers = new ArrayList<>();
        for (ByteBuffer buffer : content) {
            buffers.add(buffer);
        }
        request.content(new ByteBufferContentProvider(buffers.toArray(new ByteBuffer[buffers.size()])));
        buffers.clear();
    }
    // execute the request
    request.send(new BufferingResponseListener(httpClient.getMaxContentLength()) {

        @Override
        public void onComplete(Result result) {
            Response response = result.getResponse();
            if (result.isFailed()) {
                // Failure!!!
                // including Salesforce errors reported as exception from SalesforceSecurityHandler
                Throwable failure = result.getFailure();
                if (failure instanceof SalesforceException) {
                    callback.onResponse(null, (SalesforceException) failure);
                } else {
                    final String msg = String.format("Unexpected error {%s:%s} executing {%s:%s}", response.getStatus(), response.getReason(), request.getMethod(), request.getURI());
                    callback.onResponse(null, new SalesforceException(msg, response.getStatus(), failure));
                }
            } else {
                // HTTP error status
                final int status = response.getStatus();
                SalesforceHttpRequest request = (SalesforceHttpRequest) ((SalesforceHttpRequest) result.getRequest()).getConversation().getAttribute(SalesforceSecurityHandler.AUTHENTICATION_REQUEST_ATTRIBUTE);
                if (status == HttpStatus.BAD_REQUEST_400 && request != null) {
                    // parse login error
                    ContentResponse contentResponse = new HttpContentResponse(response, getContent(), getMediaType(), getEncoding());
                    try {
                        session.parseLoginResponse(contentResponse, getContentAsString());
                        final String msg = String.format("Unexpected Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
                        callback.onResponse(null, new SalesforceException(msg, null));
                    } catch (SalesforceException e) {
                        final String msg = String.format("Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
                        callback.onResponse(null, new SalesforceException(msg, response.getStatus(), e));
                    }
                } else if (status < HttpStatus.OK_200 || status >= HttpStatus.MULTIPLE_CHOICES_300) {
                    // Salesforce HTTP failure!
                    request = (SalesforceHttpRequest) result.getRequest();
                    final String msg = String.format("Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
                    final SalesforceException cause = createRestException(response, getContentAsInputStream());
                    // for APIs that return body on status 400, such as Composite API we need content as well
                    callback.onResponse(getContentAsInputStream(), new SalesforceException(msg, response.getStatus(), cause));
                } else {
                    // Success!!!
                    callback.onResponse(getContentAsInputStream(), null);
                }
            }
        }

        @Override
        public InputStream getContentAsInputStream() {
            if (getContent().length == 0) {
                return null;
            }
            return super.getContentAsInputStream();
        }
    });
}
Also used : HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener)

Example 14 with InputStreamContentProvider

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

the class DefaultRestClient method upsertSObject.

@Override
public void upsertSObject(String sObjectName, String fieldName, String fieldValue, InputStream sObject, ResponseCallback callback) {
    final Request patch = getRequest("PATCH", sobjectsExternalIdUrl(sObjectName, fieldName, fieldValue));
    // requires authorization token
    setAccessToken(patch);
    // input stream as entity content
    patch.content(new InputStreamContentProvider(sObject));
    // TODO will the encoding always be UTF-8??
    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)

Example 15 with InputStreamContentProvider

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

the class DefaultRestClient method approval.

@Override
public void approval(final InputStream request, final ResponseCallback callback) {
    final Request post = getRequest(HttpMethod.POST, versionUrl() + "process/approvals/");
    // authorization
    setAccessToken(post);
    // input stream as entity content
    post.content(new InputStreamContentProvider(request));
    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)

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