Search in sources :

Example 26 with BufferingResponseListener

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

the class PushedResourcesTest method testPushedResourceRedirect.

@Test
public void testPushedResourceRedirect() throws Exception {
    Random random = new Random();
    byte[] pushBytes = new byte[512];
    random.nextBytes(pushBytes);
    String oldPath = "/old";
    String newPath = "/new";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (target.equals(oldPath))
                response.sendRedirect(newPath);
            else if (target.equals(newPath))
                response.getOutputStream().write(pushBytes);
            else
                baseRequest.getPushBuilder().path(oldPath).push();
        }
    });
    CountDownLatch latch = new CountDownLatch(1);
    HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
    ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isSucceeded());
            Assert.assertEquals(oldPath, pushedRequest.getPath());
            Assert.assertEquals(newPath, result.getRequest().getPath());
            Assert.assertArrayEquals(pushBytes, getContent());
            latch.countDown();
        }
    }).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) 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) Random(java.util.Random) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 27 with BufferingResponseListener

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

the class PushedResourcesTest method testPushedResources.

@Test
public void testPushedResources() throws Exception {
    Random random = new Random();
    byte[] bytes = new byte[512];
    random.nextBytes(bytes);
    byte[] pushBytes1 = new byte[1024];
    random.nextBytes(pushBytes1);
    byte[] pushBytes2 = new byte[2048];
    random.nextBytes(pushBytes2);
    String path1 = "/secondary1";
    String path2 = "/secondary2";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (target.equals(path1)) {
                response.getOutputStream().write(pushBytes1);
            } else if (target.equals(path2)) {
                response.getOutputStream().write(pushBytes2);
            } else {
                baseRequest.getPushBuilder().path(path1).push();
                baseRequest.getPushBuilder().path(path2).push();
                response.getOutputStream().write(bytes);
            }
        }
    });
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(1);
    HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
    ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isSucceeded());
            if (pushedRequest.getPath().equals(path1)) {
                Assert.assertArrayEquals(pushBytes1, getContent());
                latch1.countDown();
            } else if (pushedRequest.getPath().equals(path2)) {
                Assert.assertArrayEquals(pushBytes2, getContent());
                latch2.countDown();
            }
        }
    }).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
    Assert.assertTrue(latch1.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) 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) Random(java.util.Random) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 28 with BufferingResponseListener

use of org.eclipse.jetty.client.util.BufferingResponseListener 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

Result (org.eclipse.jetty.client.api.Result)28 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)28 CountDownLatch (java.util.concurrent.CountDownLatch)26 HttpServletRequest (javax.servlet.http.HttpServletRequest)25 IOException (java.io.IOException)24 ServletException (javax.servlet.ServletException)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)24 Test (org.junit.Test)24 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)17 Request (org.eclipse.jetty.server.Request)16 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)12 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)12 InterruptedIOException (java.io.InterruptedIOException)11 Response (org.eclipse.jetty.client.api.Response)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Random (java.util.Random)7 ServletInputStream (javax.servlet.ServletInputStream)7 ServletOutputStream (javax.servlet.ServletOutputStream)7 HttpServlet (javax.servlet.http.HttpServlet)7 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)7