Search in sources :

Example 36 with BytesContentProvider

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

the class ProxyServletTest method testProxyWithBigRequestContentConsumed.

@Test
public void testProxyWithBigRequestContentConsumed() throws Exception {
    final byte[] content = new byte[128 * 1024];
    new Random().nextBytes(content);
    startServer(new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (req.getHeader("Via") != null)
                resp.addHeader(PROXIED_HEADER, "true");
            InputStream input = req.getInputStream();
            int index = 0;
            byte[] buffer = new byte[16 * 1024];
            while (true) {
                int value = input.read(buffer);
                if (value < 0)
                    break;
                for (int i = 0; i < value; i++) {
                    Assert.assertEquals("Content mismatch at index=" + index, content[index] & 0xFF, buffer[i] & 0xFF);
                    ++index;
                }
            }
        }
    });
    startProxy();
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).method(HttpMethod.POST).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) Test(org.junit.Test)

Example 37 with BytesContentProvider

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

the class ProxyServletTest method testExpect100ContinueRespond100Continue.

@Test
public void testExpect100ContinueRespond100Continue() throws Exception {
    CountDownLatch serverLatch1 = new CountDownLatch(1);
    CountDownLatch serverLatch2 = new CountDownLatch(1);
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            serverLatch1.countDown();
            try {
                serverLatch2.await(5, TimeUnit.SECONDS);
            } catch (Throwable x) {
                throw new InterruptedIOException();
            }
            // Send the 100 Continue.
            ServletInputStream input = request.getInputStream();
            // Echo the content.
            IO.copy(input, response.getOutputStream());
        }
    });
    startProxy();
    startClient();
    byte[] content = new byte[1024];
    CountDownLatch contentLatch = new CountDownLatch(1);
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).onRequestContent((request, buffer) -> contentLatch.countDown()).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded()) {
                if (result.getResponse().getStatus() == HttpStatus.OK_200) {
                    if (Arrays.equals(content, getContent()))
                        clientLatch.countDown();
                }
            }
        }
    });
    // Wait until we arrive on the server.
    Assert.assertTrue(serverLatch1.await(5, TimeUnit.SECONDS));
    // The client should not send the content yet.
    Assert.assertFalse(contentLatch.await(1, TimeUnit.SECONDS));
    // Make the server send the 100 Continue.
    serverLatch2.countDown();
    // The client has sent the content.
    Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Arrays(java.util.Arrays) FilterChain(javax.servlet.FilterChain) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) AsyncEvent(javax.servlet.AsyncEvent) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) HttpClient(org.eclipse.jetty.client.HttpClient) HttpCookie(java.net.HttpCookie) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AsyncListener(javax.servlet.AsyncListener) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) After(org.junit.After) Filter(javax.servlet.Filter) HttpStatus(org.eclipse.jetty.http.HttpStatus) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Server(org.eclipse.jetty.server.Server) Callback(org.eclipse.jetty.util.Callback) EnumSet(java.util.EnumSet) Parameterized(org.junit.runners.Parameterized) PrintWriter(java.io.PrintWriter) HttpServlet(javax.servlet.http.HttpServlet) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) StandardOpenOption(java.nio.file.StandardOpenOption) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) TestTracker(org.eclipse.jetty.toolchain.test.TestTracker) IO(org.eclipse.jetty.util.IO) EOFException(java.io.EOFException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ServletResponse(javax.servlet.ServletResponse) GZIPOutputStream(java.util.zip.GZIPOutputStream) Result(org.eclipse.jetty.client.api.Result) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MavenTestingUtils(org.eclipse.jetty.toolchain.test.MavenTestingUtils) ServletInputStream(javax.servlet.ServletInputStream) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) InterruptedIOException(java.io.InterruptedIOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) LinkedHashMap(java.util.LinkedHashMap) AsyncContext(javax.servlet.AsyncContext) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletOutputStream(javax.servlet.ServletOutputStream) ConnectException(java.net.ConnectException) Cookie(javax.servlet.http.Cookie) OutputStream(java.io.OutputStream) ServletRequest(javax.servlet.ServletRequest) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) 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) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpMethod(org.eclipse.jetty.http.HttpMethod) ServerConnector(org.eclipse.jetty.server.ServerConnector) Rule(org.junit.Rule) FilterConfig(javax.servlet.FilterConfig) DispatcherType(javax.servlet.DispatcherType) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) InterruptedIOException(java.io.InterruptedIOException) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 38 with BytesContentProvider

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

the class ProxyServletTest method testExpect100ContinueRespond417ExpectationFailed.

@Test
public void testExpect100ContinueRespond417ExpectationFailed() throws Exception {
    CountDownLatch serverLatch1 = new CountDownLatch(1);
    CountDownLatch serverLatch2 = new CountDownLatch(1);
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            serverLatch1.countDown();
            try {
                serverLatch2.await(5, TimeUnit.SECONDS);
            } catch (Throwable x) {
                throw new InterruptedIOException();
            }
            // Send the 417 Expectation Failed.
            response.setStatus(HttpStatus.EXPECTATION_FAILED_417);
        }
    });
    startProxy();
    startClient();
    byte[] content = new byte[1024];
    CountDownLatch contentLatch = new CountDownLatch(1);
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).onRequestContent((request, buffer) -> contentLatch.countDown()).send(result -> {
        if (result.isFailed()) {
            if (result.getResponse().getStatus() == HttpStatus.EXPECTATION_FAILED_417)
                clientLatch.countDown();
        }
    });
    // Wait until we arrive on the server.
    Assert.assertTrue(serverLatch1.await(5, TimeUnit.SECONDS));
    // The client should not send the content yet.
    Assert.assertFalse(contentLatch.await(1, TimeUnit.SECONDS));
    // Make the server send the 417 Expectation Failed.
    serverLatch2.countDown();
    // The client should not send the content.
    Assert.assertFalse(contentLatch.await(1, TimeUnit.SECONDS));
    Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Arrays(java.util.Arrays) FilterChain(javax.servlet.FilterChain) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) AsyncEvent(javax.servlet.AsyncEvent) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) HttpClient(org.eclipse.jetty.client.HttpClient) HttpCookie(java.net.HttpCookie) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AsyncListener(javax.servlet.AsyncListener) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) After(org.junit.After) Filter(javax.servlet.Filter) HttpStatus(org.eclipse.jetty.http.HttpStatus) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Server(org.eclipse.jetty.server.Server) Callback(org.eclipse.jetty.util.Callback) EnumSet(java.util.EnumSet) Parameterized(org.junit.runners.Parameterized) PrintWriter(java.io.PrintWriter) HttpServlet(javax.servlet.http.HttpServlet) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) StandardOpenOption(java.nio.file.StandardOpenOption) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) TestTracker(org.eclipse.jetty.toolchain.test.TestTracker) IO(org.eclipse.jetty.util.IO) EOFException(java.io.EOFException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ServletResponse(javax.servlet.ServletResponse) GZIPOutputStream(java.util.zip.GZIPOutputStream) Result(org.eclipse.jetty.client.api.Result) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MavenTestingUtils(org.eclipse.jetty.toolchain.test.MavenTestingUtils) ServletInputStream(javax.servlet.ServletInputStream) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) InterruptedIOException(java.io.InterruptedIOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) LinkedHashMap(java.util.LinkedHashMap) AsyncContext(javax.servlet.AsyncContext) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletOutputStream(javax.servlet.ServletOutputStream) ConnectException(java.net.ConnectException) Cookie(javax.servlet.http.Cookie) OutputStream(java.io.OutputStream) ServletRequest(javax.servlet.ServletRequest) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) 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) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpMethod(org.eclipse.jetty.http.HttpMethod) ServerConnector(org.eclipse.jetty.server.ServerConnector) Rule(org.junit.Rule) FilterConfig(javax.servlet.FilterConfig) DispatcherType(javax.servlet.DispatcherType) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) InterruptedIOException(java.io.InterruptedIOException) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) Test(org.junit.Test)

Example 39 with BytesContentProvider

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

use of org.eclipse.jetty.client.util.BytesContentProvider in project jersey by jersey.

the class JettyConnector method getBytesProvider.

private ContentProvider getBytesProvider(final ClientRequest clientRequest) {
    final Object entity = clientRequest.getEntity();
    if (entity == null) {
        return null;
    }
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {

        @Override
        public OutputStream getOutputStream(final int contentLength) throws IOException {
            return outputStream;
        }
    });
    try {
        clientRequest.writeEntity();
    } catch (final IOException e) {
        throw new ProcessingException("Failed to write request entity.", e);
    }
    return new BytesContentProvider(outputStream.toByteArray());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) OutboundMessageContext(org.glassfish.jersey.message.internal.OutboundMessageContext) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)40 HttpServletRequest (javax.servlet.http.HttpServletRequest)32 IOException (java.io.IOException)31 Test (org.junit.Test)30 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)29 HttpServletResponse (javax.servlet.http.HttpServletResponse)28 ServletException (javax.servlet.ServletException)27 Request (org.eclipse.jetty.client.api.Request)17 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 Request (org.eclipse.jetty.server.Request)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Response (org.eclipse.jetty.client.api.Response)11 Result (org.eclipse.jetty.client.api.Result)11 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)10 InputStream (java.io.InputStream)9 ByteBuffer (java.nio.ByteBuffer)9 HttpServlet (javax.servlet.http.HttpServlet)9 InterruptedIOException (java.io.InterruptedIOException)8 Random (java.util.Random)8