Search in sources :

Example 11 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project jetty.project by eclipse.

the class AbstractProxySerializationTest method testProxySerialization.

@Test
public void testProxySerialization() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int scavengePeriod = 10;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
    TestServer server = new TestServer(0, 20, scavengePeriod, cacheFactory, storeFactory);
    ServletContextHandler context = server.addContext(contextPath);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("proxy-serialization.jar");
    File testDir = MavenTestingUtils.getTargetTestingDir("proxy-serialization");
    testDir.mkdirs();
    File extractedJar = new File(testDir, "proxy-serialization.jar");
    extractedJar.createNewFile();
    IO.copy(is, new FileOutputStream(extractedJar));
    URLClassLoader loader = new URLClassLoader(new URL[] { extractedJar.toURI().toURL() }, Thread.currentThread().getContextClassLoader());
    context.setClassLoader(loader);
    context.addServlet("TestServlet", servletMapping);
    customizeContext(context);
    try {
        server.start();
        int port = server.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?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=");
            //stop the context to be sure the sesssion will be passivated
            context.stop();
            //restart the context
            context.start();
            // Make another request using the session id from before
            Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
            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) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File) Test(org.junit.Test)

Example 12 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler 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 13 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler 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 ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project jetty.project by eclipse.

the class AbstractSessionExpiryTest method testSessionExpiresWithListener.

@Test
public void testSessionExpiresWithListener() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 3;
    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(true);
    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);
        //and wait until the session should have expired
        pause(inactivePeriod + (scavengePeriod * 2));
        verifySessionDestroyed(listener, sessionId);
        assertNull(listener.ex);
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 15 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler 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)

Aggregations

ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)385 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)249 Server (org.eclipse.jetty.server.Server)216 ServerConnector (org.eclipse.jetty.server.ServerConnector)98 Test (org.junit.Test)70 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)55 IOException (java.io.IOException)42 HttpClient (org.eclipse.jetty.client.HttpClient)39 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)39 HttpServletRequest (javax.servlet.http.HttpServletRequest)38 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)37 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)37 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)32 Connector (org.eclipse.jetty.server.Connector)29 Request (org.eclipse.jetty.client.api.Request)27 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)25 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)24 ServletException (javax.servlet.ServletException)22 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)22 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)22