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
This commit is contained in:
mguessan 2010-12-21 12:42:56 +00:00
parent 5dbd82753b
commit 1615f4991b
4 changed files with 99 additions and 60 deletions

View File

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

View File

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

View File

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

View File

@ -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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buffer.append("<C:calendar-query xmlns:C=\"urn:ietf:params:xml:ns:caldav\" xmlns:D=\"DAV:\">");
buffer.append("<D:prop>");
buffer.append("<C:calendar-data/>");
buffer.append("</D:prop>");
buffer.append("<C:filter>");
buffer.append("</C:filter>");
buffer.append("</C:calendar-query>");
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<ExchangeSession.Event> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");