Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.jetty.plus.jndi.Resource;
import org.eclipse.jetty.util.IntrospectionUtil;
import org.eclipse.jetty.util.jndi.NamingUtil;
import org.eclipse.jetty.xml.XmlParser;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -167,21 +168,23 @@ public void setUp() throws Exception
Resource res2 = new Resource(ServletContextHandler.ENVIRONMENT.getName(), "eeObject2", eeObject2);

URL webXml = Thread.currentThread().getContextClassLoader().getResource("web.xml");
XmlParser xmlParser = new XmlParser(false);

webDescriptor = new WebDescriptor(context.getResourceFactory().newResource(webXml));
webDescriptor.parse(WebDescriptor.getParser(false));
webDescriptor.parse(xmlParser);

URL frag1Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-1.xml");
fragDescriptor1 = new FragmentDescriptor(context.getResourceFactory().newResource(frag1Xml));
fragDescriptor1.parse(WebDescriptor.getParser(false));
fragDescriptor1.parse(xmlParser);
URL frag2Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-2.xml");
fragDescriptor2 = new FragmentDescriptor(context.getResourceFactory().newResource(frag2Xml));
fragDescriptor2.parse(WebDescriptor.getParser(false));
fragDescriptor2.parse(xmlParser);
URL frag3Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-3.xml");
fragDescriptor3 = new FragmentDescriptor(context.getResourceFactory().newResource(frag3Xml));
fragDescriptor3.parse(WebDescriptor.getParser(false));
fragDescriptor3.parse(xmlParser);
URL frag4Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-4.xml");
fragDescriptor4 = new FragmentDescriptor(context.getResourceFactory().newResource(frag4Xml));
fragDescriptor4.parse(WebDescriptor.getParser(false));
fragDescriptor4.parse(xmlParser);
Thread.currentThread().setContextClassLoader(oldLoader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.eclipse.jetty.xml.XmlParser;
import org.eclipse.jetty.xml.XmlParser.Node;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void testStandardTestWar() throws Exception

Path webXmlPath = targetDir.resolve("WEB-INF/quickstart-web.xml");
WebDescriptor descriptor = new WebDescriptor(webapp.getResourceFactory().newResource(webXmlPath));
descriptor.parse(WebDescriptor.getParser(!QuickStartGeneratorConfiguration.LOG.isDebugEnabled()));
descriptor.parse(new XmlParser(!QuickStartGeneratorConfiguration.LOG.isDebugEnabled()));
Node node = descriptor.getRoot();
assertNotNull(node);

Expand Down Expand Up @@ -153,7 +154,7 @@ public void testSpecWar() throws Exception
assertTrue(Files.exists(webXmlPath), "Path should exist:" + webXmlPath);

WebDescriptor descriptor = new WebDescriptor(webapp.getResourceFactory().newResource(webXmlPath));
descriptor.parse(WebDescriptor.getParser(!QuickStartGeneratorConfiguration.LOG.isDebugEnabled()));
descriptor.parse(new XmlParser(!QuickStartGeneratorConfiguration.LOG.isDebugEnabled()));
Node node = descriptor.getRoot();
assertNotNull(node);

Expand Down Expand Up @@ -217,7 +218,7 @@ public void testJNDIWar() throws Exception
Path webXmlPath = targetDir.resolve("WEB-INF/quickstart-web.xml");
assertTrue(Files.exists(webXmlPath), "Exists: " + webXmlPath);
WebDescriptor descriptor = new WebDescriptor(webapp.getResourceFactory().newResource(webXmlPath));
descriptor.parse(WebDescriptor.getParser(!QuickStartGeneratorConfiguration.LOG.isDebugEnabled()));
descriptor.parse(new XmlParser(!QuickStartGeneratorConfiguration.LOG.isDebugEnabled()));
Node node = descriptor.getRoot();
assertNotNull(node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.Resources;
import org.eclipse.jetty.util.thread.AutoLock;
import org.eclipse.jetty.xml.XmlParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,11 +38,16 @@ public class MetaData
{
private static final Logger LOG = LoggerFactory.getLogger(MetaData.class);

public static final String VALIDATE_XML = "org.eclipse.jetty.ee9.webapp.validateXml";
/**
* @deprecated use {@link WebAppContext#VALIDATE_XML} instead
*/
@Deprecated(since = "12.1.6", forRemoval = true)
public static final String VALIDATE_XML = WebAppContext.VALIDATE_XML;
public static final String ORDERED_LIBS = "jakarta.servlet.context.orderedLibs";

private final AutoLock _lock = new AutoLock();
protected Map<String, OriginInfo> _origins = new HashMap<>();
protected XmlParser _xmlParser = new XmlParser(false);
protected WebDescriptor _webDefaultsRoot;
protected WebDescriptor _webXmlRoot;
protected final List<WebDescriptor> _webOverrideRoots = new ArrayList<>();
Expand All @@ -57,7 +63,6 @@ public class MetaData
protected final List<Resource> _orderedWebInfResources = new ArrayList<>();
protected Ordering _ordering; //can be set to RelativeOrdering by web-default.xml, web.xml, web-override.xml
protected boolean _allowDuplicateFragmentNames = false;
protected boolean _validateXml = false;

public enum Complete
{
Expand Down Expand Up @@ -193,7 +198,7 @@ public void setDefaultsDescriptor(DefaultsDescriptor descriptor)
throws Exception
{
_webDefaultsRoot = descriptor;
_webDefaultsRoot.parse(WebDescriptor.getParser(isValidateXml()));
_webDefaultsRoot.parse(_xmlParser);
if (_webDefaultsRoot.isOrdered())
{
Ordering ordering = getOrdering();
Expand Down Expand Up @@ -222,7 +227,7 @@ public void setWebDescriptor(WebDescriptor descriptor)
throws Exception
{
_webXmlRoot = descriptor;
_webXmlRoot.parse(WebDescriptor.getParser(isValidateXml()));
_webXmlRoot.parse(_xmlParser);
_metaDataComplete = WebDescriptor.isMetaDataComplete(_webXmlRoot);

if (_webXmlRoot.isOrdered())
Expand Down Expand Up @@ -253,7 +258,7 @@ public void setWebDescriptor(WebDescriptor descriptor)
public void addOverrideDescriptor(OverrideDescriptor descriptor)
throws Exception
{
descriptor.parse(WebDescriptor.getParser(isValidateXml()));
descriptor.parse(_xmlParser);

switch (descriptor.getMetaDataComplete())
{
Expand Down Expand Up @@ -308,7 +313,7 @@ public void addFragmentDescriptor(Resource jarResource, FragmentDescriptor descr
//Metadata-complete is not set, or there is no web.xml
_webFragmentResourceMap.put(jarResource, descriptor);
_webFragmentRoots.add(descriptor);
descriptor.parse(WebDescriptor.getParser(isValidateXml()));
descriptor.parse(_xmlParser);

if (descriptor.getName() != null)
{
Expand Down Expand Up @@ -748,7 +753,7 @@ public void setAllowDuplicateFragmentNames(boolean allowDuplicateFragmentNames)
*/
public boolean isValidateXml()
{
return _validateXml;
return _xmlParser.isValidating();
}

/**
Expand All @@ -757,7 +762,33 @@ public boolean isValidateXml()
*/
public void setValidateXml(boolean validateXml)
{
_validateXml = validateXml;
if (_xmlParser.isValidating() != validateXml)
{
_xmlParser = new XmlParser(validateXml);
}
}

/**
* Set the XmlParser to use for handling metadata.
*
* <p>This is useful when you want to configure a custom XML Parser
* with a variety of custom attributes and configurations.</p>
*
* @param xmlParser the XML parser to use.
*/
public void setXmlParser(XmlParser xmlParser)
{
_xmlParser = xmlParser;
}

/**
* The XmlParser in use for this metadata.
*
* @return the in use XML Parser
*/
public XmlParser getXmlParser()
{
return _xmlParser;
}

public Map<String, OriginInfo> getOrigins()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL

public static final String WEB_DEFAULTS_XML = "org/eclipse/jetty/ee9/webapp/webdefault-ee9.xml";
public static final String ERROR_PAGE = "org.eclipse.jetty.server.error_page";
public static final String VALIDATE_XML = "org.eclipse.jetty.ee9.webapp.validateXml";
/**
* @deprecated use {@link WebAppClassLoading#PROTECTED_CLASSES_ATTRIBUTE} instead.
*/
Expand Down Expand Up @@ -576,8 +577,11 @@ protected void doStart() throws Exception
try
{
_metadata.setAllowDuplicateFragmentNames(isAllowDuplicateFragmentNames());
Boolean validate = (Boolean)getAttribute(MetaData.VALIDATE_XML);
_metadata.setValidateXml((validate != null && validate));
Boolean validate = (Boolean)getAttribute(WebAppContext.VALIDATE_XML);
// don't set validate unless it is declared.
// user might be using a custom XmlParser will not want that XmlParser replaced.
if (validate != null)
_metadata.setValidateXml(validate);
wrapConfigurations();
preConfigure();
Thread.currentThread().setContextClassLoader(getClassLoader());
Expand Down Expand Up @@ -686,7 +690,7 @@ public Configurations getConfigurations()
*
* @return Returns the defaultsDescriptor.
*/
@ManagedAttribute(value = "default web.xml deascriptor applied before standard web.xml", readonly = true)
@ManagedAttribute(value = "default web.xml descriptor applied before standard web.xml", readonly = true)
public String getDefaultsDescriptor()
{
return _defaultsDescriptor;
Expand All @@ -709,7 +713,7 @@ public String getOverrideDescriptor()
*
* @return Returns the Override Descriptor list
*/
@ManagedAttribute(value = "web.xml deascriptors applied after standard web.xml", readonly = true)
@ManagedAttribute(value = "web.xml descriptors applied after standard web.xml", readonly = true)
public List<String> getOverrideDescriptors()
{
return Collections.unmodifiableList(_overrideDescriptors);
Expand Down Expand Up @@ -1389,8 +1393,7 @@ protected void stopContext() throws Exception
_configurations.get(i).deconfigure(this);
}

if (_metadata != null)
_metadata.clear();
_metadata.clear();
_metadata = new MetaData();
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class WebDescriptor extends Descriptor
{
private static final Logger LOG = LoggerFactory.getLogger(WebDescriptor.class);

/**
* @deprecated no direct replacement, use of {@link MetaData#getXmlParser()} is encouraged.
*/
@Deprecated(since = "12.1.6", forRemoval = true)
public static XmlParser __nonValidatingStaticParser = newParser(false);
protected MetaData.Complete _metaDataComplete;
protected int _majorVersion = 4; //default to container version
Expand Down Expand Up @@ -61,21 +65,22 @@ public static boolean isMetaDataComplete(WebDescriptor d)
*
* @param validating true if the parser should validate syntax, false otherwise
* @return an XmlParser for web descriptors
* @deprecated use {@link MetaData#getXmlParser()} to control parser behavior.
*/
@Deprecated(since = "12.1.6", forRemoval = true)
public static XmlParser getParser(boolean validating)
{
if (!validating)
return __nonValidatingStaticParser;
else
return newParser(true);
return newParser(validating);
}

/**
* Create a new parser for parsing web descriptors.
*
* @param validating if true, the parser will validate syntax
* @return an XmlParser
* @deprecated use {@link MetaData#getXmlParser()} to control parser behavior.
*/
@Deprecated(since = "12.1.6", forRemoval = true)
public static XmlParser newParser(boolean validating)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testXmlWithXsd(WorkDir workDir) throws Exception

Resource xmlRes = ResourceFactory.root().newResource(xml);
WebDescriptor webDescriptor = new WebDescriptor(xmlRes);
XmlParser xmlParser = WebDescriptor.newParser(true);
XmlParser xmlParser = new XmlParser(true);
// This should not throw an exception, if it does then you have a bad state.
// Such as missing required XML resource entities.
webDescriptor.parse(xmlParser);
Expand Down