1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-08-13 16:53:51 -04:00

EWS: implement loadVtimezone, get user timezone id from OWA settings

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1353 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-08-17 13:39:14 +00:00
parent b582494bc2
commit 93d528704d
6 changed files with 100 additions and 21 deletions

View File

@ -2672,7 +2672,7 @@ public abstract class ExchangeSession {
|| line.indexOf(']') == -1)) { || line.indexOf(']') == -1)) {
} }
if (line != null) { if (line != null) {
int start = line.toLowerCase().indexOf('[') + 1; int start = line.indexOf('[') + 1;
int end = line.indexOf(']', start); int end = line.indexOf(']', start);
result = line.substring(start, end); result = line.substring(start, end);
} }

View File

@ -305,6 +305,14 @@ public class VCalendar extends VObject {
return property.getValue().equalsIgnoreCase("mailto:" + email); return property.getValue().equalsIgnoreCase("mailto:" + email);
} }
/**
* Return VTimezone object
* @return VTimezone
*/
public VObject getVTimezone() {
return vTimezone;
}
/** /**
* Convert X-CALENDARSERVER-ACCESS to CLASS. * Convert X-CALENDARSERVER-ACCESS to CLASS.
* see http://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk/doc/Extensions/caldav-privateevents.txt * see http://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk/doc/Extensions/caldav-privateevents.txt

View File

@ -55,7 +55,7 @@ public class VObject {
type = beginProperty.getValue(); type = beginProperty.getValue();
String endLine = "END:" + type; String endLine = "END:" + type;
String line = reader.readLine(); String line = reader.readLine();
while (line != null && !endLine.equals(line)) { while (line != null && !line.startsWith(endLine)) {
handleLine(line, reader); handleLine(line, reader);
line = reader.readLine(); line = reader.readLine();
} }

View File

@ -88,8 +88,8 @@ public abstract class EWSMethod extends PostMethod {
/** /**
* Build EWS method * Build EWS method
* *
* @param itemType item type * @param itemType item type
* @param methodName method name * @param methodName method name
* @param responseCollectionName item response collection name * @param responseCollectionName item response collection name
*/ */
public EWSMethod(String itemType, String methodName, String responseCollectionName) { 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 { protected void writeIndexedPageItemView(Writer writer) throws IOException {
if (maxCount > 0) { if (maxCount > 0) {
writer.write("<m:IndexedPageItemView MaxEntriesReturned=\""); writer.write("<m:IndexedPageItemView MaxEntriesReturned=\"");
writer.write(String.valueOf(maxCount)); writer.write(String.valueOf(maxCount));
writer.write("\" Offset=\"0\" BasePoint=\"Beginning\"/>"); writer.write("\" Offset=\"0\" BasePoint=\"Beginning\"/>");
} }
@ -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 * @return server version
*/ */
public String getServerVersion() { public String getServerVersion() {
@ -413,6 +414,7 @@ public abstract class EWSMethod extends PostMethod {
/** /**
* Set Exchange server version, Exchange2010 or Exchange2007_SP1 * Set Exchange server version, Exchange2010 or Exchange2007_SP1
*
* @param serverVersion server version * @param serverVersion server version
*/ */
public void setServerVersion(String serverVersion) { public void setServerVersion(String serverVersion) {
@ -447,13 +449,19 @@ public abstract class EWSMethod extends PostMethod {
writer.write(type); writer.write(type);
writer.write(">"); writer.write(">");
for (Map.Entry<String, String> mapEntry : this.entrySet()) { for (Map.Entry<String, String> mapEntry : this.entrySet()) {
writer.write("<t:"); if ("MeetingTimeZone".equals(mapEntry.getKey())) {
writer.write(mapEntry.getKey()); writer.write("<t:MeetingTimeZone TimeZoneName=\"");
writer.write(">"); writer.write(StringUtil.xmlEncode(mapEntry.getValue()));
writer.write(StringUtil.xmlEncode(mapEntry.getValue())); writer.write("\"></t:MeetingTimeZone>");
writer.write("</t:"); } else {
writer.write(mapEntry.getKey()); writer.write("<t:");
writer.write(">"); writer.write(mapEntry.getKey());
writer.write(">");
writer.write(StringUtil.xmlEncode(mapEntry.getValue()));
writer.write("</t:");
writer.write(mapEntry.getKey());
writer.write(">");
}
} }
if (mimeContent != null) { if (mimeContent != null) {
writer.write("<t:MimeContent>"); writer.write("<t:MimeContent>");
@ -801,7 +809,7 @@ public abstract class EWSMethod extends PostMethod {
while (reader.hasNext()) { while (reader.hasNext()) {
reader.next(); reader.next();
handleErrors(reader); handleErrors(reader);
if (serverVersion == null && isStartTag(reader,"ServerVersionInfo")) { if (serverVersion == null && isStartTag(reader, "ServerVersionInfo")) {
String majorVersion = getAttributeValue(reader, "MajorVersion"); String majorVersion = getAttributeValue(reader, "MajorVersion");
if ("14".equals(majorVersion)) { if ("14".equals(majorVersion)) {
serverVersion = "Exchange2010"; serverVersion = "Exchange2010";

View File

@ -22,6 +22,7 @@ import davmail.exception.DavMailAuthenticationException;
import davmail.exception.DavMailException; import davmail.exception.DavMailException;
import davmail.exception.HttpNotFoundException; import davmail.exception.HttpNotFoundException;
import davmail.exchange.ExchangeSession; import davmail.exchange.ExchangeSession;
import davmail.exchange.VCalendar;
import davmail.http.DavGatewayHttpClientFacade; import davmail.http.DavGatewayHttpClientFacade;
import davmail.util.IOUtil; import davmail.util.IOUtil;
import davmail.util.StringUtil; 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.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.GetMethod;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -604,7 +607,7 @@ public class EwsExchangeSession extends ExchangeSession {
GetFolderMethod getFolderMethod = new GetFolderMethod(BaseShape.ID_ONLY, folderId, FOLDER_PROPERTIES); GetFolderMethod getFolderMethod = new GetFolderMethod(BaseShape.ID_ONLY, folderId, FOLDER_PROPERTIES);
executeMethod(getFolderMethod); executeMethod(getFolderMethod);
EWSMethod.Item item = getFolderMethod.getResponseItem(); EWSMethod.Item item = getFolderMethod.getResponseItem();
Folder folder = null; Folder folder;
if (item != null) { if (item != null) {
folder = buildFolder(folderId.mailbox, item); folder = buildFolder(folderId.mailbox, item);
folder.folderPath = folderPath; folder.folderPath = folderPath;
@ -1135,7 +1138,60 @@ public class EwsExchangeSession extends ExchangeSession {
@Override @Override
protected void loadVtimezone() { 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; return contact;
} }
@Override
public Map<String, ExchangeSession.Contact> galFind(Condition condition, Set<String> returningAttributes, int sizeLimit) throws IOException { public Map<String, ExchangeSession.Contact> galFind(Condition condition, Set<String> returningAttributes, int sizeLimit) throws IOException {
Map<String, ExchangeSession.Contact> contacts = new HashMap<String, ExchangeSession.Contact>(); Map<String, ExchangeSession.Contact> contacts = new HashMap<String, ExchangeSession.Contact>();
if (condition instanceof MultiCondition) { if (condition instanceof MultiCondition) {
List<Condition> conditions = ((MultiCondition) condition).getConditions(); List<Condition> conditions = ((ExchangeSession.MultiCondition) condition).getConditions();
Operator operator = ((MultiCondition) condition).getOperator(); Operator operator = ((ExchangeSession.MultiCondition) condition).getOperator();
if (operator == Operator.Or) { if (operator == Operator.Or) {
for (Condition innerCondition : conditions) { for (Condition innerCondition : conditions) {
contacts.putAll(galFind(innerCondition, returningAttributes, sizeLimit)); contacts.putAll(galFind(innerCondition, returningAttributes, sizeLimit));
@ -1292,9 +1349,9 @@ public class EwsExchangeSession extends ExchangeSession {
} }
} }
} else if (condition instanceof AttributeCondition) { } 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) { if (mappedAttributeName != null) {
String value = ((AttributeCondition) condition).getValue().toLowerCase(); String value = ((ExchangeSession.AttributeCondition) condition).getValue().toLowerCase();
Operator operator = ((AttributeCondition) condition).getOperator(); Operator operator = ((AttributeCondition) condition).getOperator();
String searchValue = value; String searchValue = value;
if (mappedAttributeName.startsWith("EmailAddress")) { if (mappedAttributeName.startsWith("EmailAddress")) {

View File

@ -86,10 +86,16 @@ public class TestExchangeSessionCalendar extends AbstractExchangeSessionTestCase
} }
public void testSearchCalendar() throws IOException { public void testSearchCalendar() throws IOException {
List<ExchangeSession.Event> events = session.getAllEvents("/users/" + session.getEmail() + "/calendar/test"); List<ExchangeSession.Event> events = null;
try {
events = session.getAllEvents("/users/" + session.getEmail() + "/calendar");
for (ExchangeSession.Event event:events) { for (ExchangeSession.Event event:events) {
System.out.println(event.getBody()); System.out.println(event.getBody());
} }
} catch (IOException e) {
System.out.println(e.getMessage());
throw e;
}
} }
public void testGetFreeBusyData() throws IOException, MessagingException { public void testGetFreeBusyData() throws IOException, MessagingException {