From a643b5741d967b97c26a2bbdc5c4d643a37e867e Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 5 Oct 2010 09:41:04 +0000 Subject: [PATCH] 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 --- src/java/davmail/caldav/CaldavConnection.java | 2 +- src/java/davmail/exchange/XMLStreamUtil.java | 34 +++++++++---------- .../exchange/dav/DavExchangeSession.java | 4 +-- src/java/davmail/exchange/ews/EWSMethod.java | 27 ++++++++++----- .../ews/GetUserAvailabilityMethod.java | 2 +- .../ews/GetUserConfigurationMethod.java | 2 +- .../exchange/ews/ResolveNamesMethod.java | 8 ++--- 7 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/java/davmail/caldav/CaldavConnection.java b/src/java/davmail/caldav/CaldavConnection.java index 386ca209..0c636628 100644 --- a/src/java/davmail/caldav/CaldavConnection.java +++ b/src/java/davmail/caldav/CaldavConnection.java @@ -1470,7 +1470,7 @@ public class CaldavConnection extends AbstractConnection { String tagLocalName = reader.getLocalName(); String tagText = null; if ("displayname".equals(tagLocalName) || reader.hasText()) { - tagText = reader.getElementText(); + tagText = XMLStreamUtil.getElementText(reader); } properties.put(tagLocalName, tagText); } diff --git a/src/java/davmail/exchange/XMLStreamUtil.java b/src/java/davmail/exchange/XMLStreamUtil.java index d131e7c1..24a71139 100644 --- a/src/java/davmail/exchange/XMLStreamUtil.java +++ b/src/java/davmail/exchange/XMLStreamUtil.java @@ -18,6 +18,8 @@ */ package davmail.exchange; +import org.apache.log4j.Logger; + import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; @@ -33,6 +35,8 @@ import java.util.Map; * XmlStreamReader utility methods */ public final class XMLStreamUtil { + protected static final Logger LOGGER = Logger.getLogger(XMLStreamUtil.class); + private XMLStreamUtil() { } @@ -96,23 +100,6 @@ public final class XMLStreamUtil { 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. * @@ -180,4 +167,17 @@ public final class XMLStreamUtil { 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; + } + } diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index a03eb0ee..d084de19 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -2172,8 +2172,8 @@ public class DavExchangeSession extends ExchangeSession { while (reader.hasNext()) { reader.next(); if (XMLStreamUtil.isStartTag(reader, "e") - && "18-timezone".equals(XMLStreamUtil.getAttributeValue(reader, "k"))) { - String value = XMLStreamUtil.getAttributeValue(reader, "v"); + && "18-timezone".equals(reader.getAttributeValue(null, "k"))) { + String value = reader.getAttributeValue(null, "v"); if (value != null && value.startsWith("18-")) { timezoneName = value.substring(3); } diff --git a/src/java/davmail/exchange/ews/EWSMethod.java b/src/java/davmail/exchange/ews/EWSMethod.java index 86c0f275..5b7667ce 100644 --- a/src/java/davmail/exchange/ews/EWSMethod.java +++ b/src/java/davmail/exchange/ews/EWSMethod.java @@ -455,10 +455,14 @@ public abstract class EWSMethod extends PostMethod { @Override public String put(String key, String value) { - if (get(key) == null) { - fieldNames.add(key); + if (value != null) { + 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. + * * @return all attendees */ public List getAttendees() { @@ -590,6 +595,7 @@ public abstract class EWSMethod extends PostMethod { /** * Add attendee. + * * @param attendee attendee object */ public void addAttendee(Attendee attendee) { @@ -701,7 +707,7 @@ public abstract class EWSMethod extends PostMethod { errorDetail = result; } 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)) { String tagLocalName = reader.getLocalName(); 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)) { attendee.email = reader.getElementText(); } else if ("Name".equals(tagLocalName)) { - attendee.name = reader.getElementText(); + attendee.name = XMLStreamUtil.getElementText(reader); } else if ("ResponseType".equals(tagLocalName)) { - String responseType = reader.getElementText(); + String responseType = XMLStreamUtil.getElementText(reader); if ("Accept".equals(responseType)) { attendee.partstat = "ACCEPTED"; } else if ("Tentative".equals(responseType)) { @@ -852,7 +858,7 @@ public abstract class EWSMethod extends PostMethod { propertyTag = getAttributeValue(reader, "PropertyName"); } } else if ("Value".equals(tagLocalName)) { - propertyValue = reader.getElementText(); + propertyValue = XMLStreamUtil.getElementText(reader); } else if ("Values".equals(tagLocalName)) { StringBuilder buffer = new StringBuilder(); while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Values"))) { @@ -862,7 +868,10 @@ public abstract class EWSMethod extends PostMethod { if (buffer.length() > 0) { buffer.append(','); } - buffer.append(reader.getElementText()); + String singleValue = XMLStreamUtil.getElementText(reader); + if (singleValue != null) { + buffer.append(singleValue); + } } } propertyValue = buffer.toString(); diff --git a/src/java/davmail/exchange/ews/GetUserAvailabilityMethod.java b/src/java/davmail/exchange/ews/GetUserAvailabilityMethod.java index b64e46c6..967a37dd 100644 --- a/src/java/davmail/exchange/ews/GetUserAvailabilityMethod.java +++ b/src/java/davmail/exchange/ews/GetUserAvailabilityMethod.java @@ -100,7 +100,7 @@ public class GetUserAvailabilityMethod extends EWSMethod { @Override protected void handleCustom(XMLStreamReader reader) throws XMLStreamException { if (XMLStreamUtil.isStartTag(reader, "MergedFreeBusy")) { - this.mergedFreeBusy = reader.getElementText(); + this.mergedFreeBusy = XMLStreamUtil.getElementText(reader); } } diff --git a/src/java/davmail/exchange/ews/GetUserConfigurationMethod.java b/src/java/davmail/exchange/ews/GetUserConfigurationMethod.java index adb27356..01a4ac9d 100644 --- a/src/java/davmail/exchange/ews/GetUserConfigurationMethod.java +++ b/src/java/davmail/exchange/ews/GetUserConfigurationMethod.java @@ -78,7 +78,7 @@ public class GetUserConfigurationMethod extends EWSMethod { if (key == null) { key = reader.getElementText(); } else { - responseItem.put(key, reader.getElementText()); + responseItem.put(key, XMLStreamUtil.getElementText(reader)); } } } diff --git a/src/java/davmail/exchange/ews/ResolveNamesMethod.java b/src/java/davmail/exchange/ews/ResolveNamesMethod.java index 52f5c4a6..82067ef2 100644 --- a/src/java/davmail/exchange/ews/ResolveNamesMethod.java +++ b/src/java/davmail/exchange/ews/ResolveNamesMethod.java @@ -69,7 +69,7 @@ public class ResolveNamesMethod extends EWSMethod { if (XMLStreamUtil.isStartTag(reader)) { String tagLocalName = reader.getLocalName(); 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)) { handlePhoneNumbers(reader, responseItem); } 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(); if ("Entry".equals(tagLocalName)) { String key = getAttributeValue(reader, "Key"); - String value = reader.getElementText(); - if (value.startsWith("smtp:") || value.startsWith("SMTP:")) { + String value = XMLStreamUtil.getElementText(reader); + if (value != null && (value.startsWith("smtp:") || value.startsWith("SMTP:"))) { value = value.substring(5); } responseItem.put(key, value);