From efc9be7c230ea5f14257b7a8f7b0c9a38b02c482 Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 9 Dec 2008 13:24:17 +0000 Subject: [PATCH] Fix another URI encoding issue with calendar and fix all day events (add X-MICROSOFT-CDO-ALLDAYEVENT) git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@223 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 59 +++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 07b23bfc..04297563 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -42,6 +42,12 @@ public class ExchangeSession { MESSAGE_REQUEST_PROPERTIES.add("http://schemas.microsoft.com/mapi/proptag/x0e080003"); } + protected static final Vector EVENT_REQUEST_PROPERTIES = new Vector(); + + static { + EVENT_REQUEST_PROPERTIES.add("DAV:getetag"); + } + protected static final Vector WELL_KNOWN_FOLDERS = new Vector(); static { @@ -903,11 +909,9 @@ public class ExchangeSession { public Event getEvent(String path) throws IOException { // TODO : refactor with getAllEvents Event event = new Event(); - final Vector EVENT_REQUEST_PROPERTIES = new Vector(); - EVENT_REQUEST_PROPERTIES.add("DAV:getetag"); //wdr.setDebug(4); - Enumeration calendarEnum = wdr.propfindMethod(calendarUrl + "/" + path, 0, EVENT_REQUEST_PROPERTIES); + Enumeration calendarEnum = wdr.propfindMethod(calendarUrl + "/" + URIUtil.decode(path), 0, EVENT_REQUEST_PROPERTIES); //wdr.setDebug(0); if (!calendarEnum.hasMoreElements()) { throw new IOException("Unable to get calendar event"); @@ -926,6 +930,53 @@ public class ExchangeSession { return event; } + protected String fixICSToExchange(String icsBody) throws IOException { + // first pass : detect + boolean isAllDay = false; + boolean hasCdoAllDay = false; + BufferedReader reader = null; + try { + reader = new BufferedReader(new StringReader(icsBody)); + String line; + while ((line = reader.readLine()) != null) { + int index = line.indexOf(':'); + if (index >= 0) { + String key = line.substring(0, index); + String value = line.substring(index + 1); + if ("DTSTART;VALUE=DATE".equals(key)) { + isAllDay = true; + } else if ("X-MICROSOFT-CDO-ALLDAYEVENT".equals(key)) { + hasCdoAllDay = true; + } + } + } + } finally { + if (reader != null) { + reader.close(); + } + } + // second pass : fix + StringBuilder result = new StringBuilder(); + try { + reader = new BufferedReader(new StringReader(icsBody)); + String line; + while ((line = reader.readLine()) != null) { + if (isAllDay && "X-MICROSOFT-CDO-ALLDAYEVENT:FALSE".equals(line)) { + line = "X-MICROSOFT-CDO-ALLDAYEVENT:TRUE"; + } else if ("END:VEVENT".equals(line) && isAllDay && !hasCdoAllDay) { + result.append("X-MICROSOFT-CDO-ALLDAYEVENT:TRUE").append((char) 13).append((char) 10); + } else if (!isAllDay && "X-MICROSOFT-CDO-ALLDAYEVENT:TRUE".equals(line)) { + line = "X-MICROSOFT-CDO-ALLDAYEVENT:FALSE"; + } + result.append(line).append((char) 13).append((char) 10); + } + } finally { + reader.close(); + } + + return result.toString(); + } + public int createOrUpdateEvent(String path, String icsBody, String etag) throws IOException { String messageUrl = URIUtil.encodePathQuery(calendarUrl + "/" + URIUtil.decode(path)); String uid = path.substring(0, path.lastIndexOf(".")); @@ -951,7 +1002,7 @@ public class ExchangeSession { "\tmethod=REQUEST;\n" + "\tcharset=\"utf-8\"\n" + "Content-Transfer-Encoding: 8bit\n\n"); - body.append(new String(icsBody.getBytes("UTF-8"), "ISO-8859-1")); + body.append(new String(fixICSToExchange(icsBody).getBytes("UTF-8"), "ISO-8859-1")); body.append("------=_NextPart_").append(uid).append("--\n"); putmethod.setRequestBody(body.toString()); int status;