Search in sources :

Example 31 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project killbill by killbill.

the class HttpServer method configure.

public void configure(final HttpServerConfig config, final Iterable<EventListener> eventListeners, final Map<FilterHolder, String> filterHolders) {
    server.setStopAtShutdown(true);
    // Setup JMX
    configureJMX(ManagementFactory.getPlatformMBeanServer());
    // HTTP Configuration
    final HttpConfiguration httpConfiguration = new HttpConfiguration();
    httpConfiguration.setSecurePort(config.getServerSslPort());
    // Configure main connector
    final ServerConnector http = configureMainConnector(httpConfiguration, config.isJettyStatsOn(), config.getServerHost(), config.getServerPort());
    // Configure SSL, if enabled
    final ServerConnector https;
    if (config.isSSLEnabled()) {
        https = configureSslConnector(httpConfiguration, config.isJettyStatsOn(), config.getServerSslPort(), config.getSSLkeystoreLocation(), config.getSSLkeystorePassword());
        server.setConnectors(new Connector[] { http, https });
    } else {
        server.setConnectors(new Connector[] { http });
    }
    // Configure the thread pool
    configureThreadPool(config);
    // Configure handlers
    final HandlerCollection handlers = new HandlerCollection();
    final ServletContextHandler servletContextHandler = createServletContextHandler(config.getResourceBase(), eventListeners, filterHolders);
    handlers.addHandler(servletContextHandler);
    final RequestLogHandler logHandler = createLogHandler(config);
    handlers.addHandler(logHandler);
    final HandlerList rootHandlers = new HandlerList();
    rootHandlers.addHandler(handlers);
    server.setHandler(rootHandlers);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HandlerList(org.eclipse.jetty.server.handler.HandlerList) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 32 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project neo4j by neo4j.

the class Jetty9WebServer method start.

@Override
public void start() throws Exception {
    if (jetty == null) {
        JettyThreadCalculator jettyThreadCalculator = new JettyThreadCalculator(jettyMaxThreads);
        jetty = new Server(createQueuedThreadPool(jettyThreadCalculator));
        jetty.addConnector(connectorFactory.createConnector(jetty, jettyAddress, jettyThreadCalculator));
        jettyHttpsAddress.ifPresent((address) -> {
            if (httpsCertificateInformation == null) {
                throw new RuntimeException("HTTPS set to enabled, but no HTTPS configuration provided");
            }
            jetty.addConnector(sslSocketFactory.createConnector(jetty, httpsCertificateInformation, address, jettyThreadCalculator));
        });
        if (jettyCreatedCallback != null) {
            jettyCreatedCallback.accept(jetty);
        }
    }
    handlers = new HandlerList();
    jetty.setHandler(handlers);
    handlers.addHandler(new MovedContextHandler());
    loadAllMounts();
    if (requestLog != null) {
        loadRequestLogging();
    }
    startJetty();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) MovedContextHandler(org.eclipse.jetty.server.handler.MovedContextHandler)

Example 33 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList 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 34 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project alluxio by Alluxio.

the class WebServer method addHandler.

/**
   * Adds a handler.
   *
   * @param handler the handler to add
   */
public void addHandler(AbstractHandler handler) {
    HandlerList handlers = new HandlerList();
    handlers.addHandler(handler);
    for (Handler h : mServer.getHandlers()) {
        handlers.addHandler(h);
    }
    mServer.setHandler(handlers);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 35 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project Anserini by castorini.

the class TweetSearcher method main.

public static void main(String[] args) throws IOException, InterruptedException, ParseException {
    Options options = new Options();
    options.addOption(INDEX_OPTION, true, "index path");
    options.addOption(PORT_OPTION, true, "port");
    CommandLine cmdline = null;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (org.apache.commons.cli.ParseException e) {
        System.err.println("Error parsing command line: " + e.getMessage());
        System.exit(-1);
    }
    if (!cmdline.hasOption(INDEX_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(TweetSearcher.class.getName(), options);
        System.exit(-1);
    }
    int port = cmdline.hasOption(PORT_OPTION) ? Integer.parseInt(cmdline.getOptionValue(PORT_OPTION)) : 8080;
    TweetSearcher nrtsearch = new TweetSearcher(cmdline.getOptionValue(INDEX_OPTION));
    TweetStreamIndexer its = new TweetStreamIndexer();
    Thread itsThread = new Thread(its);
    itsThread.start();
    LOG.info("Starting HTTP server on port " + port);
    HandlerList mainHandler = new HandlerList();
    Server server = new Server(port);
    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setWelcomeFiles(new String[] { "index.html" });
    resource_handler.setResourceBase("src/main/java/io/anserini/nrts/public");
    ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    handler.setContextPath("/");
    ServletHolder jerseyServlet = new ServletHolder(ServletContainer.class);
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", TweetSearcherAPI.class.getCanonicalName());
    handler.addServlet(jerseyServlet, "/*");
    mainHandler.addHandler(resource_handler);
    mainHandler.addHandler(handler);
    server.setHandler(mainHandler);
    try {
        server.start();
        LOG.info("Accepting connections on port " + port);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    server.join();
    itsThread.join();
    nrtsearch.close();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Options(org.apache.commons.cli.Options) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) GnuParser(org.apache.commons.cli.GnuParser) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ParseException(org.apache.lucene.queryparser.classic.ParseException) IOException(java.io.IOException) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

HandlerList (org.eclipse.jetty.server.handler.HandlerList)35 Server (org.eclipse.jetty.server.Server)19 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)17 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)13 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)11 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)8 Handler (org.eclipse.jetty.server.Handler)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 ServerConnector (org.eclipse.jetty.server.ServerConnector)5 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 File (java.io.File)3 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 ServletException (javax.servlet.ServletException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)3 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)3