Search in sources :

Example 6 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class ConnectHandler method onConnectSuccess.

protected void onConnectSuccess(ConnectContext connectContext, UpstreamConnection upstreamConnection) {
    ConcurrentMap<String, Object> context = connectContext.getContext();
    HttpServletRequest request = connectContext.getRequest();
    prepareContext(request, context);
    HttpConnection httpConnection = connectContext.getHttpConnection();
    EndPoint downstreamEndPoint = httpConnection.getEndPoint();
    DownstreamConnection downstreamConnection = newDownstreamConnection(downstreamEndPoint, context);
    downstreamConnection.setInputBufferSize(getBufferSize());
    upstreamConnection.setConnection(downstreamConnection);
    downstreamConnection.setConnection(upstreamConnection);
    if (LOG.isDebugEnabled())
        LOG.debug("Connection setup completed: {}<->{}", downstreamConnection, upstreamConnection);
    HttpServletResponse response = connectContext.getResponse();
    sendConnectResponse(request, response, HttpServletResponse.SC_OK);
    upgradeConnection(request, response, downstreamConnection);
    connectContext.getAsyncContext().complete();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpConnection(org.eclipse.jetty.server.HttpConnection) HttpServletResponse(javax.servlet.http.HttpServletResponse) EndPoint(org.eclipse.jetty.io.EndPoint) SelectChannelEndPoint(org.eclipse.jetty.io.SelectChannelEndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint)

Example 7 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testAfterContentTransformerDoNoTransform.

private void testAfterContentTransformerDoNoTransform(final boolean readSource, final boolean useDisk) throws Exception {
    final String key0 = "id";
    long value0 = 1;
    final String key1 = "channel";
    String value1 = "foo";
    final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
        }
    });
    startProxy(new AsyncMiddleManServlet() {

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

                {
                    if (useDisk)
                        setMaxInputBufferSize(0);
                }

                @Override
                public boolean transform(Source source, Sink sink) throws IOException {
                    if (readSource) {
                        InputStream input = source.getInputStream();
                        JSON.parse(new InputStreamReader(input, "UTF-8"));
                    }
                    // No transformation.
                    return false;
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    @SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(response.getContentAsString());
    Assert.assertNotNull(obj);
    Assert.assertEquals(2, obj.size());
    Assert.assertEquals(value0, obj.get(key0));
    Assert.assertEquals(value1, obj.get(key1));
}
Also used : InputStreamReader(java.io.InputStreamReader) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) GZIPInputStream(java.util.zip.GZIPInputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) 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) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testProxyResponseWriteFails.

private void testProxyResponseWriteFails(final int writeCount) throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletOutputStream output = response.getOutputStream();
            output.write(new byte[512]);
            output.flush();
            output.write(new byte[512]);
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        private int count;

        @Override
        protected void writeProxyResponseContent(ServletOutputStream output, ByteBuffer content) throws IOException {
            if (++count < writeCount)
                super.writeProxyResponseContent(output, content);
            else
                throw new IOException("explicitly_thrown_by_test");
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(502, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) 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)

Example 9 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testAfterContentTransformerInputStreamReset.

private void testAfterContentTransformerInputStreamReset(final boolean overflow) throws Exception {
    final byte[] data = new byte[] { 'c', 'o', 'f', 'f', 'e', 'e' };
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // Write the content in two chunks.
            int chunk = data.length / 2;
            ServletOutputStream output = response.getOutputStream();
            output.write(data, 0, chunk);
            sleep(1000);
            output.write(data, chunk, data.length - chunk);
        }
    });
    startProxy(new AsyncMiddleManServlet() {

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

                {
                    setMaxInputBufferSize(overflow ? data.length / 2 : data.length * 2);
                }

                @Override
                public boolean transform(Source source, Sink sink) throws IOException {
                    // Consume the stream once.
                    InputStream input = source.getInputStream();
                    IO.copy(input, IO.getNullStream());
                    // Reset the stream and re-read it.
                    input.reset();
                    IO.copy(input, sink.getOutputStream());
                    return true;
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) GZIPInputStream(java.util.zip.GZIPInputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) 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)

Example 10 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testAfterContentTransformer.

@Test
public void testAfterContentTransformer() throws Exception {
    final String key0 = "id";
    long value0 = 1;
    final String key1 = "channel";
    String value1 = "foo";
    final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
        }
    });
    final String key2 = "c";
    startProxy(new AsyncMiddleManServlet() {

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

                @Override
                public boolean transform(Source source, Sink sink) throws IOException {
                    InputStream input = source.getInputStream();
                    @SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(new InputStreamReader(input, "UTF-8"));
                    // Transform the object.
                    obj.put(key2, obj.remove(key1));
                    try (OutputStream output = sink.getOutputStream()) {
                        output.write(JSON.toString(obj).getBytes(StandardCharsets.UTF_8));
                        return true;
                    }
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    @SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(response.getContentAsString());
    Assert.assertNotNull(obj);
    Assert.assertEquals(2, obj.size());
    Assert.assertEquals(value0, obj.get(key0));
    Assert.assertEquals(value1, obj.get(key2));
}
Also used : InputStreamReader(java.io.InputStreamReader) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) GZIPInputStream(java.util.zip.GZIPInputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) 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) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)3285 HttpServletRequest (javax.servlet.http.HttpServletRequest)2624 Test (org.junit.Test)1229 IOException (java.io.IOException)873 ServletException (javax.servlet.ServletException)671 PrintWriter (java.io.PrintWriter)286 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)250 FilterChain (javax.servlet.FilterChain)243 Request (org.eclipse.jetty.server.Request)223 HashMap (java.util.HashMap)204 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)195 HttpServlet (javax.servlet.http.HttpServlet)188 CountDownLatch (java.util.concurrent.CountDownLatch)183 ServletOutputStream (javax.servlet.ServletOutputStream)183 StringWriter (java.io.StringWriter)180 Test (org.testng.annotations.Test)176 HttpSession (javax.servlet.http.HttpSession)175 ServletContext (javax.servlet.ServletContext)173 InputStream (java.io.InputStream)141 Map (java.util.Map)137