Search in sources :

Example 6 with ContentProvider

use of org.eclipse.jetty.client.api.ContentProvider in project camel by apache.

the class DefaultCompositeApiClient method submitCompositeTree.

@Override
public void submitCompositeTree(final SObjectTree tree, final ResponseCallback<SObjectTreeResponse> callback) throws SalesforceException {
    final String url = versionUrl() + "composite/tree/" + tree.getObjectType();
    final Request post = createRequest(HttpMethod.POST, url);
    final ContentProvider content = serialize(tree, tree.objectTypes());
    post.content(content);
    doHttpRequest(post, (response, exception) -> callback.onResponse(tryToReadResponse(SObjectTreeResponse.class, response), exception));
}
Also used : InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Request(org.eclipse.jetty.client.api.Request)

Example 7 with ContentProvider

use of org.eclipse.jetty.client.api.ContentProvider in project jetty.project by eclipse.

the class HttpClientTest method test_ExchangeIsComplete_WhenRequestFailsMidway_WithResponse.

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

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // Echo back
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).content(new ContentProvider() {

        @Override
        public long getLength() {
            return -1;
        }

        @Override
        public Iterator<ByteBuffer> iterator() {
            return new Iterator<ByteBuffer>() {

                @Override
                public boolean hasNext() {
                    return true;
                }

                @Override
                public ByteBuffer next() {
                    throw new NoSuchElementException("explicitly_thrown_by_test");
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onComplete(Result result) {
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Iterator(java.util.Iterator) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 8 with ContentProvider

use of org.eclipse.jetty.client.api.ContentProvider in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testClientRequestContentKnownLengthGzipped.

private void testClientRequestContentKnownLengthGzipped(int length, final boolean expectChunked) throws Exception {
    byte[] bytes = new byte[length];
    new Random().nextBytes(bytes);
    startServer(new EchoHttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String transferEncoding = request.getHeader(HttpHeader.TRANSFER_ENCODING.asString());
            if (expectChunked)
                Assert.assertNotNull(transferEncoding);
            else
                Assert.assertNull(transferEncoding);
            response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
            super.service(request, response);
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
            return new GZIPContentTransformer(ContentTransformer.IDENTITY);
        }
    });
    startClient();
    byte[] gzipBytes = gzip(bytes);
    ContentProvider gzipContent = new BytesContentProvider(gzipBytes);
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").content(gzipContent).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random)

Example 9 with ContentProvider

use of org.eclipse.jetty.client.api.ContentProvider in project jetty.project by eclipse.

the class ProxyServletFailureTest method testProxyRequestStallsContentServerIdlesTimeout.

@Test
public void testProxyRequestStallsContentServerIdlesTimeout() throws Exception {
    final byte[] content = new byte[] { 'C', '0', 'F', 'F', 'E', 'E' };
    int expected;
    if (proxyServlet instanceof AsyncProxyServlet) {
        // TODO should this be a 502 also???
        expected = 500;
        proxyServlet = new AsyncProxyServlet() {

            @Override
            protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
                DeferredContentProvider provider = new DeferredContentProvider() {

                    @Override
                    public boolean offer(ByteBuffer buffer, Callback callback) {
                        // Send less content to trigger the test condition.
                        buffer.limit(buffer.limit() - 1);
                        return super.offer(buffer.slice(), callback);
                    }
                };
                request.getInputStream().setReadListener(newReadListener(request, response, proxyRequest, provider));
                return provider;
            }
        };
    } else {
        expected = 502;
        proxyServlet = new ProxyServlet() {

            @Override
            protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
                return new BytesContentProvider(content) {

                    @Override
                    public long getLength() {
                        // Increase the content length to trigger the test condition.
                        return content.length + 1;
                    }
                };
            }
        };
    }
    prepareProxy();
    prepareServer(new EchoHttpServlet());
    long idleTimeout = 1000;
    serverConnector.setIdleTimeout(idleTimeout);
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(content)).send();
        Assert.assertEquals(expected, response.getStatus());
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) Callback(org.eclipse.jetty.util.Callback) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Example 10 with ContentProvider

use of org.eclipse.jetty.client.api.ContentProvider 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)

Aggregations

ContentProvider (org.eclipse.jetty.client.api.ContentProvider)11 Request (org.eclipse.jetty.client.api.Request)8 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)5 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)4 InputStreamContentProvider (org.eclipse.jetty.client.util.InputStreamContentProvider)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Response (org.eclipse.jetty.client.api.Response)3 Result (org.eclipse.jetty.client.api.Result)3 CancellationException (java.util.concurrent.CancellationException)2 ServletException (javax.servlet.ServletException)2 ProcessingException (javax.ws.rs.ProcessingException)2 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)2 OutputStreamContentProvider (org.eclipse.jetty.client.util.OutputStreamContentProvider)2 ClientRequest (org.glassfish.jersey.client.ClientRequest)2 ClientResponse (org.glassfish.jersey.client.ClientResponse)2 Test (org.junit.Test)2