Search in sources :

Example 41 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project graphql-java by graphql-java.

the class HttpMain method main.

public static void main(String[] args) throws Exception {
    // 
    // This example uses Jetty as an embedded HTTP server
    Server server = new Server(PORT);
    // 
    // In Jetty, handlers are how your get called backed on a request
    HttpMain main_handler = new HttpMain();
    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(false);
    resource_handler.setWelcomeFiles(new String[] { "index.html" });
    resource_handler.setResourceBase("./src/test/resources/httpmain");
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, main_handler });
    server.setHandler(handlers);
    server.start();
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler)

Example 42 with ResourceHandler

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

the class AngularAndWebDriverTest method before_suite.

@BeforeSuite
public void before_suite() throws Exception {
    // Launch Protractor's own test app on http://localhost:8080
    ((StdErrLog) Log.getRootLogger()).setLevel(StdErrLog.LEVEL_OFF);
    webServer = new Server(new QueuedThreadPool(6));
    ServerConnector connector = new ServerConnector(webServer, new HttpConnectionFactory());
    connector.setPort(8080);
    webServer.addConnector(connector);
    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[] { "index.html" });
    resource_handler.setResourceBase("src/test/webapp");
    HandlerList handlers = new HandlerList();
    MovedContextHandler effective_symlink = new MovedContextHandler(webServer, "/lib/angular", "/lib/angular_v1.2.9");
    handlers.setHandlers(new Handler[] { effective_symlink, resource_handler, new DefaultHandler() });
    webServer.setHandler(handlers);
    webServer.start();
    setupBrowser("chrome");
    driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
    ngWebDriver = new NgWebDriver(driver);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HandlerList(org.eclipse.jetty.server.handler.HandlerList) StdErrLog(org.eclipse.jetty.util.log.StdErrLog) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) MovedContextHandler(org.eclipse.jetty.server.handler.MovedContextHandler) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) BeforeSuite(org.testng.annotations.BeforeSuite)

Example 43 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 44 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 45 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)

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