EWS: fix CalendarItem creation, no need to wrap ics in a MIME message

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1165 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-07-10 22:42:48 +00:00
parent a0339007c4
commit 72e57df57a
6 changed files with 82 additions and 21 deletions

View File

@ -1952,6 +1952,7 @@ public abstract class ExchangeSession {
String eventClass = null;
String organizer = null;
String action = null;
String method = null;
boolean sound = false;
List<AllDayState> allDayStates = new ArrayList<AllDayState>();
@ -2007,6 +2008,8 @@ public abstract class ExchangeSession {
hasCdoBusyStatus = true;
} else if ("BEGIN:VTIMEZONE".equals(line)) {
hasTimezone = true;
} else if (key.equals("METHOD")) {
method = value;
}
}
}
@ -2031,6 +2034,14 @@ public abstract class ExchangeSession {
if (validTimezoneId != null && line.indexOf(";TZID=") >= 0) {
line = fixTimezoneId(line, validTimezoneId);
}
if (!fromServer && "BEGIN:VCALENDAR".equals(line) && method == null) {
result.writeLine(line);
// append missing method
if (method == null) {
result.writeLine("METHOD:PUBLISH");
}
continue;
}
if (fromServer && line.startsWith("PRODID:") && eventClass != null) {
result.writeLine(line);
// set global calendarserver access for iCal 4

View File

@ -22,10 +22,19 @@ package davmail.exchange.ews;
* Create Item method.
*/
public class CreateItemMethod extends EWSMethod {
public CreateItemMethod(MessageDisposition messageDisposition, FolderId savedItemFolderId, EWSMethod.Item item) {
public CreateItemMethod(MessageDisposition messageDisposition, FolderId savedItemFolderId, EWSMethod.Item item) {
super("Item", "CreateItem");
this.savedItemFolderId = savedItemFolderId;
this.item = item;
addMethodOption(messageDisposition);
}
public CreateItemMethod(MessageDisposition messageDisposition, SendMeetingInvitations sendMeetingInvitations, FolderId savedItemFolderId, EWSMethod.Item item) {
super("Item", "CreateItem");
this.savedItemFolderId = savedItemFolderId;
this.item = item;
addMethodOption(messageDisposition);
addMethodOption(sendMeetingInvitations);
}
}

View File

@ -30,7 +30,6 @@ import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.HeadMethod;
import javax.mail.MessagingException;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -194,7 +193,7 @@ public class EwsExchangeSession extends ExchangeSession {
localProperties.put("bcc", bcc);
UpdateItemMethod updateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
ConflictResolution.AlwaysOverwrite,
CalendarItemCreateOrDeleteOperation.SendToNone,
SendMeetingInvitationsOrCancellations.SendToNone,
itemId, buildProperties(localProperties));
executeMethod(updateItemMethod);
}
@ -205,7 +204,7 @@ public class EwsExchangeSession extends ExchangeSession {
public void updateMessage(ExchangeSession.Message message, Map<String, String> properties) throws IOException {
UpdateItemMethod updateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
ConflictResolution.AlwaysOverwrite,
CalendarItemCreateOrDeleteOperation.SendToNone,
SendMeetingInvitationsOrCancellations.SendToNone,
((EwsExchangeSession.Message) message).itemId, buildProperties(properties));
executeMethod(updateItemMethod);
}
@ -738,7 +737,7 @@ public class EwsExchangeSession extends ExchangeSession {
// update
createOrUpdateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
ConflictResolution.AlwaysOverwrite,
CalendarItemCreateOrDeleteOperation.SendToNone,
SendMeetingInvitationsOrCancellations.SendToNone,
currentItemId, buildProperties());
} else {
// create
@ -753,7 +752,7 @@ public class EwsExchangeSession extends ExchangeSession {
itemResult.status = createOrUpdateItemMethod.getStatusCode();
if (itemResult.status == HttpURLConnection.HTTP_OK) {
//noinspection VariableNotUsedInsideIf
if (etag != null) {
if (etag == null) {
itemResult.status = HttpStatus.SC_CREATED;
LOGGER.debug("Updated event " + getHref());
} else {
@ -763,6 +762,7 @@ public class EwsExchangeSession extends ExchangeSession {
ItemId newItemId = new ItemId(createOrUpdateItemMethod.getResponseItem());
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, newItemId, false);
getItemMethod.addAdditionalProperty(Field.get("etag"));
executeMethod(getItemMethod);
itemResult.etag = getItemMethod.getResponseItem().get(Field.get("etag").getResponseName());
@ -790,6 +790,11 @@ public class EwsExchangeSession extends ExchangeSession {
super(folderPath, itemName, contentClass, itemBody, etag, noneMatch);
}
@Override
public ItemResult createOrUpdate() throws IOException {
return createOrUpdate(fixICS(itemBody, false).getBytes("UTF-8"));
}
@Override
protected ItemResult createOrUpdate(byte[] mimeContent) throws IOException {
@ -826,14 +831,19 @@ public class EwsExchangeSession extends ExchangeSession {
// update
createOrUpdateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
ConflictResolution.AlwaysOverwrite,
CalendarItemCreateOrDeleteOperation.SendToNone,
SendMeetingInvitationsOrCancellations.SendToNone,
currentItemId, updates);
} else {
// create
EWSMethod.Item newItem = new EWSMethod.Item();
newItem.type = "Message";
newItem.type = "CalendarItem";
newItem.mimeContent = Base64.encodeBase64(mimeContent);
createOrUpdateItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, getFolderId(folderPath), newItem);
HashSet<FieldUpdate> updates = new HashSet<FieldUpdate>();
// force urlcompname
updates.add(Field.createFieldUpdate("urlcompname", convertItemNameToEML(itemName)));
//updates.add(Field.createFieldUpdate("outlookmessageclass", "IPM.Appointment"));
newItem.setFieldUpdates(updates);
createOrUpdateItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, SendMeetingInvitations.SendToNone, getFolderId(folderPath), newItem);
}
executeMethod(createOrUpdateItemMethod);
@ -841,7 +851,7 @@ public class EwsExchangeSession extends ExchangeSession {
itemResult.status = createOrUpdateItemMethod.getStatusCode();
if (itemResult.status == HttpURLConnection.HTTP_OK) {
//noinspection VariableNotUsedInsideIf
if (etag != null) {
if (etag == null) {
itemResult.status = HttpStatus.SC_CREATED;
LOGGER.debug("Updated event " + getHref());
} else {
@ -851,6 +861,7 @@ public class EwsExchangeSession extends ExchangeSession {
ItemId newItemId = new ItemId(createOrUpdateItemMethod.getResponseItem());
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, newItemId, false);
getItemMethod.addAdditionalProperty(Field.get("etag"));
executeMethod(getItemMethod);
itemResult.etag = getItemMethod.getResponseItem().get(Field.get("etag").getResponseName());
@ -864,11 +875,9 @@ public class EwsExchangeSession extends ExchangeSession {
LOGGER.debug("Get event: " + permanentUrl);
try {
byte[] content = getContent(itemId);
result = getICS(new ByteArrayInputStream(content));
result = new String(content);
} catch (IOException e) {
throw buildHttpException(e);
} catch (MessagingException e) {
throw buildHttpException(e);
}
return result;
}
@ -955,7 +964,7 @@ public class EwsExchangeSession extends ExchangeSession {
localProperties.put("read", "1");
UpdateItemMethod updateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
ConflictResolution.AlwaysOverwrite,
CalendarItemCreateOrDeleteOperation.SendToNone,
SendMeetingInvitationsOrCancellations.SendToNone,
new ItemId(responses.get(0)), buildProperties(localProperties));
executeMethod(updateItemMethod);
}

View File

@ -0,0 +1,32 @@
/*
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
* Copyright (C) 2010 Mickael Guessant
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package davmail.exchange.ews;
/**
* Item update option.
*/
public class SendMeetingInvitations extends AttributeOption {
private SendMeetingInvitations(String value) {
super("SendMeetingInvitations", value);
}
public static final SendMeetingInvitations SendToNone = new SendMeetingInvitations("SendToNone");
public static final SendMeetingInvitations SendOnlyToAll = new SendMeetingInvitations("SendOnlyToAll");
public static final SendMeetingInvitations SendToAllAndSaveCopy = new SendMeetingInvitations("SendToAllAndSaveCopy");
}

View File

@ -21,12 +21,12 @@ package davmail.exchange.ews;
/**
* Item update option.
*/
public class CalendarItemCreateOrDeleteOperation extends AttributeOption {
private CalendarItemCreateOrDeleteOperation(String value) {
public class SendMeetingInvitationsOrCancellations extends AttributeOption {
private SendMeetingInvitationsOrCancellations(String value) {
super("SendMeetingInvitationsOrCancellations", value);
}
public static final CalendarItemCreateOrDeleteOperation SendToNone = new CalendarItemCreateOrDeleteOperation("SendToNone");
public static final CalendarItemCreateOrDeleteOperation SendOnlyToAll = new CalendarItemCreateOrDeleteOperation("SendOnlyToAll");
public static final CalendarItemCreateOrDeleteOperation SendToAllAndSaveCopy = new CalendarItemCreateOrDeleteOperation("SendToAllAndSaveCopy");
public static final SendMeetingInvitationsOrCancellations SendToNone = new SendMeetingInvitationsOrCancellations("SendToNone");
public static final SendMeetingInvitationsOrCancellations SendOnlyToAll = new SendMeetingInvitationsOrCancellations("SendOnlyToAll");
public static final SendMeetingInvitationsOrCancellations SendToAllAndSaveCopy = new SendMeetingInvitationsOrCancellations("SendToAllAndSaveCopy");
}

View File

@ -25,13 +25,13 @@ import java.util.Set;
*/
public class UpdateItemMethod extends EWSMethod {
public UpdateItemMethod(MessageDisposition messageDisposition, ConflictResolution conflictResolution,
CalendarItemCreateOrDeleteOperation calendarItemCreateOrDeleteOperation,
SendMeetingInvitationsOrCancellations sendMeetingInvitationsOrCancellations,
ItemId itemId, Set<FieldUpdate> updates) {
super("Item", "UpdateItem");
this.itemId = itemId;
this.updates = updates;
addMethodOption(messageDisposition);
addMethodOption(conflictResolution);
addMethodOption(calendarItemCreateOrDeleteOperation);
addMethodOption(sendMeetingInvitationsOrCancellations);
}
}