Search in sources :

Example 26 with ServletMapping

use of org.eclipse.jetty.servlet.ServletMapping in project knox by apache.

the class HttpServer2 method addInternalServlet.

/**
 * Add an internal servlet in the server, specifying whether or not to
 * protect with Kerberos authentication.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param requireAuth Require Kerberos authenticate to access servlet
 */
public void addInternalServlet(String name, String pathSpec, Class<? extends HttpServlet> clazz, boolean requireAuth) {
    ServletHolder holder = new ServletHolder(clazz);
    if (name != null) {
        holder.setName(name);
    }
    // Jetty doesn't like the same path spec mapping to different servlets, so
    // if there's already a mapping for this pathSpec, remove it and assume that
    // the newest one is the one we want
    final ServletMapping[] servletMappings = webAppContext.getServletHandler().getServletMappings();
    for (ServletMapping servletMapping : servletMappings) {
        if (servletMapping.containsPathSpec(pathSpec)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found existing " + servletMapping.getServletName() + " servlet at path " + pathSpec + "; will replace mapping" + " with " + holder.getName() + " servlet");
            }
            ServletMapping[] newServletMappings = ArrayUtil.removeFromArray(servletMappings, servletMapping);
            webAppContext.getServletHandler().setServletMappings(newServletMappings);
            break;
        }
    }
    webAppContext.addServlet(holder, pathSpec);
    if (requireAuth && UserGroupInformation.isSecurityEnabled()) {
        LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
        ServletHandler handler = webAppContext.getServletHandler();
        FilterMapping fmap = new FilterMapping();
        fmap.setPathSpec(pathSpec);
        fmap.setFilterName(SPNEGO_FILTER);
        fmap.setDispatches(FilterMapping.ALL);
        handler.addFilterMapping(fmap);
    }
}
Also used : ServletMapping(org.eclipse.jetty.servlet.ServletMapping) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) FilterMapping(org.eclipse.jetty.servlet.FilterMapping)

Example 27 with ServletMapping

use of org.eclipse.jetty.servlet.ServletMapping in project knox by apache.

the class HttpServer2 method addInternalServlet.

/**
 * Add an internal servlet in the server, with initialization parameters.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param params init parameters
 */
public void addInternalServlet(String name, String pathSpec, Class<? extends HttpServlet> clazz, Map<String, String> params) {
    // Jetty doesn't like the same path spec mapping to different servlets, so
    // if there's already a mapping for this pathSpec, remove it and assume that
    // the newest one is the one we want
    final ServletHolder sh = new ServletHolder(clazz);
    sh.setName(name);
    sh.setInitParameters(params);
    final ServletMapping[] servletMappings = webAppContext.getServletHandler().getServletMappings();
    for (ServletMapping servletMapping : servletMappings) {
        if (servletMapping.containsPathSpec(pathSpec)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found existing " + servletMapping.getServletName() + " servlet at path " + pathSpec + "; will replace mapping" + " with " + sh.getName() + " servlet");
            }
            ServletMapping[] newServletMappings = ArrayUtil.removeFromArray(servletMappings, servletMapping);
            webAppContext.getServletHandler().setServletMappings(newServletMappings);
            break;
        }
    }
    webAppContext.addServlet(sh, pathSpec);
}
Also used : ServletMapping(org.eclipse.jetty.servlet.ServletMapping) ServletHolder(org.eclipse.jetty.servlet.ServletHolder)

Aggregations

ServletMapping (org.eclipse.jetty.servlet.ServletMapping)27 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)20 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)8 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)5 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)3 Source (org.eclipse.jetty.servlet.Source)3 ConstraintAware (org.eclipse.jetty.security.ConstraintAware)2 ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)2 JspConfig (org.eclipse.jetty.servlet.ServletContextHandler.JspConfig)2 Constraint (org.eclipse.jetty.util.security.Constraint)2 MetaData (org.eclipse.jetty.webapp.MetaData)2 Node (org.eclipse.jetty.xml.XmlParser.Node)2 MalformedURLException (java.net.MalformedURLException)1 CertificateException (java.security.cert.CertificateException)1 EventListener (java.util.EventListener)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Servlet (javax.servlet.Servlet)1