Search in sources :

Example 61 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 62 with HandlerList

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

the class Main method main.

public static void main(String[] args) throws Exception {
    String configPort = "8080";
    if (args.length > 0) {
        configPort = args[0];
    }
    String port = System.getProperty("server.http.port", configPort);
    System.out.println("Server started on port: " + port);
    Server server = new Server();
    server.setConnectors(new Connector[] { createConnector(server, Integer.parseInt(port)) });
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
    context.setContextPath("/api");
    ServletHolder servletHolder = new ServletHolder(createJerseyServlet());
    servletHolder.setInitOrder(1);
    context.addServlet(servletHolder, "/*");
    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setServer(server);
    webAppContext.setContextPath("/");
    String resourceBase = System.getProperty("resourceBase");
    if (resourceBase != null) {
        webAppContext.setResourceBase(resourceBase);
    } else {
        webAppContext.setResourceBase(Main.class.getResource("/webapp").toExternalForm());
    }
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { context, webAppContext });
    server.setHandler(handlers);
    server.start();
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 63 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) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler)

Example 64 with HandlerList

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

the class TViewServer method doWork.

@Override
public int doWork(final List<String> args) {
    Server server = null;
    try {
        final List<File> samFiles = IOUtil.unrollFiles(args.stream().map(S -> new File(S)).collect(Collectors.toList()), ".bam");
        if (samFiles.isEmpty()) {
            LOG.error("No BAM file defined");
            return -1;
        }
        if (this.optionalReferenceFile == null) {
            LOG.warn("No reference file defined");
        } else {
            final SAMSequenceDictionary dict = SAMSequenceDictionaryExtractor.extractDictionary(this.optionalReferenceFile);
            if (dict == null || dict.isEmpty()) {
                LOG.error("Empty/No dict in " + this.optionalReferenceFile);
                return -1;
            }
            samFiles.forEach(F -> {
                final SAMSequenceDictionary samdict = SAMSequenceDictionaryExtractor.extractDictionary(F);
                if (!SequenceUtil.areSequenceDictionariesEqual(samdict, dict)) {
                    throw new JvarkitException.DictionariesAreNotTheSame(samdict, dict);
                }
            });
        }
        samFiles.forEach(F -> {
            IOUtil.assertFileIsReadable(F);
        });
        server = new Server(this.port);
        final HandlerList handlers = new HandlerList();
        handlers.addHandler(new SamViewHandler(samFiles));
        server.setHandler(handlers);
        LOG.info("Starting " + TViewServer.class.getName() + " on http://localhost:" + this.port);
        server.start();
        if (this.shutdownAferSeconds > 0) {
            final Server theServer = server;
            new java.util.Timer().schedule(new java.util.TimerTask() {

                @Override
                public void run() {
                    LOG.info("automatic shutdown after " + shutdownAferSeconds);
                    try {
                        theServer.stop();
                    } catch (final Throwable err2) {
                        LOG.error(err2);
                    }
                }
            }, 1000 * this.shutdownAferSeconds);
        }
        server.join();
        return 0;
    } catch (final Throwable err) {
        LOG.error(err);
        return -1;
    } finally {
        if (server != null) {
            server.destroy();
        }
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) File(java.io.File) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary)

Example 65 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project onebusaway-application-modules by camsys.

the class WebappBootstrapMain method run.

public static void run(URL warUrl, boolean consoleMode, String[] args) throws Exception {
    if (args.length == 0 || isHelp(args)) {
        BootstrapCommon.printUsage(WebappBootstrapMain.class, "usage-webapp.txt");
        System.exit(-1);
    }
    Options options = createOptions();
    Parser parser = new GnuParser();
    CommandLine cli = parser.parse(options, args);
    args = cli.getArgs();
    if (args.length != 1) {
        BootstrapCommon.printUsage(WebappBootstrapMain.class, "usage-webapp.txt");
        System.exit(-1);
    }
    String bundlePath = args[0];
    System.setProperty("bundlePath", bundlePath);
    int port = 8080;
    if (cli.hasOption(ARG_PORT))
        port = Integer.parseInt(cli.getOptionValue(ARG_PORT));
    Server server = new Server();
    SocketConnector connector = new SocketConnector();
    // Set some timeout options to make debugging easier.
    connector.setMaxIdleTime(1000 * 60 * 60);
    connector.setSoLingerTime(-1);
    connector.setPort(port);
    server.setConnectors(new Connector[] { connector });
    WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    // context.setWelcomeFiles(new String[] {"index.action"});
    context.setWar(warUrl.toExternalForm());
    // We store the command line object as a webapp context attribute so it can
    // be used by the context loader to inject additional beans as needed
    context.setAttribute(WebappCommon.COMMAND_LINE_CONTEXT_ATTRIBUTE, cli);
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { new WelcomeFileHandler(context), context });
    server.setHandler(handlers);
    /**
     * We specify a Jetty override descriptor to inject additional definitions
     * into our web.xml configuration. Specifically, we specify a
     * <context-param> to specify the "contextClass" that will be used to create
     * our Spring ApplicationContext. We create a custom version that will
     * inject additional beans based on command-line arguments.
     */
    context.addOverrideDescriptor("/WEB-INF/override-web.xml");
    if (cli.hasOption(WebappCommon.ARG_BUILD)) {
        System.setProperty("hibernate.hbm2ddl.auto", "update");
        System.setProperty("bundleCacheDir", bundlePath + "/cache");
        System.setProperty("gtfsPath", cli.getOptionValue(WebappCommon.ARG_GTFS_PATH));
        context.addOverrideDescriptor("/WEB-INF/builder-override-web.xml");
    }
    try {
        server.start();
        System.err.println("=============================================================");
        System.err.println("=");
        System.err.println("= Your OneBusAway instance has started.  Browse to:");
        System.err.println("=");
        System.err.println("= http://localhost:" + port + "/");
        System.err.println("=");
        System.err.println("= to see your instance in action.");
        if (consoleMode) {
            System.err.println("=");
            System.err.println("= When you are finished, press return to exit gracefully...");
        }
        System.err.println("=============================================================");
        if (consoleMode) {
            System.in.read();
            server.stop();
            server.join();
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(100);
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Options(org.apache.commons.cli.Options) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) CommandLine(org.apache.commons.cli.CommandLine) Server(org.eclipse.jetty.server.Server) GnuParser(org.apache.commons.cli.GnuParser) SocketConnector(org.eclipse.jetty.server.bio.SocketConnector) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Parser(org.apache.commons.cli.Parser) GnuParser(org.apache.commons.cli.GnuParser)

Aggregations

HandlerList (org.eclipse.jetty.server.handler.HandlerList)71 Server (org.eclipse.jetty.server.Server)44 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)32 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)26 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)21 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)20 ServerConnector (org.eclipse.jetty.server.ServerConnector)16 IOException (java.io.IOException)13 Handler (org.eclipse.jetty.server.Handler)12 File (java.io.File)11 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)10 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)10 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)8 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)8 ArrayList (java.util.ArrayList)7 ServletException (javax.servlet.ServletException)6 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)6 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)6 Test (org.junit.Test)6 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)5