Search in sources :

Example 6 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testDownstreamTransformationKnownContentLengthDroppingLastChunk.

@Test
public void testDownstreamTransformationKnownContentLengthDroppingLastChunk() throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            byte[] chunk = new byte[1024];
            int contentLength = 2 * chunk.length;
            response.setContentLength(contentLength);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk);
            output.flush();
            sleep(1000);
            output.write(chunk);
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new ContentTransformer() {

                @Override
                public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
                    if (!finished)
                        output.add(input);
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 7 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.

the class NetworkTrafficListenerTest method testTrafficWithResponseContentChunkedOnPersistentConnection.

@Test
public void testTrafficWithResponseContentChunkedOnPersistentConnection() throws Exception {
    final String responseContent = "response_content";
    final String responseChunk1 = "response_content".substring(0, responseContent.length() / 2);
    final String responseChunk2 = "response_content".substring(responseContent.length() / 2, responseContent.length());
    initConnector(new AbstractHandler() {

        @Override
        public void handle(String uri, Request request, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException, ServletException {
            request.setHandled(true);
            ServletOutputStream output = servletResponse.getOutputStream();
            output.write(responseChunk1.getBytes(StandardCharsets.UTF_8));
            output.flush();
            output.write(responseChunk2.getBytes(StandardCharsets.UTF_8));
            output.flush();
        }
    });
    final AtomicReference<String> incomingData = new AtomicReference<>();
    final CountDownLatch incomingLatch = new CountDownLatch(1);
    final AtomicReference<String> outgoingData = new AtomicReference<>("");
    final CountDownLatch outgoingLatch = new CountDownLatch(1);
    connector.addNetworkTrafficListener(new NetworkTrafficListener.Adapter() {

        @Override
        public void incoming(Socket socket, ByteBuffer bytes) {
            incomingData.set(BufferUtil.toString(bytes, StandardCharsets.UTF_8));
            incomingLatch.countDown();
        }

        @Override
        public void outgoing(Socket socket, ByteBuffer bytes) {
            outgoingData.set(outgoingData.get() + BufferUtil.toString(bytes, StandardCharsets.UTF_8));
            if (outgoingData.get().endsWith("\r\n0\r\n\r\n"))
                outgoingLatch.countDown();
        }
    });
    int port = connector.getLocalPort();
    String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost:" + port + "\r\n" + "\r\n";
    String expectedResponse = "" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + responseChunk1.length() + "\r\n" + responseChunk1 + "\r\n" + responseChunk2.length() + "\r\n" + responseChunk2 + "\r\n" + "0\r\n" + "\r\n";
    Socket socket = new Socket("localhost", port);
    OutputStream output = socket.getOutputStream();
    output.write(request.getBytes(StandardCharsets.UTF_8));
    output.flush();
    assertTrue(incomingLatch.await(1, TimeUnit.SECONDS));
    assertEquals(request, incomingData.get());
    assertTrue(outgoingLatch.await(1, TimeUnit.SECONDS));
    assertEquals(expectedResponse, outgoingData.get());
    byte[] responseBytes = readResponse(socket);
    String response = new String(responseBytes, StandardCharsets.UTF_8);
    assertEquals(expectedResponse, response);
    socket.close();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) NetworkTrafficListener(org.eclipse.jetty.io.NetworkTrafficListener) Socket(java.net.Socket) Test(org.junit.Test)

Example 8 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.

the class ResponseTest method testFlushAfterFullContent.

@Test
public void testFlushAfterFullContent() throws Exception {
    Response response = getResponse();
    byte[] data = new byte[] { (byte) 0xCA, (byte) 0xFE };
    ServletOutputStream output = response.getOutputStream();
    response.setContentLength(data.length);
    // Write the whole content
    output.write(data);
    // Must not throw
    output.flush();
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletOutputStream(javax.servlet.ServletOutputStream) Test(org.junit.Test)

Example 9 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.

the class HttpClientGZIPTest method testGZIPContentSentTwiceInOneWrite.

@Test
public void testGZIPContentSentTwiceInOneWrite() throws Exception {
    final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader("Content-Encoding", "gzip");
            ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
            GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
            gzipOutput.write(data);
            gzipOutput.finish();
            byte[] gzipBytes = gzipData.toByteArray();
            byte[] content = Arrays.copyOf(gzipBytes, 2 * gzipBytes.length);
            System.arraycopy(gzipBytes, 0, content, gzipBytes.length, gzipBytes.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(content);
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    byte[] expected = Arrays.copyOf(data, 2 * data.length);
    System.arraycopy(data, 0, expected, data.length, data.length);
    Assert.assertArrayEquals(expected, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPOutputStream(java.util.zip.GZIPOutputStream) Test(org.junit.Test)

Example 10 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.

the class HttpClientGZIPTest method testGZIPContentFragmented.

private void testGZIPContentFragmented(final int fragment) throws Exception {
    final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader("Content-Encoding", "gzip");
            ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
            GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
            gzipOutput.write(data);
            gzipOutput.finish();
            byte[] gzipBytes = gzipData.toByteArray();
            byte[] chunk1 = Arrays.copyOfRange(gzipBytes, 0, gzipBytes.length - fragment);
            byte[] chunk2 = Arrays.copyOfRange(gzipBytes, gzipBytes.length - fragment, gzipBytes.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            output.flush();
            sleep(500);
            output.write(chunk2);
            output.flush();
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Aggregations

ServletOutputStream (javax.servlet.ServletOutputStream)515 IOException (java.io.IOException)211 HttpServletResponse (javax.servlet.http.HttpServletResponse)148 Test (org.junit.Test)112 HttpServletRequest (javax.servlet.http.HttpServletRequest)108 ServletException (javax.servlet.ServletException)90 InputStream (java.io.InputStream)63 File (java.io.File)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 FileInputStream (java.io.FileInputStream)40 PrintWriter (java.io.PrintWriter)27 CountDownLatch (java.util.concurrent.CountDownLatch)27 WriteListener (javax.servlet.WriteListener)27 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)27 HttpServlet (javax.servlet.http.HttpServlet)25 AsyncContext (javax.servlet.AsyncContext)23 ServletInputStream (javax.servlet.ServletInputStream)23 ArrayList (java.util.ArrayList)21 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19