Search in sources :

Example 16 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.

the class SslContextFactoryTest method testResourceTsWrongPWResourceKs.

@Test
public void testResourceTsWrongPWResourceKs() throws Exception {
    Resource keystoreResource = Resource.newSystemResource("keystore");
    Resource truststoreResource = Resource.newSystemResource("keystore");
    cf.setKeyStoreResource(keystoreResource);
    cf.setTrustStoreResource(truststoreResource);
    cf.setKeyStorePassword("storepwd");
    cf.setKeyManagerPassword("keypwd");
    cf.setTrustStorePassword("wrong_storepwd");
    try (StacklessLogging stackless = new StacklessLogging(AbstractLifeCycle.class)) {
        cf.start();
        Assert.fail();
    } catch (IOException e) {
        Assert.assertThat(e.toString(), Matchers.containsString("java.io.IOException: Keystore was tampered with, or password was incorrect"));
    }
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IOException(java.io.IOException) Test(org.junit.Test)

Example 17 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging 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 18 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.

the class CreationTest method testSessionCreateForward.

/**
     * Create a session in a context, forward to another context and create a 
     * session in it too. Check that both sessions exist after the response
     * completes.
     * @throws Exception
     */
@Test
public void testSessionCreateForward() throws Exception {
    String contextPath = "";
    String contextB = "/contextB";
    String servletMapping = "/server";
    int inactivePeriod = 20;
    int scavengePeriod = 3;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    _server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletHolder holder = new ServletHolder(_servlet);
    ServletContextHandler contextHandler = _server1.addContext(contextPath);
    contextHandler.addServlet(holder, servletMapping);
    ServletContextHandler ctxB = _server1.addContext(contextB);
    ctxB.addServlet(TestServletB.class, servletMapping);
    _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;
        //make a request to set up a session on the server
        ContentResponse response = client.GET(url + "?action=forward");
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        //ensure work has finished on the server side
        _synchronizer.await();
        //check that the sessions exist persisted
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(_servlet._id));
        assertTrue(ctxB.getSessionHandler().getSessionCache().getSessionDataStore().exists(_servlet._id));
    } finally {
        _server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 19 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging 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 20 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging 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)

Aggregations

StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)143 Test (org.junit.Test)134 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)55 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)51 ArrayList (java.util.ArrayList)46 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)45 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)35 ByteBuffer (java.nio.ByteBuffer)29 IOException (java.io.IOException)26 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)22 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)21 ServletException (javax.servlet.ServletException)20 CountDownLatch (java.util.concurrent.CountDownLatch)17 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)17 Matchers.containsString (org.hamcrest.Matchers.containsString)17 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)14 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)13 OutputStream (java.io.OutputStream)12 Socket (java.net.Socket)12