Search in sources :

Example 46 with ResourceHandler

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

the class RunnerRun method run.

@Override
public void run(final File baseDir) throws IOException, ExecutorException {
    // Generate site:
    clean.run(baseDir);
    gen.run(baseDir);
    // Start monitoring service:
    startMonitoring(baseDir);
    // Start server:
    try {
        LOG.log(Level.INFO, "Starting HTTP server at port: {0}", cmd.port);
        Server server = new Server(cmd.port);
        ResourceHandler rh = new ResourceHandler();
        rh.setDirectoriesListed(true);
        rh.setWelcomeFiles(new String[] { "index.html" });
        rh.setResourceBase(Constants.getOutDir(baseDir).getPath());
        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { rh, new DefaultHandler() });
        server.setHandler(handlers);
        server.start();
        server.join();
    } catch (Exception ex) {
        throw new ExecutorException(ex);
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) IOException(java.io.IOException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 47 with ResourceHandler

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

the class EventHubHandler method main.

public static void main(String[] args) throws Exception {
    Properties properties = new Properties();
    properties.load(EventHub.class.getClassLoader().getResourceAsStream("hub.properties"));
    properties.load(EventHubHandler.class.getClassLoader().getResourceAsStream("web.properties"));
    properties.putAll(System.getProperties());
    Injector injector = Guice.createInjector(Modules.override(new DmaIdListModule(), new DatedEventIndexModule(), new ShardedEventIndexModule(), new PropertiesIndexModule(), new UserEventIndexModule(), new EventStorageModule(), new UserStorageModule(), new EventHubModule(properties)).with(new Module()));
    final EventHubHandler eventHubHandler = injector.getInstance(EventHubHandler.class);
    int port = injector.getInstance(Key.get(Integer.class, Names.named("eventhubhandler.port")));
    final Server server = new Server(port);
    @SuppressWarnings("ConstantConditions") String webDir = EventHubHandler.class.getClassLoader().getResource("frontend").toExternalForm();
    HashLoginService loginService = new HashLoginService();
    loginService.putUser(properties.getProperty("eventhubhandler.username"), new Password(properties.getProperty("eventhubhandler.password")), new String[] { "user" });
    server.addBean(loginService);
    ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
    Constraint constraint = new Constraint();
    constraint.setName("auth");
    constraint.setAuthenticate(true);
    constraint.setRoles(new String[] { "user", "admin" });
    ConstraintMapping mapping = new ConstraintMapping();
    mapping.setPathSpec("/*");
    mapping.setConstraint(constraint);
    securityHandler.setConstraintMappings(Collections.singletonList(mapping));
    securityHandler.setAuthenticator(new BasicAuthenticator());
    securityHandler.setLoginService(loginService);
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(false);
    resourceHandler.setWelcomeFiles(new String[] { "main.html" });
    resourceHandler.setResourceBase(webDir);
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { new JsonpCallbackHandler(eventHubHandler), securityHandler });
    server.setHandler(handlers);
    securityHandler.setHandler(resourceHandler);
    server.start();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            if (server.isStarted()) {
                try {
                    server.stop();
                    eventHubHandler.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }, "Stop Jetty Hook"));
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) UserEventIndexModule(com.codecademy.eventhub.index.UserEventIndexModule) Server(org.eclipse.jetty.server.Server) Constraint(org.eclipse.jetty.util.security.Constraint) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) Properties(java.util.Properties) HashLoginService(org.eclipse.jetty.security.HashLoginService) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Injector(com.google.inject.Injector) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) DatedEventIndexModule(com.codecademy.eventhub.index.DatedEventIndexModule) Password(org.eclipse.jetty.util.security.Password) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) PropertiesIndexModule(com.codecademy.eventhub.index.PropertiesIndexModule) EventHubModule(com.codecademy.eventhub.EventHubModule) Constraint(org.eclipse.jetty.util.security.Constraint) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) DmaIdListModule(com.codecademy.eventhub.list.DmaIdListModule) ShardedEventIndexModule(com.codecademy.eventhub.index.ShardedEventIndexModule) EventStorageModule(com.codecademy.eventhub.storage.EventStorageModule) UserStorageModule(com.codecademy.eventhub.storage.UserStorageModule) DatedEventIndexModule(com.codecademy.eventhub.index.DatedEventIndexModule) UserEventIndexModule(com.codecademy.eventhub.index.UserEventIndexModule) PropertiesIndexModule(com.codecademy.eventhub.index.PropertiesIndexModule) EventHubModule(com.codecademy.eventhub.EventHubModule) DmaIdListModule(com.codecademy.eventhub.list.DmaIdListModule) UserStorageModule(com.codecademy.eventhub.storage.UserStorageModule) ShardedEventIndexModule(com.codecademy.eventhub.index.ShardedEventIndexModule) EventStorageModule(com.codecademy.eventhub.storage.EventStorageModule)

Example 48 with ResourceHandler

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

the class FileServer method start.

public int start() {
    final SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(0);
    server.addConnector(connector);
    final HandlerCollection handlers = new HandlerCollection();
    final ResourceHandler resourceHandler = new ResourceHandler() {

        @Override
        protected void doResponseHeaders(final HttpServletResponse response, final Resource resource, final String mimeType) {
            response.addDateHeader("Expires", 0L);
        }
    };
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setResourceBase(resourceBase);
    handlers.addHandler(resourceHandler);
    server.setHandler(handlers);
    try {
        server.start();
        logger.info("File server started on port {}", connector.getLocalPort());
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
    return connector.getLocalPort();
}
Also used : SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Resource(org.eclipse.jetty.util.resource.Resource) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler)

Example 49 with ResourceHandler

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

the class TerminalThread method run.

public void run() {
    ServerConnector connector = new ServerConnector(jettyServer);
    connector.setPort(port);
    jettyServer.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/terminal/");
    // We look for a file, as ClassLoader.getResource() is not
    // designed to look for directories (we resolve the directory later)
    ClassLoader clazz = TerminalThread.class.getClassLoader();
    URL url = clazz.getResource("html");
    if (url == null) {
        throw new RuntimeException("Unable to find resource directory");
    }
    ResourceHandler resourceHandler = new ResourceHandler();
    // Resolve file to directory
    String webRootUri = url.toExternalForm();
    LOGGER.info("WebRoot is " + webRootUri);
    // debug
    // webRootUri = "/home/hadoop/zeppelin-current/interpreter/sh";
    resourceHandler.setResourceBase(webRootUri);
    HandlerCollection handlers = new HandlerCollection(context, resourceHandler);
    jettyServer.setHandler(handlers);
    try {
        ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
        container.addEndpoint(TerminalSocket.class);
        jettyServer.start();
        jettyServer.join();
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) URL(java.net.URL) ServerContainer(javax.websocket.server.ServerContainer)

Example 50 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project gravel by gravel-st.

the class StartJetty method main.

public static void main(String[] args) throws Exception {
    File fn;
    int port = 8080;
    if (args.length == 0) {
        fn = ImageBootstrapper.defaultSourceFolder();
    } else {
        fn = new File(args[0]);
        if (args.length != 1) {
            port = Integer.parseInt(args[1]);
        }
    }
    ImageBootstrapper.bootstrap(fn);
    Server server = new Server(port);
    ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContext.setContextPath("/browser");
    ResourceHandler staticFilesHandler = new ResourceHandler();
    staticFilesHandler.setResourceBase("src/main/html");
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { servletContext, staticFilesHandler, new DefaultHandler() });
    Object stServlet = getStServlet();
    servletContext.addServlet(new ServletHolder(new JettyToStHttpServletConverter(stServlet)), "/*");
    server.setHandler(handlers);
    server.start();
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

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)16 ServerConnector (org.eclipse.jetty.server.ServerConnector)14 File (java.io.File)13 IOException (java.io.IOException)7 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)7 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)7 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)7 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