From 1615f4991bbd987ac54c1153b8217b2a44955ae5 Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 21 Dec 2010 12:42:56 +0000 Subject: [PATCH] EWS: fix Caldav inbox handling over EWS git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1583 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 62 +++++++++++++++++++ .../exchange/dav/DavExchangeSession.java | 60 ------------------ .../exchange/ews/EwsExchangeSession.java | 6 ++ src/test/davmail/caldav/TestCaldav.java | 31 ++++++++++ 4 files changed, 99 insertions(+), 60 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 2da521b0..0641a9d3 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -40,6 +40,8 @@ import org.htmlcleaner.TagNode; import javax.mail.MessagingException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import javax.mail.internet.MimePart; import javax.mail.util.SharedByteArrayInputStream; import java.io.*; import java.net.NoRouteToHostException; @@ -2107,6 +2109,66 @@ public abstract class ExchangeSession { */ public abstract byte[] getEventContent() throws IOException; + protected static final String TEXT_CALENDAR = "text/calendar"; + protected static final String APPLICATION_ICS = "application/ics"; + + protected boolean isCalendarContentType(String contentType) { + return TEXT_CALENDAR.regionMatches(true, 0, contentType, 0, TEXT_CALENDAR.length()) || + APPLICATION_ICS.regionMatches(true, 0, contentType, 0, APPLICATION_ICS.length()); + } + + protected MimePart getCalendarMimePart(MimeMultipart multiPart) throws IOException, MessagingException { + MimePart bodyPart = null; + for (int i = 0; i < multiPart.getCount(); i++) { + String contentType = multiPart.getBodyPart(i).getContentType(); + if (isCalendarContentType(contentType)) { + bodyPart = (MimePart) multiPart.getBodyPart(i); + break; + } else if (contentType.startsWith("multipart")) { + Object content = multiPart.getBodyPart(i).getContent(); + if (content instanceof MimeMultipart) { + bodyPart = getCalendarMimePart((MimeMultipart) content); + } + } + } + + return bodyPart; + } + + /** + * Load ICS content from MIME message input stream + * + * @param mimeInputStream mime message input stream + * @return mime message ics attachment body + * @throws IOException on error + * @throws MessagingException on error + */ + protected byte[] getICS(InputStream mimeInputStream) throws IOException, MessagingException { + byte[] result; + MimeMessage mimeMessage = new MimeMessage(null, mimeInputStream); + Object mimeBody = mimeMessage.getContent(); + MimePart bodyPart = null; + if (mimeBody instanceof MimeMultipart) { + bodyPart = getCalendarMimePart((MimeMultipart) mimeBody); + } else if (isCalendarContentType(mimeMessage.getContentType())) { + // no multipart, single body + bodyPart = mimeMessage; + } + + if (bodyPart != null) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + bodyPart.getDataHandler().writeTo(baos); + baos.close(); + result = baos.toByteArray(); + } else { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + mimeMessage.writeTo(baos); + baos.close(); + throw new DavMailException("EXCEPTION_INVALID_MESSAGE_CONTENT", new String(baos.toByteArray(), "UTF-8")); + } + return result; + } + protected void fixICS(byte[] icsContent, boolean fromServer) throws IOException { if (LOGGER.isDebugEnabled() && fromServer) { dumpIndex++; diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index 19a56d58..33ed3a1f 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -1197,66 +1197,6 @@ public class DavExchangeSession extends ExchangeSession { super(folderPath, itemName, contentClass, itemBody, etag, noneMatch); } - protected static final String TEXT_CALENDAR = "text/calendar"; - protected static final String APPLICATION_ICS = "application/ics"; - - protected boolean isCalendarContentType(String contentType) { - return TEXT_CALENDAR.regionMatches(true, 0, contentType, 0, TEXT_CALENDAR.length()) || - APPLICATION_ICS.regionMatches(true, 0, contentType, 0, APPLICATION_ICS.length()); - } - - protected MimePart getCalendarMimePart(MimeMultipart multiPart) throws IOException, MessagingException { - MimePart bodyPart = null; - for (int i = 0; i < multiPart.getCount(); i++) { - String contentType = multiPart.getBodyPart(i).getContentType(); - if (isCalendarContentType(contentType)) { - bodyPart = (MimePart) multiPart.getBodyPart(i); - break; - } else if (contentType.startsWith("multipart")) { - Object content = multiPart.getBodyPart(i).getContent(); - if (content instanceof MimeMultipart) { - bodyPart = getCalendarMimePart((MimeMultipart) content); - } - } - } - - return bodyPart; - } - - /** - * Load ICS content from MIME message input stream - * - * @param mimeInputStream mime message input stream - * @return mime message ics attachment body - * @throws IOException on error - * @throws MessagingException on error - */ - protected byte[] getICS(InputStream mimeInputStream) throws IOException, MessagingException { - byte[] result; - MimeMessage mimeMessage = new MimeMessage(null, mimeInputStream); - Object mimeBody = mimeMessage.getContent(); - MimePart bodyPart = null; - if (mimeBody instanceof MimeMultipart) { - bodyPart = getCalendarMimePart((MimeMultipart) mimeBody); - } else if (isCalendarContentType(mimeMessage.getContentType())) { - // no multipart, single body - bodyPart = mimeMessage; - } - - if (bodyPart != null) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - bodyPart.getDataHandler().writeTo(baos); - baos.close(); - result = baos.toByteArray(); - } else { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - mimeMessage.writeTo(baos); - baos.close(); - throw new DavMailException("EXCEPTION_INVALID_MESSAGE_CONTENT", new String(baos.toByteArray(), "UTF-8")); - } - return result; - } - protected byte[] getICSFromInternetContentProperty() throws IOException, DavException, MessagingException { byte[] result = null; // PropFind PR_INTERNET_CONTENT diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index c36e74df..bf768621 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -35,6 +35,7 @@ import org.apache.commons.httpclient.methods.PostMethod; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; +import javax.mail.util.SharedByteArrayInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -1273,6 +1274,9 @@ public class EwsExchangeSession extends ExchangeSession { executeMethod(getItemMethod); content = getItemMethod.getMimeContent(); + if (!"CalendarItem".equals(type)) { + content = getICS(new SharedByteArrayInputStream(content)); + } VCalendar localVCalendar = new VCalendar(content, email, getVTimezone()); // remove additional reminder if (!"true".equals(getItemMethod.getResponseItem().get(Field.get("reminderset").getResponseName()))) { @@ -1301,6 +1305,8 @@ public class EwsExchangeSession extends ExchangeSession { } catch (IOException e) { throw buildHttpException(e); + } catch (MessagingException e) { + throw buildHttpException(e); } return content; } diff --git a/src/test/davmail/caldav/TestCaldav.java b/src/test/davmail/caldav/TestCaldav.java index ca9c0405..ee6c55fd 100644 --- a/src/test/davmail/caldav/TestCaldav.java +++ b/src/test/davmail/caldav/TestCaldav.java @@ -129,6 +129,12 @@ public class TestCaldav extends AbstractDavMailTestCase { assertEquals(HttpStatus.SC_OK, method.getStatusCode()); } + public void testGetInbox() throws IOException { + GetMethod method = new GetMethod("/users/" + session.getEmail() + "/inbox/"); + httpClient.executeMethod(method); + assertEquals(HttpStatus.SC_OK, method.getStatusCode()); + } + public void testGetOtherUserCalendar() throws IOException { Settings.setLoggingLevel("httpclient.wire", Level.DEBUG); PropFindMethod method = new PropFindMethod("/principals/users/" + Settings.getProperty("davmail.to") + "/calendar/"); @@ -176,6 +182,31 @@ public class TestCaldav extends AbstractDavMailTestCase { assertEquals(events.size(), responses.length); } + public void testReportInbox() throws IOException, DavException { + + StringBuilder buffer = new StringBuilder(); + buffer.append(""); + buffer.append(""); + buffer.append(""); + buffer.append(""); + buffer.append(""); + buffer.append(""); + buffer.append(""); + buffer.append(""); + SearchReportMethod method = new SearchReportMethod("/users/" + session.getEmail() + "/inbox/", buffer.toString()); + httpClient.executeMethod(method); + assertEquals(HttpStatus.SC_MULTI_STATUS, method.getStatusCode()); + MultiStatus multiStatus = method.getResponseBodyAsMultiStatus(); + MultiStatusResponse[] responses = multiStatus.getResponses(); + /*List events = session.searchEvents("/users/" + session.getEmail() + "/calendar/", + session.or(session.isEqualTo("instancetype", 1), + session.and(session.isEqualTo("instancetype", 0), dateCondition)) + + );*/ + + //assertEquals(events.size(), responses.length); + } + public void testReportTasks() throws IOException, DavException { StringBuilder buffer = new StringBuilder(); buffer.append("");