Skip to content

Commit 845f20f

Browse files
- Add a flag to ignore XML errors in the EwsXmlReader, and enabled by default (from OfficeDev#353)
- Add a unit test (taken from OfficeDev@e7b7505)
1 parent a102ead commit 845f20f

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

src/main/java/microsoft/exchange/webservices/data/core/EwsServiceMultiResponseXmlReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private static XMLEventReader createXmlReader(InputStream stream)
9898
* @throws Exception on error
9999
*/
100100
@Override
101-
protected XMLEventReader initializeXmlReader(InputStream stream)
101+
protected XMLEventReader initializeXmlReader(InputStream stream, boolean ignoreErrors)
102102
throws Exception {
103103
return createXmlReader(stream);
104104
}

src/main/java/microsoft/exchange/webservices/data/core/EwsXmlReader.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
package microsoft.exchange.webservices.data.core;
2525

26+
import com.sun.org.apache.xerces.internal.impl.Constants;
27+
import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
2628
import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
2729
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlDeserializationException;
2830
import microsoft.exchange.webservices.data.misc.OutParam;
@@ -63,6 +65,13 @@ public class EwsXmlReader {
6365
*/
6466
private static final int ReadWriteBufferSize = 4096;
6567

68+
/**
69+
* Default setting for ignore xml errors
70+
*/
71+
72+
private static final boolean IgnoreErrorsDefault = true;
73+
74+
6675
/**
6776
* The xml reader.
6877
*/
@@ -85,7 +94,17 @@ public class EwsXmlReader {
8594
* @throws Exception on error
8695
*/
8796
public EwsXmlReader(InputStream stream) throws Exception {
88-
this.xmlReader = initializeXmlReader(stream);
97+
this.xmlReader = initializeXmlReader(stream, IgnoreErrorsDefault);
98+
}
99+
100+
/**
101+
* Initializes a new instance of the EwsXmlReader class.
102+
*
103+
* @param stream the stream
104+
* @throws Exception on error
105+
*/
106+
public EwsXmlReader(InputStream stream, boolean ignoreErrors) throws Exception {
107+
this.xmlReader = initializeXmlReader(stream, ignoreErrors);
89108
}
90109

91110
/**
@@ -95,13 +114,22 @@ public EwsXmlReader(InputStream stream) throws Exception {
95114
* @return An XML reader to use.
96115
* @throws Exception on error
97116
*/
98-
protected XMLEventReader initializeXmlReader(InputStream stream) throws Exception {
117+
protected XMLEventReader initializeXmlReader(InputStream stream, boolean ignoreErrors) throws Exception {
99118
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
100119
inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
101120

102-
return inputFactory.createXMLEventReader(stream);
103-
}
121+
XMLEventReader reader = inputFactory.createXMLEventReader(stream);
104122

123+
if (ignoreErrors) {
124+
//continue after fatal error to prevent "invalid character reference"
125+
XMLErrorReporter errorReporter =
126+
(XMLErrorReporter) reader.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY);
127+
128+
errorReporter.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE, true);
129+
}
130+
131+
return reader;
132+
}
105133

106134
/**
107135
* Formats the name of the element.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package microsoft.exchange.webservices.data.core;
2+
3+
import microsoft.exchange.webservices.data.security.XmlNodeType;
4+
import org.junit.Assert;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.ExpectedException;
8+
9+
import javax.xml.stream.XMLStreamException;
10+
11+
import java.io.ByteArrayInputStream;
12+
13+
public class EwsXmlReaderIgnoreErrorsTest {
14+
15+
/*
16+
From: https://github.com/OfficeDev/ews-java-api/pull/409/commits/e7b7505bc2d8e5432b69d412392387f71fe7bdb5
17+
*/
18+
19+
@Rule
20+
public final ExpectedException exception = ExpectedException.none();
21+
22+
final String validDocument = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
23+
"<test>testContent</test>";
24+
25+
final String invalidDocument = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
26+
"<test>test&#x5;Content</test>";
27+
28+
@Test
29+
public void testReadValidDocument() throws Exception {
30+
byte[] bytes = validDocument.getBytes("UTF-8");
31+
EwsXmlReader impl = new EwsXmlReader(new ByteArrayInputStream(bytes), false);
32+
impl.read(new XmlNodeType(XmlNodeType.START_DOCUMENT));
33+
impl.read(new XmlNodeType(XmlNodeType.START_ELEMENT));
34+
String content = impl.readValue();
35+
Assert.assertEquals(content, "testContent");
36+
impl.read(new XmlNodeType(XmlNodeType.END_DOCUMENT));
37+
}
38+
39+
@Test
40+
public void testReadInvalidDocument() throws Exception {
41+
byte[] bytes = invalidDocument.getBytes("UTF-8");
42+
EwsXmlReader impl = new EwsXmlReader(new ByteArrayInputStream(bytes), false);
43+
impl.read(new XmlNodeType(XmlNodeType.START_DOCUMENT));
44+
impl.read(new XmlNodeType(XmlNodeType.START_ELEMENT));
45+
exception.expect(XMLStreamException.class);
46+
String content = impl.readValue();
47+
Assert.assertEquals(content, "test\u0005Content");
48+
}
49+
50+
@Test
51+
public void testReadInvalidDocumentWithIgnoreErrors() throws Exception {
52+
byte[] bytes = invalidDocument.getBytes("UTF-8");
53+
EwsXmlReader impl = new EwsXmlReader(new ByteArrayInputStream(bytes), true);
54+
impl.read(new XmlNodeType(XmlNodeType.START_DOCUMENT));
55+
impl.read(new XmlNodeType(XmlNodeType.START_ELEMENT));
56+
String content = impl.readValue();
57+
Assert.assertEquals(content, "test\u0005Content");
58+
impl.read(new XmlNodeType(XmlNodeType.END_DOCUMENT));
59+
}
60+
}

0 commit comments

Comments
 (0)