Search in sources :

Example 41 with ContentResponse

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

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

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

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

Example 45 with ContentResponse

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

the class ReloadedSessionMissingClassTest method testSessionReloadWithMissingClass.

@Test
public void testSessionReloadWithMissingClass() throws Exception {
    Resource.setDefaultUseCaches(false);
    String contextPath = "/foo";
    File unpackedWarDir = testdir.getEmptyPathDir().toFile();
    File webInfDir = new File(unpackedWarDir, "WEB-INF");
    webInfDir.mkdir();
    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" + "<session-config>\n" + " <session-timeout>1</session-timeout>\n" + "</session-config>\n" + "</web-app>";
    FileWriter w = new FileWriter(webXml);
    w.write(xml);
    w.close();
    File foobarJar = MavenTestingUtils.getTestResourceFile("foobar.jar");
    File foobarNOfooJar = MavenTestingUtils.getTestResourceFile("foobarNOfoo.jar");
    URL[] foobarUrls = new URL[] { foobarJar.toURI().toURL() };
    URL[] barUrls = new URL[] { foobarNOfooJar.toURI().toURL() };
    URLClassLoader loaderWithFoo = new URLClassLoader(foobarUrls, Thread.currentThread().getContextClassLoader());
    URLClassLoader loaderWithoutFoo = new URLClassLoader(barUrls, Thread.currentThread().getContextClassLoader());
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = JdbcTestHelper.newSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server1 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC, cacheFactory, storeFactory);
    WebAppContext webApp = server1.addWebAppContext(unpackedWarDir.getCanonicalPath(), contextPath);
    webApp.getSessionHandler().getSessionCache().setRemoveUnloadableSessions(true);
    webApp.setClassLoader(loaderWithFoo);
    webApp.addServlet("Bar", "/bar");
    server1.start();
    int port1 = server1.getPort();
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform one request to server1 to create a session
            ContentResponse response = client.GET("http://localhost:" + port1 + contextPath + "/bar?action=set");
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            String sessionId = (String) webApp.getServletContext().getAttribute("foo");
            assertNotNull(sessionId);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //Stop the webapp
            webApp.stop();
            webApp.setClassLoader(loaderWithoutFoo);
            //restart webapp
            webApp.start();
            Request request = client.newRequest("http://localhost:" + port1 + contextPath + "/bar?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String afterStopSessionId = (String) webApp.getServletContext().getAttribute("foo.session");
            Boolean fooPresent = (Boolean) webApp.getServletContext().getAttribute("foo.present");
            assertFalse(fooPresent);
            assertNotNull(afterStopSessionId);
            assertTrue(!afterStopSessionId.equals(sessionId));
        } finally {
            client.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FileWriter(java.io.FileWriter) Request(org.eclipse.jetty.client.api.Request) URL(java.net.URL) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) URLClassLoader(java.net.URLClassLoader) HttpClient(org.eclipse.jetty.client.HttpClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) File(java.io.File) Test(org.junit.Test)

Aggregations

ContentResponse (org.eclipse.jetty.client.api.ContentResponse)436 Test (org.junit.Test)343 HttpServletRequest (javax.servlet.http.HttpServletRequest)204 IOException (java.io.IOException)166 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)109 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 Test (org.testng.annotations.Test)30 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)29 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)28 HardCodedExecutionFactory (org.teiid.runtime.HardCodedExecutionFactory)27