Search in sources :

Example 1 with ContextHandler

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

the class WeldDeploymentBinding method processBinding.

public void processBinding(Node node, App app) throws Exception {
    ContextHandler handler = app.getContextHandler();
    if (handler == null) {
        throw new NullPointerException("No Handler created for App: " + app);
    }
    if (handler instanceof WebAppContext) {
        // Do webapp specific init
        WebAppContext webapp = (WebAppContext) handler;
        JettyWeldInitializer.initWebApp(webapp);
    } else {
        // Do general init
        JettyWeldInitializer.initContext(handler);
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext)

Example 2 with ContextHandler

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

the class WebSocketCdiInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException {
    ContextHandler handler = ContextHandler.getContextHandler(context);
    if (handler == null) {
        throw new ServletException("Not running on Jetty, WebSocket+CDI support unavailable");
    }
    if (!(handler instanceof ServletContextHandler)) {
        throw new ServletException("Not running in Jetty ServletContextHandler, WebSocket+CDI support unavailable");
    }
    ServletContextHandler jettyContext = (ServletContextHandler) handler;
    try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(context.getClassLoader())) {
        addListeners(jettyContext);
    }
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletException(javax.servlet.ServletException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ThreadClassLoaderScope(org.eclipse.jetty.util.thread.ThreadClassLoaderScope)

Example 3 with ContextHandler

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

the class JettyHttpServer method checkIfContextIsFree.

private void checkIfContextIsFree(String path) {
    Handler serverHandler = _server.getHandler();
    if (serverHandler instanceof ContextHandler) {
        ContextHandler ctx = (ContextHandler) serverHandler;
        if (ctx.getContextPath().equals(path))
            throw new RuntimeException("another context already bound to path " + path);
    }
    Handler[] handlers = _server.getHandlers();
    if (handlers == null)
        return;
    for (Handler handler : handlers) {
        if (handler instanceof ContextHandler) {
            ContextHandler ctx = (ContextHandler) handler;
            if (ctx.getContextPath().equals(path))
                throw new RuntimeException("another context already bound to path " + path);
        }
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Handler(org.eclipse.jetty.server.Handler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpHandler(com.sun.net.httpserver.HttpHandler)

Example 4 with ContextHandler

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

the class RequestTest method testMultiPart.

@Test
public void testMultiPart() throws Exception {
    final File testTmpDir = File.createTempFile("reqtest", null);
    if (testTmpDir.exists())
        testTmpDir.delete();
    testTmpDir.mkdir();
    testTmpDir.deleteOnExit();
    assertTrue(testTmpDir.list().length == 0);
    ContextHandler contextHandler = new ContextHandler();
    contextHandler.setContextPath("/foo");
    contextHandler.setResourceBase(".");
    contextHandler.setHandler(new MultiPartRequestHandler(testTmpDir));
    contextHandler.addEventListener(new MultiPartCleanerListener() {

        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            MultiPartInputStreamParser m = (MultiPartInputStreamParser) sre.getServletRequest().getAttribute(Request.__MULTIPART_INPUT_STREAM);
            ContextHandler.Context c = (ContextHandler.Context) sre.getServletRequest().getAttribute(Request.__MULTIPART_CONTEXT);
            assertNotNull(m);
            assertNotNull(c);
            assertTrue(c == sre.getServletContext());
            assertTrue(!m.getParsedParts().isEmpty());
            assertTrue(testTmpDir.list().length == 2);
            super.requestDestroyed(sre);
            String[] files = testTmpDir.list();
            assertTrue(files.length == 0);
        }
    });
    _server.stop();
    _server.setHandler(contextHandler);
    _server.start();
    String multipart = "--AaB03x\r\n" + "content-disposition: form-data; name=\"field1\"\r\n" + "\r\n" + "Joe Blow\r\n" + "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"foo.upload\"\r\n" + "Content-Type: text/plain;charset=ISO-8859-1\r\n" + "\r\n" + "000000000000000000000000000000000000000000000000000\r\n" + "--AaB03x--\r\n";
    String request = "GET /foo/x.html HTTP/1.1\r\n" + "Host: whatever\r\n" + "Content-Type: multipart/form-data; boundary=\"AaB03x\"\r\n" + "Content-Length: " + multipart.getBytes().length + "\r\n" + "Connection: close\r\n" + "\r\n" + multipart;
    String responses = _connector.getResponse(request);
    // System.err.println(responses);
    assertTrue(responses.startsWith("HTTP/1.1 200"));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletRequestEvent(javax.servlet.ServletRequestEvent) MultiPartInputStreamParser(org.eclipse.jetty.util.MultiPartInputStreamParser) File(java.io.File) Test(org.junit.Test)

Example 5 with ContextHandler

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

the class RequestTest method testInvalidHostHeader.

@Test
public void testInvalidHostHeader() throws Exception {
    // Use a contextHandler with vhosts to force call to Request.getServerName()
    ContextHandler context = new ContextHandler();
    context.addVirtualHosts(new String[] { "something" });
    _server.stop();
    _server.setHandler(context);
    _server.start();
    // Request with illegal Host header
    String request = "GET / HTTP/1.1\n" + "Host: whatever.com:xxxx\n" + "Content-Type: text/html;charset=utf8\n" + "Connection: close\n" + "\n";
    String responses = _connector.getResponse(request);
    assertThat(responses, Matchers.startsWith("HTTP/1.1 400"));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Test(org.junit.Test)

Aggregations

ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)127 Server (org.eclipse.jetty.server.Server)47 ServerConnector (org.eclipse.jetty.server.ServerConnector)27 URI (java.net.URI)21 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)20 Test (org.junit.Test)20 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)19 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)19 IOException (java.io.IOException)18 Handler (org.eclipse.jetty.server.Handler)14 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)14 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)14 File (java.io.File)13 ServletException (javax.servlet.ServletException)13 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)13 BeforeClass (org.junit.BeforeClass)11 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)10 Config (com.ctrip.framework.apollo.Config)10 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)10 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)10