Search in sources :

Example 81 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse 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 82 with ContentResponse

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

the class HttpClientTest method testRequestListenerForMultipleEventsIsInvokedOncePerEvent.

@Test
public void testRequestListenerForMultipleEventsIsInvokedOncePerEvent() throws Exception {
    start(new EmptyServerHandler());
    final AtomicInteger counter = new AtomicInteger();
    Request.Listener listener = new Request.Listener() {

        @Override
        public void onQueued(Request request) {
            counter.incrementAndGet();
        }

        @Override
        public void onBegin(Request request) {
            counter.incrementAndGet();
        }

        @Override
        public void onHeaders(Request request) {
            counter.incrementAndGet();
        }

        @Override
        public void onCommit(Request request) {
            counter.incrementAndGet();
        }

        @Override
        public void onContent(Request request, ByteBuffer content) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onFailure(Request request, Throwable failure) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onSuccess(Request request) {
            counter.incrementAndGet();
        }
    };
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestQueued(listener).onRequestBegin(listener).onRequestHeaders(listener).onRequestCommit(listener).onRequestContent(listener).onRequestSuccess(listener).onRequestFailure(listener).listener(listener).send();
    Assert.assertEquals(200, response.getStatus());
    int expectedEventsTriggeredByOnRequestXXXListeners = 5;
    int expectedEventsTriggeredByListener = 5;
    int expected = expectedEventsTriggeredByOnRequestXXXListeners + expectedEventsTriggeredByListener;
    Assert.assertEquals(expected, counter.get());
}
Also used : FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) Test(org.junit.Test)

Example 83 with ContentResponse

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

the class HttpClientAuthenticationTest method test_BasicAuthentication_WithAuthenticationRemoved.

@Test
public void test_BasicAuthentication_WithAuthenticationRemoved() throws Exception {
    startBasic(new EmptyServerHandler());
    final AtomicReference<CountDownLatch> requests = new AtomicReference<>(new CountDownLatch(2));
    Request.Listener.Adapter requestListener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            requests.get().countDown();
        }
    };
    client.getRequestListeners().add(requestListener);
    AuthenticationStore authenticationStore = client.getAuthenticationStore();
    URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
    BasicAuthentication authentication = new BasicAuthentication(uri, realm, "basic", "basic");
    authenticationStore.addAuthentication(authentication);
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    ContentResponse response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
    authenticationStore.removeAuthentication(authentication);
    requests.set(new CountDownLatch(1));
    request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
    Authentication.Result result = authenticationStore.findAuthenticationResult(request.getURI());
    Assert.assertNotNull(result);
    authenticationStore.removeAuthenticationResult(result);
    requests.set(new CountDownLatch(1));
    request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(401, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Authentication(org.eclipse.jetty.client.api.Authentication) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 84 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse 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 85 with ContentResponse

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

the class ResponseNotifier method forwardSuccess.

public void forwardSuccess(List<Response.ResponseListener> listeners, Response response) {
    notifyBegin(listeners, response);
    for (Iterator<HttpField> iterator = response.getHeaders().iterator(); iterator.hasNext(); ) {
        HttpField field = iterator.next();
        if (!notifyHeader(listeners, response, field))
            iterator.remove();
    }
    notifyHeaders(listeners, response);
    if (response instanceof ContentResponse)
        notifyContent(listeners, response, ByteBuffer.wrap(((ContentResponse) response).getContent()), Callback.NOOP);
    notifySuccess(listeners, response);
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpField(org.eclipse.jetty.http.HttpField)

Aggregations

ContentResponse (org.eclipse.jetty.client.api.ContentResponse)408 Test (org.junit.Test)322 HttpServletRequest (javax.servlet.http.HttpServletRequest)204 IOException (java.io.IOException)164 HttpServletResponse (javax.servlet.http.HttpServletResponse)159 ServletException (javax.servlet.ServletException)150 Request (org.eclipse.jetty.client.api.Request)117 HttpClient (org.eclipse.jetty.client.HttpClient)105 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)92 HttpServlet (javax.servlet.http.HttpServlet)48 Properties (java.util.Properties)45 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)45 InterruptedIOException (java.io.InterruptedIOException)42 Request (org.eclipse.jetty.server.Request)39 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)38 CountDownLatch (java.util.concurrent.CountDownLatch)37 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)29 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)28 Test (org.testng.annotations.Test)28 HardCodedExecutionFactory (org.teiid.runtime.HardCodedExecutionFactory)27