Search in sources :

Example 96 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testServer401.

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

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setStatus(HttpStatus.UNAUTHORIZED_401);
            response.setHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Basic realm=\"test\"");
        }
    });
    final AtomicBoolean transformed = new AtomicBoolean();
    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 {
                    transformed.set(true);
                    return false;
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.UNAUTHORIZED_401, response.getStatus());
    Assert.assertFalse(transformed.get());
}
Also used : 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) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 97 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testAfterContentTransformerClosingFilesOnServerResponseException.

@Test
public void testAfterContentTransformerClosingFilesOnServerResponseException() throws Exception {
    final Path targetTestsDir = prepareTargetTestsDir();
    final CountDownLatch serviceLatch = new CountDownLatch(1);
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
            response.setContentLength(2);
            // Send only part of the content.
            OutputStream output = response.getOutputStream();
            output.write('x');
            output.flush();
            serviceLatch.countDown();
        }
    });
    final CountDownLatch destroyLatch = new CountDownLatch(1);
    startProxy(new AsyncMiddleManServlet() {

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

                {
                    setOverflowDirectory(targetTestsDir);
                    setMaxInputBufferSize(0);
                    setMaxOutputBufferSize(0);
                }

                @Override
                public boolean transform(Source source, Sink sink) throws IOException {
                    IO.copy(source.getInputStream(), sink.getOutputStream());
                    return true;
                }

                @Override
                public void destroy() {
                    super.destroy();
                    destroyLatch.countDown();
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertTrue(serviceLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(HttpStatus.BAD_GATEWAY_502, response.getStatus());
}
Also used : Path(java.nio.file.Path) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) 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) CountDownLatch(java.util.concurrent.CountDownLatch) 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 98 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class WebSocketUpgradeRequest method onComplete.

@Override
public void onComplete(Result result) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("onComplete() - {}", result);
    }
    URI requestURI = result.getRequest().getURI();
    Response response = result.getResponse();
    int responseStatusCode = response.getStatus();
    String responseLine = responseStatusCode + " " + response.getReason();
    if (result.isFailed()) {
        if (LOG.isDebugEnabled()) {
            if (result.getFailure() != null)
                LOG.debug("General Failure", result.getFailure());
            if (result.getRequestFailure() != null)
                LOG.debug("Request Failure", result.getRequestFailure());
            if (result.getResponseFailure() != null)
                LOG.debug("Response Failure", result.getResponseFailure());
        }
        Throwable failure = result.getFailure();
        if ((failure instanceof java.net.ConnectException) || (failure instanceof UpgradeException)) {
            // handle as-is
            handleException(failure);
        } else {
            // wrap in UpgradeException 
            handleException(new UpgradeException(requestURI, responseStatusCode, responseLine, failure));
        }
    }
    if (responseStatusCode != HttpStatus.SWITCHING_PROTOCOLS_101) {
        // Failed to upgrade (other reason)
        handleException(new UpgradeException(requestURI, responseStatusCode, responseLine));
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpResponse(org.eclipse.jetty.client.HttpResponse) UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) URI(java.net.URI) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 99 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class HttpReceiverOverHTTPTest method test_Receive_NoResponseContent.

@Test
public void test_Receive_NoResponseContent() throws Exception {
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: 0\r\n" + "\r\n");
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("OK", response.getReason());
    Assert.assertSame(HttpVersion.HTTP_1_1, response.getVersion());
    HttpFields headers = response.getHeaders();
    Assert.assertNotNull(headers);
    Assert.assertEquals(1, headers.size());
    Assert.assertEquals("0", headers.get(HttpHeader.CONTENT_LENGTH));
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpFields(org.eclipse.jetty.http.HttpFields) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 100 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class HttpReceiverOverHTTPTest method test_FillInterested_RacingWith_BufferRelease.

@Test
public void test_FillInterested_RacingWith_BufferRelease() throws Exception {
    connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<>()) {

        @Override
        protected HttpChannelOverHTTP newHttpChannel() {
            return new HttpChannelOverHTTP(this) {

                @Override
                protected HttpReceiverOverHTTP newHttpReceiver() {
                    return new HttpReceiverOverHTTP(this) {

                        @Override
                        protected void fillInterested() {
                            // Verify that the buffer has been released
                            // before fillInterested() is called.
                            Assert.assertNull(getResponseBuffer());
                            // Fill the endpoint so receive is called again.
                            endPoint.addInput("X");
                            super.fillInterested();
                        }
                    };
                }
            };
        }
    };
    endPoint.setConnection(connection);
    // Partial response to trigger the call to fillInterested().
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 1\r\n" + "\r\n");
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.client.api.Response)113 Test (org.junit.Test)90 HttpServletResponse (javax.servlet.http.HttpServletResponse)89 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)84 IOException (java.io.IOException)77 HttpServletRequest (javax.servlet.http.HttpServletRequest)70 ServletException (javax.servlet.ServletException)68 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ByteBuffer (java.nio.ByteBuffer)34 ServletOutputStream (javax.servlet.ServletOutputStream)34 Request (org.eclipse.jetty.client.api.Request)31 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)29 Request (org.eclipse.jetty.server.Request)26 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22