diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 07dbb9f3..f260761b 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -2672,7 +2672,7 @@ public abstract class ExchangeSession { || line.indexOf(']') == -1)) { } if (line != null) { - int start = line.toLowerCase().indexOf('[') + 1; + int start = line.indexOf('[') + 1; int end = line.indexOf(']', start); result = line.substring(start, end); } diff --git a/src/java/davmail/exchange/VCalendar.java b/src/java/davmail/exchange/VCalendar.java index 93ffdb8f..d6ee9aad 100644 --- a/src/java/davmail/exchange/VCalendar.java +++ b/src/java/davmail/exchange/VCalendar.java @@ -305,6 +305,14 @@ public class VCalendar extends VObject { return property.getValue().equalsIgnoreCase("mailto:" + email); } + /** + * Return VTimezone object + * @return VTimezone + */ + public VObject getVTimezone() { + return vTimezone; + } + /** * Convert X-CALENDARSERVER-ACCESS to CLASS. * see http://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk/doc/Extensions/caldav-privateevents.txt diff --git a/src/java/davmail/exchange/VObject.java b/src/java/davmail/exchange/VObject.java index 08cb21ce..0d946eae 100644 --- a/src/java/davmail/exchange/VObject.java +++ b/src/java/davmail/exchange/VObject.java @@ -55,7 +55,7 @@ public class VObject { type = beginProperty.getValue(); String endLine = "END:" + type; String line = reader.readLine(); - while (line != null && !endLine.equals(line)) { + while (line != null && !line.startsWith(endLine)) { handleLine(line, reader); line = reader.readLine(); } diff --git a/src/java/davmail/exchange/ews/EWSMethod.java b/src/java/davmail/exchange/ews/EWSMethod.java index d6ce8603..902e7677 100644 --- a/src/java/davmail/exchange/ews/EWSMethod.java +++ b/src/java/davmail/exchange/ews/EWSMethod.java @@ -88,8 +88,8 @@ public abstract class EWSMethod extends PostMethod { /** * Build EWS method * - * @param itemType item type - * @param methodName method name + * @param itemType item type + * @param methodName method name * @param responseCollectionName item response collection name */ public EWSMethod(String itemType, String methodName, String responseCollectionName) { @@ -374,7 +374,7 @@ public abstract class EWSMethod extends PostMethod { protected void writeIndexedPageItemView(Writer writer) throws IOException { if (maxCount > 0) { - writer.write(""); } @@ -404,7 +404,8 @@ public abstract class EWSMethod extends PostMethod { } /** - * Get Exchange server version, Exchange2010 or Exchange2007_SP1 + * Get Exchange server version, Exchange2010 or Exchange2007_SP1 + * * @return server version */ public String getServerVersion() { @@ -413,6 +414,7 @@ public abstract class EWSMethod extends PostMethod { /** * Set Exchange server version, Exchange2010 or Exchange2007_SP1 + * * @param serverVersion server version */ public void setServerVersion(String serverVersion) { @@ -447,13 +449,19 @@ public abstract class EWSMethod extends PostMethod { writer.write(type); writer.write(">"); for (Map.Entry mapEntry : this.entrySet()) { - writer.write(""); - writer.write(StringUtil.xmlEncode(mapEntry.getValue())); - writer.write(""); + if ("MeetingTimeZone".equals(mapEntry.getKey())) { + writer.write(""); + } else { + writer.write(""); + writer.write(StringUtil.xmlEncode(mapEntry.getValue())); + writer.write(""); + } } if (mimeContent != null) { writer.write(""); @@ -801,7 +809,7 @@ public abstract class EWSMethod extends PostMethod { while (reader.hasNext()) { reader.next(); handleErrors(reader); - if (serverVersion == null && isStartTag(reader,"ServerVersionInfo")) { + if (serverVersion == null && isStartTag(reader, "ServerVersionInfo")) { String majorVersion = getAttributeValue(reader, "MajorVersion"); if ("14".equals(majorVersion)) { serverVersion = "Exchange2010"; diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index d2b75527..0c9176d3 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -22,6 +22,7 @@ import davmail.exception.DavMailAuthenticationException; import davmail.exception.DavMailException; import davmail.exception.HttpNotFoundException; import davmail.exchange.ExchangeSession; +import davmail.exchange.VCalendar; import davmail.http.DavGatewayHttpClientFacade; import davmail.util.IOUtil; import davmail.util.StringUtil; @@ -30,7 +31,9 @@ import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -604,7 +607,7 @@ public class EwsExchangeSession extends ExchangeSession { GetFolderMethod getFolderMethod = new GetFolderMethod(BaseShape.ID_ONLY, folderId, FOLDER_PROPERTIES); executeMethod(getFolderMethod); EWSMethod.Item item = getFolderMethod.getResponseItem(); - Folder folder = null; + Folder folder; if (item != null) { folder = buildFolder(folderId.mailbox, item); folder.folderPath = folderPath; @@ -1135,7 +1138,60 @@ public class EwsExchangeSession extends ExchangeSession { @Override protected void loadVtimezone() { - throw new UnsupportedOperationException(); + String timezoneId = getTimezoneidFromOptions(); + try { + deleteFolder("davmailtemp"); + createCalendarFolder("davmailtemp", null); + EWSMethod.Item item = new EWSMethod.Item(); + item.type = "CalendarItem"; + item.put("MeetingTimeZone", timezoneId); + CreateItemMethod createItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, SendMeetingInvitations.SendToNone, getFolderId("davmailtemp"), item); + executeMethod(createItemMethod); + item = createItemMethod.getResponseItem(); + VCalendar vCalendar = new VCalendar(getContent(new ItemId(item)), email, null); + this.vTimezone = vCalendar.getVTimezone(); + // delete temporary folder + deleteFolder("davmailtemp"); + } catch (IOException e) { + LOGGER.warn("Unable to get VTIMEZONE info: " + e, e); + } + } + + protected static final String TMZN = "tblTmZn"; + + protected String getTimezoneidFromOptions() { + String result = null; + // get user mail URL from html body + BufferedReader optionsPageReader = null; + GetMethod optionsMethod = new GetMethod("/owa/?ae=Options&t=Regional"); + try { + DavGatewayHttpClientFacade.executeGetMethod(httpClient, optionsMethod, false); + optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream())); + String line; + // find email + //noinspection StatementWithEmptyBody + while ((line = optionsPageReader.readLine()) != null + && (line.indexOf(TMZN) == -1)) { + } + if (line != null) { + int start = line.indexOf("oV=\"") + 4; + int end = line.indexOf('\"', start); + result = line.substring(start, end); + } + } catch (IOException e) { + LOGGER.error("Error parsing options page at " + optionsMethod.getPath()); + } finally { + if (optionsPageReader != null) { + try { + optionsPageReader.close(); + } catch (IOException e) { + LOGGER.error("Error parsing options page at " + optionsMethod.getPath()); + } + } + optionsMethod.releaseConnection(); + } + + return result; } @@ -1274,11 +1330,12 @@ public class EwsExchangeSession extends ExchangeSession { return contact; } + @Override public Map galFind(Condition condition, Set returningAttributes, int sizeLimit) throws IOException { Map contacts = new HashMap(); if (condition instanceof MultiCondition) { - List conditions = ((MultiCondition) condition).getConditions(); - Operator operator = ((MultiCondition) condition).getOperator(); + List conditions = ((ExchangeSession.MultiCondition) condition).getConditions(); + Operator operator = ((ExchangeSession.MultiCondition) condition).getOperator(); if (operator == Operator.Or) { for (Condition innerCondition : conditions) { contacts.putAll(galFind(innerCondition, returningAttributes, sizeLimit)); @@ -1292,9 +1349,9 @@ public class EwsExchangeSession extends ExchangeSession { } } } else if (condition instanceof AttributeCondition) { - String mappedAttributeName = GALFIND_ATTRIBUTE_MAP.get(((AttributeCondition) condition).getAttributeName()); + String mappedAttributeName = GALFIND_ATTRIBUTE_MAP.get(((ExchangeSession.AttributeCondition) condition).getAttributeName()); if (mappedAttributeName != null) { - String value = ((AttributeCondition) condition).getValue().toLowerCase(); + String value = ((ExchangeSession.AttributeCondition) condition).getValue().toLowerCase(); Operator operator = ((AttributeCondition) condition).getOperator(); String searchValue = value; if (mappedAttributeName.startsWith("EmailAddress")) { diff --git a/src/test/davmail/exchange/TestExchangeSessionCalendar.java b/src/test/davmail/exchange/TestExchangeSessionCalendar.java index 4dc2513f..a917057a 100644 --- a/src/test/davmail/exchange/TestExchangeSessionCalendar.java +++ b/src/test/davmail/exchange/TestExchangeSessionCalendar.java @@ -86,10 +86,16 @@ public class TestExchangeSessionCalendar extends AbstractExchangeSessionTestCase } public void testSearchCalendar() throws IOException { - List events = session.getAllEvents("/users/" + session.getEmail() + "/calendar/test"); + List events = null; + try { + events = session.getAllEvents("/users/" + session.getEmail() + "/calendar"); for (ExchangeSession.Event event:events) { System.out.println(event.getBody()); } + } catch (IOException e) { + System.out.println(e.getMessage()); + throw e; + } } public void testGetFreeBusyData() throws IOException, MessagingException {