Search in sources :

Example 46 with ServletHolder

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

the class TestMemcachedSessions method testMemcached.

@Test
public void testMemcached() throws Exception {
    String contextPath = "/";
    Server server = new Server(0);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase(System.getProperty("java.io.tmpdir"));
    server.setHandler(context);
    NullSessionCache dsc = new NullSessionCache(context.getSessionHandler());
    dsc.setSessionDataStore(new CachingSessionDataStore(new MemcachedSessionDataMap("localhost", "11211"), new NullSessionDataStore()));
    context.getSessionHandler().setSessionCache(dsc);
    // Add a test servlet
    ServletHolder h = new ServletHolder();
    h.setServlet(new TestServlet());
    context.addServlet(h, "/");
    try {
        server.start();
        int port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            int value = 42;
            ContentResponse response = client.GET("http://localhost:" + port + contextPath + "?action=set&value=" + value);
            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=");
            String resp = response.getContentAsString();
            assertEquals(resp.trim(), String.valueOf(value));
            // Be sure the session value is still there
            Request request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            resp = response.getContentAsString();
            assertEquals(String.valueOf(value), resp.trim());
            //Delete the session
            request = client.newRequest("http://localhost:" + port + contextPath + "?action=del");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //Check that the session is gone
            request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            resp = response.getContentAsString();
            assertEquals("No session", resp.trim());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) CachingSessionDataStore(org.eclipse.jetty.server.session.CachingSessionDataStore) 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) NullSessionDataStore(org.eclipse.jetty.server.session.NullSessionDataStore) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 47 with ServletHolder

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

the class EventSourceServletTest method testBasicFunctionality.

@Test
public void testBasicFunctionality() throws Exception {
    final AtomicReference<EventSource.Emitter> emitterRef = new AtomicReference<EventSource.Emitter>();
    final CountDownLatch emitterLatch = new CountDownLatch(1);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    class S extends EventSourceServlet {

        @Override
        protected EventSource newEventSource(HttpServletRequest request) {
            return new EventSource() {

                public void onOpen(Emitter emitter) throws IOException {
                    emitterRef.set(emitter);
                    emitterLatch.countDown();
                }

                public void onClose() {
                    closeLatch.countDown();
                }
            };
        }
    }
    String servletPath = "/eventsource";
    ServletHolder servletHolder = new ServletHolder(new S());
    int heartBeatPeriod = 2;
    servletHolder.setInitParameter("heartBeatPeriod", String.valueOf(heartBeatPeriod));
    context.addServlet(servletHolder, servletPath);
    Socket socket = new Socket("localhost", connector.getLocalPort());
    writeHTTPRequest(socket, servletPath);
    BufferedReader reader = readAndDiscardHTTPResponse(socket);
    Assert.assertTrue(emitterLatch.await(1, TimeUnit.SECONDS));
    EventSource.Emitter emitter = emitterRef.get();
    Assert.assertNotNull(emitter);
    String data = "foo";
    emitter.data(data);
    String line = reader.readLine();
    String received = "";
    while (line != null) {
        received += line;
        if (line.length() == 0)
            break;
        line = reader.readLine();
    }
    Assert.assertEquals("data: " + data, received);
    socket.close();
    Assert.assertTrue(closeLatch.await(heartBeatPeriod * 3, TimeUnit.SECONDS));
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) Test(org.junit.Test)

Example 48 with ServletHolder

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

the class EventSourceServletTest method testEncoding.

@Test
public void testEncoding() throws Exception {
    // The EURO symbol
    final String data = "€";
    class S extends EventSourceServlet {

        @Override
        protected EventSource newEventSource(HttpServletRequest request) {
            return new EventSource() {

                public void onOpen(Emitter emitter) throws IOException {
                    emitter.data(data);
                }

                public void onClose() {
                }
            };
        }
    }
    String servletPath = "/eventsource";
    context.addServlet(new ServletHolder(new S()), servletPath);
    Socket socket = new Socket("localhost", connector.getLocalPort());
    writeHTTPRequest(socket, servletPath);
    BufferedReader reader = readAndDiscardHTTPResponse(socket);
    String line = reader.readLine();
    String received = "";
    while (line != null) {
        received += line;
        if (line.length() == 0)
            break;
        line = reader.readLine();
    }
    Assert.assertEquals("data: " + data, received);
    socket.close();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) Test(org.junit.Test)

Example 49 with ServletHolder

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

the class EventSourceServletTest method testServerSideClose.

@Test
public void testServerSideClose() throws Exception {
    final AtomicReference<EventSource.Emitter> emitterRef = new AtomicReference<EventSource.Emitter>();
    final CountDownLatch emitterLatch = new CountDownLatch(1);
    class S extends EventSourceServlet {

        @Override
        protected EventSource newEventSource(HttpServletRequest request) {
            return new EventSource() {

                public void onOpen(Emitter emitter) throws IOException {
                    emitterRef.set(emitter);
                    emitterLatch.countDown();
                }

                public void onClose() {
                }
            };
        }
    }
    String servletPath = "/eventsource";
    context.addServlet(new ServletHolder(new S()), servletPath);
    Socket socket = new Socket("localhost", connector.getLocalPort());
    writeHTTPRequest(socket, servletPath);
    BufferedReader reader = readAndDiscardHTTPResponse(socket);
    Assert.assertTrue(emitterLatch.await(1, TimeUnit.SECONDS));
    EventSource.Emitter emitter = emitterRef.get();
    Assert.assertNotNull(emitter);
    String comment = "foo";
    emitter.comment(comment);
    String line = reader.readLine();
    String received = "";
    while (line != null) {
        received += line;
        if (line.length() == 0)
            break;
        line = reader.readLine();
    }
    Assert.assertEquals(": " + comment, received);
    emitter.close();
    line = reader.readLine();
    Assert.assertNull(line);
    socket.close();
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) Test(org.junit.Test)

Example 50 with ServletHolder

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

the class EventSourceServletTest method testMultiLineData.

@Test
public void testMultiLineData() throws Exception {
    String data1 = "data1";
    String data2 = "data2";
    String data3 = "data3";
    String data4 = "data4";
    final String data = data1 + "\r\n" + data2 + "\r" + data3 + "\n" + data4;
    class S extends EventSourceServlet {

        @Override
        protected EventSource newEventSource(HttpServletRequest request) {
            return new EventSource() {

                public void onOpen(Emitter emitter) throws IOException {
                    emitter.data(data);
                }

                public void onClose() {
                }
            };
        }
    }
    String servletPath = "/eventsource";
    context.addServlet(new ServletHolder(new S()), servletPath);
    Socket socket = new Socket("localhost", connector.getLocalPort());
    writeHTTPRequest(socket, servletPath);
    BufferedReader reader = readAndDiscardHTTPResponse(socket);
    String line1 = reader.readLine();
    Assert.assertEquals("data: " + data1, line1);
    String line2 = reader.readLine();
    Assert.assertEquals("data: " + data2, line2);
    String line3 = reader.readLine();
    Assert.assertEquals("data: " + data3, line3);
    String line4 = reader.readLine();
    Assert.assertEquals("data: " + data4, line4);
    String line5 = reader.readLine();
    Assert.assertEquals(0, line5.length());
    socket.close();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

ServletHolder (org.eclipse.jetty.servlet.ServletHolder)287 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)158 Server (org.eclipse.jetty.server.Server)111 Test (org.junit.Test)77 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)46 ServerConnector (org.eclipse.jetty.server.ServerConnector)45 HttpServletRequest (javax.servlet.http.HttpServletRequest)38 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)25 HttpClient (org.eclipse.jetty.client.HttpClient)23 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)23 IOException (java.io.IOException)22 CountDownLatch (java.util.concurrent.CountDownLatch)22 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)22 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)19 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)18 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 BeforeClass (org.junit.BeforeClass)17 File (java.io.File)15 ServletException (javax.servlet.ServletException)15