Search in sources :

Example 21 with Request

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

the class AbstractClusteredSessionMigrationTest method testSessionMigration.

@Test
public void testSessionMigration() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server1 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC, cacheFactory, storeFactory);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    try {
        server1.start();
        int port1 = server1.getPort();
        TestServer server2 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC, cacheFactory, storeFactory);
        server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
        try {
            server2.start();
            int port2 = server2.getPort();
            HttpClient client = new HttpClient();
            client.start();
            try {
                // Perform one request to server1 to create a session
                int value = 1;
                Request request1 = client.POST("http://localhost:" + port1 + contextPath + servletMapping + "?action=set&value=" + value);
                ContentResponse response1 = request1.send();
                assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
                String sessionCookie = response1.getHeaders().get("Set-Cookie");
                assertTrue(sessionCookie != null);
                // Mangle the cookie, replacing Path with $Path, etc.
                sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
                // Perform a request to server2 using the session cookie from the previous request
                // This should migrate the session from server1 to server2.
                Request request2 = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=get");
                request2.header("Cookie", sessionCookie);
                ContentResponse response2 = request2.send();
                assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
                String response = response2.getContentAsString();
                assertEquals(response.trim(), String.valueOf(value));
            } finally {
                client.stop();
            }
        } finally {
            server2.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.junit.Test)

Example 22 with Request

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

the class AbstractModifyMaxInactiveIntervalTest method testNoExpireSessionInUse.

@Test
public void testNoExpireSessionInUse() throws Exception {
    int maxInactive = 3;
    int sleep = maxInactive + (int) (maxInactive * 0.8);
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server = new TestServer(0, maxInactive, 1, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestModServlet.class, "/test");
    server.start();
    int port = server.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform a request to create a session
            ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
            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=");
            //do another request that will sleep long enough for the session expiry time to have passed
            //before trying to access the session and ensure it is still there
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=sleep&val=" + sleep);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 23 with Request

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

the class CreationTest method testSessionCreateWithEviction.

/**
     * Test creating a session when the cache is set to
     * evict after the request exits.
     * @throws Exception
     */
@Test
public void testSessionCreateWithEviction() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.EVICT_ON_SESSION_EXIT);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    _server1 = new TestServer(0, -1, -1, cacheFactory, storeFactory);
    ServletHolder holder = new ServletHolder(_servlet);
    ServletContextHandler contextHandler = _server1.addContext(contextPath);
    contextHandler.addServlet(holder, servletMapping);
    _servlet.setStore(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore());
    _server1.start();
    int port1 = _server1.getPort();
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping + "?action=create&check=false";
        //make a request to set up a session on the server
        ContentResponse response = client.GET(url);
        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=");
        //session should now be evicted from the cache
        String id = TestServer.extractSessionId(sessionCookie);
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
        //make another request for the same session
        Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=test");
        request.header("Cookie", sessionCookie);
        response = request.send();
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        //session should now be evicted from the cache again
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(TestServer.extractSessionId(sessionCookie)));
    } finally {
        _server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 24 with Request

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

the class DeleteUnloadableSessionTest method testDeleteUnloadableSession.

/**
     * Test that session data that can't be loaded results in a null Session object
     * @throws Exception
     */
@Test
public void testDeleteUnloadableSession() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = -1;
    int scavengePeriod = 100;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    cacheFactory.setRemoveUnloadableSessions(true);
    SessionDataStoreFactory storeFactory = new DelSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
    TestServer server = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletContextHandler context = server.addContext(contextPath);
    TestServlet servlet = new TestServlet();
    ServletHolder holder = new ServletHolder(servlet);
    context.addServlet(holder, servletMapping);
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        server.start();
        int port = server.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            String sessionCookie = "JSESSIONID=w0rm3zxpa6h1zg1mevtv76b3te00.w0;$Path=/";
            Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
            request.header("Cookie", sessionCookie);
            ContentResponse response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            assertFalse(context.getSessionHandler().getSessionCache().getSessionDataStore().exists(TestServer.extractSessionId(sessionCookie)));
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : 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) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 25 with Request

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

the class DirtyAttributeTest method testDirtyWrite.

@Test
public void testDirtyWrite() throws Exception {
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = new TestPassivatingSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(SCAVENGE);
    TestServer server = new TestServer(0, INACTIVE, SCAVENGE, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestDirtyServlet.class, "/test");
    server.start();
    int port = server.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform a request to create a session              
            ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
            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=");
            //do another request to change the session attribute
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=setA");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            A_VALUE.assertPassivatesEquals(1);
            A_VALUE.assertActivatesEquals(1);
            A_VALUE.assertBindsEquals(1);
            A_VALUE.assertUnbindsEquals(0);
            //do another request using the cookie to try changing the session attribute to the same value again              
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=setA");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            A_VALUE.assertPassivatesEquals(2);
            A_VALUE.assertActivatesEquals(2);
            A_VALUE.assertBindsEquals(1);
            A_VALUE.assertUnbindsEquals(0);
            //do another request using the cookie and change to a different value             
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=setB");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            B_VALUE.assertPassivatesEquals(1);
            B_VALUE.assertActivatesEquals(1);
            B_VALUE.assertBindsEquals(1);
            B_VALUE.assertUnbindsEquals(0);
            A_VALUE.assertBindsEquals(1);
            A_VALUE.assertUnbindsEquals(1);
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Aggregations

Request (org.eclipse.jetty.client.api.Request)259 Test (org.junit.Test)145 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)132 HttpServletRequest (javax.servlet.http.HttpServletRequest)123 IOException (java.io.IOException)71 HttpServletResponse (javax.servlet.http.HttpServletResponse)65 CountDownLatch (java.util.concurrent.CountDownLatch)58 HttpClient (org.eclipse.jetty.client.HttpClient)58 ServletException (javax.servlet.ServletException)55 Response (org.eclipse.jetty.client.api.Response)45 InputStream (java.io.InputStream)39 Result (org.eclipse.jetty.client.api.Result)38 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)35 URI (java.net.URI)34 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)30 ExecutionException (java.util.concurrent.ExecutionException)28 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)28 ByteBuffer (java.nio.ByteBuffer)27 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)26 Connection (org.eclipse.jetty.client.api.Connection)26