Search in sources :

Example 11 with Request

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

the class AbstractSessionExpiryTest method testRequestForSessionWithChangedTimeout.

@Test
public void testRequestForSessionWithChangedTimeout() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 5;
    int scavengePeriod = 1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
    ServletHolder holder = new ServletHolder(servlet);
    ServletContextHandler context = server1.addContext(contextPath);
    context.addServlet(holder, servletMapping);
    TestHttpSessionListener listener = new TestHttpSessionListener();
    context.getSessionHandler().addEventListener(listener);
    server1.start();
    int port1 = server1.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make a request to set up a session on the server with the session manager's inactive timeout
        ContentResponse response = client.GET(url + "?action=init");
        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=");
        //make another request to change the session timeout to a larger value
        int newInactivePeriod = 100;
        Request request = client.newRequest(url + "?action=change&val=" + newInactivePeriod);
        request.getHeaders().add("Cookie", sessionCookie);
        response = request.send();
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        //stop and restart the session manager to ensure it needs to reload the session
        context.stop();
        context.start();
        //wait until the session manager timeout has passed and re-request the session
        //which should still be valid
        pause(inactivePeriod);
        request = client.newRequest(url + "?action=check");
        request.getHeaders().add("Cookie", sessionCookie);
        response = request.send();
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        String sessionCookie2 = response.getHeaders().get("Set-Cookie");
        assertNull(sessionCookie2);
    } finally {
        server1.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) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 12 with Request

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

the class AbstractSessionExpiryTest method testSessionNotExpired.

/**
     * Check session is preserved over stop/start
     * @throws Exception
     */
@Test
public void testSessionNotExpired() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 20;
    int scavengePeriod = 10;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    TestServlet servlet = new TestServlet();
    ServletHolder holder = new ServletHolder(servlet);
    server1.addContext(contextPath).addServlet(holder, servletMapping);
    HttpClient client = new HttpClient();
    try {
        server1.start();
        int port1 = server1.getPort();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make a request to set up a session on the server
        ContentResponse response = client.GET(url + "?action=init");
        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=");
        //now stop the server
        server1.stop();
        //start the server again, before the session times out
        server1.start();
        port1 = server1.getPort();
        url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make another request, the session should not have expired
        Request request = client.newRequest(url + "?action=notexpired");
        request.getHeaders().add("Cookie", sessionCookie);
        ContentResponse response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
    } finally {
        client.stop();
        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) Test(org.junit.Test)

Example 13 with Request

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

the class AbstractSessionExpiryTest method testSessionExpiry.

/**
     * Check that a session that expires whilst the server is stopped will not be
     * able to be used when the server restarts
     * @throws Exception
     */
@Test
public void testSessionExpiry() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 4;
    int scavengePeriod = 1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    TestServlet servlet = new TestServlet();
    ServletHolder holder = new ServletHolder(servlet);
    ServletContextHandler context = server1.addContext(contextPath);
    context.addServlet(holder, servletMapping);
    TestHttpSessionListener listener = new TestHttpSessionListener();
    context.getSessionHandler().addEventListener(listener);
    server1.start();
    int port1 = server1.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make a request to set up a session on the server
        ContentResponse response1 = client.GET(url + "?action=init");
        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=");
        String sessionId = TestServer.extractSessionId(sessionCookie);
        verifySessionCreated(listener, sessionId);
        //now stop the server
        server1.stop();
        //and wait until the session should have expired
        pause(inactivePeriod);
        //restart the server
        server1.start();
        //and wait until the scavenger has run
        pause(inactivePeriod + (scavengePeriod * 2));
        port1 = server1.getPort();
        url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make another request, the session should have expired
        Request request = client.newRequest(url + "?action=test");
        request.getHeaders().add("Cookie", sessionCookie);
        ContentResponse response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        String cookie2 = response2.getHeaders().get("Set-Cookie");
        assertTrue(!cookie2.equals(sessionCookie));
        verifySessionDestroyed(listener, sessionId);
    } finally {
        server1.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) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 14 with Request

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

the class AbstractSessionInvalidateCreateScavengeTest method testSessionScavenge.

@Test
public void testSessionScavenge() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 6;
    int scavengePeriod = 3;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((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);
    MySessionListener listener = new MySessionListener();
    context.getSessionHandler().addEventListener(listener);
    try {
        server.start();
        int port1 = server.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            String url = "http://localhost:" + port1 + contextPath + servletMapping;
            // Create the session
            ContentResponse response1 = client.GET(url + "?action=init");
            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=");
            // Make a request which will invalidate the existing session and create a new one
            Request request2 = client.newRequest(url + "?action=test");
            request2.header("Cookie", sessionCookie);
            ContentResponse response2 = request2.send();
            assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
            // Wait for the scavenger to run
            pause(inactivePeriod + (2 * scavengePeriod));
            //test that the session created in the last test is scavenged:
            //the HttpSessionListener should have been called when session1 was invalidated and session2 was scavenged
            assertTrue(listener.destroys.size() == 2);
            //ensure 2 different objects
            assertTrue(listener.destroys.get(0) != listener.destroys.get(1));
            //session2's HttpSessionBindingListener should have been called when it was scavenged
            assertTrue(servlet.listener.unbound);
        } 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) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 15 with Request

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

the class AbstractWebAppObjectInSessionTest method testWebappObjectInSession.

@Test
public void testWebappObjectInSession() throws Exception {
    String contextName = "webappObjectInSessionTest";
    String contextPath = "/" + contextName;
    String servletMapping = "/server";
    File targetDir = new File(System.getProperty("basedir"), "target");
    File warDir = new File(targetDir, contextName);
    warDir.mkdir();
    File webInfDir = new File(warDir, "WEB-INF");
    webInfDir.mkdir();
    // Write web.xml
    File webXml = new File(webInfDir, "web.xml");
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<web-app xmlns=\"http://java.sun.com/xml/ns/j2ee\"\n" + "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd\"\n" + "         version=\"2.4\">\n" + "\n" + "</web-app>";
    FileWriter w = new FileWriter(webXml);
    w.write(xml);
    w.close();
    File classesDir = new File(webInfDir, "classes");
    classesDir.mkdir();
    String packageName = WebAppObjectInSessionServlet.class.getPackage().getName();
    File packageDirs = new File(classesDir, packageName.replace('.', File.separatorChar));
    packageDirs.mkdirs();
    String resourceName = WebAppObjectInSessionServlet.class.getSimpleName() + ".class";
    Resource resource = Resource.newResource(getClass().getResource(resourceName));
    //File sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
    File targetFile = new File(packageDirs, resourceName);
    //copy(sourceFile, targetFile);
    IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
    resourceName = WebAppObjectInSessionServlet.class.getSimpleName() + "$" + WebAppObjectInSessionServlet.TestSharedStatic.class.getSimpleName() + ".class";
    resource = Resource.newResource(getClass().getResource(resourceName));
    //sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
    targetFile = new File(packageDirs, resourceName);
    //copy(sourceFile, targetFile);
    IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
    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.addWebAppContext(warDir.getCanonicalPath(), contextPath).addServlet(WebAppObjectInSessionServlet.class.getName(), servletMapping);
    try {
        server1.start();
        int port1 = server1.getPort();
        TestServer server2 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC, cacheFactory, storeFactory);
        server2.addWebAppContext(warDir.getCanonicalPath(), contextPath).addServlet(WebAppObjectInSessionServlet.class.getName(), servletMapping);
        try {
            server2.start();
            int port2 = server2.getPort();
            HttpClient client = new HttpClient();
            client.start();
            try {
                // Perform one request to server1 to create a session
                Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=set");
                request.method(HttpMethod.GET);
                ContentResponse response = request.send();
                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=");
                // Perform a request to server2 using the session cookie from the previous request
                Request request2 = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=get");
                request2.method(HttpMethod.GET);
                request2.header("Cookie", sessionCookie);
                ContentResponse response2 = request2.send();
                assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
            } finally {
                client.stop();
            }
        } finally {
            server2.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FileWriter(java.io.FileWriter) Resource(org.eclipse.jetty.util.resource.Resource) Request(org.eclipse.jetty.client.api.Request) FileOutputStream(java.io.FileOutputStream) HttpClient(org.eclipse.jetty.client.HttpClient) File(java.io.File) 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