Skip to content

Commit d4d8832

Browse files
joakimegregw
andauthored
Issue #10066 - Allow customization of SAXParserFactory and SAXParser in XmlParser (#10299)
Backports of * Issue #10066 - Allow customization of `SAXParserFactory` and `SAXParser` in `XmlParser` (#10067) * Updating various old/moved URL references found across project (`jetty-10.0.x`) (#10098) Consisting of * Allow customization of SAXParserFactory / SAXParser in XmlParser * Introduce method `.getSAXParser()` * Prepending doctype in testcases * More comprehensive changes to redirectEntity config * Updating various old/moved URL references found across project (`jetty-10.0.x`) (#10098) * Now that the migration of `https://eclipse.org/jetty/` to `https://eclipse.dev/jetty/` has occurred, it is time to review the URI use in our project * Added more URIs to XmlConfiguration * Better SAXParseException handling to report resource that is causing the problem * Add missing DOCTYPE declarations * Enforcing unique XML ids --------- Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com> Co-authored-by: Greg Wilkins <gregw@webtide.com>
1 parent 490626e commit d4d8832

File tree

15 files changed

+192
-43
lines changed

15 files changed

+192
-43
lines changed

jetty-deploy/src/test/java/org/eclipse/jetty/deploy/DeploymentTempDirTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ public void stop() throws Exception
101101
public void testTmpDirectory() throws Exception
102102
{
103103
Path warPath = MavenTestingUtils.getTestResourcePath("webapps/foo-webapp-1.war");
104-
String deploymentXml = "<Configure class=\"org.eclipse.jetty.webapp.WebAppContext\">\n" +
104+
String deploymentXml =
105+
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
106+
"<!DOCTYPE Configure PUBLIC \"-//Jetty//Configure//EN\" \"https://eclipse.dev/jetty/configure.dtd\">\n" +
107+
"<Configure class=\"org.eclipse.jetty.webapp.WebAppContext\">\n" +
105108
"<Set name=\"war\">" + warPath + "</Set>\n" +
106109
"<Set name=\"tempDirectory\">" + tmpDir + "</Set>\n" +
107110
"<Set name=\"persistTempDirectory\">false</Set>\n" +
@@ -141,7 +144,10 @@ public void testTmpDirectory() throws Exception
141144
public void testPersistentTmpDirectory() throws Exception
142145
{
143146
Path warPath = MavenTestingUtils.getTestResourcePath("webapps/foo-webapp-1.war");
144-
String deploymentXml = "<Configure class=\"org.eclipse.jetty.webapp.WebAppContext\">\n" +
147+
String deploymentXml =
148+
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
149+
"<!DOCTYPE Configure PUBLIC \"-//Jetty//Configure//EN\" \"https://eclipse.dev/jetty/configure.dtd\">\n" +
150+
"<Configure class=\"org.eclipse.jetty.webapp.WebAppContext\">\n" +
145151
"<Set name=\"war\">" + warPath + "</Set>\n" +
146152
"<Set name=\"tempDirectory\">" + tmpDir + "</Set>\n" +
147153
"<Set name=\"persistTempDirectory\">true</Set>\n" +

jetty-deploy/src/test/resources/context-binding-test-1.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_9_3.dtd">
13
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
24

35
<Call name="addServerClass">

jetty-deploy/src/test/resources/jetty.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.org/jetty/configure_9_3.dtd">
23

34
<!-- =============================================================== -->
45
<!-- Documentation of this file format can be found at: -->

jetty-documentation/src/main/asciidoc/quick-start/configuring/what-to-configure.adoc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ The deployer discovers and hot deploys context IoC descriptors like the followin
141141

142142
[source, xml]
143143
----
144-
<?xml version="1.0" encoding="UTF-8"?>
144+
<?xml version="1.0" encoding="UTF-8"?>
145145
<!DOCTYPE Configure PUBLIC
146-
"-//Mort Bay Consulting//DTD Configure//EN"
147-
"http://www.eclipse.org/jetty/configure_9_3.dtd">
146+
"-//Jetty//Configure//EN"
147+
"https://eclipse.org/jetty/configure_9_3.dtd">
148148
149149
<!--
150150
Configure a custom context for serving javadoc as static resources
@@ -202,10 +202,10 @@ To set the contextPath from within the WAR file, you can include a `WEB-INF/jett
202202

203203
[source, xml]
204204
----
205-
<?xml version="1.0" encoding="UTF-8"?>
205+
<?xml version="1.0" encoding="UTF-8"?>
206206
<!DOCTYPE Configure PUBLIC
207-
"-//Mort Bay Consulting//DTD Configure//EN"
208-
"http://www.eclipse.org/jetty/configure_9_3.dtd">
207+
"-//Jetty//Configure//EN"
208+
"https://eclipse.org/jetty/configure_9_3.dtd">
209209
210210
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
211211
<Set name="contextPath">/contextpath</Set>
@@ -217,10 +217,10 @@ Instead of allowing the WAR file to be discovered by the deployer, an IoC XML fi
217217

218218
[source, xml]
219219
----
220-
<?xml version="1.0" encoding="UTF-8"?>
220+
<?xml version="1.0" encoding="UTF-8"?>
221221
<!DOCTYPE Configure PUBLIC
222-
"-//Mort Bay Consulting//DTD Configure//EN"
223-
"http://www.eclipse.org/jetty/configure_9_3.dtd">
222+
"-//Jetty//Configure//EN"
223+
"https://eclipse.org/jetty/configure_9_3.dtd">
224224
225225
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
226226
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set>

jetty-maven-plugin/src/it/run-mojo-gwt-it/beer-server/src/config/jetty.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_9_3.dtd">
23

34
<Configure id="Server" class="org.eclipse.jetty.server.Server">
45
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">

jetty-maven-plugin/src/it/run-mojo-gwt-it/beer-server/src/main/jettyconf/context.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_9_3.dtd">
3+
14
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
25
<Call name="setInitParameter">
36
<Arg>org.eclipse.jetty.servlet.Default.useFileMappedBuffer</Arg>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<?xml version="1.0"?>
2+
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_9_3.dtd">
23
<Configure />

jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.eclipse.jetty.xml;
2020

21+
import java.io.Closeable;
2122
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.io.StringReader;
@@ -67,6 +68,7 @@
6768
import org.eclipse.jetty.util.resource.Resource;
6869
import org.xml.sax.InputSource;
6970
import org.xml.sax.SAXException;
71+
import org.xml.sax.SAXParseException;
7072

7173
/**
7274
* <p>Configures objects from XML.</p>
@@ -203,7 +205,7 @@ public static String normalizeURI(String uri)
203205
private final String _dtd;
204206
private ConfigurationProcessor _processor;
205207

206-
ConfigurationParser getParser()
208+
public XmlParser getXmlParser()
207209
{
208210
Pool<ConfigurationParser>.Entry entry = __parsers.acquire(ConfigurationParser::new);
209211
if (entry == null)
@@ -220,12 +222,22 @@ ConfigurationParser getParser()
220222
*/
221223
public XmlConfiguration(Resource resource) throws SAXException, IOException
222224
{
223-
try (ConfigurationParser parser = getParser(); InputStream inputStream = resource.getInputStream())
225+
XmlParser parser = getXmlParser();
226+
try (InputStream inputStream = resource.getInputStream())
224227
{
225228
_location = resource;
226229
setConfig(parser.parse(inputStream));
227230
_dtd = parser.getDTD();
228231
}
232+
catch (SAXParseException e)
233+
{
234+
throw new SAXParseException("Unable to parse: " + resource, null, e);
235+
}
236+
finally
237+
{
238+
if (parser instanceof Closeable)
239+
((Closeable)parser).close();
240+
}
229241
}
230242

231243
/**
@@ -257,13 +269,19 @@ public XmlConfiguration(String configuration) throws SAXException, IOException
257269
configuration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
258270
"<!DOCTYPE Configure PUBLIC \"-//Jetty//Configure//EN\" \"http://www.eclipse.org/jetty/configure_9_3.dtd\">" +
259271
configuration;
260-
try (ConfigurationParser parser = getParser(); StringReader reader = new StringReader(configuration))
272+
XmlParser parser = getXmlParser();
273+
try (StringReader reader = new StringReader(configuration))
261274
{
262275
InputSource source = new InputSource(reader);
263276
_location = null;
264277
setConfig(parser.parse(source));
265278
_dtd = parser.getDTD();
266279
}
280+
finally
281+
{
282+
if (parser instanceof Closeable)
283+
((Closeable)parser).close();
284+
}
267285
}
268286

269287
/**
@@ -278,12 +296,18 @@ public XmlConfiguration(String configuration) throws SAXException, IOException
278296
public XmlConfiguration(InputStream configuration) throws SAXException, IOException
279297
{
280298
InputSource source = new InputSource(configuration);
281-
try (ConfigurationParser parser = getParser())
299+
XmlParser parser = getXmlParser();
300+
try
282301
{
283302
_location = null;
284303
setConfig(parser.parse(source));
285304
_dtd = parser.getDTD();
286305
}
306+
finally
307+
{
308+
if (parser instanceof Closeable)
309+
((Closeable)parser).close();
310+
}
287311
}
288312

289313
@Override
@@ -1918,7 +1942,7 @@ else if (arg.toLowerCase(Locale.ENGLISH).endsWith(".properties"))
19181942
}
19191943
}
19201944

1921-
private static class ConfigurationParser extends XmlParser implements AutoCloseable
1945+
private static class ConfigurationParser extends XmlParser implements Closeable
19221946
{
19231947
private final Pool<ConfigurationParser>.Entry _entry;
19241948

@@ -1942,8 +1966,23 @@ private ConfigurationParser(Pool<ConfigurationParser>.Entry entry)
19421966
redirectEntity("http://jetty.mortbay.org/configure.dtd", config93);
19431967
redirectEntity("http://jetty.eclipse.org/configure.dtd", config93);
19441968
redirectEntity("https://jetty.eclipse.org/configure.dtd", config93);
1945-
redirectEntity("http://www.eclipse.org/jetty/configure.dtd", config93);
1946-
redirectEntity("https://www.eclipse.org/jetty/configure.dtd", config93);
1969+
redirectEntity("http://jetty.mortbay.org/configure.dtd", config93);
1970+
1971+
// Register all variations of DOCTYPE entity references for Config 9.3
1972+
String[] schemes = {"http", "https"};
1973+
String[] hosts = {"www.eclipse.org", "eclipse.org", "www.eclipse.dev", "eclipse.dev"};
1974+
String[] paths = {"/jetty/configure.dtd", "/jetty/configure_9_3.dtd"};
1975+
1976+
for (String scheme : schemes)
1977+
{
1978+
for (String host : hosts)
1979+
{
1980+
for (String path : paths)
1981+
{
1982+
redirectEntity(String.format("%s://%s%s", scheme, host, path), config93);
1983+
}
1984+
}
1985+
}
19471986
redirectEntity("-//Mort Bay Consulting//DTD Configure//EN", config93);
19481987
redirectEntity("-//Jetty//Configure//EN", config93);
19491988
}

jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlParser.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public class XmlParser
6969
*/
7070
public XmlParser()
7171
{
72-
SAXParserFactory factory = SAXParserFactory.newInstance();
73-
boolean validatingDft = factory.getClass().toString().startsWith("org.apache.xerces.");
74-
String validatingProp = System.getProperty("org.eclipse.jetty.xml.XmlParser.Validating", validatingDft ? "true" : "false");
72+
SAXParserFactory factory = newSAXParserFactory();
73+
boolean validatingDefault = factory.getClass().toString().contains("org.apache.xerces.");
74+
String validatingProp = System.getProperty("org.eclipse.jetty.xml.XmlParser.Validating", validatingDefault ? "true" : "false");
7575
boolean validating = Boolean.valueOf(validatingProp).booleanValue();
7676
setValidating(validating);
7777
}
@@ -81,11 +81,16 @@ public XmlParser(boolean validating)
8181
setValidating(validating);
8282
}
8383

84+
protected SAXParserFactory newSAXParserFactory()
85+
{
86+
return SAXParserFactory.newInstance();
87+
}
88+
8489
public void setValidating(boolean validating)
8590
{
8691
try
8792
{
88-
SAXParserFactory factory = SAXParserFactory.newInstance();
93+
SAXParserFactory factory = newSAXParserFactory();
8994
factory.setValidating(validating);
9095
_parser = factory.newSAXParser();
9196

@@ -127,6 +132,11 @@ public boolean isValidating()
127132
return _parser.isValidating();
128133
}
129134

135+
public SAXParser getSAXParser()
136+
{
137+
return _parser;
138+
}
139+
130140
public synchronized void redirectEntity(String name, URL entity)
131141
{
132142
if (entity != null)
@@ -261,18 +271,10 @@ protected InputSource resolveEntity(String pid, String sid)
261271
if (pid != null)
262272
entity = _redirectMap.get(pid);
263273
if (entity == null)
264-
entity = _redirectMap.get(sid);
265-
if (entity == null)
266-
{
267-
String dtd = sid;
268-
if (dtd.lastIndexOf('/') >= 0)
269-
dtd = dtd.substring(dtd.lastIndexOf('/') + 1);
270-
271-
if (LOG.isDebugEnabled())
272-
LOG.debug("Can't exact match entity in redirect map, trying " + dtd);
273-
entity = _redirectMap.get(dtd);
274-
}
274+
entity = (URL)_redirectMap.get(sid);
275275

276+
// Only serve entity if found.
277+
// We don't want to serve from unknown hosts or random paths.
276278
if (entity != null)
277279
{
278280
try
@@ -289,6 +291,9 @@ protected InputSource resolveEntity(String pid, String sid)
289291
LOG.ignore(e);
290292
}
291293
}
294+
295+
if (LOG.isDebugEnabled())
296+
LOG.debug("Entity not found for PID:{} / SID:{}", pid, sid);
292297
return null;
293298
}
294299

0 commit comments

Comments
 (0)