Search in sources :

Example 26 with ContextHandler

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

the class DispatcherTest method init.

@Before
public void init() throws Exception {
    _server = new Server();
    _connector = new LocalConnector(_server);
    _connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendServerVersion(false);
    _connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendDateHeader(false);
    _contextCollection = new ContextHandlerCollection();
    _contextHandler = new ServletContextHandler();
    _contextHandler.setContextPath("/context");
    _contextCollection.addHandler(_contextHandler);
    _resourceHandler = new ResourceHandler();
    _resourceHandler.setResourceBase(MavenTestingUtils.getTestResourceDir("dispatchResourceTest").getAbsolutePath());
    _resourceHandler.setPathInfoOnly(true);
    ContextHandler resourceContextHandler = new ContextHandler("/resource");
    resourceContextHandler.setHandler(_resourceHandler);
    _contextCollection.addHandler(resourceContextHandler);
    _server.setHandler(_contextCollection);
    _server.addConnector(_connector);
    _server.start();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) Before(org.junit.Before)

Example 27 with ContextHandler

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

the class WebServerTest method testCreateHandlersCoversExpectedContextPaths.

@Test
public void testCreateHandlersCoversExpectedContextPaths() {
    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    WebServer webServer = new WebServer(/* port */
    9999, projectFilesystem, "/static/", ObjectMappers.newDefaultInstance());
    ImmutableList<ContextHandler> handlers = webServer.createHandlers();
    final Map<String, ContextHandler> contextPathToHandler = Maps.newHashMap();
    for (ContextHandler handler : handlers) {
        contextPathToHandler.put(handler.getContextPath(), handler);
    }
    Function<String, TemplateHandlerDelegate> getDelegate = contextPath -> ((TemplateHandler) contextPathToHandler.get(contextPath).getHandler()).getDelegate();
    assertTrue(getDelegate.apply("/") instanceof IndexHandlerDelegate);
    assertTrue(contextPathToHandler.get("/static").getHandler() instanceof ResourceHandler);
    assertTrue(getDelegate.apply("/trace") instanceof TraceHandlerDelegate);
    assertTrue(getDelegate.apply("/traces") instanceof TracesHandlerDelegate);
    assertTrue(contextPathToHandler.get("/tracedata").getHandler() instanceof TraceDataHandler);
}
Also used : ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) Function(com.google.common.base.Function) ImmutableList(com.google.common.collect.ImmutableList) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Map(java.util.Map) Assert.assertTrue(org.junit.Assert.assertTrue) ObjectMappers(com.facebook.buck.util.ObjectMappers) Test(org.junit.Test) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Maps(com.google.common.collect.Maps) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Test(org.junit.Test)

Example 28 with ContextHandler

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

the class HttpProcess method start.

@Override
public void start() {
    writeTimeToFile("startingAt");
    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setClassLoader(Thread.currentThread().getContextClassLoader());
    server.setHandler(context);
    context.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
            if ("/ping".equals(target)) {
                request.setHandled(true);
                httpServletResponse.getWriter().print("ping");
            } else if ("/restart".equals(target)) {
                writeTimeToFile("restartAskedAt");
                request.setHandled(true);
                processCommands.askForRestart();
                httpServletResponse.getWriter().print("ok");
            } else if ("/kill".equals(target)) {
                writeTimeToFile("killedAt");
                System.exit(0);
            }
        }
    });
    try {
        server.start();
    } catch (Exception e) {
        throw new IllegalStateException("Fail to start Jetty", e);
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 29 with ContextHandler

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

the class JettyServer method makeContext.

private Handler makeContext(final JettyEnv env, final Config cfg) {
    final Set<String> paths = new HashSet<>();
    for (URI u : listenURLs(cfg)) {
        String p = u.getPath();
        if (p == null || p.isEmpty()) {
            p = "/";
        }
        while (1 < p.length() && p.endsWith("/")) {
            p = p.substring(0, p.length() - 1);
        }
        paths.add(p);
    }
    final List<ContextHandler> all = new ArrayList<>();
    for (String path : paths) {
        all.add(makeContext(path, env, cfg));
    }
    if (all.size() == 1) {
        //
        return all.get(0);
    }
    // We have more than one path served out of this container so
    // combine them in a handler which supports dispatching to the
    // individual contexts.
    //
    final ContextHandlerCollection r = new ContextHandlerCollection();
    r.setHandlers(all.toArray(new Handler[0]));
    return r;
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ArrayList(java.util.ArrayList) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) URI(java.net.URI) HashSet(java.util.HashSet)

Example 30 with ContextHandler

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

the class BaseHttpTest method contextHandler.

protected ContextHandler contextHandler(String context, Handler handler) {
    ContextHandler contextHandler = new ContextHandler(context);
    contextHandler.setHandler(handler);
    return contextHandler;
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler)

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