Search in sources :

Example 31 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 32 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)

Example 33 with ServletHolder

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

the class ConcatServletTest method testConcatenation.

@Test
public void testConcatenation() throws Exception {
    String contextPath = "";
    ServletContextHandler context = new ServletContextHandler(server, contextPath);
    server.setHandler(context);
    String concatPath = "/concat";
    context.addServlet(ConcatServlet.class, concatPath);
    ServletHolder resourceServletHolder = new ServletHolder(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String includedURI = (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
            response.getOutputStream().println(includedURI);
        }
    });
    context.addServlet(resourceServletHolder, "/resource/*");
    server.start();
    String resource1 = "/resource/one.js";
    String resource2 = "/resource/two.js";
    String uri = contextPath + concatPath + "?" + resource1 + "&" + resource2;
    String request = "" + "GET " + uri + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
    String response = connector.getResponses(request);
    try (BufferedReader reader = new BufferedReader(new StringReader(response))) {
        while (true) {
            String line = reader.readLine();
            if (line == null)
                Assert.fail();
            if (line.trim().isEmpty())
                break;
        }
        Assert.assertEquals(resource1, reader.readLine());
        Assert.assertEquals(resource2, reader.readLine());
        Assert.assertNull(reader.readLine());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServlet(javax.servlet.http.HttpServlet) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 34 with ServletHolder

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

the class CrossOriginFilterTest method testNonSimpleRequestWithoutPreflight.

@Test
public void testNonSimpleRequestWithoutPreflight() throws Exception {
    // We cannot know if an actual request has performed the preflight before:
    // we'll trust browsers to do it right, so responses to actual requests
    // will contain the CORS response headers.
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String request = "" + "PUT / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "Origin: http://localhost\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 35 with ServletHolder

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

the class CrossOriginFilterTest method testSimpleRequestWithMatchingOriginAndNonMatchingTimingOrigin.

@Test
public void testSimpleRequestWithMatchingOriginAndNonMatchingTimingOrigin() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    String origin = "http://localhost";
    String timingOrigin = "http://127.0.0.1";
    filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, origin);
    filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_TIMING_ORIGINS_PARAM, timingOrigin);
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertFalse(response.contains(CrossOriginFilter.TIMING_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains("Vary"));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ServletHolder (org.eclipse.jetty.servlet.ServletHolder)473 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)255 Server (org.eclipse.jetty.server.Server)177 Test (org.junit.Test)81 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)74 ServerConnector (org.eclipse.jetty.server.ServerConnector)67 HttpServletRequest (javax.servlet.http.HttpServletRequest)47 IOException (java.io.IOException)44 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)36 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)33 File (java.io.File)32 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)32 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)30 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)27 HttpServletResponse (javax.servlet.http.HttpServletResponse)26 HttpClient (org.eclipse.jetty.client.HttpClient)24 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 ServletException (javax.servlet.ServletException)23 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)23