Search in sources :

Example 91 with ContentResponse

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

the class HttpClientProxyTest method testProxyAuthenticationWithServerAuthentication.

@Test
public void testProxyAuthenticationWithServerAuthentication() throws Exception {
    String proxyRealm = "proxyRealm";
    String serverRealm = "serverRealm";
    int status = HttpStatus.NO_CONTENT_204;
    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);
            String authorization = request.getHeader(HttpHeader.PROXY_AUTHORIZATION.asString());
            if (authorization == null) {
                response.setStatus(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
                response.setHeader(HttpHeader.PROXY_AUTHENTICATE.asString(), "Basic realm=\"" + proxyRealm + "\"");
            } else {
                authorization = request.getHeader(HttpHeader.AUTHORIZATION.asString());
                if (authorization == null) {
                    response.setStatus(HttpStatus.UNAUTHORIZED_401);
                    response.setHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Basic realm=\"" + serverRealm + "\"");
                } else {
                    response.setStatus(status);
                }
            }
        }
    });
    String proxyHost = "localhost";
    int proxyPort = connector.getLocalPort();
    String serverHost = "server";
    int serverPort = proxyPort + 1;
    URI proxyURI = URI.create(scheme + "://" + proxyHost + ":" + proxyPort);
    client.getAuthenticationStore().addAuthentication(new BasicAuthentication(proxyURI, proxyRealm, "proxyUser", "proxyPassword"));
    URI serverURI = URI.create(scheme + "://" + serverHost + ":" + serverPort);
    client.getAuthenticationStore().addAuthentication(new BasicAuthentication(serverURI, serverRealm, "serverUser", "serverPassword"));
    client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
    final AtomicInteger requests = new AtomicInteger();
    client.getRequestListeners().add(new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            requests.incrementAndGet();
        }
    });
    // Make a request, expect 407 + 401 + 204.
    ContentResponse response1 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(status, response1.getStatus());
    Assert.assertEquals(3, requests.get());
    // Make again the request, authentication is cached, expect 204.
    requests.set(0);
    ContentResponse response2 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(status, response2.getStatus());
    Assert.assertEquals(1, requests.get());
}
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) URI(java.net.URI) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) Test(org.junit.Test)

Example 92 with ContentResponse

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

the class HttpClientRedirectTest method testMethodRedirect.

private void testMethodRedirect(final HttpMethod requestMethod, final HttpMethod redirectMethod, int redirectCode) throws Exception {
    start(new RedirectHandler());
    final AtomicInteger passes = new AtomicInteger();
    client.getRequestListeners().add(new org.eclipse.jetty.client.api.Request.Listener.Adapter() {

        @Override
        public void onBegin(org.eclipse.jetty.client.api.Request request) {
            int pass = passes.incrementAndGet();
            if (pass == 1) {
                if (!requestMethod.is(request.getMethod()))
                    request.abort(new Exception());
            } else if (pass == 2) {
                if (!redirectMethod.is(request.getMethod()))
                    request.abort(new Exception());
            } else {
                request.abort(new Exception());
            }
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(requestMethod).path("/" + redirectCode + "/localhost/done").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Example 93 with ContentResponse

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

the class HttpClientRedirectTest method test_307_WithRequestContent.

@Test
public void test_307_WithRequestContent() throws Exception {
    start(new RedirectHandler());
    byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).path("/307/localhost/done").content(new ByteBufferContentProvider(ByteBuffer.wrap(data))).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) Test(org.junit.Test)

Example 94 with ContentResponse

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

the class TestMemcachedSessions method testMemcached.

@Test
public void testMemcached() throws Exception {
    String contextPath = "/";
    Server server = new Server(0);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase(System.getProperty("java.io.tmpdir"));
    server.setHandler(context);
    NullSessionCache dsc = new NullSessionCache(context.getSessionHandler());
    dsc.setSessionDataStore(new CachingSessionDataStore(new MemcachedSessionDataMap("localhost", "11211"), new NullSessionDataStore()));
    context.getSessionHandler().setSessionCache(dsc);
    // Add a test servlet
    ServletHolder h = new ServletHolder();
    h.setServlet(new TestServlet());
    context.addServlet(h, "/");
    try {
        server.start();
        int port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            int value = 42;
            ContentResponse response = client.GET("http://localhost:" + port + contextPath + "?action=set&value=" + value);
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            String resp = response.getContentAsString();
            assertEquals(resp.trim(), String.valueOf(value));
            // Be sure the session value is still there
            Request request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            resp = response.getContentAsString();
            assertEquals(String.valueOf(value), resp.trim());
            //Delete the session
            request = client.newRequest("http://localhost:" + port + contextPath + "?action=del");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //Check that the session is gone
            request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            resp = response.getContentAsString();
            assertEquals("No session", resp.trim());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) CachingSessionDataStore(org.eclipse.jetty.server.session.CachingSessionDataStore) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpClient(org.eclipse.jetty.client.HttpClient) NullSessionDataStore(org.eclipse.jetty.server.session.NullSessionDataStore) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 95 with ContentResponse

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

the class JavaMonitorAction method sendRequest.

/* ------------------------------------------------------------ */
/**
     * @param request
     * @return
     * @throws Exception 
     */
private Properties sendRequest(Properties request) throws Exception {
    ByteArrayOutputStream reqStream = null;
    ByteArrayInputStream resStream = null;
    Properties response = null;
    try {
        reqStream = new ByteArrayOutputStream();
        request.storeToXML(reqStream, null);
        ContentResponse r3sponse = _client.POST(_url).header("Connection", "close").content(new BytesContentProvider(reqStream.toByteArray())).send();
        if (r3sponse.getStatus() == HttpStatus.OK_200) {
            response = new Properties();
            resStream = new ByteArrayInputStream(r3sponse.getContent());
            response.loadFromXML(resStream);
        }
    } finally {
        try {
            if (reqStream != null)
                reqStream.close();
        } catch (IOException ex) {
            LOG.ignore(ex);
        }
        try {
            if (resStream != null)
                resStream.close();
        } catch (IOException ex) {
            LOG.ignore(ex);
        }
    }
    return response;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider)

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