From 18584e259ded44ebaa712368038b1959f5040c3d Mon Sep 17 00:00:00 2001 From: mguessan Date: Wed, 27 Oct 2010 21:52:33 +0000 Subject: [PATCH] Ews: improve invalid item in calendar error handling git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1520 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/exchange/ews/EWSMethod.java | 54 ++++++++++--------- .../exchange/ews/EwsExchangeSession.java | 47 +++++++++++----- .../exchange/TestExchangeSessionCalendar.java | 15 ++++++ 3 files changed, 79 insertions(+), 37 deletions(-) diff --git a/src/java/davmail/exchange/ews/EWSMethod.java b/src/java/davmail/exchange/ews/EWSMethod.java index 5b7667ce..3887dead 100644 --- a/src/java/davmail/exchange/ews/EWSMethod.java +++ b/src/java/davmail/exchange/ews/EWSMethod.java @@ -912,35 +912,41 @@ public abstract class EWSMethod extends PostMethod { protected void processResponseBody(HttpState httpState, HttpConnection httpConnection) { Header contentTypeHeader = getResponseHeader("Content-Type"); if (contentTypeHeader != null && "text/xml; charset=utf-8".equals(contentTypeHeader.getValue())) { - responseItems = new ArrayList(); - XMLStreamReader reader; try { - reader = XMLStreamUtil.createXMLStreamReader(getResponseBodyAsStream()); - while (reader.hasNext()) { - reader.next(); - handleErrors(reader); - if (serverVersion == null && XMLStreamUtil.isStartTag(reader, "ServerVersionInfo")) { - String majorVersion = getAttributeValue(reader, "MajorVersion"); - if ("14".equals(majorVersion)) { - serverVersion = "Exchange2010"; - } else { - serverVersion = "Exchange2007_SP1"; - } - } else if (XMLStreamUtil.isStartTag(reader, responseCollectionName)) { - handleItems(reader); - } else { - handleCustom(reader); - } - } - + processResponseStream(getResponseBodyAsStream()); } catch (IOException e) { logger.error("Error while parsing soap response: " + e, e); - } catch (XMLStreamException e) { - logger.error("Error while parsing soap response: " + e, e); } - if (errorDetail != null) { - logger.debug(errorDetail); + } + } + + protected void processResponseStream(InputStream inputStream) { + responseItems = new ArrayList(); + XMLStreamReader reader; + try { + reader = XMLStreamUtil.createXMLStreamReader(inputStream); + while (reader.hasNext()) { + reader.next(); + handleErrors(reader); + if (serverVersion == null && XMLStreamUtil.isStartTag(reader, "ServerVersionInfo")) { + String majorVersion = getAttributeValue(reader, "MajorVersion"); + if ("14".equals(majorVersion)) { + serverVersion = "Exchange2010"; + } else { + serverVersion = "Exchange2007_SP1"; + } + } else if (XMLStreamUtil.isStartTag(reader, responseCollectionName)) { + handleItems(reader); + } else { + handleCustom(reader); + } } + + } catch (XMLStreamException e) { + logger.error("Error while parsing soap response: " + e, e); + } + if (errorDetail != null) { + logger.debug(errorDetail); } } diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index ee35351a..69705a39 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -79,7 +79,6 @@ public class EwsExchangeSession extends ExchangeSession { super(url, userName, password); } - /** * Check endpoint url. * @@ -109,6 +108,9 @@ public class EwsExchangeSession extends ExchangeSession { @Override protected void buildSessionInfo(HttpMethod method) throws DavMailException { + // no need to check logon method body + method.releaseConnection(); + // also need to retrieve email and alias getEmailAndAliasFromOptions(); if (email == null || alias == null) { @@ -1038,10 +1040,13 @@ public class EwsExchangeSession extends ExchangeSession { protected class Event extends ExchangeSession.Event { // item id ItemId itemId; + String type; protected Event(EWSMethod.Item response) { itemId = new ItemId(response); + type = response.type; + permanentUrl = response.get(Field.get("permanenturl").getResponseName()); etag = response.get(Field.get("etag").getResponseName()); displayName = response.get(Field.get("displayname").getResponseName()); @@ -1122,11 +1127,11 @@ public class EwsExchangeSession extends ExchangeSession { // handle mozilla alarm String xMozLastack = vCalendar.getFirstVeventPropertyValue("X-MOZ-LASTACK"); if (xMozLastack != null) { - updates.add(Field.createFieldUpdate("xmozlastack", xMozLastack)); + updates.add(Field.createFieldUpdate("xmozlastack", xMozLastack)); } String xMozSnoozeTime = vCalendar.getFirstVeventPropertyValue("X-MOZ-SNOOZE-TIME"); if (xMozSnoozeTime != null) { - updates.add(Field.createFieldUpdate("xmozsnoozetime", xMozSnoozeTime)); + updates.add(Field.createFieldUpdate("xmozsnoozetime", xMozSnoozeTime)); } if (vCalendar.isMeeting()) { @@ -1191,12 +1196,14 @@ public class EwsExchangeSession extends ExchangeSession { } try { GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, true); - getItemMethod.addAdditionalProperty(Field.get("reminderset")); - getItemMethod.addAdditionalProperty(Field.get("calendaruid")); - getItemMethod.addAdditionalProperty(Field.get("requiredattendees")); - getItemMethod.addAdditionalProperty(Field.get("optionalattendees")); - getItemMethod.addAdditionalProperty(Field.get("xmozlastack")); - getItemMethod.addAdditionalProperty(Field.get("xmozsnoozetime")); + if (!"Message".equals(type)) { + getItemMethod.addAdditionalProperty(Field.get("reminderset")); + getItemMethod.addAdditionalProperty(Field.get("calendaruid")); + getItemMethod.addAdditionalProperty(Field.get("requiredattendees")); + getItemMethod.addAdditionalProperty(Field.get("optionalattendees")); + getItemMethod.addAdditionalProperty(Field.get("xmozlastack")); + getItemMethod.addAdditionalProperty(Field.get("xmozsnoozetime")); + } executeMethod(getItemMethod); content = getItemMethod.getMimeContent(); @@ -1212,7 +1219,7 @@ public class EwsExchangeSession extends ExchangeSession { List attendees = getItemMethod.getResponseItem().getAttendees(); if (attendees != null) { for (EWSMethod.Attendee attendee : attendees) { - VProperty attendeeProperty = new VProperty("ATTENDEE", "mailto:"+attendee.email); + VProperty attendeeProperty = new VProperty("ATTENDEE", "mailto:" + attendee.email); attendeeProperty.addParam("CN", attendee.name); attendeeProperty.addParam("PARTSTAT", attendee.partstat); attendeeProperty.addParam("ROLE", attendee.role); @@ -1259,7 +1266,19 @@ public class EwsExchangeSession extends ExchangeSession { condition, FolderQueryTraversal.SHALLOW, 0); for (EWSMethod.Item response : responses) { - events.add(new Event(response)); + Event event = new Event(response); + if ("Message".equals(event.type)) { + // need to check body + try { + event.getEventContent(); + events.add(event); + } catch (HttpException e) { + LOGGER.warn("Ignore invalid event "+event.getHref()); + } + } else { + events.add(event); + } + } return events; @@ -1319,7 +1338,9 @@ public class EwsExchangeSession extends ExchangeSession { } return new Contact(item); } else if ("CalendarItem".equals(itemType) - || "MeetingRequest".equals(itemType)) { + || "MeetingRequest".equals(itemType) + // VTODOs appear as Messages + || "Message".equals(itemType)) { return new Event(item); } else { throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND"); @@ -1660,7 +1681,7 @@ public class EwsExchangeSession extends ExchangeSession { executeMethod(resolveNamesMethod); List responses = resolveNamesMethod.getResponseItems(); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("ResolveNames(" + searchValue + ") returned "+responses.size()+" results"); + LOGGER.debug("ResolveNames(" + searchValue + ") returned " + responses.size() + " results"); } for (EWSMethod.Item response : responses) { Contact contact = buildGalfindContact(response); diff --git a/src/test/davmail/exchange/TestExchangeSessionCalendar.java b/src/test/davmail/exchange/TestExchangeSessionCalendar.java index 2480b6ff..46fdb85d 100644 --- a/src/test/davmail/exchange/TestExchangeSessionCalendar.java +++ b/src/test/davmail/exchange/TestExchangeSessionCalendar.java @@ -98,6 +98,21 @@ public class TestExchangeSessionCalendar extends AbstractExchangeSessionTestCase } } + public void testReportCalendar() throws IOException { + List events = null; + try { + events = session.getAllEvents("/users/" + session.getEmail() + "/calendar"); + for (ExchangeSession.Event event : events) { + System.out.println(event.subject); + ExchangeSession.Item item = session.getItem("/users/" + session.getEmail() + "/calendar", event.itemName); + System.out.println(item.getBody()); + } + } catch (IOException e) { + System.out.println(e.getMessage()); + throw e; + } + } + public void testGetFreeBusyData() throws IOException, MessagingException { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); cal.set(Calendar.MONTH, 7);