EWS: implement loadVTimezone for Exchange 2010

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1363 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-08-18 10:33:25 +00:00
parent c644f792bb
commit a51d910df6
6 changed files with 177 additions and 45 deletions

View File

@ -432,12 +432,21 @@ public abstract class EWSMethod extends PostMethod {
protected byte[] mimeContent;
protected Set<FieldUpdate> fieldUpdates;
protected List<FileAttachment> attachments;
protected List<String> fieldNames = new ArrayList<String>();
@Override
public String toString() {
return "type: " + type + ' ' + super.toString();
}
@Override
public String put(String key, String value) {
if (get(key) == null) {
fieldNames.add(key);
}
return super.put(key, value);
}
/**
* Write XML content to writer.
*
@ -448,18 +457,23 @@ public abstract class EWSMethod extends PostMethod {
writer.write("<t:");
writer.write(type);
writer.write(">");
for (Map.Entry<String, String> mapEntry : this.entrySet()) {
if ("MeetingTimeZone".equals(mapEntry.getKey())) {
// write ordered fields
for (String key:fieldNames) {
if ("MeetingTimeZone".equals(key)) {
writer.write("<t:MeetingTimeZone TimeZoneName=\"");
writer.write(StringUtil.xmlEncode(mapEntry.getValue()));
writer.write(StringUtil.xmlEncode(get(key)));
writer.write("\"></t:MeetingTimeZone>");
} else if ("StartTimeZone".equals(key)) {
writer.write("<t:StartTimeZone Id=\"");
writer.write(StringUtil.xmlEncode(get(key)));
writer.write("\"></t:StartTimeZone>");
} else {
writer.write("<t:");
writer.write(mapEntry.getKey());
writer.write(key);
writer.write(">");
writer.write(StringUtil.xmlEncode(mapEntry.getValue()));
writer.write(StringUtil.xmlEncode(get(key)));
writer.write("</t:");
writer.write(mapEntry.getKey());
writer.write(key);
writer.write(">");
}
}
@ -643,6 +657,7 @@ public abstract class EWSMethod extends PostMethod {
&& !"NoError".equals(result)
&& !"ErrorNameResolutionMultipleResults".equals(result)
&& !"ErrorNameResolutionNoResults".equals(result)
&& !"ErrorFolderExists".equals(result)
) {
errorDetail = result;
}

View File

@ -612,7 +612,7 @@ public class EwsExchangeSession extends ExchangeSession {
folder = buildFolder(folderId.mailbox, item);
folder.folderPath = folderPath;
} else {
throw new HttpNotFoundException("Folder "+folderPath+" not found");
throw new HttpNotFoundException("Folder " + folderPath + " not found");
}
return folder;
}
@ -625,8 +625,8 @@ public class EwsExchangeSession extends ExchangeSession {
FolderPath path = new FolderPath(folderPath);
EWSMethod.Item folder = new EWSMethod.Item();
folder.type = "Folder";
folder.put("DisplayName", path.folderName);
folder.put("FolderClass", folderClass);
folder.put("DisplayName", path.folderName);
// TODO: handle properties
CreateFolderMethod createFolderMethod = new CreateFolderMethod(getFolderId(path.parentPath), folder);
executeMethod(createFolderMethod);
@ -875,7 +875,7 @@ public class EwsExchangeSession extends ExchangeSession {
@Override
public ItemResult createOrUpdate() throws IOException {
byte[] itemContent = Base64.encodeBase64(vCalendar.toString().getBytes("UTF-8"));
byte[] itemContent = Base64.encodeBase64(createMimeContent());
ItemResult itemResult = new ItemResult();
EWSMethod createOrUpdateItemMethod;
@ -887,7 +887,7 @@ public class EwsExchangeSession extends ExchangeSession {
if (currentItem != null) {
currentItemId = new ItemId(currentItem);
currentEtag = currentItem.get(Field.get("etag").getResponseName());
LOGGER.debug("Existing item found with etag: "+currentEtag+" id: "+currentItemId.id);
LOGGER.debug("Existing item found with etag: " + currentEtag + " id: " + currentItemId.id);
}
if ("*".equals(noneMatch)) {
// create requested
@ -915,16 +915,16 @@ public class EwsExchangeSession extends ExchangeSession {
DeleteItemMethod deleteItemMethod = new DeleteItemMethod(currentItemId, DeleteType.HardDelete, SendMeetingCancellations.SendToNone);
executeMethod(deleteItemMethod);
} //else {
// create
EWSMethod.Item newItem = new EWSMethod.Item();
newItem.type = "CalendarItem";
newItem.mimeContent = itemContent;
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);
// create
EWSMethod.Item newItem = new EWSMethod.Item();
newItem.type = "CalendarItem";
newItem.mimeContent = itemContent;
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);
@ -1142,19 +1142,36 @@ public class EwsExchangeSession extends ExchangeSession {
@Override
protected void loadVtimezone() {
String timezoneId = getTimezoneidFromOptions();
try {
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");
String timezoneId = null;
if ("Exchange2010".equals(serverVersion)) {
GetUserConfigurationMethod getUserConfigurationMethod = new GetUserConfigurationMethod();
executeMethod(getUserConfigurationMethod);
EWSMethod.Item item = getUserConfigurationMethod.getResponseItem();
if (item != null) {
timezoneId = item.get("timezone");
}
} else {
getTimezoneidFromOptions();
}
if (timezoneId != null) {
createCalendarFolder("davmailtemp", null);
EWSMethod.Item item = new EWSMethod.Item();
item.type = "CalendarItem";
if ("Exchange2010".equals(serverVersion)) {
item.put("StartTimeZone", timezoneId);
} else {
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);
}

View File

@ -95,11 +95,11 @@ public class GetUserAvailabilityMethod extends EWSMethod {
"</t:FreeBusyViewOptions>");
}
@Override
protected void handleCustom(XMLStreamReader reader) throws XMLStreamException {
if (isStartTag(reader, "MergedFreeBusy")) {
this.mergedFreeBusy = reader.getElementText();
}
@Override
protected void handleCustom(XMLStreamReader reader) throws XMLStreamException {
if (isStartTag(reader, "MergedFreeBusy")) {
this.mergedFreeBusy = reader.getElementText();
}
}
public String getMergedFreeBusy() {

View File

@ -0,0 +1,87 @@
/*
* 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;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.io.Writer;
/**
* Get User Configuration method.
*/
public class GetUserConfigurationMethod extends EWSMethod {
/**
* Get User Configuration method.
*
*/
public GetUserConfigurationMethod() {
super("UserConfiguration", "GetUserConfiguration");
folderId = DistinguishedFolderId.getInstance(null, DistinguishedFolderId.Name.root);
}
@Override
protected void writeSoapBody(Writer writer) throws IOException {
writer.write("<m:UserConfigurationName Name=\"OWA.UserOptions\">");
folderId.write(writer);
writer.write("</m:UserConfigurationName>");
writer.write("<m:UserConfigurationProperties>All</m:UserConfigurationProperties>");
}
@Override
protected void handleCustom(XMLStreamReader reader) throws XMLStreamException {
if (isStartTag(reader, "UserConfiguration")) {
responseItems.add(handleUserConfiguration(reader));
}
}
private Item handleUserConfiguration(XMLStreamReader reader) throws XMLStreamException {
Item responseItem = new Item();
while (reader.hasNext() && !(isEndTag(reader, "UserConfiguration"))) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
String tagLocalName = reader.getLocalName();
if ("DictionaryEntry".equals(tagLocalName)) {
handleDictionaryEntry(reader, responseItem);
}
}
}
return responseItem;
}
private void handleDictionaryEntry(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
String key = null;
while (reader.hasNext() && !(isEndTag(reader, "DictionaryEntry"))) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
String tagLocalName = reader.getLocalName();
if ("Value".equals(tagLocalName)) {
if (key == null) {
key = reader.getElementText();
} else {
responseItem.put(key, reader.getElementText());
}
}
}
}
}
}

View File

@ -178,14 +178,16 @@ public final class StringUtil {
*/
public static String xmlEncode(String name) {
String result = name;
if (name.indexOf('&') >= 0) {
result = AMP_PATTERN.matcher(result).replaceAll("&amp;");
}
if (name.indexOf('<') >= 0) {
result = LT_PATTERN.matcher(result).replaceAll("&lt;");
}
if (name.indexOf('>') >= 0) {
result = GT_PATTERN.matcher(result).replaceAll("&gt;");
if (name != null) {
if (name.indexOf('&') >= 0) {
result = AMP_PATTERN.matcher(result).replaceAll("&amp;");
}
if (name.indexOf('<') >= 0) {
result = LT_PATTERN.matcher(result).replaceAll("&lt;");
}
if (name.indexOf('>') >= 0) {
result = GT_PATTERN.matcher(result).replaceAll("&gt;");
}
}
return result;
}

View File

@ -66,4 +66,15 @@ public class TestEwsExchangeSession extends AbstractExchangeSessionTestCase {
}
}
public void testGetUserConfiguration() throws IOException {
GetUserConfigurationMethod getUserConfigurationMethod = new GetUserConfigurationMethod();
ewsSession.executeMethod(getUserConfigurationMethod);
EWSMethod.Item item = getUserConfigurationMethod.getResponseItem();
assertNotNull(item);
}
public void testTimezone() {
ewsSession.loadVtimezone();
}
}