mirror of
https://github.com/moparisthebest/davmail
synced 2025-01-05 18:58:02 -05:00
Handle exceptions on invalid UTF-8 characters or unexpected content triggered by XmlStreamReader.getElementText (based on patch 3081264)
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1492 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
9a41a12980
commit
a643b5741d
@ -1470,7 +1470,7 @@ public class CaldavConnection extends AbstractConnection {
|
|||||||
String tagLocalName = reader.getLocalName();
|
String tagLocalName = reader.getLocalName();
|
||||||
String tagText = null;
|
String tagText = null;
|
||||||
if ("displayname".equals(tagLocalName) || reader.hasText()) {
|
if ("displayname".equals(tagLocalName) || reader.hasText()) {
|
||||||
tagText = reader.getElementText();
|
tagText = XMLStreamUtil.getElementText(reader);
|
||||||
}
|
}
|
||||||
properties.put(tagLocalName, tagText);
|
properties.put(tagLocalName, tagText);
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
package davmail.exchange;
|
package davmail.exchange;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import javax.xml.stream.XMLInputFactory;
|
import javax.xml.stream.XMLInputFactory;
|
||||||
import javax.xml.stream.XMLStreamConstants;
|
import javax.xml.stream.XMLStreamConstants;
|
||||||
import javax.xml.stream.XMLStreamException;
|
import javax.xml.stream.XMLStreamException;
|
||||||
@ -33,6 +35,8 @@ import java.util.Map;
|
|||||||
* XmlStreamReader utility methods
|
* XmlStreamReader utility methods
|
||||||
*/
|
*/
|
||||||
public final class XMLStreamUtil {
|
public final class XMLStreamUtil {
|
||||||
|
protected static final Logger LOGGER = Logger.getLogger(XMLStreamUtil.class);
|
||||||
|
|
||||||
private XMLStreamUtil() {
|
private XMLStreamUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,23 +100,6 @@ public final class XMLStreamUtil {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get attribute value for attribute name.
|
|
||||||
* reader must be at START_ELEMENT state
|
|
||||||
*
|
|
||||||
* @param reader xml stream reader
|
|
||||||
* @param attributeName attribute name
|
|
||||||
* @return attribute value
|
|
||||||
*/
|
|
||||||
public static String getAttributeValue(XMLStreamReader reader, String attributeName) {
|
|
||||||
for (int i = 0; i < reader.getAttributeCount(); i++) {
|
|
||||||
if (attributeName.equals(reader.getAttributeLocalName(i))) {
|
|
||||||
return reader.getAttributeValue(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test if reader is on a start tag named tagLocalName.
|
* Test if reader is on a start tag named tagLocalName.
|
||||||
*
|
*
|
||||||
@ -180,4 +167,17 @@ public final class XMLStreamUtil {
|
|||||||
return xmlInputFactory.createXMLStreamReader(inputStream);
|
return xmlInputFactory.createXMLStreamReader(inputStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getElementText(XMLStreamReader reader) {
|
||||||
|
String value = null;
|
||||||
|
try {
|
||||||
|
value = reader.getElementText();
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
LOGGER.warn(e.getMessage());
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
// probably com.ctc.wstx.exc.WstxLazyException on invalid character sequence
|
||||||
|
LOGGER.warn(e.getMessage());
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2172,8 +2172,8 @@ public class DavExchangeSession extends ExchangeSession {
|
|||||||
while (reader.hasNext()) {
|
while (reader.hasNext()) {
|
||||||
reader.next();
|
reader.next();
|
||||||
if (XMLStreamUtil.isStartTag(reader, "e")
|
if (XMLStreamUtil.isStartTag(reader, "e")
|
||||||
&& "18-timezone".equals(XMLStreamUtil.getAttributeValue(reader, "k"))) {
|
&& "18-timezone".equals(reader.getAttributeValue(null, "k"))) {
|
||||||
String value = XMLStreamUtil.getAttributeValue(reader, "v");
|
String value = reader.getAttributeValue(null, "v");
|
||||||
if (value != null && value.startsWith("18-")) {
|
if (value != null && value.startsWith("18-")) {
|
||||||
timezoneName = value.substring(3);
|
timezoneName = value.substring(3);
|
||||||
}
|
}
|
||||||
|
@ -455,10 +455,14 @@ public abstract class EWSMethod extends PostMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String put(String key, String value) {
|
public String put(String key, String value) {
|
||||||
if (get(key) == null) {
|
if (value != null) {
|
||||||
fieldNames.add(key);
|
if (get(key) == null) {
|
||||||
|
fieldNames.add(key);
|
||||||
|
}
|
||||||
|
return super.put(key, value);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return super.put(key, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -582,6 +586,7 @@ public abstract class EWSMethod extends PostMethod {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all attendees.
|
* Get all attendees.
|
||||||
|
*
|
||||||
* @return all attendees
|
* @return all attendees
|
||||||
*/
|
*/
|
||||||
public List<Attendee> getAttendees() {
|
public List<Attendee> getAttendees() {
|
||||||
@ -590,6 +595,7 @@ public abstract class EWSMethod extends PostMethod {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add attendee.
|
* Add attendee.
|
||||||
|
*
|
||||||
* @param attendee attendee object
|
* @param attendee attendee object
|
||||||
*/
|
*/
|
||||||
public void addAttendee(Attendee attendee) {
|
public void addAttendee(Attendee attendee) {
|
||||||
@ -701,7 +707,7 @@ public abstract class EWSMethod extends PostMethod {
|
|||||||
errorDetail = result;
|
errorDetail = result;
|
||||||
}
|
}
|
||||||
if (XMLStreamUtil.isStartTag(reader, "faultstring")) {
|
if (XMLStreamUtil.isStartTag(reader, "faultstring")) {
|
||||||
errorDetail = reader.getElementText();
|
errorDetail = XMLStreamUtil.getElementText(reader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -747,7 +753,7 @@ public abstract class EWSMethod extends PostMethod {
|
|||||||
if (XMLStreamUtil.isStartTag(reader)) {
|
if (XMLStreamUtil.isStartTag(reader)) {
|
||||||
String tagLocalName = reader.getLocalName();
|
String tagLocalName = reader.getLocalName();
|
||||||
if ("Entry".equals(tagLocalName)) {
|
if ("Entry".equals(tagLocalName)) {
|
||||||
item.put(reader.getAttributeValue(null, "Key"), reader.getElementText());
|
item.put(reader.getAttributeValue(null, "Key"), XMLStreamUtil.getElementText(reader));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -779,9 +785,9 @@ public abstract class EWSMethod extends PostMethod {
|
|||||||
if ("EmailAddress".equals(tagLocalName)) {
|
if ("EmailAddress".equals(tagLocalName)) {
|
||||||
attendee.email = reader.getElementText();
|
attendee.email = reader.getElementText();
|
||||||
} else if ("Name".equals(tagLocalName)) {
|
} else if ("Name".equals(tagLocalName)) {
|
||||||
attendee.name = reader.getElementText();
|
attendee.name = XMLStreamUtil.getElementText(reader);
|
||||||
} else if ("ResponseType".equals(tagLocalName)) {
|
} else if ("ResponseType".equals(tagLocalName)) {
|
||||||
String responseType = reader.getElementText();
|
String responseType = XMLStreamUtil.getElementText(reader);
|
||||||
if ("Accept".equals(responseType)) {
|
if ("Accept".equals(responseType)) {
|
||||||
attendee.partstat = "ACCEPTED";
|
attendee.partstat = "ACCEPTED";
|
||||||
} else if ("Tentative".equals(responseType)) {
|
} else if ("Tentative".equals(responseType)) {
|
||||||
@ -852,7 +858,7 @@ public abstract class EWSMethod extends PostMethod {
|
|||||||
propertyTag = getAttributeValue(reader, "PropertyName");
|
propertyTag = getAttributeValue(reader, "PropertyName");
|
||||||
}
|
}
|
||||||
} else if ("Value".equals(tagLocalName)) {
|
} else if ("Value".equals(tagLocalName)) {
|
||||||
propertyValue = reader.getElementText();
|
propertyValue = XMLStreamUtil.getElementText(reader);
|
||||||
} else if ("Values".equals(tagLocalName)) {
|
} else if ("Values".equals(tagLocalName)) {
|
||||||
StringBuilder buffer = new StringBuilder();
|
StringBuilder buffer = new StringBuilder();
|
||||||
while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Values"))) {
|
while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Values"))) {
|
||||||
@ -862,7 +868,10 @@ public abstract class EWSMethod extends PostMethod {
|
|||||||
if (buffer.length() > 0) {
|
if (buffer.length() > 0) {
|
||||||
buffer.append(',');
|
buffer.append(',');
|
||||||
}
|
}
|
||||||
buffer.append(reader.getElementText());
|
String singleValue = XMLStreamUtil.getElementText(reader);
|
||||||
|
if (singleValue != null) {
|
||||||
|
buffer.append(singleValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
propertyValue = buffer.toString();
|
propertyValue = buffer.toString();
|
||||||
|
@ -100,7 +100,7 @@ public class GetUserAvailabilityMethod extends EWSMethod {
|
|||||||
@Override
|
@Override
|
||||||
protected void handleCustom(XMLStreamReader reader) throws XMLStreamException {
|
protected void handleCustom(XMLStreamReader reader) throws XMLStreamException {
|
||||||
if (XMLStreamUtil.isStartTag(reader, "MergedFreeBusy")) {
|
if (XMLStreamUtil.isStartTag(reader, "MergedFreeBusy")) {
|
||||||
this.mergedFreeBusy = reader.getElementText();
|
this.mergedFreeBusy = XMLStreamUtil.getElementText(reader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ public class GetUserConfigurationMethod extends EWSMethod {
|
|||||||
if (key == null) {
|
if (key == null) {
|
||||||
key = reader.getElementText();
|
key = reader.getElementText();
|
||||||
} else {
|
} else {
|
||||||
responseItem.put(key, reader.getElementText());
|
responseItem.put(key, XMLStreamUtil.getElementText(reader));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ public class ResolveNamesMethod extends EWSMethod {
|
|||||||
if (XMLStreamUtil.isStartTag(reader)) {
|
if (XMLStreamUtil.isStartTag(reader)) {
|
||||||
String tagLocalName = reader.getLocalName();
|
String tagLocalName = reader.getLocalName();
|
||||||
if ("Name".equals(tagLocalName)) {
|
if ("Name".equals(tagLocalName)) {
|
||||||
responseItem.put(tagLocalName, reader.getElementText());
|
responseItem.put(tagLocalName, XMLStreamUtil.getElementText(reader));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,7 +87,7 @@ public class ResolveNamesMethod extends EWSMethod {
|
|||||||
} else if ("PhoneNumbers".equals(tagLocalName)) {
|
} else if ("PhoneNumbers".equals(tagLocalName)) {
|
||||||
handlePhoneNumbers(reader, responseItem);
|
handlePhoneNumbers(reader, responseItem);
|
||||||
} else {
|
} else {
|
||||||
responseItem.put(tagLocalName, reader.getElementText());
|
responseItem.put(tagLocalName, XMLStreamUtil.getElementText(reader));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,8 +121,8 @@ public class ResolveNamesMethod extends EWSMethod {
|
|||||||
String tagLocalName = reader.getLocalName();
|
String tagLocalName = reader.getLocalName();
|
||||||
if ("Entry".equals(tagLocalName)) {
|
if ("Entry".equals(tagLocalName)) {
|
||||||
String key = getAttributeValue(reader, "Key");
|
String key = getAttributeValue(reader, "Key");
|
||||||
String value = reader.getElementText();
|
String value = XMLStreamUtil.getElementText(reader);
|
||||||
if (value.startsWith("smtp:") || value.startsWith("SMTP:")) {
|
if (value != null && (value.startsWith("smtp:") || value.startsWith("SMTP:"))) {
|
||||||
value = value.substring(5);
|
value = value.substring(5);
|
||||||
}
|
}
|
||||||
responseItem.put(key, value);
|
responseItem.put(key, value);
|
||||||
|
Loading…
Reference in New Issue
Block a user