Search in sources :

Example 21 with HandlerCollection

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

the class DatabaseLoginServiceTestServer method configureServer.

protected void configureServer() throws Exception {
    _protocol = "http";
    _server.addBean(_loginService);
    ConstraintSecurityHandler security = new ConstraintSecurityHandler();
    _server.setHandler(security);
    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);
    Set<String> knownRoles = new HashSet<>();
    knownRoles.add("user");
    knownRoles.add("admin");
    security.setConstraintMappings(Collections.singletonList(mapping), knownRoles);
    security.setAuthenticator(new BasicAuthenticator());
    security.setLoginService(_loginService);
    ServletContextHandler root = new ServletContextHandler();
    root.setContextPath("/");
    root.setResourceBase(_resourceBase);
    ServletHolder servletHolder = new ServletHolder(new DefaultServlet());
    servletHolder.setInitParameter("gzip", "true");
    root.addServlet(servletHolder, "/*");
    _handler = new TestHandler(_resourceBase);
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { _handler, root });
    security.setHandler(handlers);
}
Also used : ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Constraint(org.eclipse.jetty.util.security.Constraint) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) HashSet(java.util.HashSet)

Example 22 with HandlerCollection

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

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

the class ServerProxyImpl method configureHandlers.

/**
     * 
     */
private void configureHandlers() {
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    if (requestLog != null)
        requestLogHandler.setRequestLog(requestLog);
    contexts = (ContextHandlerCollection) server.getChildHandlerByClass(ContextHandlerCollection.class);
    if (contexts == null) {
        contexts = new ContextHandlerCollection();
        HandlerCollection handlers = (HandlerCollection) server.getChildHandlerByClass(HandlerCollection.class);
        if (handlers == null) {
            handlers = new HandlerCollection();
            server.setHandler(handlers);
            handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
        } else {
            handlers.addHandler(contexts);
        }
    }
    //if there are any extra contexts to deploy
    if (contextHandlers != null && contextHandlers.getContextHandlers() != null) {
        for (ContextHandler c : contextHandlers.getContextHandlers()) contexts.addHandler(c);
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 24 with HandlerCollection

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

the class PartialRFC2616Test method init.

@Before
public void init() throws Exception {
    server = new Server();
    connector = new LocalConnector(server);
    connector.setIdleTimeout(10000);
    server.addConnector(connector);
    ContextHandler vcontext = new ContextHandler();
    vcontext.setContextPath("/");
    vcontext.setVirtualHosts(new String[] { "VirtualHost" });
    vcontext.setHandler(new DumpHandler("Virtual Dump"));
    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setHandler(new DumpHandler());
    HandlerCollection collection = new HandlerCollection();
    collection.setHandlers(new Handler[] { vcontext, context });
    server.setHandler(collection);
    server.start();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) Before(org.junit.Before)

Example 25 with HandlerCollection

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

the class WSServer method start.

public void start() throws Exception {
    server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.addConnector(connector);
    HandlerCollection handlers = new HandlerCollection();
    contexts = new ContextHandlerCollection();
    handlers.addHandler(contexts);
    server.setHandler(handlers);
    server.start();
    String host = connector.getHost();
    if (host == null) {
        host = "localhost";
    }
    int port = connector.getLocalPort();
    serverUri = new URI(String.format("ws://%s:%d%s/", host, port, contextPath));
    if (LOG.isDebugEnabled())
        LOG.debug("Server started on {}", serverUri);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) URI(java.net.URI)

Aggregations

HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)50 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)25 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)22 Server (org.eclipse.jetty.server.Server)21 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)18 ServerConnector (org.eclipse.jetty.server.ServerConnector)17 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)14 Handler (org.eclipse.jetty.server.Handler)13 Test (org.junit.Test)12 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)8 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)8 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)7 URI (java.net.URI)6 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)6 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)6 File (java.io.File)5 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)5 HttpURLConnection (java.net.HttpURLConnection)4 NCSARequestLog (org.eclipse.jetty.server.NCSARequestLog)4 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)4