Search in sources :

Example 26 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class TestSchemaValidation method testWebapp_2_3.

@Test
public void testWebapp_2_3() throws Exception {
    XmlErrorHandler handler = new XmlErrorHandler();
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
    digester.setErrorHandler(handler);
    digester.push(new WebXml());
    WebXml desc = (WebXml) digester.parse(new File("test/webapp-2.3/WEB-INF/web.xml"));
    Assert.assertEquals("2.3", desc.getVersion());
    Assert.assertEquals(XmlIdentifiers.WEB_23_PUBLIC, desc.getPublicId());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) WebRuleSet(org.apache.tomcat.util.descriptor.web.WebRuleSet) File(java.io.File) Test(org.junit.Test)

Example 27 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class TestSchemaValidation method testWebapp_2_4.

@Test
public void testWebapp_2_4() throws Exception {
    XmlErrorHandler handler = new XmlErrorHandler();
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
    digester.setErrorHandler(handler);
    digester.push(new WebXml());
    WebXml desc = (WebXml) digester.parse(new File("test/webapp-2.4/WEB-INF/web.xml"));
    Assert.assertEquals("2.4", desc.getVersion());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) WebRuleSet(org.apache.tomcat.util.descriptor.web.WebRuleSet) File(java.io.File) Test(org.junit.Test)

Example 28 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class TestWebXml method getDefaultWebXmlFragment.

// A simplified copy of ContextConfig.getDefaultWebXmlFragment().
// Assuming that global web.xml exists, host-specific web.xml does not exist.
private WebXml getDefaultWebXmlFragment() throws IOException, SAXException {
    InputSource globalWebXml = new InputSource(new File("conf/web.xml").getAbsoluteFile().toURI().toString());
    WebXml webXmlDefaultFragment = new WebXml();
    webXmlDefaultFragment.setOverridable(true);
    webXmlDefaultFragment.setDistributable(true);
    webXmlDefaultFragment.setAlwaysAddWelcomeFiles(false);
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(), true);
    XmlErrorHandler handler = new XmlErrorHandler();
    digester.setErrorHandler(handler);
    digester.push(webXmlDefaultFragment);
    digester.parse(globalWebXml);
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
    webXmlDefaultFragment.setReplaceWelcomeFiles(true);
    // Assert that web.xml was parsed and is not empty. Default servlet is known to be there.
    Assert.assertNotNull(webXmlDefaultFragment.getServlets().get("default"));
    // Manually add some version specific features to ensure that these do
    // not cause problems for the merged web.xml
    // Filters were added in 2.3 so should be excluded in 2.2
    FilterDef filterDef = new FilterDef();
    filterDef.setFilterClass("org.apache.tomcat.DummyFilter");
    filterDef.setFilterName("Dummy");
    webXmlDefaultFragment.addFilter(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName("Dummy");
    filterMap.addURLPatternDecoded("/*");
    webXmlDefaultFragment.addFilterMapping(filterMap);
    // Listeners were added in 2.3 so should be excluded in 2.2
    webXmlDefaultFragment.addListener("org.apache.tomcat.DummyListener");
    // resource-env-ref was added in 2.3 so should be excluded in 2.2
    ContextResourceEnvRef resourceEnvRef = new ContextResourceEnvRef();
    resourceEnvRef.setName("dummy");
    resourceEnvRef.setType("dummy");
    webXmlDefaultFragment.addResourceEnvRef(resourceEnvRef);
    // ejb-local-ref was added in 2.3 so should be excluded in 2.2
    ContextLocalEjb ejbLocalRef = new ContextLocalEjb();
    ejbLocalRef.setName("dummy");
    ejbLocalRef.setType("Session");
    ejbLocalRef.setLocal("dummy");
    ejbLocalRef.setHome("dummy");
    webXmlDefaultFragment.addEjbLocalRef(ejbLocalRef);
    // Servlet/run-as was added in 2.3 so should be excluded in 2.2
    ServletDef servletDef = new ServletDef();
    servletDef.setServletName("Dummy");
    servletDef.setServletClass("org.apache.tomcat.DummyServlet");
    servletDef.setRunAs("dummy");
    webXmlDefaultFragment.addServlet(servletDef);
    webXmlDefaultFragment.addServletMapping("/dummy", "Dummy");
    // resource-ref/res-sharing-scope was added in 2.3 so should be excluded
    // in 2.2
    ContextResource contextResource = new ContextResource();
    contextResource.setName("dummy");
    contextResource.setType("dummy");
    contextResource.setAuth("Container");
    contextResource.setScope("Shareable");
    webXmlDefaultFragment.addResourceRef(contextResource);
    // security-constraint/display-name was added in 2.3 so should be
    // excluded in 2.2
    SecurityConstraint sc = new SecurityConstraint();
    sc.setDisplayName("dummy");
    SecurityCollection collection = new SecurityCollection();
    collection.setName("dummy");
    collection.addPatternDecoded("/*");
    collection.addMethod("DELETE");
    sc.addCollection(collection);
    webXmlDefaultFragment.addSecurityConstraint(sc);
    // service-ref was added in 2.4 so should be excluded in 2.3 and earlier
    ContextService serviceRef = new ContextService();
    serviceRef.setName("dummy");
    serviceRef.setInterface("dummy");
    webXmlDefaultFragment.addServiceRef(serviceRef);
    // message-destination-ref was added in 2.4 so should be excluded in 2.3
    // and earlier
    MessageDestinationRef mdRef = new MessageDestinationRef();
    mdRef.setName("dummy");
    mdRef.setType("dummy");
    mdRef.setUsage("Consumes");
    webXmlDefaultFragment.addMessageDestinationRef(mdRef);
    // message-destination was added in 2.4 so should be excluded in 2.3
    // and earlier
    MessageDestination md = new MessageDestination();
    md.setName("dummy");
    webXmlDefaultFragment.addMessageDestination(md);
    // local-encoding-mapping-list was added in 2.4 so should be excluded in
    // 2.3 and earlier
    webXmlDefaultFragment.addLocaleEncodingMapping("en", "UTF-8");
    // jsp-config was added in Servlet 2.4
    webXmlDefaultFragment.addTaglib("dummy", "dummy");
    // filter-mapping/dispatcher added in Servlet 2.4
    filterMap.setDispatcher("REQUEST");
    // listener-[description|display-name|icon] added in Servlet 2.4
    // None of these are supported in WebXml
    // filter-mapping/dispatcher/ASYNC added in Servlet 3.0
    filterMap.setDispatcher("ASYNC");
    // error-page with just location allowed in Servlet 3.0+
    ErrorPage errorPage = new ErrorPage();
    errorPage.setLocation("/dummy");
    webXmlDefaultFragment.addErrorPage(errorPage);
    // async-supported added to Servlet and Filter in 3.0
    filterDef.setAsyncSupported("false");
    servletDef.setAsyncSupported("false");
    // session-cookie-config added in 3.0
    SessionConfig sessionConfig = new SessionConfig();
    sessionConfig.setCookieDomain("dummy");
    webXmlDefaultFragment.setSessionConfig(sessionConfig);
    // http-method-omission added in Servlet 3.0
    // Let this trigger a validation error as dropping it silently could
    // be a security concern
    // multi-part-config added in Servlet 3.0
    MultipartDef multiPart = new MultipartDef();
    servletDef.setMultipartDef(multiPart);
    // deny-uncovered-http-methods added in Servlet 3.1
    webXmlDefaultFragment.setDenyUncoveredHttpMethods(true);
    return webXmlDefaultFragment;
}
Also used : InputSource(org.xml.sax.InputSource) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) File(java.io.File)

Aggregations

Digester (org.apache.tomcat.util.digester.Digester)28 File (java.io.File)14 XmlErrorHandler (org.apache.tomcat.util.descriptor.XmlErrorHandler)11 IOException (java.io.IOException)8 WebRuleSet (org.apache.tomcat.util.descriptor.web.WebRuleSet)8 WebXml (org.apache.tomcat.util.descriptor.web.WebXml)8 Test (org.junit.Test)8 InputStream (java.io.InputStream)6 FileInputStream (java.io.FileInputStream)5 InputSource (org.xml.sax.InputSource)5 LifecycleException (org.apache.catalina.LifecycleException)4 SAXParseException (org.xml.sax.SAXParseException)3 ConnectException (java.net.ConnectException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 BufferedInputStream (java.io.BufferedInputStream)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 Socket (java.net.Socket)1