Search in sources :

Example 51 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project jetty.project by eclipse.

the class HttpInputIntegrationTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    __config = new HttpConfiguration();
    __server = new Server();
    LocalConnector local = new LocalConnector(__server, new HttpConnectionFactory(__config));
    local.setIdleTimeout(4000);
    __server.addConnector(local);
    ServerConnector http = new ServerConnector(__server, new HttpConnectionFactory(__config), new HTTP2CServerConnectionFactory(__config));
    http.setIdleTimeout(4000);
    __server.addConnector(http);
    // SSL Context Factory for HTTPS and HTTP/2
    String jetty_distro = System.getProperty("jetty.distro", "../../jetty-distribution/target/distribution");
    __sslContextFactory = new SslContextFactory();
    __sslContextFactory.setKeyStorePath(jetty_distro + "/../../../jetty-server/src/test/config/etc/keystore");
    __sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    __sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
    // HTTPS Configuration
    __sslConfig = new HttpConfiguration(__config);
    __sslConfig.addCustomizer(new SecureRequestCustomizer());
    // HTTP/1 Connection Factory
    HttpConnectionFactory h1 = new HttpConnectionFactory(__sslConfig);
    /* TODO
        // HTTP/2 Connection Factory
        HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(__sslConfig);
        
        NegotiatingServerConnectionFactory.checkProtocolNegotiationAvailable();
        ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
        alpn.setDefaultProtocol(h1.getProtocol());
        */
    // SSL Connection Factory
    SslConnectionFactory ssl = new SslConnectionFactory(__sslContextFactory, h1.getProtocol());
    // HTTP/2 Connector
    ServerConnector http2 = new ServerConnector(__server, ssl, /*TODO alpn,h2,*/
    h1);
    http2.setIdleTimeout(4000);
    __server.addConnector(http2);
    ServletContextHandler context = new ServletContextHandler(__server, "/ctx");
    ServletHolder holder = new ServletHolder(new TestServlet());
    holder.setAsyncSupported(true);
    context.addServlet(holder, "/*");
    __server.start();
}
Also used : SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) LocalConnector(org.eclipse.jetty.server.LocalConnector) 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) HTTP2CServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) BeforeClass(org.junit.BeforeClass)

Example 52 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project hadoop by apache.

the class TestWebAppProxyServlet method start.

/**
   * Simple http server. Server should send answer with status 200
   */
@BeforeClass
public static void start() throws Exception {
    server = new Server(0);
    ((QueuedThreadPool) server.getThreadPool()).setMaxThreads(10);
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/foo");
    server.setHandler(context);
    context.addServlet(new ServletHolder(TestServlet.class), "/bar");
    ((ServerConnector) server.getConnectors()[0]).setHost("localhost");
    server.start();
    originalPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
    LOG.info("Running embedded servlet container at: http://localhost:" + originalPort);
    // This property needs to be set otherwise CORS Headers will be dropped
    // by HttpUrlConnection
    System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) BeforeClass(org.junit.BeforeClass)

Example 53 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project hive by apache.

the class HttpServer method initializeWebServer.

void initializeWebServer(Builder b) {
    // Create the thread pool for the web server to handle HTTP requests
    QueuedThreadPool threadPool = new QueuedThreadPool();
    if (b.maxThreads > 0) {
        threadPool.setMaxThreads(b.maxThreads);
    }
    threadPool.setDaemon(true);
    threadPool.setName(b.name + "-web");
    webServer.setThreadPool(threadPool);
    // Create the channel connector for the web server
    Connector connector = createChannelConnector(threadPool.getMaxThreads(), b);
    connector.setHost(b.host);
    connector.setPort(b.port);
    webServer.addConnector(connector);
    RewriteHandler rwHandler = new RewriteHandler();
    rwHandler.setRewriteRequestURI(true);
    rwHandler.setRewritePathInfo(false);
    RewriteRegexRule rootRule = new RewriteRegexRule();
    rootRule.setRegex("^/$");
    rootRule.setReplacement(b.contextRootRewriteTarget);
    rootRule.setTerminating(true);
    rwHandler.addRule(rootRule);
    rwHandler.setHandler(webAppContext);
    // Configure web application contexts for the web server
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.addHandler(rwHandler);
    webServer.setHandler(contexts);
    addServlet("jmx", "/jmx", JMXJsonServlet.class);
    addServlet("conf", "/conf", ConfServlet.class);
    addServlet("stacks", "/stacks", StackServlet.class);
    for (Pair<String, Class<? extends HttpServlet>> p : b.servlets) {
        addServlet(p.getFirst(), "/" + p.getFirst(), p.getSecond());
    }
    ServletContextHandler staticCtx = new ServletContextHandler(contexts, "/static");
    staticCtx.setResourceBase(appDir + "/static");
    staticCtx.addServlet(DefaultServlet.class, "/*");
    staticCtx.setDisplayName("static");
    String logDir = getLogDir(b.conf);
    if (logDir != null) {
        ServletContextHandler logCtx = new ServletContextHandler(contexts, "/logs");
        setContextAttributes(logCtx.getServletContext(), b.contextAttrs);
        logCtx.addServlet(AdminAuthorizedServlet.class, "/*");
        logCtx.setResourceBase(logDir);
        logCtx.setDisplayName("logs");
    }
}
Also used : SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) SslSelectChannelConnector(org.eclipse.jetty.server.ssl.SslSelectChannelConnector) Connector(org.eclipse.jetty.server.Connector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RewriteRegexRule(org.eclipse.jetty.rewrite.handler.RewriteRegexRule) HttpServlet(javax.servlet.http.HttpServlet) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) RewriteHandler(org.eclipse.jetty.rewrite.handler.RewriteHandler)

Example 54 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project hbase by apache.

the class HBaseRESTTestingUtility method startServletContainer.

public void startServletContainer(Configuration conf) throws Exception {
    if (server != null) {
        LOG.error("ServletContainer already running");
        return;
    }
    // Inject the conf for the test by being first to make singleton
    RESTServlet.getInstance(conf, UserProvider.instantiate(conf));
    // set up the Jersey servlet container for Jetty
    ResourceConfig app = new ResourceConfig().packages("org.apache.hadoop.hbase.rest").register(Jackson1Feature.class);
    ServletHolder sh = new ServletHolder(new ServletContainer(app));
    // set up Jetty and run the embedded server
    server = new Server(0);
    LOG.info("configured " + ServletContainer.class.getName());
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setSendDateHeader(false);
    httpConfig.setSendServerVersion(false);
    ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
    serverConnector.setPort(testServletPort);
    server.addConnector(serverConnector);
    // set up context
    ServletContextHandler ctxHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    ctxHandler.addServlet(sh, "/*");
    // Load filters specified from configuration.
    String[] filterClasses = conf.getStrings(Constants.FILTER_CLASSES, ArrayUtils.EMPTY_STRING_ARRAY);
    for (String filter : filterClasses) {
        filter = filter.trim();
        ctxHandler.addFilter(filter, "/*", EnumSet.of(DispatcherType.REQUEST));
    }
    LOG.info("Loaded filter classes :" + filterClasses);
    conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*");
    RESTServer.addCSRFFilter(ctxHandler, conf);
    HttpServerUtil.constrainHttpMethods(ctxHandler);
    // start the server
    server.start();
    // get the port
    testServletPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
    LOG.info("started " + server.getClass().getName() + " on port " + testServletPort);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 55 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project kafka by apache.

the class RestServer method start.

public void start(Herder herder) {
    log.info("Starting REST server");
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.register(new JacksonJsonProvider());
    resourceConfig.register(RootResource.class);
    resourceConfig.register(new ConnectorsResource(herder));
    resourceConfig.register(new ConnectorPluginsResource(herder));
    resourceConfig.register(ConnectExceptionMapper.class);
    ServletContainer servletContainer = new ServletContainer(resourceConfig);
    ServletHolder servletHolder = new ServletHolder(servletContainer);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addServlet(servletHolder, "/*");
    String allowedOrigins = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_ORIGIN_CONFIG);
    if (allowedOrigins != null && !allowedOrigins.trim().isEmpty()) {
        FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
        filterHolder.setName("cross-origin");
        filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, allowedOrigins);
        String allowedMethods = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_METHODS_CONFIG);
        if (allowedMethods != null && !allowedOrigins.trim().isEmpty()) {
            filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, allowedMethods);
        }
        context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    }
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    Slf4jRequestLog requestLog = new Slf4jRequestLog();
    requestLog.setLoggerName(RestServer.class.getCanonicalName());
    requestLog.setLogLatency(true);
    requestLogHandler.setRequestLog(requestLog);
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { context, new DefaultHandler(), requestLogHandler });
    /* Needed for graceful shutdown as per `setStopTimeout` documentation */
    StatisticsHandler statsHandler = new StatisticsHandler();
    statsHandler.setHandler(handlers);
    jettyServer.setHandler(statsHandler);
    jettyServer.setStopTimeout(GRACEFUL_SHUTDOWN_TIMEOUT_MS);
    jettyServer.setStopAtShutdown(true);
    try {
        jettyServer.start();
    } catch (Exception e) {
        throw new ConnectException("Unable to start REST server", e);
    }
    log.info("REST server listening at " + jettyServer.getURI() + ", advertising URL " + advertisedUrl());
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) ConnectorsResource(org.apache.kafka.connect.runtime.rest.resources.ConnectorsResource) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ConnectorPluginsResource(org.apache.kafka.connect.runtime.rest.resources.ConnectorPluginsResource) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) IOException(java.io.IOException) ConnectException(org.apache.kafka.connect.errors.ConnectException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)390 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)253 Server (org.eclipse.jetty.server.Server)217 ServerConnector (org.eclipse.jetty.server.ServerConnector)98 Test (org.junit.Test)70 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)56 IOException (java.io.IOException)42 HttpServletRequest (javax.servlet.http.HttpServletRequest)39 HttpClient (org.eclipse.jetty.client.HttpClient)39 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)39 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)37 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)37 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)32 Connector (org.eclipse.jetty.server.Connector)29 Request (org.eclipse.jetty.client.api.Request)27 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)24 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)24 ServletException (javax.servlet.ServletException)22 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)22 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)22