diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 100ec07a..3e7dfe86 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -1952,6 +1952,7 @@ public abstract class ExchangeSession { String eventClass = null; String organizer = null; String action = null; + String method = null; boolean sound = false; List allDayStates = new ArrayList(); @@ -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 diff --git a/src/java/davmail/exchange/ews/CreateItemMethod.java b/src/java/davmail/exchange/ews/CreateItemMethod.java index f492995a..de7e4334 100644 --- a/src/java/davmail/exchange/ews/CreateItemMethod.java +++ b/src/java/davmail/exchange/ews/CreateItemMethod.java @@ -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); + } + } diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 93097e37..066eb919 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -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 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 updates = new HashSet(); + // 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); } diff --git a/src/java/davmail/exchange/ews/SendMeetingInvitations.java b/src/java/davmail/exchange/ews/SendMeetingInvitations.java new file mode 100644 index 00000000..b00c9f6c --- /dev/null +++ b/src/java/davmail/exchange/ews/SendMeetingInvitations.java @@ -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"); +} diff --git a/src/java/davmail/exchange/ews/CalendarItemCreateOrDeleteOperation.java b/src/java/davmail/exchange/ews/SendMeetingInvitationsOrCancellations.java similarity index 61% rename from src/java/davmail/exchange/ews/CalendarItemCreateOrDeleteOperation.java rename to src/java/davmail/exchange/ews/SendMeetingInvitationsOrCancellations.java index ffd4efd5..4bbe2144 100644 --- a/src/java/davmail/exchange/ews/CalendarItemCreateOrDeleteOperation.java +++ b/src/java/davmail/exchange/ews/SendMeetingInvitationsOrCancellations.java @@ -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"); } diff --git a/src/java/davmail/exchange/ews/UpdateItemMethod.java b/src/java/davmail/exchange/ews/UpdateItemMethod.java index 94ef19f9..16dfbcd7 100644 --- a/src/java/davmail/exchange/ews/UpdateItemMethod.java +++ b/src/java/davmail/exchange/ews/UpdateItemMethod.java @@ -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 updates) { super("Item", "UpdateItem"); this.itemId = itemId; this.updates = updates; addMethodOption(messageDisposition); addMethodOption(conflictResolution); - addMethodOption(calendarItemCreateOrDeleteOperation); + addMethodOption(sendMeetingInvitationsOrCancellations); } } \ No newline at end of file