Search in sources :

Example 46 with Request

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

Example 47 with Request

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

the class AttributeNameTest method testAttributeNamesWithDots.

@Test
public void testAttributeNamesWithDots() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int maxInactivePeriod = 10000;
    int scavengePeriod = 20000;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
    storeFactory.setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    server1.start();
    int port1 = server1.getPort();
    TestServer server2 = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    server2.start();
    int port2 = server2.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform one request to server1 to create a session with attribute with dotted name
            ContentResponse response = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String resp = response.getContentAsString();
            String[] sessionTestResponse = resp.split("/");
            assertEquals("a.b.c", sessionTestResponse[0]);
            String sessionCookie = response.getHeaders().get(HttpHeader.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 to the 2nd server which will do a refresh, use TestServlet to ensure that the
            //session attribute with dotted name is not removed
            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());
        } finally {
            client.stop();
        }
    } finally {
        server1.stop();
        server2.stop();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) 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) TestServer(org.eclipse.jetty.server.session.TestServer) Test(org.junit.Test)

Example 48 with Request

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

the class SessionExpiryTest method changeSessionTimeout.

@Test
public void changeSessionTimeout() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 10;
    int scavengePeriod = 1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
    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
        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);
        DBCollection sessions = MongoTestHelper.getCollection();
        verifySessionCreated(listener, sessionId);
        //verify that the session timeout is set in mongo
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
        //get the session expiry time from mongo
        long expiry = getSessionExpiry(sessions, sessionId);
        //make another request to change the session timeout to a smaller value
        inactivePeriod = 5;
        Request request = client.newRequest(url + "?action=change&val=" + inactivePeriod);
        request.getHeaders().add("Cookie", sessionCookie);
        ContentResponse response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //check the timeout in mongo
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
        //check the session expiry time has decreased from previous value
        assertTrue(getSessionExpiry(sessions, sessionId) < expiry);
        expiry = getSessionExpiry(sessions, sessionId);
        //increase the session timeout
        inactivePeriod = 20;
        request = client.newRequest(url + "?action=change&val=" + inactivePeriod);
        request.getHeaders().add("Cookie", sessionCookie);
        response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //verify that the session timeout is set in mongo
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
        long latestExpiry = getSessionExpiry(sessions, sessionId);
        assertTrue(latestExpiry > expiry);
        assertTrue(getSessionAccessed(sessions, sessionId) + (1000L * inactivePeriod) <= getSessionExpiry(sessions, sessionId));
        //old inactive expired in 5, new inactive expired in 20
        assertTrue(latestExpiry >= 15);
    } finally {
        server1.stop();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) 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) TestServer(org.eclipse.jetty.server.session.TestServer) DBCollection(com.mongodb.DBCollection) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 49 with Request

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

the class AbstractClusteredInvalidationSessionTest method testInvalidation.

@Test
public void testInvalidation() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int maxInactiveInterval = 30;
    int scavengeInterval = 1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.EVICT_ON_SESSION_EXIT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengeInterval);
    TestServer server1 = new TestServer(0, maxInactiveInterval, scavengeInterval, cacheFactory, storeFactory);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    try {
        server1.start();
        int port1 = server1.getPort();
        TestServer server2 = new TestServer(0, maxInactiveInterval, scavengeInterval, cacheFactory, storeFactory);
        server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
        try {
            server2.start();
            int port2 = server2.getPort();
            HttpClient client = new HttpClient();
            QueuedThreadPool executor = new QueuedThreadPool();
            client.setExecutor(executor);
            client.start();
            try {
                String[] urls = new String[2];
                urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
                urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
                // Create the session on node1
                ContentResponse response1 = client.GET(urls[0] + "?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=");
                // Be sure the session is also present in node2
                Request request2 = client.newRequest(urls[1] + "?action=increment");
                request2.header("Cookie", sessionCookie);
                ContentResponse response2 = request2.send();
                assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
                // Invalidate on node1
                Request request1 = client.newRequest(urls[0] + "?action=invalidate");
                request1.header("Cookie", sessionCookie);
                response1 = request1.send();
                assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
                // Be sure on node2 we don't see the session anymore
                request2 = client.newRequest(urls[1] + "?action=test");
                request2.header("Cookie", sessionCookie);
                response2 = request2.send();
                assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
            } finally {
                client.stop();
            }
        } finally {
            server2.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) 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 50 with Request

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

the class AbstractClusteredOrphanedSessionTest method testOrphanedSession.

/**
     * @throws Exception on test failure
     */
@Test
public void testOrphanedSession() throws Exception {
    // Disable scavenging for the first server, so that we simulate its "crash".
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 5;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    TestServer server1 = new TestServer(0, inactivePeriod, -1, cacheFactory, storeFactory);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    try {
        server1.start();
        int port1 = server1.getPort();
        int scavengePeriod = 2;
        DefaultSessionCacheFactory evictCacheFactory = new DefaultSessionCacheFactory();
        //evict after idle for 2 sec
        cacheFactory.setEvictionPolicy(2);
        TestServer server2 = new TestServer(0, inactivePeriod, scavengePeriod, evictCacheFactory, storeFactory);
        server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
        try {
            server2.start();
            int port2 = server2.getPort();
            HttpClient client = new HttpClient();
            client.start();
            try {
                // Connect to server1 to create a session and get its session cookie
                ContentResponse response1 = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?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=");
                // Wait for the session to expire.
                // The first node does not do any scavenging, but the session
                // must be removed by scavenging done in the other node.
                Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + 2L * scavengePeriod));
                // Perform one request to server2 to be sure that the session has been expired
                Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=check");
                request.header("Cookie", sessionCookie);
                ContentResponse response2 = request.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) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.junit.Test)

Aggregations

Request (org.eclipse.jetty.client.api.Request)223 Test (org.junit.Test)137 HttpServletRequest (javax.servlet.http.HttpServletRequest)121 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 HttpServletResponse (javax.servlet.http.HttpServletResponse)63 IOException (java.io.IOException)62 CountDownLatch (java.util.concurrent.CountDownLatch)58 ServletException (javax.servlet.ServletException)53 HttpClient (org.eclipse.jetty.client.HttpClient)48 Response (org.eclipse.jetty.client.api.Response)37 Result (org.eclipse.jetty.client.api.Result)37 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)35 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)30 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 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)25 URI (java.net.URI)24