Search in sources :

Example 6 with ServletMapping

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

the class ServletMappingMBean method getObjectNameBasis.

public String getObjectNameBasis() {
    if (_managed != null && _managed instanceof ServletMapping) {
        ServletMapping mapping = (ServletMapping) _managed;
        String name = mapping.getServletName();
        if (name != null)
            return name;
    }
    return super.getObjectNameBasis();
}
Also used : ServletMapping(org.eclipse.jetty.servlet.ServletMapping)

Example 7 with ServletMapping

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

the class ServletSecurityAnnotationHandler method doHandle.

/**
     * @see org.eclipse.jetty.annotations.AnnotationIntrospector.IntrospectableAnnotationHandler#handle(java.lang.Class)
     */
public void doHandle(Class clazz) {
    if (!(_context.getSecurityHandler() instanceof ConstraintAware)) {
        LOG.warn("SecurityHandler not ConstraintAware, skipping security annotation processing");
        return;
    }
    ServletSecurity servletSecurity = (ServletSecurity) clazz.getAnnotation(ServletSecurity.class);
    if (servletSecurity == null)
        return;
    //If there are already constraints defined (ie from web.xml) that match any 
    //of the url patterns defined for this servlet, then skip the security annotation.
    List<ServletMapping> servletMappings = getServletMappings(clazz.getCanonicalName());
    List<ConstraintMapping> constraintMappings = ((ConstraintAware) _context.getSecurityHandler()).getConstraintMappings();
    if (constraintsExist(servletMappings, constraintMappings)) {
        LOG.warn("Constraints already defined for " + clazz.getName() + ", skipping ServletSecurity annotation");
        return;
    }
    //Make a fresh list
    constraintMappings = new ArrayList<ConstraintMapping>();
    ServletSecurityElement securityElement = new ServletSecurityElement(servletSecurity);
    for (ServletMapping sm : servletMappings) {
        for (String url : sm.getPathSpecs()) {
            _context.getMetaData().setOrigin("constraint.url." + url, servletSecurity, clazz);
            constraintMappings.addAll(ConstraintSecurityHandler.createConstraintsWithMappingsForPath(clazz.getName(), url, securityElement));
        }
    }
    //set up the security constraints produced by the annotation
    ConstraintAware securityHandler = (ConstraintAware) _context.getSecurityHandler();
    for (ConstraintMapping m : constraintMappings) securityHandler.addConstraintMapping(m);
    //Servlet Spec 3.1 requires paths with uncovered http methods to be reported
    securityHandler.checkPathsWithUncoveredHttpMethods();
}
Also used : ServletMapping(org.eclipse.jetty.servlet.ServletMapping) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) ServletSecurity(javax.servlet.annotation.ServletSecurity) ConstraintAware(org.eclipse.jetty.security.ConstraintAware) ServletSecurityElement(javax.servlet.ServletSecurityElement)

Example 8 with ServletMapping

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

the class ServletSecurityAnnotationHandler method getServletMappings.

/**
     * Get the ServletMappings for the servlet's class.
     * @param className the class name
     * @return the servlet mappings for the class
     */
protected List<ServletMapping> getServletMappings(String className) {
    List<ServletMapping> results = new ArrayList<ServletMapping>();
    ServletMapping[] mappings = _context.getServletHandler().getServletMappings();
    for (ServletMapping mapping : mappings) {
        //Check the name of the servlet that this mapping applies to, and then find the ServletHolder for it to find it's class
        ServletHolder holder = _context.getServletHandler().getServlet(mapping.getServletName());
        if (holder.getClassName() != null && holder.getClassName().equals(className))
            results.add(mapping);
    }
    return results;
}
Also used : ServletMapping(org.eclipse.jetty.servlet.ServletMapping) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ArrayList(java.util.ArrayList)

Example 9 with ServletMapping

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

the class WebServletAnnotation method getServletMappingsForServlet.

/**
     * @param name
     * @return
     */
private List<ServletMapping> getServletMappingsForServlet(String name) {
    ServletMapping[] allMappings = _context.getServletHandler().getServletMappings();
    if (allMappings == null)
        return Collections.emptyList();
    List<ServletMapping> mappings = new ArrayList<ServletMapping>();
    for (ServletMapping m : allMappings) {
        if (m.getServletName() != null && name.equals(m.getServletName())) {
            mappings.add(m);
        }
    }
    return mappings;
}
Also used : ServletMapping(org.eclipse.jetty.servlet.ServletMapping) ArrayList(java.util.ArrayList)

Example 10 with ServletMapping

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

the class TestServletAnnotations method testWebServletAnnotationOverrideDefault.

@Test
public void testWebServletAnnotationOverrideDefault() throws Exception {
    //if the existing servlet mapping TO A DIFFERENT SERVLET IS from a default descriptor we
    //DO allow the annotation to replace the mapping.
    WebAppContext wac = new WebAppContext();
    ServletHolder defaultServlet = new ServletHolder();
    defaultServlet.setClassName("org.eclipse.jetty.servlet.DefaultServlet");
    defaultServlet.setName("default");
    wac.getServletHandler().addServlet(defaultServlet);
    ServletMapping m = new ServletMapping();
    m.setPathSpec("/");
    m.setServletName("default");
    //this mapping will be from a default descriptor
    m.setDefault(true);
    wac.getServletHandler().addServletMapping(m);
    WebServletAnnotation annotation = new WebServletAnnotation(wac, "org.eclipse.jetty.annotations.ServletD", null);
    annotation.apply();
    //test that as the original servlet mapping had only 1 pathspec, then the whole
    //servlet mapping should be deleted as that pathspec will be remapped to the DServlet
    ServletMapping[] resultMappings = wac.getServletHandler().getServletMappings();
    assertNotNull(resultMappings);
    assertEquals(1, resultMappings.length);
    assertEquals(2, resultMappings[0].getPathSpecs().length);
    resultMappings[0].getServletName().equals("DServlet");
    for (String s : resultMappings[0].getPathSpecs()) {
        assertTrue(s.equals("/") || s.equals("/bah/*"));
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ServletMapping(org.eclipse.jetty.servlet.ServletMapping) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Test(org.junit.Test)

Aggregations

ServletMapping (org.eclipse.jetty.servlet.ServletMapping)20 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)14 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Source (org.eclipse.jetty.servlet.Source)3 ConstraintAware (org.eclipse.jetty.security.ConstraintAware)2 ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)2 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)2 JspConfig (org.eclipse.jetty.servlet.ServletContextHandler.JspConfig)2 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)2 Constraint (org.eclipse.jetty.util.security.Constraint)2 MetaData (org.eclipse.jetty.webapp.MetaData)2 Node (org.eclipse.jetty.xml.XmlParser.Node)2 EventListener (java.util.EventListener)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Servlet (javax.servlet.Servlet)1 ServletSecurityElement (javax.servlet.ServletSecurityElement)1 SessionCookieConfig (javax.servlet.SessionCookieConfig)1