Search in sources :

Example 66 with ServletException

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

the class HttpClientTest method test_HEAD_With_ResponseContentLength.

@Test
public void test_HEAD_With_ResponseContentLength() throws Exception {
    final int length = 1024;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.getOutputStream().write(new byte[length]);
        }
    });
    // HEAD requests receive a Content-Length header, but do not
    // receive the content so they must handle this case properly
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.HEAD).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(0, response.getContent().length);
    // Perform a normal GET request to be sure the content is now read
    response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(length, response.getContent().length);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 67 with ServletException

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

the class HttpClientAuthenticationTest method test_Redirect_ThenBasicAuthentication.

@Test
public void test_Redirect_ThenBasicAuthentication() throws Exception {
    startBasic(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (request.getRequestURI().endsWith("/redirect"))
                response.sendRedirect(URIUtil.newURI(scheme, request.getServerName(), request.getServerPort(), "/secure", null));
        }
    });
    URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
    client.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, realm, "basic", "basic"));
    final CountDownLatch requests = new CountDownLatch(3);
    Request.Listener.Adapter requestListener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            requests.countDown();
        }
    };
    client.getRequestListeners().add(requestListener);
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/redirect").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(requests.await(5, TimeUnit.SECONDS));
    client.getRequestListeners().remove(requestListener);
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) Test(org.junit.Test)

Example 68 with ServletException

use of javax.servlet.ServletException 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 69 with ServletException

use of javax.servlet.ServletException 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)

Example 70 with ServletException

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

the class HttpClientAsyncContentTest method testSmallAsyncContent.

@Test
public void testSmallAsyncContent() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            ServletOutputStream output = response.getOutputStream();
            output.write(65);
            output.flush();
            output.write(66);
        }
    });
    final AtomicInteger contentCount = new AtomicInteger();
    final AtomicReference<Callback> callbackRef = new AtomicReference<>();
    final AtomicReference<CountDownLatch> contentLatch = new AtomicReference<>(new CountDownLatch(1));
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseContentAsync(new Response.AsyncContentListener() {

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            contentCount.incrementAndGet();
            callbackRef.set(callback);
            contentLatch.get().countDown();
        }
    }).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            completeLatch.countDown();
        }
    });
    Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
    Callback callback = callbackRef.get();
    // Wait a while to be sure that the parsing does not proceed.
    TimeUnit.MILLISECONDS.sleep(1000);
    Assert.assertEquals(1, contentCount.get());
    // Succeed the content callback to proceed with parsing.
    callbackRef.set(null);
    contentLatch.set(new CountDownLatch(1));
    callback.succeeded();
    Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
    callback = callbackRef.get();
    // Wait a while to be sure that the parsing does not proceed.
    TimeUnit.MILLISECONDS.sleep(1000);
    Assert.assertEquals(2, contentCount.get());
    Assert.assertEquals(1, completeLatch.getCount());
    // Succeed the content callback to proceed with parsing.
    callbackRef.set(null);
    contentLatch.set(new CountDownLatch(1));
    callback.succeeded();
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(2, contentCount.get());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) Request(org.eclipse.jetty.server.Request) 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) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

ServletException (javax.servlet.ServletException)2492 IOException (java.io.IOException)1623 HttpServletRequest (javax.servlet.http.HttpServletRequest)698 HttpServletResponse (javax.servlet.http.HttpServletResponse)658 Test (org.junit.Test)444 PrintWriter (java.io.PrintWriter)238 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)226 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)188 CountDownLatch (java.util.concurrent.CountDownLatch)169 HttpServlet (javax.servlet.http.HttpServlet)151 Request (org.eclipse.jetty.server.Request)148 HashMap (java.util.HashMap)147 InputStream (java.io.InputStream)146 ArrayList (java.util.ArrayList)133 HttpSession (javax.servlet.http.HttpSession)127 SQLException (java.sql.SQLException)124 OutputStream (java.io.OutputStream)116 ServletOutputStream (javax.servlet.ServletOutputStream)113 Properties (java.util.Properties)108 List (java.util.List)107