Search in sources :

Example 41 with HandlerList

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

the class AsyncContextDispatchWithQueryStrings method setUp.

@Before
public void setUp() throws Exception {
    _connector.setIdleTimeout(30000);
    _server.setConnectors(new Connector[] { _connector });
    _contextHandler.setContextPath("/");
    _contextHandler.addServlet(new ServletHolder(new TestServlet()), "/initialCall");
    _contextHandler.addServlet(new ServletHolder(new TestServlet()), "/firstDispatchWithNewQueryString");
    _contextHandler.addServlet(new ServletHolder(new TestServlet()), "/secondDispatchNewValueForExistingQueryString");
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { _contextHandler, new DefaultHandler() });
    _server.setHandler(handlers);
    _server.start();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) Before(org.junit.Before)

Example 42 with HandlerList

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

the class AsyncContextTest method setUp.

@Before
public void setUp() throws Exception {
    _server = new Server();
    _connector = new LocalConnector(_server);
    _connector.setIdleTimeout(5000);
    _connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration().setSendDateHeader(false);
    _server.addConnector(_connector);
    _contextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
    _contextHandler.setContextPath("/ctx");
    _contextHandler.addServlet(new ServletHolder(new TestServlet()), "/servletPath");
    _contextHandler.addServlet(new ServletHolder(new TestServlet()), "/path with spaces/servletPath");
    _contextHandler.addServlet(new ServletHolder(new TestServlet2()), "/servletPath2");
    _contextHandler.addServlet(new ServletHolder(new TestStartThrowServlet()), "/startthrow/*");
    _contextHandler.addServlet(new ServletHolder(new ForwardingServlet()), "/forward");
    _contextHandler.addServlet(new ServletHolder(new AsyncDispatchingServlet()), "/dispatchingServlet");
    _contextHandler.addServlet(new ServletHolder(new ExpireServlet()), "/expire/*");
    _contextHandler.addServlet(new ServletHolder(new BadExpireServlet()), "/badexpire/*");
    _contextHandler.addServlet(new ServletHolder(new ErrorServlet()), "/error/*");
    ErrorPageErrorHandler error_handler = new ErrorPageErrorHandler();
    _contextHandler.setErrorHandler(error_handler);
    error_handler.addErrorPage(500, "/error/500");
    error_handler.addErrorPage(IOException.class.getName(), "/error/IOE");
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { _contextHandler, new DefaultHandler() });
    _server.setHandler(handlers);
    _server.start();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) IOException(java.io.IOException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) Before(org.junit.Before)

Example 43 with HandlerList

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

the class EmbedJettyServer method startup.

@Override
public void startup(int port, String contextPath, String webRoot) throws EmbedServerException {
    this.port = port;
    Config config = Blade.$().config();
    int minThreads = config.getInt("server.jetty.min-threads", 8);
    int maxThreads = config.getInt("server.jetty.max-threads", 200);
    String poolName = config.get("server.jetty.pool-name", "blade-pool");
    // Setup Threadpool
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMinThreads(minThreads);
    threadPool.setMaxThreads(maxThreads);
    threadPool.setName(poolName);
    this.server = new org.eclipse.jetty.server.Server(threadPool);
    this.webAppContext = new WebAppContext();
    this.webAppContext.setContextPath(contextPath);
    this.webAppContext.setResourceBase("");
    int securePort = config.getInt("server.jetty.http.secure-port", 9443);
    int outputBufferSize = config.getInt("server.jetty.http.output-buffersize", 32 * 1024);
    int requestHeaderSize = config.getInt("server.jetty.http.request-headersize", 8 * 1024);
    int responseHeaderSize = config.getInt("server.jetty.http.response-headersize", 8 * 1024);
    // HTTP Configuration
    HttpConfiguration http_config = new HttpConfiguration();
    http_config.setSecurePort(securePort);
    http_config.setOutputBufferSize(outputBufferSize);
    http_config.setRequestHeaderSize(requestHeaderSize);
    http_config.setResponseHeaderSize(responseHeaderSize);
    long idleTimeout = config.getLong("server.jetty.http.idle-timeout", 30000L);
    String host = config.get("server.host", "0.0.0.0");
    ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory(http_config));
    serverConnector.setHost(host);
    serverConnector.setPort(this.port);
    serverConnector.setIdleTimeout(idleTimeout);
    server.setConnectors(new Connector[] { serverConnector });
    boolean isAsync = config.getBoolean("server.async", false);
    Class<? extends Servlet> servlet = isAsync ? AsyncDispatcherServlet.class : DispatcherServlet.class;
    ServletHolder servletHolder = new ServletHolder(servlet);
    servletHolder.setAsyncSupported(isAsync);
    servletHolder.setInitOrder(1);
    webAppContext.addEventListener(new BladeInitListener());
    Set<String> statics = Blade.$().bConfig().getStatics();
    defaultHolder = new ServletHolder(DefaultServlet.class);
    defaultHolder.setInitOrder(0);
    if (StringKit.isNotBlank(classPath)) {
        LOGGER.info("add classpath : {}", classPath);
        defaultHolder.setInitParameter("resourceBase", classPath);
    }
    statics.forEach(s -> {
        if (s.indexOf(".") != -1) {
            webAppContext.addServlet(defaultHolder, s);
        } else {
            s = s.endsWith("/") ? s + '*' : s + "/*";
            webAppContext.addServlet(defaultHolder, s);
        }
    });
    webAppContext.addServlet(defaultHolder, "/favicon.ico");
    webAppContext.addServlet(servletHolder, "/");
    try {
        this.loadServlets(webAppContext);
        this.loadFilters(webAppContext);
        HandlerList handlerList = new HandlerList();
        handlerList.setHandlers(new Handler[] { webAppContext, new DefaultHandler() });
        server.setHandler(handlerList);
        server.setStopAtShutdown(true);
        server.start();
        LOGGER.info("Blade Server Listen on {}:{}", host, this.port);
        server.join();
    } catch (Exception e) {
        throw new EmbedServerException(e);
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) org.eclipse.jetty.server(org.eclipse.jetty.server) EmbedServerException(com.blade.exception.EmbedServerException) Config(com.blade.kit.base.Config) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) BladeInitListener(com.blade.mvc.context.BladeInitListener) EmbedServerException(com.blade.exception.EmbedServerException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet)

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

Example 45 with HandlerList

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

the class PreviewStarter method run.

protected void run() {
    try {
        System.setProperty("org.mortbay.log.class", "org.eclipse.wst.server.preview.internal.WTPLogger");
        ServerConfig config = new ServerConfig(configPath);
        System.out.println("Starting preview server on port " + config.getPort());
        System.out.println();
        Module[] m = config.getModules();
        int size = m.length;
        if (size > 0) {
            System.out.println("Modules:");
            for (Module mm : m) System.out.println("  " + mm.getName() + " (" + mm.getContext() + ")");
            System.out.println();
        }
        server = new Server(config.getPort());
        server.setStopAtShutdown(true);
        WTPErrorHandler errorHandler = new WTPErrorHandler();
        HandlerList handlers = new HandlerList();
        for (Module module : m) {
            if (module.isStaticWeb()) {
                ContextResourceHandler resourceHandler = new ContextResourceHandler();
                resourceHandler.setResourceBase(module.getPath());
                resourceHandler.setContext(module.getContext());
                handlers.addHandler(resourceHandler);
            } else {
                WebAppContext wac = new WebAppContext();
                wac.setContextPath(module.getContext());
                wac.setWar(module.getPath());
                wac.setErrorHandler(errorHandler);
                handlers.addHandler(wac);
            }
        }
        handlers.addHandler(new WTPDefaultHandler(config.getPort(), m));
        server.setHandler(handlers);
        try {
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server)

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)27 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)21 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)21 ServerConnector (org.eclipse.jetty.server.ServerConnector)16 IOException (java.io.IOException)12 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 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)6 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)6 Test (org.junit.Test)6 ServletException (javax.servlet.ServletException)5 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)5