Search in sources :

Example 11 with Node

use of org.eclipse.jetty.xml.XmlParser.Node in project jetty.project by eclipse.

the class StandardDescriptorProcessor method addFilterMapping.

public void addFilterMapping(String filterName, XmlParser.Node node, WebAppContext context, Descriptor descriptor) {
    FilterMapping mapping = new FilterMapping();
    mapping.setFilterName(filterName);
    List<String> paths = new ArrayList<String>();
    Iterator<XmlParser.Node> iter = node.iterator("url-pattern");
    while (iter.hasNext()) {
        String p = iter.next().toString(false, true);
        p = ServletPathSpec.normalize(p);
        paths.add(p);
        context.getMetaData().setOrigin(filterName + ".filter.mapping." + p, descriptor);
    }
    mapping.setPathSpecs((String[]) paths.toArray(new String[paths.size()]));
    List<String> names = new ArrayList<String>();
    iter = node.iterator("servlet-name");
    while (iter.hasNext()) {
        String n = ((XmlParser.Node) iter.next()).toString(false, true);
        names.add(n);
    }
    mapping.setServletNames((String[]) names.toArray(new String[names.size()]));
    List<DispatcherType> dispatches = new ArrayList<DispatcherType>();
    iter = node.iterator("dispatcher");
    while (iter.hasNext()) {
        String d = ((XmlParser.Node) iter.next()).toString(false, true);
        dispatches.add(FilterMapping.dispatch(d));
    }
    if (dispatches.size() > 0)
        mapping.setDispatcherTypes(EnumSet.copyOf(dispatches));
    _filterMappings.add(mapping);
}
Also used : Node(org.eclipse.jetty.xml.XmlParser.Node) ArrayList(java.util.ArrayList) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) DispatcherType(javax.servlet.DispatcherType)

Example 12 with Node

use of org.eclipse.jetty.xml.XmlParser.Node in project jetty.project by eclipse.

the class StandardDescriptorProcessor method addServletMapping.

public ServletMapping addServletMapping(String servletName, XmlParser.Node node, WebAppContext context, Descriptor descriptor) {
    ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString()));
    mapping.setServletName(servletName);
    mapping.setDefault(descriptor instanceof DefaultsDescriptor);
    List<String> paths = new ArrayList<String>();
    Iterator<XmlParser.Node> iter = node.iterator("url-pattern");
    while (iter.hasNext()) {
        String p = iter.next().toString(false, true);
        p = ServletPathSpec.normalize(p);
        //check if there is already a mapping for this path
        ListIterator<ServletMapping> listItor = _servletMappings.listIterator();
        boolean found = false;
        while (listItor.hasNext() && !found) {
            ServletMapping sm = listItor.next();
            if (sm.getPathSpecs() != null) {
                for (String ps : sm.getPathSpecs()) {
                    //If its a different servlet, this is only valid to do if the old mapping was from a default descriptor.
                    if (p.equals(ps) && (sm.isDefault() || servletName.equals(sm.getServletName()))) {
                        if (sm.isDefault()) {
                            if (LOG.isDebugEnabled())
                                LOG.debug("{} in mapping {} from defaults descriptor is overridden by ", ps, sm, servletName);
                        } else
                            LOG.warn("Duplicate mapping from {} to {}", p, servletName);
                        //remove ps from the path specs on the existing mapping
                        //if the mapping now has no pathspecs, remove it
                        String[] updatedPaths = ArrayUtil.removeFromArray(sm.getPathSpecs(), ps);
                        if (updatedPaths == null || updatedPaths.length == 0) {
                            if (LOG.isDebugEnabled())
                                LOG.debug("Removed empty mapping {}", sm);
                            listItor.remove();
                        } else {
                            sm.setPathSpecs(updatedPaths);
                            if (LOG.isDebugEnabled())
                                LOG.debug("Removed path {} from mapping {}", p, sm);
                        }
                        found = true;
                        break;
                    }
                }
            }
        }
        paths.add(p);
        context.getMetaData().setOrigin(servletName + ".servlet.mapping." + p, descriptor);
    }
    mapping.setPathSpecs((String[]) paths.toArray(new String[paths.size()]));
    if (LOG.isDebugEnabled())
        LOG.debug("Added mapping {} ", mapping);
    _servletMappings.add(mapping);
    return mapping;
}
Also used : ServletMapping(org.eclipse.jetty.servlet.ServletMapping) Node(org.eclipse.jetty.xml.XmlParser.Node) ArrayList(java.util.ArrayList) Source(org.eclipse.jetty.servlet.Source)

Example 13 with Node

use of org.eclipse.jetty.xml.XmlParser.Node in project jetty.project by eclipse.

the class QuickStartTest method testSpecWar.

@Test
public void testSpecWar() throws Exception {
    PreconfigureSpecWar.main(new String[] {});
    WebDescriptor descriptor = new WebDescriptor(Resource.newResource("./target/test-spec-preconfigured/WEB-INF/quickstart-web.xml"));
    descriptor.setValidating(true);
    descriptor.parse();
    Node node = descriptor.getRoot();
    assertThat(node, Matchers.notNullValue());
    System.setProperty("jetty.home", "target");
    //war file or dir to start
    String war = "target/test-spec-preconfigured";
    //optional jetty context xml file to configure the webapp
    Resource contextXml = Resource.newResource("src/test/resources/test-spec.xml");
    Server server = new Server(0);
    QuickStartWebApp webapp = new QuickStartWebApp();
    webapp.setAutoPreconfigure(true);
    webapp.setWar(war);
    webapp.setContextPath("/");
    //apply context xml file
    if (contextXml != null) {
        // System.err.println("Applying "+contextXml);
        XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURL());
        xmlConfiguration.configure(webapp);
    }
    server.setHandler(webapp);
    server.start();
    URL url = new URL("http://127.0.0.1:" + server.getBean(NetworkConnector.class).getLocalPort() + "/");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    assertEquals(200, connection.getResponseCode());
    assertThat(IO.toString((InputStream) connection.getContent()), Matchers.containsString("Test Specification WebApp"));
    server.stop();
}
Also used : WebDescriptor(org.eclipse.jetty.webapp.WebDescriptor) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) InputStream(java.io.InputStream) Node(org.eclipse.jetty.xml.XmlParser.Node) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 14 with Node

use of org.eclipse.jetty.xml.XmlParser.Node in project jetty.project by eclipse.

the class QuickStartTest method testJNDIWar.

@Test
public void testJNDIWar() throws Exception {
    PreconfigureJNDIWar.main(new String[] {});
    WebDescriptor descriptor = new WebDescriptor(Resource.newResource("./target/test-jndi-preconfigured/WEB-INF/quickstart-web.xml"));
    descriptor.setValidating(true);
    descriptor.parse();
    Node node = descriptor.getRoot();
    assertThat(node, Matchers.notNullValue());
    System.setProperty("jetty.home", "target");
    //war file or dir to start
    String war = "target/test-jndi-preconfigured";
    //optional jetty context xml file to configure the webapp
    Resource contextXml = Resource.newResource("src/test/resources/test-jndi.xml");
    Server server = new Server(0);
    QuickStartWebApp webapp = new QuickStartWebApp();
    webapp.setAutoPreconfigure(true);
    webapp.setWar(war);
    webapp.setContextPath("/");
    //apply context xml file
    if (contextXml != null) {
        // System.err.println("Applying "+contextXml);
        XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURI().toURL());
        xmlConfiguration.configure(webapp);
    }
    server.setHandler(webapp);
    server.start();
    URL url = new URL("http://127.0.0.1:" + server.getBean(NetworkConnector.class).getLocalPort() + "/");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    assertEquals(200, connection.getResponseCode());
    String content = IO.toString((InputStream) connection.getContent());
    assertThat(content, Matchers.containsString("JNDI Test WebApp"));
    server.stop();
}
Also used : WebDescriptor(org.eclipse.jetty.webapp.WebDescriptor) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) Node(org.eclipse.jetty.xml.XmlParser.Node) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Node (org.eclipse.jetty.xml.XmlParser.Node)14 XmlParser (org.eclipse.jetty.xml.XmlParser)9 ArrayList (java.util.ArrayList)4 Source (org.eclipse.jetty.servlet.Source)4 Constraint (org.eclipse.jetty.util.security.Constraint)4 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 Server (org.eclipse.jetty.server.Server)3 Resource (org.eclipse.jetty.util.resource.Resource)3 WebDescriptor (org.eclipse.jetty.webapp.WebDescriptor)3 XmlConfiguration (org.eclipse.jetty.xml.XmlConfiguration)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 ConstraintAware (org.eclipse.jetty.security.ConstraintAware)2 ServletMapping (org.eclipse.jetty.servlet.ServletMapping)2 HashSet (java.util.HashSet)1 DispatcherType (javax.servlet.DispatcherType)1 MultipartConfigElement (javax.servlet.MultipartConfigElement)1 ServletException (javax.servlet.ServletException)1 SessionTrackingMode (javax.servlet.SessionTrackingMode)1