Search in sources :

Example 26 with Handler

use of org.eclipse.jetty.server.Handler in project otter by alibaba.

the class JettyEmbedIntegration method main.

public static void main(String[] args) throws Exception {
    Resource jetty_xml = Resource.newSystemResource("jetty/jetty.xml");
    XmlConfiguration configuration = new XmlConfiguration(jetty_xml.getInputStream());
    Server server = (Server) configuration.configure();
    int port = 8081;
    Connector[] connectors = server.getConnectors();
    for (Connector connector : connectors) {
        connector.setPort(port);
    }
    Handler handler = server.getHandler();
    if (handler != null && handler instanceof ServletContextHandler) {
        ServletContextHandler servletHandler = (ServletContextHandler) handler;
        servletHandler.getInitParams().put("org.eclipse.jetty.servlet.Default.resourceBase", "/tmp/");
    }
    server.start();
    server.join();
}
Also used : Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) Resource(org.eclipse.jetty.util.resource.Resource) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 27 with Handler

use of org.eclipse.jetty.server.Handler in project otter by alibaba.

the class JettyEmbedServer method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    Resource configXml = Resource.newSystemResource(config);
    XmlConfiguration configuration = new XmlConfiguration(configXml.getInputStream());
    server = (Server) configuration.configure();
    Integer port = getPort();
    if (port != null && port > 0) {
        Connector[] connectors = server.getConnectors();
        for (Connector connector : connectors) {
            connector.setPort(port);
        }
    }
    Handler handler = server.getHandler();
    if (handler != null && handler instanceof ServletContextHandler) {
        ServletContextHandler servletHandler = (ServletContextHandler) handler;
        servletHandler.getInitParams().put("org.eclipse.jetty.servlet.Default.resourceBase", htdocsDir);
    }
    server.start();
    if (logger.isInfoEnabled()) {
        logger.info("##Jetty Embed Server is startup!");
    }
}
Also used : Connector(org.eclipse.jetty.server.Connector) Resource(org.eclipse.jetty.util.resource.Resource) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 28 with Handler

use of org.eclipse.jetty.server.Handler in project pulsar by yahoo.

the class ProxyServer method start.

public void start() throws PulsarServerException {
    log.info("Starting web socket proxy at port {}", conf.getWebServicePort());
    try {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setExtended(true);
        requestLog.setLogTimeZone("GMT");
        requestLog.setLogLatency(true);
        requestLogHandler.setRequestLog(requestLog);
        handlers.add(0, new ContextHandlerCollection());
        handlers.add(requestLogHandler);
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(handlers.toArray(new Handler[handlers.size()]));
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
        server.setHandler(handlerCollection);
        server.start();
    } catch (Exception e) {
        throw new PulsarServerException(e);
    }
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) 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) ServletException(javax.servlet.ServletException) DeploymentException(javax.websocket.DeploymentException) GeneralSecurityException(java.security.GeneralSecurityException) MalformedURLException(java.net.MalformedURLException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 29 with Handler

use of org.eclipse.jetty.server.Handler in project pulsar by yahoo.

the class ServerManager method start.

public void start() throws Exception {
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    Slf4jRequestLog requestLog = new Slf4jRequestLog();
    requestLog.setExtended(true);
    requestLog.setLogTimeZone("GMT");
    requestLog.setLogLatency(true);
    requestLogHandler.setRequestLog(requestLog);
    handlers.add(0, new ContextHandlerCollection());
    handlers.add(requestLogHandler);
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(handlers.toArray(new Handler[handlers.size()]));
    HandlerCollection handlerCollection = new HandlerCollection();
    handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
    server.setHandler(handlerCollection);
    server.start();
    log.info("Server started at end point {}", getServiceUri());
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) 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 30 with Handler

use of org.eclipse.jetty.server.Handler in project opennms by OpenNMS.

the class JUnitServer method initializeServerWithConfig.

protected void initializeServerWithConfig(final JUnitHttpServer config) {
    Server server = null;
    if (config.https()) {
        server = new Server();
        // SSL context configuration
        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setKeyStorePath(config.keystore());
        sslContextFactory.setKeyStorePassword(config.keystorePassword());
        sslContextFactory.setKeyManagerPassword(config.keyPassword());
        sslContextFactory.setTrustStorePath(config.keystore());
        sslContextFactory.setTrustStorePassword(config.keystorePassword());
        // HTTP Configuration
        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setSecureScheme("https");
        http_config.setSecurePort(config.port());
        http_config.setOutputBufferSize(32768);
        http_config.setRequestHeaderSize(8192);
        http_config.setResponseHeaderSize(8192);
        http_config.setSendServerVersion(true);
        http_config.setSendDateHeader(false);
        // 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(config.port());
        server.addConnector(sslConnector);
    } else {
        server = new Server(config.port());
    }
    m_server = server;
    final ContextHandler context1 = new ContextHandler();
    context1.setContextPath("/");
    context1.setWelcomeFiles(new String[] { "index.html" });
    context1.setResourceBase(config.resource());
    context1.setClassLoader(Thread.currentThread().getContextClassLoader());
    context1.setVirtualHosts(config.vhosts());
    final ContextHandler context = context1;
    Handler topLevelHandler = null;
    final HandlerList handlers = new HandlerList();
    if (config.basicAuth()) {
        // check for basic auth if we're configured to do so
        LOG.debug("configuring basic auth");
        final HashLoginService loginService = new HashLoginService("MyRealm", config.basicAuthFile());
        loginService.setHotReload(true);
        m_server.addBean(loginService);
        final ConstraintSecurityHandler security = new ConstraintSecurityHandler();
        final Set<String> knownRoles = new HashSet<String>();
        knownRoles.add("user");
        knownRoles.add("admin");
        knownRoles.add("moderator");
        final Constraint constraint = new Constraint();
        constraint.setName("auth");
        constraint.setAuthenticate(true);
        constraint.setRoles(knownRoles.toArray(new String[0]));
        final ConstraintMapping mapping = new ConstraintMapping();
        mapping.setPathSpec("/*");
        mapping.setConstraint(constraint);
        security.setConstraintMappings(Collections.singletonList(mapping), knownRoles);
        security.setAuthenticator(new BasicAuthenticator());
        security.setLoginService(loginService);
        security.setRealmName("MyRealm");
        security.setHandler(context);
        topLevelHandler = security;
    } else {
        topLevelHandler = context;
    }
    final Webapp[] webapps = config.webapps();
    if (webapps != null) {
        for (final Webapp webapp : webapps) {
            final WebAppContext wac = new WebAppContext();
            String path = null;
            if (!"".equals(webapp.pathSystemProperty()) && System.getProperty(webapp.pathSystemProperty()) != null) {
                path = System.getProperty(webapp.pathSystemProperty());
            } else {
                path = webapp.path();
            }
            if (path == null || "".equals(path)) {
                throw new IllegalArgumentException("path or pathSystemProperty of @Webapp points to a null or blank value");
            }
            wac.setWar(path);
            wac.setContextPath(webapp.context());
            handlers.addHandler(wac);
        }
    }
    final ResourceHandler rh = new ResourceHandler();
    rh.setWelcomeFiles(new String[] { "index.html" });
    rh.setResourceBase(config.resource());
    handlers.addHandler(rh);
    // fall through to default
    handlers.addHandler(new DefaultHandler());
    context.setHandler(handlers);
    m_server.setHandler(topLevelHandler);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) Constraint(org.eclipse.jetty.util.security.Constraint) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HashLoginService(org.eclipse.jetty.security.HashLoginService) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) HashSet(java.util.HashSet) Webapp(org.opennms.core.test.http.annotations.Webapp)

Aggregations

Handler (org.eclipse.jetty.server.Handler)63 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)18 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)18 Server (org.eclipse.jetty.server.Server)14 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)13 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)12 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)10 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)10 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)10 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)7 ServerConnector (org.eclipse.jetty.server.ServerConnector)7 ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)7 HandlerList (org.eclipse.jetty.server.handler.HandlerList)7 ArrayList (java.util.ArrayList)6 Connector (org.eclipse.jetty.server.Connector)6 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)6 Test (org.junit.Test)6 HandlerWrapper (org.eclipse.jetty.server.handler.HandlerWrapper)5 GzipHandler (org.eclipse.jetty.server.handler.gzip.GzipHandler)5 URI (java.net.URI)4