Search in sources :

Example 76 with WebAppContext

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

the class TestSecurityAnnotationConversions method testMethodAnnotation.

@Test
public void testMethodAnnotation() throws Exception {
    //ServletSecurity annotation with HttpConstraint of TransportGuarantee.CONFIDENTIAL, and a list of rolesAllowed, and
    //a HttpMethodConstraint for GET method that permits all and has TransportGuarantee.NONE (ie is default)
    WebAppContext wac = makeWebAppContext(Method1Servlet.class.getCanonicalName(), "method1Servlet", new String[] { "/foo/*", "*.foo" });
    //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 PermitAll on the doGet method with a userdata
    //constraint of DC_CONFIDENTIAL inherited from the class
    Constraint expectedConstraint2 = new Constraint();
    expectedConstraint2.setDataConstraint(Constraint.DC_NONE);
    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");
    AnnotationIntrospector introspector = new AnnotationIntrospector();
    ServletSecurityAnnotationHandler annotationHandler = new ServletSecurityAnnotationHandler(wac);
    introspector.registerHandler(annotationHandler);
    introspector.introspect(Method1Servlet.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 77 with WebAppContext

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

the class TestServletAnnotations method testDeclareRoles.

@Test
public void testDeclareRoles() throws Exception {
    WebAppContext wac = new WebAppContext();
    ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
    wac.setSecurityHandler(sh);
    sh.setRoles(new HashSet<String>(Arrays.asList(new String[] { "humpty", "dumpty" })));
    DeclareRolesAnnotationHandler handler = new DeclareRolesAnnotationHandler(wac);
    handler.doHandle(ServletC.class);
    assertTrue(sh.getRoles().contains("alice"));
    assertTrue(sh.getRoles().contains("humpty"));
    assertTrue(sh.getRoles().contains("dumpty"));
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) Test(org.junit.Test)

Example 78 with WebAppContext

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

the class ServerWithAnnotations method main.

public static final void main(String[] args) throws Exception {
    // Create the server
    Server server = new Server(8080);
    // Enable parsing of jndi-related parts of web.xml and jetty-env.xml
    Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
    classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
    // Create a WebApp
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    File warFile = new File("../../jetty-distribution/target/distribution/demo-base/webapps/test.war");
    webapp.setWar(warFile.getAbsolutePath());
    webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/javax.servlet-[^/]*\\.jar$|.*/servlet-api-[^/]*\\.jar$");
    server.setHandler(webapp);
    // Register new transaction manager in JNDI
    // At runtime, the webapp accesses this as java:comp/UserTransaction
    new Transaction(new com.acme.MockUserTransaction());
    // Define an env entry with webapp scope.
    new EnvEntry(webapp, "maxAmount", new Double(100), true);
    // Register a mock DataSource scoped to the webapp
    new Resource(webapp, "jdbc/mydatasource", new com.acme.MockDataSource());
    // Configure a LoginService
    HashLoginService loginService = new HashLoginService();
    loginService.setName("Test Realm");
    loginService.setConfig("src/test/resources/realm.properties");
    server.addBean(loginService);
    server.start();
    server.join();
}
Also used : Server(org.eclipse.jetty.server.Server) Configuration(org.eclipse.jetty.webapp.Configuration) Resource(org.eclipse.jetty.plus.jndi.Resource) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) HashLoginService(org.eclipse.jetty.security.HashLoginService) Transaction(org.eclipse.jetty.plus.jndi.Transaction) File(java.io.File) EnvEntry(org.eclipse.jetty.plus.jndi.EnvEntry)

Example 79 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext 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)

Example 80 with WebAppContext

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

the class TestServletAnnotations method testWebServletAnnotationReplaceDefault.

@Test
public void testWebServletAnnotationReplaceDefault() 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);
    ServletMapping m2 = new ServletMapping();
    m2.setPathSpec("/other");
    m2.setServletName("default");
    //this mapping will be from a default descriptor
    m2.setDefault(true);
    wac.getServletHandler().addServletMapping(m2);
    WebServletAnnotation annotation = new WebServletAnnotation(wac, "org.eclipse.jetty.annotations.ServletD", null);
    annotation.apply();
    //test that only the mapping for "/" was removed from the mappings to the default servlet
    ServletMapping[] resultMappings = wac.getServletHandler().getServletMappings();
    assertNotNull(resultMappings);
    assertEquals(2, resultMappings.length);
    for (ServletMapping r : resultMappings) {
        if (r.getServletName().equals("default")) {
            assertEquals(1, r.getPathSpecs().length);
            assertEquals("/other", r.getPathSpecs()[0]);
        } else if (r.getServletName().equals("DServlet")) {
            assertEquals(2, r.getPathSpecs().length);
            for (String p : r.getPathSpecs()) {
                if (!p.equals("/") && !p.equals("/bah/*"))
                    fail("Unexpected path");
            }
        } else
            fail("Unexpected servlet mapping");
    }
}
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

WebAppContext (org.eclipse.jetty.webapp.WebAppContext)142 Server (org.eclipse.jetty.server.Server)58 File (java.io.File)37 Test (org.junit.Test)29 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)20 ServerConnector (org.eclipse.jetty.server.ServerConnector)18 URL (java.net.URL)16 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)10 URI (java.net.URI)10 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)9 FileWriter (java.io.FileWriter)7 Configuration (org.apache.hadoop.conf.Configuration)7 HashLoginService (org.eclipse.jetty.security.HashLoginService)7 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)7 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)7 ServletMapping (org.eclipse.jetty.servlet.ServletMapping)7 BeforeClass (org.junit.BeforeClass)7 OutputStream (java.io.OutputStream)6 InitialContext (javax.naming.InitialContext)6