Search in sources :

Example 11 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project winery by eclipse.

the class WineryUsingHttpServer method createHttpServerForRepositoryUi.

/**
 * Starts the repository UI on port {@value #REPOSITORY_UI_PORT}
 */
public static Server createHttpServerForRepositoryUi() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(REPOSITORY_UI_PORT);
    server.setConnectors(new Connector[] { connector });
    ResourceHandler rh0 = new ResourceHandler();
    ContextHandler context0 = new ContextHandler();
    context0.setContextPath("/");
    // Path indexHtmlPath = MavenTestingUtils.getProjectFilePath("../org.eclipse.winery.repository.ui/dist/index.html").getParent();
    Path indexHtmlPath = MavenTestingUtils.getBasePath().resolve("org.eclipse.winery.repository.ui").resolve("dist");
    if (Files.exists(indexHtmlPath)) {
        LOGGER.debug("Serving UI from " + indexHtmlPath.toString());
    } else {
        // not sure, why we sometimes have to use `getParent()`.
        indexHtmlPath = MavenTestingUtils.getBasePath().getParent().resolve("org.eclipse.winery.repository.ui").resolve("dist");
        if (Files.exists(indexHtmlPath)) {
            LOGGER.debug("Serving UI from " + indexHtmlPath.toString());
        } else {
            LOGGER.error("Path does not exist " + indexHtmlPath);
        }
    }
    context0.setBaseResource(Resource.newResource(indexHtmlPath.toFile()));
    context0.setHandler(rh0);
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(new Handler[] { context0 });
    server.setHandler(contexts);
    return server;
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection)

Example 12 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project pf4j-update by pf4j.

the class WebServer method start.

public void start() throws Exception {
    Server server = new Server();
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(port);
    server.addConnector(connector);
    server.setStopAtShutdown(true);
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase(resourceBase);
    resourceHandler.setDirectoriesListed(true);
    // server.setHandler(contextHandler);
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resourceHandler, new DefaultHandler() });
    server.setHandler(handlers);
    server.start();
// server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Server(org.eclipse.jetty.server.Server) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 13 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project gitiles by GerritCodeReview.

the class DevServer method staticHandler.

private Handler staticHandler() throws IOException {
    Path staticRoot = sourceRoot.resolve("resources/com/google/gitiles/static");
    ResourceHandler rh = new ResourceHandler();
    try {
        rh.setBaseResource(new PathResource(staticRoot.toUri().toURL()));
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
    rh.setWelcomeFiles(new String[] {});
    rh.setDirectoriesListed(false);
    ContextHandler handler = new ContextHandler("/+static");
    handler.setHandler(rh);
    return handler;
}
Also used : Path(java.nio.file.Path) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) PathResource(org.eclipse.jetty.util.resource.PathResource) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 14 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project traccar by traccar.

the class WebServer method initWebApp.

private void initWebApp() {
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase(config.getString("web.path"));
    if (config.getBoolean("web.debug")) {
        resourceHandler.setWelcomeFiles(new String[] { "debug.html", "index.html" });
        // avoid locking files on Windows
        resourceHandler.setMinMemoryMappedContentLength(-1);
    } else {
        String cache = config.getString("web.cacheControl");
        if (cache != null && !cache.isEmpty()) {
            resourceHandler.setCacheControl(cache);
        }
        resourceHandler.setWelcomeFiles(new String[] { "release.html", "index.html" });
    }
    handlers.addHandler(resourceHandler);
}
Also used : ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler)

Example 15 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project Zong by Xenoage.

the class Webserver method enableWebServer.

private void enableWebServer() {
    if (server.isRunning())
        throw new IllegalStateException("Webserver can not be enabled while running");
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setWelcomeFiles(new String[] { "index.html" });
    resourceHandler.setResourceBase(webPath);
    // cache 10 minutes
    resourceHandler.setCacheControl("max-age=600,public");
    ContextHandler contentHandler = new ContextHandler();
    contentHandler.setContextPath("/");
    contentHandler.setResourceBase(".");
    contentHandler.setClassLoader(Thread.currentThread().getContextClassLoader());
    contentHandler.setHandler(resourceHandler);
    handlers.add(contentHandler);
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler)

Aggregations

ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)53 Server (org.eclipse.jetty.server.Server)31 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)20 HandlerList (org.eclipse.jetty.server.handler.HandlerList)20 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)16 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)15 ServerConnector (org.eclipse.jetty.server.ServerConnector)14 File (java.io.File)13 IOException (java.io.IOException)8 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)7 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)7 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)6 HashLoginService (org.eclipse.jetty.security.HashLoginService)5 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)5 Constraint (org.eclipse.jetty.util.security.Constraint)5 ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)4 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)4 Handler (org.eclipse.jetty.server.Handler)4 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)4 Path (java.nio.file.Path)3