Search in sources :

Example 16 with HttpServletResponse

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

the class ProxyServletLoadTest method test.

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

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (req.getHeader("Via") != null)
                resp.addHeader(PROXIED_HEADER, "true");
            IO.copy(req.getInputStream(), resp.getOutputStream());
        }
    });
    startProxy();
    startClient();
    // Number of clients to simulate
    int clientCount = Runtime.getRuntime().availableProcessors();
    // Latch for number of clients still active (used to terminate test)
    final CountDownLatch activeClientLatch = new CountDownLatch(clientCount);
    // Atomic Boolean to track that its OK to still continue looping.
    // When this goes false, that means one of the client threads has
    // encountered an error condition, and should allow all remaining
    // client threads to finish cleanly.
    final AtomicBoolean success = new AtomicBoolean(true);
    int iterations = 1000;
    // Start clients
    for (int i = 0; i < clientCount; i++) {
        ClientLoop r = new ClientLoop(activeClientLatch, success, client, "localhost", serverConnector.getLocalPort(), iterations);
        String name = "client-" + i;
        Thread thread = new Thread(r, name);
        thread.start();
    }
    Assert.assertTrue(activeClientLatch.await(Math.max(clientCount * iterations * 10, 5000), TimeUnit.MILLISECONDS));
    Assert.assertTrue(success.get());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 17 with HttpServletResponse

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

the class ProxyServletTest method testExpect100ContinueRespond100ContinueDelayedRequestContent.

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

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // Send the 100 Continue.
            ServletInputStream input = request.getInputStream();
            // Echo the content.
            IO.copy(input, response.getOutputStream());
        }
    });
    startProxy();
    startClient();
    byte[] content = new byte[1024];
    new Random().nextBytes(content);
    int chunk1 = content.length / 2;
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    contentProvider.offer(ByteBuffer.wrap(content, 0, chunk1));
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(contentProvider).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 a while and then offer more content.
    Thread.sleep(1000);
    contentProvider.offer(ByteBuffer.wrap(content, chunk1, content.length - chunk1));
    contentProvider.close();
    Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) Random(java.util.Random) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 18 with HttpServletResponse

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

the class ProxyServletTest method testClientExcludedHosts.

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

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (req.getHeader("Via") != null)
                resp.addHeader(PROXIED_HEADER, "true");
        }
    });
    startProxy();
    startClient();
    int port = serverConnector.getLocalPort();
    client.getProxyConfiguration().getProxies().get(0).getExcludedAddresses().add("127.0.0.1:" + port);
    // Try with a proxied host
    ContentResponse response = client.newRequest("localhost", port).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
    // Try again with an excluded host
    response = client.newRequest("127.0.0.1", port).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertFalse(response.getHeaders().containsKey(PROXIED_HEADER));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 19 with HttpServletResponse

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

the class ProxyServletTest method testCachingProxy.

@Test
public void testCachingProxy() throws Exception {
    final byte[] content = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
    startServer(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (req.getHeader("Via") != null)
                resp.addHeader(PROXIED_HEADER, "true");
            resp.getOutputStream().write(content);
        }
    });
    // Don't do this at home: this example is not concurrent, not complete,
    // it is only used for this test and to verify that ProxyServlet can be
    // subclassed enough to write your own caching servlet
    final String cacheHeader = "X-Cached";
    proxyServlet = new ProxyServlet() {

        private Map<String, ContentResponse> cache = new HashMap<>();

        private Map<String, ByteArrayOutputStream> temp = new HashMap<>();

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ContentResponse cachedResponse = cache.get(request.getRequestURI());
            if (cachedResponse != null) {
                response.setStatus(cachedResponse.getStatus());
                // Should copy headers too, but keep it simple
                response.addHeader(cacheHeader, "true");
                response.getOutputStream().write(cachedResponse.getContent());
            } else {
                super.service(request, response);
            }
        }

        @Override
        protected void onResponseContent(HttpServletRequest request, HttpServletResponse response, Response proxyResponse, byte[] buffer, int offset, int length, Callback callback) {
            // Accumulate the response content
            ByteArrayOutputStream baos = temp.get(request.getRequestURI());
            if (baos == null) {
                baos = new ByteArrayOutputStream();
                temp.put(request.getRequestURI(), baos);
            }
            baos.write(buffer, offset, length);
            super.onResponseContent(request, response, proxyResponse, buffer, offset, length, callback);
        }

        @Override
        protected void onProxyResponseSuccess(HttpServletRequest request, HttpServletResponse response, Response proxyResponse) {
            byte[] content = temp.remove(request.getRequestURI()).toByteArray();
            ContentResponse cached = new HttpContentResponse(proxyResponse, content, null, null);
            cache.put(request.getRequestURI(), cached);
            super.onProxyResponseSuccess(request, response, proxyResponse);
        }
    };
    startProxy();
    startClient();
    // First request
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
    Assert.assertArrayEquals(content, response.getContent());
    // Second request should be cached
    response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getHeaders().containsKey(cacheHeader));
    Assert.assertArrayEquals(content, response.getContent());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) Test(org.junit.Test)

Example 20 with HttpServletResponse

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

the class ProxyServletTest method testProxyWithoutContent.

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

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (req.getHeader("Via") != null)
                resp.addHeader(PROXIED_HEADER, "true");
        }
    });
    startProxy();
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals("OK", response.getReason());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) 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