All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.turbospaces.jetty.AbstractWebAppJettyChannel Maven / Gradle / Ivy

There is a newer version: 2.0.33
Show newest version
package com.turbospaces.jetty;

import java.net.URL;
import java.security.ProtectionDomain;

import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jgit.http.server.GitServlet;

import com.codahale.metrics.servlets.HealthCheckServlet;
import com.codahale.metrics.servlets.PingServlet;

public abstract class AbstractWebAppJettyChannel extends JettyChannel {
    protected AbstractWebAppJettyChannel(int port) throws Throwable {
        super(port);
    }
    protected ServletContextHandler createPingServlet(String contextPath) {
        ServletMapping servletMapping = new ServletMapping();
        servletMapping.setServletName("ping");
        servletMapping.setPathSpec("/*");

        ServletContextHandler ping = new ServletContextHandler();
        ServletHandler sh = new ServletHandler();
        sh.setServlets(new ServletHolder[] { new ServletHolder(servletMapping.getServletName(), PingServlet.class) });
        sh.setServletMappings(new ServletMapping[] { servletMapping });
        ping.setServletHandler(sh);
        ping.setContextPath(contextPath);

        return ping;
    }
    protected ServletContextHandler createGitServlet(String contextPath, GitServlet servlet) {
        ServletMapping servletMapping = new ServletMapping();
        servletMapping.setServletName("jgit");
        servletMapping.setPathSpec("/*");

        ServletContextHandler jgit = new ServletContextHandler();
        ServletHandler sh = new ServletHandler();
        sh.setServlets(new ServletHolder[] { new ServletHolder(servletMapping.getServletName(), servlet) });
        sh.setServletMappings(new ServletMapping[] { servletMapping });
        jgit.setServletHandler(sh);
        jgit.setContextPath(contextPath);

        return jgit;
    }
    protected ServletContextHandler createHealthCheckServlet(String contextPath) {
        ServletMapping servletMapping = new ServletMapping();
        servletMapping.setServletName("healthcheck");
        servletMapping.setPathSpec("/*");

        HealthCheckServlet servlet = new HealthCheckServlet(bootstrap.healthCheckRegistry());

        ServletContextHandler ping = new ServletContextHandler();
        ServletHandler sh = new ServletHandler();
        sh.setServlets(new ServletHolder[] { new ServletHolder(servletMapping.getServletName(), servlet) });
        sh.setServletMappings(new ServletMapping[] { servletMapping });
        ping.setServletHandler(sh);
        ping.setContextPath(contextPath);

        return ping;
    }
    protected WebAppContext createWebAppWithWebXmlContext(String contextPath, ProtectionDomain domain) throws Exception {
        URL location = domain.getCodeSource().getLocation();
        String resourceBase = location.toExternalForm();
        String webXml = location.toExternalForm() + WEB_XML;

        if ("file".equals(location.getProtocol()) && location.getPath().toLowerCase().endsWith(".jar")) {
            resourceBase = "jar:file:" + location.getPath() + "!/";
            webXml = resourceBase + WEB_XML;
        }

        logger.info("init webapp(base={},xml={}) ...", resourceBase, webXml);

        WebAppContext webapp = new WebAppContext();
        webapp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
        webapp.setContextPath(contextPath);
        webapp.setResourceBase(resourceBase);
        webapp.setDescriptor(webXml);

        return webapp;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy