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