Search in sources :

Example 1 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.

the class TestJNDI method testThreadContextClassloaderAndCurrentContext.

@Test
public void testThreadContextClassloaderAndCurrentContext() throws Exception {
    //create a jetty context, and start it so that its classloader it created
    //and it is the current context
    ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
    ContextHandler ch = new ContextHandler();
    URLClassLoader chLoader = new URLClassLoader(new URL[0], currentLoader);
    ch.setClassLoader(chLoader);
    Server server = new Server();
    HandlerList hl = new HandlerList();
    server.setHandler(hl);
    hl.addHandler(ch);
    //Create another one
    ContextHandler ch2 = new ContextHandler();
    URLClassLoader ch2Loader = new URLClassLoader(new URL[0], currentLoader);
    ch2.setClassLoader(ch2Loader);
    hl.addHandler(ch2);
    try {
        ch.setContextPath("/ch");
        ch.addEventListener(new ServletContextListener() {

            private Context comp;

            private Object testObj = new Object();

            public void contextInitialized(ServletContextEvent sce) {
                try {
                    InitialContext initCtx = new InitialContext();
                    Context java = (Context) initCtx.lookup("java:");
                    assertNotNull(java);
                    comp = (Context) initCtx.lookup("java:comp");
                    assertNotNull(comp);
                    Context env = ((Context) comp).createSubcontext("env");
                    assertNotNull(env);
                    env.bind("ch", testObj);
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }

            public void contextDestroyed(ServletContextEvent sce) {
                try {
                    assertNotNull(comp);
                    assertEquals(testObj, comp.lookup("env/ch"));
                    comp.destroySubcontext("env");
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        });
        //Starting the context makes it current and creates a classloader for it
        ch.start();
        ch2.setContextPath("/ch2");
        ch2.addEventListener(new ServletContextListener() {

            private Context comp;

            private Object testObj = new Object();

            public void contextInitialized(ServletContextEvent sce) {
                try {
                    InitialContext initCtx = new InitialContext();
                    comp = (Context) initCtx.lookup("java:comp");
                    assertNotNull(comp);
                    //another context's bindings should not be visible
                    Context env = ((Context) comp).createSubcontext("env");
                    try {
                        env.lookup("ch");
                        fail("java:comp/env visible from another context!");
                    } catch (NameNotFoundException e) {
                    //expected
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }

            public void contextDestroyed(ServletContextEvent sce) {
                try {
                    assertNotNull(comp);
                    comp.destroySubcontext("env");
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        });
        //make the new context the current one
        ch2.start();
    } finally {
        ch.stop();
        ch2.stop();
        Thread.currentThread().setContextClassLoader(currentLoader);
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) Server(org.eclipse.jetty.server.Server) ServletContextListener(javax.servlet.ServletContextListener) NameNotFoundException(javax.naming.NameNotFoundException) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 2 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.

the class ServerConnectorTest method testReuseAddress_Default.

@Test
public void testReuseAddress_Default() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.addConnector(connector);
    HandlerList handlers = new HandlerList();
    handlers.addHandler(new ReuseInfoHandler());
    handlers.addHandler(new DefaultHandler());
    server.setHandler(handlers);
    try {
        server.start();
        URI uri = toServerURI(connector);
        String response = getResponse(uri);
        assertThat("Response", response, containsString("connector.getReuseAddress() = true"));
        assertThat("Response", response, containsString("connector._reuseAddress() = true"));
        // Java on Windows is incapable of propagating reuse-address this to the opened socket.
        if (!OS.IS_WINDOWS) {
            assertThat("Response", response, containsString("socket.getReuseAddress() = true"));
        }
    } finally {
        server.stop();
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) Test(org.junit.Test)

Example 3 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.

the class ServerConnectorTest method testReuseAddress_True.

@Test
public void testReuseAddress_True() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    connector.setReuseAddress(true);
    server.addConnector(connector);
    HandlerList handlers = new HandlerList();
    handlers.addHandler(new ReuseInfoHandler());
    handlers.addHandler(new DefaultHandler());
    server.setHandler(handlers);
    try {
        server.start();
        URI uri = toServerURI(connector);
        String response = getResponse(uri);
        assertThat("Response", response, containsString("connector.getReuseAddress() = true"));
        assertThat("Response", response, containsString("connector._reuseAddress() = true"));
        // Java on Windows is incapable of propagating reuse-address this to the opened socket.
        if (!OS.IS_WINDOWS) {
            assertThat("Response", response, containsString("socket.getReuseAddress() = true"));
        }
    } finally {
        server.stop();
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) Test(org.junit.Test)

Example 4 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.

the class ServerConnectorTest method testReuseAddress_False.

@Test
public void testReuseAddress_False() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    connector.setReuseAddress(false);
    server.addConnector(connector);
    HandlerList handlers = new HandlerList();
    handlers.addHandler(new ReuseInfoHandler());
    handlers.addHandler(new DefaultHandler());
    server.setHandler(handlers);
    try {
        server.start();
        URI uri = toServerURI(connector);
        String response = getResponse(uri);
        assertThat("Response", response, containsString("connector.getReuseAddress() = false"));
        assertThat("Response", response, containsString("connector._reuseAddress() = false"));
        // Java on Windows is incapable of propagating reuse-address this to the opened socket.
        if (!OS.IS_WINDOWS) {
            assertThat("Response", response, containsString("socket.getReuseAddress() = false"));
        }
    } finally {
        server.stop();
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) Test(org.junit.Test)

Example 5 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.

the class WebAppContextTest method testNullPath.

@Test
public void testNullPath() throws Exception {
    Server server = new Server(0);
    HandlerList handlers = new HandlerList();
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    WebAppContext context = new WebAppContext();
    context.setBaseResource(Resource.newResource("./src/test/webapp"));
    context.setContextPath("/");
    server.setHandler(handlers);
    handlers.addHandler(contexts);
    contexts.addHandler(context);
    LocalConnector connector = new LocalConnector(server);
    server.addConnector(connector);
    server.start();
    try {
        String response = connector.getResponses("GET http://localhost:8080 HTTP/1.1\r\nHost: localhost:8080\r\nConnection: close\r\n\r\n");
        Assert.assertTrue(response.indexOf("200 OK") >= 0);
    } finally {
        server.stop();
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) Test(org.junit.Test)

Aggregations

HandlerList (org.eclipse.jetty.server.handler.HandlerList)71 Server (org.eclipse.jetty.server.Server)44 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)32 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)26 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)21 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)20 ServerConnector (org.eclipse.jetty.server.ServerConnector)16 IOException (java.io.IOException)13 Handler (org.eclipse.jetty.server.Handler)12 File (java.io.File)11 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)10 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)10 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)8 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)8 ArrayList (java.util.ArrayList)7 ServletException (javax.servlet.ServletException)6 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)6 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)6 Test (org.junit.Test)6 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)5