Search in sources :

Example 1 with RewriteRegexRule

use of org.eclipse.jetty.rewrite.handler.RewriteRegexRule in project jetty.project by eclipse.

the class RewriteServer method main.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    HttpConfiguration config = server.getConnectors()[0].getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
    RewriteCustomizer rewrite = new RewriteCustomizer();
    config.addCustomizer(rewrite);
    rewrite.addRule(new CompactPathRule());
    rewrite.addRule(new RewriteRegexRule("(.*)foo(.*)", "$1FOO$2"));
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);
    context.addServlet(DumpServlet.class, "/*");
    server.start();
    server.join();
}
Also used : CompactPathRule(org.eclipse.jetty.rewrite.handler.CompactPathRule) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) RewriteRegexRule(org.eclipse.jetty.rewrite.handler.RewriteRegexRule) RewriteCustomizer(org.eclipse.jetty.rewrite.RewriteCustomizer) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 2 with RewriteRegexRule

use of org.eclipse.jetty.rewrite.handler.RewriteRegexRule 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 3 with RewriteRegexRule

use of org.eclipse.jetty.rewrite.handler.RewriteRegexRule in project hive by apache.

the class HttpServer method initializeWebServer.

private void initializeWebServer(final Builder b, int queueSize) throws IOException {
    // Set handling for low resource conditions.
    final LowResourceMonitor low = new LowResourceMonitor(webServer);
    low.setLowResourcesIdleTimeout(10000);
    webServer.addBean(low);
    Connector connector = createChannelConnector(queueSize, b);
    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);
    if (b.usePAM) {
        setupPam(b, contexts);
    }
    addServlet("jmx", "/jmx", JMXJsonServlet.class);
    addServlet("conf", "/conf", ConfServlet.class);
    addServlet("stacks", "/stacks", StackServlet.class);
    addServlet("conflog", "/conflog", Log4j2ConfiguratorServlet.class);
    final String asyncProfilerHome = ProfileServlet.getAsyncProfilerHome();
    if (asyncProfilerHome != null && !asyncProfilerHome.trim().isEmpty()) {
        addServlet("prof", "/prof", ProfileServlet.class);
        Path tmpDir = Paths.get(ProfileServlet.OUTPUT_DIR);
        if (Files.notExists(tmpDir)) {
            Files.createDirectories(tmpDir);
        }
        ServletContextHandler genCtx = new ServletContextHandler(contexts, "/prof-output");
        setContextAttributes(genCtx.getServletContext(), b.contextAttrs);
        genCtx.addServlet(ProfileOutputServlet.class, "/*");
        genCtx.setResourceBase(tmpDir.toAbsolutePath().toString());
        genCtx.setDisplayName("prof-output");
    } else {
        LOG.info("ASYNC_PROFILER_HOME env or -Dasync.profiler.home not specified. Disabling /prof endpoint..");
    }
    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");
    disableDirectoryListingOnServlet(staticCtx);
    String logDir = getLogDir(b.conf);
    if (logDir != null) {
        ServletContextHandler logCtx = new ServletContextHandler(contexts, "/logs");
        setContextAttributes(logCtx.getServletContext(), b.contextAttrs);
        if (b.useSPNEGO) {
            setupSpnegoFilter(b, logCtx);
        }
        logCtx.addServlet(AdminAuthorizedServlet.class, "/*");
        logCtx.setResourceBase(logDir);
        logCtx.setDisplayName("logs");
    }
}
Also used : Path(java.nio.file.Path) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) RewriteRegexRule(org.eclipse.jetty.rewrite.handler.RewriteRegexRule) HttpServlet(javax.servlet.http.HttpServlet) LowResourceMonitor(org.eclipse.jetty.server.LowResourceMonitor) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) RewriteHandler(org.eclipse.jetty.rewrite.handler.RewriteHandler)

Aggregations

RewriteRegexRule (org.eclipse.jetty.rewrite.handler.RewriteRegexRule)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)3 HttpServlet (javax.servlet.http.HttpServlet)2 RewriteHandler (org.eclipse.jetty.rewrite.handler.RewriteHandler)2 Connector (org.eclipse.jetty.server.Connector)2 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)2 Path (java.nio.file.Path)1 RewriteCustomizer (org.eclipse.jetty.rewrite.RewriteCustomizer)1 CompactPathRule (org.eclipse.jetty.rewrite.handler.CompactPathRule)1 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)1 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)1 LowResourceMonitor (org.eclipse.jetty.server.LowResourceMonitor)1 Server (org.eclipse.jetty.server.Server)1 ServerConnector (org.eclipse.jetty.server.ServerConnector)1 SelectChannelConnector (org.eclipse.jetty.server.nio.SelectChannelConnector)1 SslSelectChannelConnector (org.eclipse.jetty.server.ssl.SslSelectChannelConnector)1 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)1