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();
}
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();
}
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;
}
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;
}
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/*"));
}
}
Aggregations