Search in sources :

Example 1 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class TestServletAnnotations method testServletAnnotation.

@Test
public void testServletAnnotation() throws Exception {
    List<String> classes = new ArrayList<String>();
    classes.add("org.eclipse.jetty.annotations.ServletC");
    AnnotationParser parser = new AnnotationParser();
    WebAppContext wac = new WebAppContext();
    List<DiscoveredAnnotation> results = new ArrayList<DiscoveredAnnotation>();
    TestWebServletAnnotationHandler handler = new TestWebServletAnnotationHandler(wac, results);
    parser.parse(Collections.singleton(handler), classes);
    assertEquals(1, results.size());
    assertTrue(results.get(0) instanceof WebServletAnnotation);
    results.get(0).apply();
    ServletHolder[] holders = wac.getServletHandler().getServlets();
    assertNotNull(holders);
    assertEquals(1, holders.length);
    // Verify servlet annotations
    ServletHolder cholder = holders[0];
    assertThat("Servlet Name", cholder.getName(), is("CServlet"));
    assertThat("InitParameter[x]", cholder.getInitParameter("x"), is("y"));
    assertThat("Init Order", cholder.getInitOrder(), is(2));
    assertThat("Async Supported", cholder.isAsyncSupported(), is(false));
    // Verify mappings
    ServletMapping[] mappings = wac.getServletHandler().getServletMappings();
    assertNotNull(mappings);
    assertEquals(1, mappings.length);
    String[] paths = mappings[0].getPathSpecs();
    assertNotNull(paths);
    assertEquals(2, paths.length);
}
Also used : ServletMapping(org.eclipse.jetty.servlet.ServletMapping) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ArrayList(java.util.ArrayList) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) DiscoveredAnnotation(org.eclipse.jetty.webapp.DiscoveredAnnotation) Test(org.junit.Test)

Example 2 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class TestAnnotationConfiguration method testGetFragmentFromJar.

@Test
public void testGetFragmentFromJar() throws Exception {
    String dir = MavenTestingUtils.getTargetTestingDir("getFragmentFromJar").getAbsolutePath();
    File file = new File(dir);
    file = new File(file.getCanonicalPath());
    URL url = file.toURL();
    Resource jar1 = Resource.newResource(url + "file.jar");
    AnnotationConfiguration config = new AnnotationConfiguration();
    WebAppContext wac = new WebAppContext();
    List<FragmentDescriptor> frags = new ArrayList<FragmentDescriptor>();
    frags.add(new FragmentDescriptor(Resource.newResource("jar:" + url + "file.jar!/fooa.props")));
    frags.add(new FragmentDescriptor(Resource.newResource("jar:" + url + "file2.jar!/foob.props")));
    assertNotNull(config.getFragmentFromJar(jar1, frags));
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Resource(org.eclipse.jetty.util.resource.Resource) ArrayList(java.util.ArrayList) File(java.io.File) FragmentDescriptor(org.eclipse.jetty.webapp.FragmentDescriptor) URL(java.net.URL) Test(org.junit.Test)

Example 3 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class TestSecurityAnnotationConversions method testMethodAnnotation2.

@Test
public void testMethodAnnotation2() throws Exception {
    //A ServletSecurity annotation that has HttpConstraint of CONFIDENTIAL with defined roles, but a
    //HttpMethodConstraint for GET that permits all, but also requires CONFIDENTIAL
    WebAppContext wac = makeWebAppContext(Method2Servlet.class.getCanonicalName(), "method2Servlet", new String[] { "/foo/*", "*.foo" });
    AnnotationIntrospector introspector = new AnnotationIntrospector();
    ServletSecurityAnnotationHandler annotationHandler = new ServletSecurityAnnotationHandler(wac);
    introspector.registerHandler(annotationHandler);
    //set up the expected outcomes: - a Constraint for the RolesAllowed on the class
    //with userdata constraint of DC_CONFIDENTIAL
    //and mappings for each of the pathSpecs
    Constraint expectedConstraint1 = new Constraint();
    expectedConstraint1.setAuthenticate(true);
    expectedConstraint1.setRoles(new String[] { "tom", "dick", "harry" });
    expectedConstraint1.setDataConstraint(Constraint.DC_CONFIDENTIAL);
    //a Constraint for the Permit on the GET method with a userdata
    //constraint of DC_CONFIDENTIAL
    Constraint expectedConstraint2 = new Constraint();
    expectedConstraint2.setDataConstraint(Constraint.DC_CONFIDENTIAL);
    ConstraintMapping[] expectedMappings = new ConstraintMapping[4];
    expectedMappings[0] = new ConstraintMapping();
    expectedMappings[0].setConstraint(expectedConstraint1);
    expectedMappings[0].setPathSpec("/foo/*");
    expectedMappings[0].setMethodOmissions(new String[] { "GET" });
    expectedMappings[1] = new ConstraintMapping();
    expectedMappings[1].setConstraint(expectedConstraint1);
    expectedMappings[1].setPathSpec("*.foo");
    expectedMappings[1].setMethodOmissions(new String[] { "GET" });
    expectedMappings[2] = new ConstraintMapping();
    expectedMappings[2].setConstraint(expectedConstraint2);
    expectedMappings[2].setPathSpec("/foo/*");
    expectedMappings[2].setMethod("GET");
    expectedMappings[3] = new ConstraintMapping();
    expectedMappings[3].setConstraint(expectedConstraint2);
    expectedMappings[3].setPathSpec("*.foo");
    expectedMappings[3].setMethod("GET");
    introspector.introspect(Method2Servlet.class);
    compareResults(expectedMappings, ((ConstraintAware) wac.getSecurityHandler()).getConstraintMappings());
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) HttpConstraint(javax.servlet.annotation.HttpConstraint) HttpMethodConstraint(javax.servlet.annotation.HttpMethodConstraint) Constraint(org.eclipse.jetty.util.security.Constraint) Test(org.junit.Test)

Example 4 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class TestSecurityAnnotationConversions method makeWebAppContext.

private WebAppContext makeWebAppContext(String className, String servletName, String[] paths) {
    WebAppContext wac = new WebAppContext();
    ServletHolder[] holders = new ServletHolder[1];
    holders[0] = new ServletHolder();
    holders[0].setClassName(className);
    holders[0].setName(servletName);
    holders[0].setServletHandler(wac.getServletHandler());
    wac.getServletHandler().setServlets(holders);
    wac.setSecurityHandler(new ConstraintSecurityHandler());
    ServletMapping[] servletMappings = new ServletMapping[1];
    servletMappings[0] = new ServletMapping();
    servletMappings[0].setPathSpecs(paths);
    servletMappings[0].setServletName(servletName);
    wac.getServletHandler().setServletMappings(servletMappings);
    return wac;
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ServletMapping(org.eclipse.jetty.servlet.ServletMapping) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler)

Example 5 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class TestSecurityAnnotationConversions method testDenyAllOnClass.

@Test
public void testDenyAllOnClass() throws Exception {
    WebAppContext wac = makeWebAppContext(DenyServlet.class.getCanonicalName(), "denyServlet", new String[] { "/foo/*", "*.foo" });
    //Assume we found 1 servlet with a @HttpConstraint with value=EmptyRoleSemantic.DENY security annotation
    ServletSecurityAnnotationHandler annotationHandler = new ServletSecurityAnnotationHandler(wac);
    AnnotationIntrospector introspector = new AnnotationIntrospector();
    introspector.registerHandler(annotationHandler);
    //set up the expected outcomes:
    //1 ConstraintMapping per ServletMapping pathSpec
    Constraint expectedConstraint = new Constraint();
    expectedConstraint.setAuthenticate(true);
    expectedConstraint.setDataConstraint(Constraint.DC_NONE);
    ConstraintMapping[] expectedMappings = new ConstraintMapping[2];
    expectedMappings[0] = new ConstraintMapping();
    expectedMappings[0].setConstraint(expectedConstraint);
    expectedMappings[0].setPathSpec("/foo/*");
    expectedMappings[1] = new ConstraintMapping();
    expectedMappings[1].setConstraint(expectedConstraint);
    expectedMappings[1].setPathSpec("*.foo");
    introspector.introspect(DenyServlet.class);
    compareResults(expectedMappings, ((ConstraintAware) wac.getSecurityHandler()).getConstraintMappings());
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) HttpConstraint(javax.servlet.annotation.HttpConstraint) HttpMethodConstraint(javax.servlet.annotation.HttpMethodConstraint) Constraint(org.eclipse.jetty.util.security.Constraint) Test(org.junit.Test)

Aggregations

WebAppContext (org.eclipse.jetty.webapp.WebAppContext)291 Server (org.eclipse.jetty.server.Server)108 File (java.io.File)74 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)38 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)35 IOException (java.io.IOException)31 ServerConnector (org.eclipse.jetty.server.ServerConnector)31 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)31 Test (org.junit.Test)27 ArrayList (java.util.ArrayList)22 URL (java.net.URL)21 URISyntaxException (java.net.URISyntaxException)20 Handler (org.eclipse.jetty.server.Handler)17 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)17 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)15 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)15 WebXmlConfiguration (org.eclipse.jetty.webapp.WebXmlConfiguration)15 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)14 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)14 Resource (org.eclipse.jetty.util.resource.Resource)13