1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 03:02:22 -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:
mguessan 2010-10-05 09:41:04 +00:00
parent 9a41a12980
commit a643b5741d
7 changed files with 44 additions and 35 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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<Attendee> 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();

View File

@ -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);
}
}

View File

@ -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));
}
}
}

View File

@ -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);