Search in sources :

Example 26 with ContextHandlerCollection

use of org.eclipse.jetty.server.handler.ContextHandlerCollection in project JMRI by JMRI.

the class WebServer method registerDenial.

/**
     * Register a URL pattern to be denied access.
     *
     * @param urlPattern the pattern to deny access to
     */
public void registerDenial(String urlPattern) {
    this.registeredUrls.put(urlPattern, Registration.DENIAL);
    ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
    servletContext.setContextPath(urlPattern);
    DenialServlet servlet = new DenialServlet();
    // NOI18N
    servletContext.addServlet(new ServletHolder(servlet), "/*");
    ((ContextHandlerCollection) this.server.getHandler()).addHandler(servletContext);
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DenialServlet(jmri.web.servlet.DenialServlet)

Example 27 with ContextHandlerCollection

use of org.eclipse.jetty.server.handler.ContextHandlerCollection in project processdash by dtuma.

the class WebServer method createServer.

private void createServer() {
    // compile JSPs using an internal compiler, not a JDK javac executable
    System.setProperty("org.apache.jasper.compiler.disablejsr199", "true");
    // create a new Jetty server
    server = new Server();
    server.setAttribute(DEFAULT_ENV_KEY, Collections.unmodifiableMap(DEFAULT_ENV));
    // the server thread pool should use daemon threads
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setName("WebServer");
    threadPool.setDaemon(true);
    server.setThreadPool(threadPool);
    // create a local connector which can be used for internal requests
    localConnector = new LocalConnector();
    server.addConnector(localConnector);
    // create a handler chain to serve requests
    webApps = new ContextHandlerCollection();
    webApps.setHandlers(new Handler[0]);
    HandlerWrapper interceptor = new DashboardHttpRequestInterceptor(startupTimestamp);
    interceptor.setHandler(webApps);
    server.setHandler(interceptor);
    // start the server
    try {
        server.start();
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Could not start web server", e);
    }
}
Also used : Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 28 with ContextHandlerCollection

use of org.eclipse.jetty.server.handler.ContextHandlerCollection in project hbase by apache.

the class HttpServer method initializeWebServer.

private void initializeWebServer(String name, String hostName, Configuration conf, String[] pathSpecs) throws FileNotFoundException, IOException {
    Preconditions.checkNotNull(webAppContext);
    HandlerCollection handlerCollection = new HandlerCollection();
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    RequestLog requestLog = HttpRequestLog.getRequestLog(name);
    if (requestLog != null) {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        requestLogHandler.setRequestLog(requestLog);
        handlerCollection.addHandler(requestLogHandler);
    }
    final String appDir = getWebAppsPath(name);
    handlerCollection.addHandler(contexts);
    handlerCollection.addHandler(webAppContext);
    webServer.setHandler(handlerCollection);
    addDefaultApps(contexts, appDir, conf);
    addGlobalFilter("safety", QuotingInputFilter.class.getName(), null);
    Map<String, String> params = new HashMap<>();
    params.put("xframeoptions", conf.get("hbase.http.filter.xframeoptions.mode", "DENY"));
    addGlobalFilter("clickjackingprevention", ClickjackingPreventionFilter.class.getName(), params);
    final FilterInitializer[] initializers = getFilterInitializers(conf);
    if (initializers != null) {
        conf = new Configuration(conf);
        conf.set(BIND_ADDRESS, hostName);
        for (FilterInitializer c : initializers) {
            c.initFilter(this, conf);
        }
    }
    addDefaultServlets();
    if (pathSpecs != null) {
        for (String path : pathSpecs) {
            LOG.info("adding path spec: " + path);
            addFilterPathMapping(path, webAppContext);
        }
    }
}
Also used : RequestLog(org.eclipse.jetty.server.RequestLog) Configuration(org.apache.hadoop.conf.Configuration) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HashMap(java.util.HashMap) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection)

Example 29 with ContextHandlerCollection

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

the class LikeJettyXml method main.

public static void main(String[] args) throws Exception {
    // Path to as-built jetty-distribution directory
    String jettyHomeBuild = "../../jetty-distribution/target/distribution";
    // Find jetty home and base directories
    String homePath = System.getProperty("jetty.home", jettyHomeBuild);
    File start_jar = new File(homePath, "start.jar");
    if (!start_jar.exists()) {
        homePath = jettyHomeBuild = "jetty-distribution/target/distribution";
        start_jar = new File(homePath, "start.jar");
        if (!start_jar.exists())
            throw new FileNotFoundException(start_jar.toString());
    }
    File homeDir = new File(homePath);
    String basePath = System.getProperty("jetty.base", homeDir + "/demo-base");
    File baseDir = new File(basePath);
    if (!baseDir.exists()) {
        throw new FileNotFoundException(baseDir.getAbsolutePath());
    }
    // Configure jetty.home and jetty.base system properties
    String jetty_home = homeDir.getAbsolutePath();
    String jetty_base = baseDir.getAbsolutePath();
    System.setProperty("jetty.home", jetty_home);
    System.setProperty("jetty.base", jetty_base);
    // === jetty.xml ===
    // Setup Threadpool
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMaxThreads(500);
    // Server
    Server server = new Server(threadPool);
    // Scheduler
    server.addBean(new ScheduledExecutorScheduler());
    // HTTP Configuration
    HttpConfiguration http_config = new HttpConfiguration();
    http_config.setSecureScheme("https");
    http_config.setSecurePort(8443);
    http_config.setOutputBufferSize(32768);
    http_config.setRequestHeaderSize(8192);
    http_config.setResponseHeaderSize(8192);
    http_config.setSendServerVersion(true);
    http_config.setSendDateHeader(false);
    // httpConfig.addCustomizer(new ForwardedRequestCustomizer());
    // Handler Structure
    HandlerCollection handlers = new HandlerCollection();
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    handlers.setHandlers(new Handler[] { contexts, new DefaultHandler() });
    server.setHandler(handlers);
    // Extra options
    server.setDumpAfterStart(false);
    server.setDumpBeforeStop(false);
    server.setStopAtShutdown(true);
    // === jetty-jmx.xml ===
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);
    // === jetty-http.xml ===
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
    http.setPort(8080);
    http.setIdleTimeout(30000);
    server.addConnector(http);
    // === jetty-https.xml ===
    // SSL Context Factory
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(jetty_home + "/../../../jetty-server/src/test/config/etc/keystore");
    sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
    sslContextFactory.setTrustStorePath(jetty_home + "/../../../jetty-server/src/test/config/etc/keystore");
    sslContextFactory.setTrustStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA", "SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA", "SSL_RSA_EXPORT_WITH_RC4_40_MD5", "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
    // SSL HTTP Configuration
    HttpConfiguration https_config = new HttpConfiguration(http_config);
    https_config.addCustomizer(new SecureRequestCustomizer());
    // SSL Connector
    ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config));
    sslConnector.setPort(8443);
    server.addConnector(sslConnector);
    // === jetty-deploy.xml ===
    DeploymentManager deployer = new DeploymentManager();
    DebugListener debug = new DebugListener(System.err, true, true, true);
    server.addBean(debug);
    deployer.addLifeCycleBinding(new DebugListenerBinding(debug));
    deployer.setContexts(contexts);
    deployer.setContextAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/servlet-api-[^/]*\\.jar$");
    WebAppProvider webapp_provider = new WebAppProvider();
    webapp_provider.setMonitoredDirName(jetty_base + "/webapps");
    webapp_provider.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml");
    webapp_provider.setScanInterval(1);
    webapp_provider.setExtractWars(true);
    webapp_provider.setConfigurationManager(new PropertiesConfigurationManager());
    deployer.addAppProvider(webapp_provider);
    server.addBean(deployer);
    // === setup jetty plus ==
    Configuration.ClassList.setServerDefault(server).addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
    // === jetty-stats.xml ===
    StatisticsHandler stats = new StatisticsHandler();
    stats.setHandler(server.getHandler());
    server.setHandler(stats);
    ServerConnectionStatistics.addToAllConnectors(server);
    // === Rewrite Handler
    RewriteHandler rewrite = new RewriteHandler();
    rewrite.setHandler(server.getHandler());
    server.setHandler(rewrite);
    // === jetty-requestlog.xml ===
    NCSARequestLog requestLog = new NCSARequestLog();
    requestLog.setFilename(jetty_home + "/logs/yyyy_mm_dd.request.log");
    requestLog.setFilenameDateFormat("yyyy_MM_dd");
    requestLog.setRetainDays(90);
    requestLog.setAppend(true);
    requestLog.setExtended(true);
    requestLog.setLogCookies(false);
    requestLog.setLogTimeZone("GMT");
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    requestLogHandler.setRequestLog(requestLog);
    handlers.addHandler(requestLogHandler);
    // === jetty-lowresources.xml ===
    LowResourceMonitor lowResourcesMonitor = new LowResourceMonitor(server);
    lowResourcesMonitor.setPeriod(1000);
    lowResourcesMonitor.setLowResourcesIdleTimeout(200);
    lowResourcesMonitor.setMonitorThreads(true);
    lowResourcesMonitor.setMaxConnections(0);
    lowResourcesMonitor.setMaxMemory(0);
    lowResourcesMonitor.setMaxLowResourcesTime(5000);
    server.addBean(lowResourcesMonitor);
    // === test-realm.xml ===
    HashLoginService login = new HashLoginService();
    login.setName("Test Realm");
    login.setConfig(jetty_base + "/etc/realm.properties");
    login.setHotReload(false);
    server.addBean(login);
    // Start the server
    server.start();
    server.join();
}
Also used : Server(org.eclipse.jetty.server.Server) DeploymentManager(org.eclipse.jetty.deploy.DeploymentManager) FileNotFoundException(java.io.FileNotFoundException) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HashLoginService(org.eclipse.jetty.security.HashLoginService) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) DebugListenerBinding(org.eclipse.jetty.deploy.bindings.DebugListenerBinding) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) PropertiesConfigurationManager(org.eclipse.jetty.deploy.PropertiesConfigurationManager) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) RewriteHandler(org.eclipse.jetty.rewrite.handler.RewriteHandler) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) DebugListener(org.eclipse.jetty.server.DebugListener) WebAppProvider(org.eclipse.jetty.deploy.providers.WebAppProvider) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) LowResourceMonitor(org.eclipse.jetty.server.LowResourceMonitor) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) File(java.io.File)

Example 30 with ContextHandlerCollection

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

the class ManyContexts method main.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ContextHandler context = new ContextHandler("/");
    context.setContextPath("/");
    context.setHandler(new HelloHandler("Root Hello"));
    ContextHandler contextFR = new ContextHandler("/fr");
    contextFR.setHandler(new HelloHandler("Bonjoir"));
    ContextHandler contextIT = new ContextHandler("/it");
    contextIT.setHandler(new HelloHandler("Bongiorno"));
    ContextHandler contextV = new ContextHandler("/");
    contextV.setVirtualHosts(new String[] { "127.0.0.2" });
    contextV.setHandler(new HelloHandler("Virtual Hello"));
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(new Handler[] { context, contextFR, contextIT, contextV });
    server.setHandler(contexts);
    server.start();
    server.join();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection)

Aggregations

ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)54 Server (org.eclipse.jetty.server.Server)25 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)21 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)17 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)16 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)14 ServerConnector (org.eclipse.jetty.server.ServerConnector)13 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)13 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)10 Test (org.junit.Test)10 Handler (org.eclipse.jetty.server.Handler)9 URI (java.net.URI)7 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)7 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)6 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)6 File (java.io.File)5 ArrayList (java.util.ArrayList)5 MBeanContainer (org.eclipse.jetty.jmx.MBeanContainer)5 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)5 HttpURLConnection (java.net.HttpURLConnection)4