2010-06-02 04:46:46 -04:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
import davmail.exception.DavMailAuthenticationException;
|
2010-06-02 04:46:46 -04:00
|
|
|
import davmail.exception.DavMailException;
|
|
|
|
import davmail.exchange.ExchangeSession;
|
2010-06-07 17:17:07 -04:00
|
|
|
import davmail.http.DavGatewayHttpClientFacade;
|
|
|
|
import davmail.util.StringUtil;
|
2010-07-05 05:58:18 -04:00
|
|
|
import org.apache.commons.codec.binary.Base64;
|
2010-07-07 05:20:42 -04:00
|
|
|
import org.apache.commons.httpclient.HttpException;
|
2010-06-02 04:46:46 -04:00
|
|
|
import org.apache.commons.httpclient.HttpMethod;
|
2010-06-07 17:17:07 -04:00
|
|
|
import org.apache.commons.httpclient.HttpStatus;
|
2010-07-01 12:12:57 -04:00
|
|
|
import org.apache.commons.httpclient.URIException;
|
2010-06-07 17:17:07 -04:00
|
|
|
import org.apache.commons.httpclient.methods.HeadMethod;
|
2010-06-02 04:46:46 -04:00
|
|
|
|
2010-07-07 05:20:42 -04:00
|
|
|
import javax.mail.MessagingException;
|
2010-06-09 05:52:12 -04:00
|
|
|
import java.io.BufferedReader;
|
2010-07-05 06:23:51 -04:00
|
|
|
import java.io.ByteArrayInputStream;
|
2010-06-02 04:46:46 -04:00
|
|
|
import java.io.IOException;
|
2010-07-05 06:23:51 -04:00
|
|
|
import java.io.InputStreamReader;
|
2010-07-07 05:20:42 -04:00
|
|
|
import java.net.HttpURLConnection;
|
2010-06-09 05:52:12 -04:00
|
|
|
import java.net.NoRouteToHostException;
|
|
|
|
import java.net.UnknownHostException;
|
2010-07-07 05:20:42 -04:00
|
|
|
import java.text.ParseException;
|
2010-06-10 16:47:55 -04:00
|
|
|
import java.util.*;
|
2010-06-02 04:46:46 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* EWS Exchange adapter.
|
|
|
|
* Compatible with Exchange 2007 and hopefully 2010.
|
|
|
|
*/
|
|
|
|
public class EwsExchangeSession extends ExchangeSession {
|
|
|
|
|
2010-06-08 06:44:47 -04:00
|
|
|
protected Map<String, String> folderIdMap;
|
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
protected class Folder extends ExchangeSession.Folder {
|
2010-06-07 05:07:31 -04:00
|
|
|
public FolderId folderId;
|
|
|
|
}
|
|
|
|
|
2010-06-30 17:36:13 -04:00
|
|
|
protected static class FolderPath {
|
2010-06-29 10:15:50 -04:00
|
|
|
protected String parentPath;
|
|
|
|
protected String folderName;
|
|
|
|
|
|
|
|
protected FolderPath(String folderPath) {
|
|
|
|
int slashIndex = folderPath.lastIndexOf('/');
|
|
|
|
if (slashIndex < 0) {
|
|
|
|
parentPath = "";
|
|
|
|
folderName = folderPath;
|
|
|
|
} else {
|
|
|
|
parentPath = folderPath.substring(0, slashIndex);
|
|
|
|
folderName = folderPath.substring(slashIndex + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-02 04:46:46 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public EwsExchangeSession(String url, String userName, String password) throws IOException {
|
|
|
|
super(url, userName, password);
|
|
|
|
}
|
|
|
|
|
2010-06-09 05:52:12 -04:00
|
|
|
@Override
|
|
|
|
public boolean isExpired() throws NoRouteToHostException, UnknownHostException {
|
2010-06-10 16:47:55 -04:00
|
|
|
// TODO: implement
|
|
|
|
return false;
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
2010-06-02 04:46:46 -04:00
|
|
|
@Override
|
|
|
|
protected void buildSessionInfo(HttpMethod method) throws DavMailException {
|
|
|
|
// nothing to do, mailPath not used in EWS mode
|
2010-06-07 17:17:07 -04:00
|
|
|
// check EWS access
|
|
|
|
HttpMethod headMethod = new HeadMethod("/ews/services.wsdl");
|
|
|
|
try {
|
|
|
|
headMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, headMethod);
|
|
|
|
if (headMethod.getStatusCode() != HttpStatus.SC_OK) {
|
|
|
|
throw DavGatewayHttpClientFacade.buildHttpException(headMethod);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.error(e.getMessage());
|
|
|
|
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
|
|
|
|
} finally {
|
|
|
|
headMethod.releaseConnection();
|
|
|
|
}
|
2010-06-08 06:44:47 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
folderIdMap = new HashMap<String, String>();
|
|
|
|
// load actual well known folder ids
|
2010-06-30 17:36:13 -04:00
|
|
|
folderIdMap.put(internalGetFolder(INBOX).folderId.value, INBOX);
|
|
|
|
folderIdMap.put(internalGetFolder(CALENDAR).folderId.value, CALENDAR);
|
|
|
|
folderIdMap.put(internalGetFolder(CONTACTS).folderId.value, CONTACTS);
|
|
|
|
folderIdMap.put(internalGetFolder(SENT).folderId.value, SENT);
|
|
|
|
folderIdMap.put(internalGetFolder(DRAFTS).folderId.value, DRAFTS);
|
|
|
|
folderIdMap.put(internalGetFolder(TRASH).folderId.value, TRASH);
|
|
|
|
folderIdMap.put(internalGetFolder(JUNK).folderId.value, JUNK);
|
|
|
|
folderIdMap.put(internalGetFolder(UNSENT).folderId.value, UNSENT);
|
2010-06-08 06:44:47 -04:00
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.error(e.getMessage(), e);
|
|
|
|
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
|
|
|
|
}
|
2010-06-07 17:17:07 -04:00
|
|
|
}
|
|
|
|
|
2010-07-05 05:58:18 -04:00
|
|
|
class Message extends ExchangeSession.Message {
|
|
|
|
// message item id
|
|
|
|
ItemId itemId;
|
|
|
|
}
|
2010-06-09 05:52:12 -04:00
|
|
|
|
2010-07-05 05:58:18 -04:00
|
|
|
/**
|
|
|
|
* Message create/update properties
|
|
|
|
*
|
|
|
|
* @param properties flag values map
|
|
|
|
* @return field values
|
|
|
|
*/
|
|
|
|
protected Set<FieldUpdate> buildProperties(Map<String, String> properties) {
|
|
|
|
HashSet<FieldUpdate> list = new HashSet<FieldUpdate>();
|
|
|
|
for (Map.Entry<String, String> entry : properties.entrySet()) {
|
|
|
|
if ("read".equals(entry.getKey())) {
|
|
|
|
list.add(Field.createFieldUpdate("read", entry.getValue()));
|
|
|
|
} else if ("junk".equals(entry.getKey())) {
|
|
|
|
list.add(Field.createFieldUpdate("junk", entry.getValue()));
|
|
|
|
} else if ("flagged".equals(entry.getKey())) {
|
|
|
|
list.add(Field.createFieldUpdate("flagStatus", entry.getValue()));
|
|
|
|
} else if ("answered".equals(entry.getKey())) {
|
|
|
|
list.add(Field.createFieldUpdate("lastVerbExecuted", entry.getValue()));
|
|
|
|
if ("102".equals(entry.getValue())) {
|
|
|
|
list.add(Field.createFieldUpdate("iconIndex", "261"));
|
|
|
|
}
|
|
|
|
} else if ("forwarded".equals(entry.getKey())) {
|
|
|
|
list.add(Field.createFieldUpdate("lastVerbExecuted", entry.getValue()));
|
|
|
|
if ("104".equals(entry.getValue())) {
|
|
|
|
list.add(Field.createFieldUpdate("iconIndex", "262"));
|
|
|
|
}
|
|
|
|
} else if ("bcc".equals(entry.getKey())) {
|
|
|
|
list.add(Field.createFieldUpdate("bcc", entry.getValue()));
|
|
|
|
} else if ("draft".equals(entry.getKey())) {
|
|
|
|
// note: draft is readonly after create
|
|
|
|
list.add(Field.createFieldUpdate("messageFlags", entry.getValue()));
|
|
|
|
} else if ("deleted".equals(entry.getKey())) {
|
|
|
|
list.add(Field.createFieldUpdate("deleted", entry.getValue()));
|
|
|
|
} else if ("datereceived".equals(entry.getKey())) {
|
|
|
|
list.add(Field.createFieldUpdate("datereceived", entry.getValue()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-07-05 05:58:18 -04:00
|
|
|
public void createMessage(String folderPath, String messageName, HashMap<String, String> properties, String messageBody) throws IOException {
|
|
|
|
EWSMethod.Item item = new EWSMethod.Item();
|
|
|
|
item.type = "Message";
|
|
|
|
item.mimeContent = Base64.encodeBase64(messageBody.getBytes());
|
2010-07-05 10:23:26 -04:00
|
|
|
String bcc = properties.get("bcc");
|
|
|
|
// Exchange 2007 is unable to handle bcc field on create
|
2010-07-07 05:20:42 -04:00
|
|
|
//noinspection VariableNotUsedInsideIf
|
2010-07-05 10:23:26 -04:00
|
|
|
if (bcc != null) {
|
|
|
|
properties.remove("bcc");
|
|
|
|
}
|
2010-07-05 05:58:18 -04:00
|
|
|
Set<FieldUpdate> fieldUpdates = buildProperties(properties);
|
|
|
|
if (!properties.containsKey("draft")) {
|
|
|
|
// need to force draft flag to false
|
2010-07-05 09:48:43 -04:00
|
|
|
if (properties.containsKey("read")) {
|
|
|
|
fieldUpdates.add(Field.createFieldUpdate("messageFlags", "1"));
|
|
|
|
} else {
|
|
|
|
fieldUpdates.add(Field.createFieldUpdate("messageFlags", "0"));
|
|
|
|
}
|
2010-07-05 05:58:18 -04:00
|
|
|
}
|
|
|
|
item.setFieldUpdates(fieldUpdates);
|
2010-07-05 09:48:43 -04:00
|
|
|
CreateItemMethod createItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, getFolderId(folderPath), item);
|
2010-07-05 05:58:18 -04:00
|
|
|
executeMethod(createItemMethod);
|
2010-07-05 10:23:26 -04:00
|
|
|
|
|
|
|
if (bcc != null) {
|
|
|
|
ItemId itemId = new ItemId(createItemMethod.getResponseItem().get("ItemId"), createItemMethod.getResponseItem().get("ChangeKey"));
|
2010-07-05 10:35:30 -04:00
|
|
|
HashMap<String, String> localProperties = new HashMap<String, String>();
|
|
|
|
localProperties.put("bcc", bcc);
|
2010-07-05 11:03:35 -04:00
|
|
|
UpdateItemMethod updateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
|
|
|
|
ConflictResolution.AlwaysOverwrite,
|
|
|
|
CalendarItemCreateOrDeleteOperation.SendToNone,
|
|
|
|
itemId, buildProperties(localProperties));
|
2010-07-05 10:23:26 -04:00
|
|
|
executeMethod(updateItemMethod);
|
|
|
|
}
|
|
|
|
|
2010-07-05 05:58:18 -04:00
|
|
|
}
|
2010-06-09 05:52:12 -04:00
|
|
|
|
2010-07-05 05:58:18 -04:00
|
|
|
@Override
|
|
|
|
public void updateMessage(ExchangeSession.Message message, Map<String, String> properties) throws IOException {
|
2010-07-05 11:03:35 -04:00
|
|
|
UpdateItemMethod updateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
|
|
|
|
ConflictResolution.AlwaysOverwrite,
|
|
|
|
CalendarItemCreateOrDeleteOperation.SendToNone,
|
|
|
|
((EwsExchangeSession.Message) message).itemId, buildProperties(properties));
|
2010-07-05 05:58:18 -04:00
|
|
|
executeMethod(updateItemMethod);
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-07-05 05:58:18 -04:00
|
|
|
public void deleteMessage(ExchangeSession.Message message) throws IOException {
|
2010-07-05 09:48:43 -04:00
|
|
|
DeleteItemMethod deleteItemMethod = new DeleteItemMethod(((EwsExchangeSession.Message) message).itemId, DeleteType.HardDelete);
|
|
|
|
executeMethod(deleteItemMethod);
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendMessage(HashMap<String, String> properties, String messageBody) throws IOException {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-05 06:23:51 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2010-06-09 05:52:12 -04:00
|
|
|
@Override
|
2010-07-05 05:58:18 -04:00
|
|
|
protected BufferedReader getContentReader(ExchangeSession.Message message) throws IOException {
|
2010-07-05 06:23:51 -04:00
|
|
|
byte[] content = getContent(message);
|
|
|
|
return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(content)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected byte[] getContent(ExchangeSession.Message message) throws IOException {
|
2010-07-07 05:20:42 -04:00
|
|
|
return getContent(((EwsExchangeSession.Message) message).itemId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get item MIME content.
|
|
|
|
*/
|
|
|
|
protected byte[] getContent(ItemId itemId) throws IOException {
|
|
|
|
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, true);
|
2010-07-05 06:23:51 -04:00
|
|
|
executeMethod(getItemMethod);
|
|
|
|
return getItemMethod.getMimeContent();
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
2010-07-01 12:12:57 -04:00
|
|
|
protected Message buildMessage(EWSMethod.Item response) throws URIException {
|
|
|
|
Message message = new Message();
|
|
|
|
|
2010-07-05 05:58:18 -04:00
|
|
|
// get item id
|
|
|
|
message.itemId = new ItemId(response.get("ItemId"), response.get("ChangeKey"));
|
|
|
|
|
2010-07-05 12:28:10 -04:00
|
|
|
message.permanentUrl = response.get(Field.get("permanenturl").getResponseName());
|
|
|
|
|
2010-07-05 05:58:18 -04:00
|
|
|
message.size = response.getInt(Field.get("messageSize").getResponseName());
|
|
|
|
message.uid = response.get(Field.get("uid").getResponseName());
|
|
|
|
message.imapUid = response.getLong(Field.get("imapUid").getResponseName());
|
|
|
|
message.read = response.getBoolean(Field.get("read").getResponseName());
|
|
|
|
message.junk = response.getBoolean(Field.get("junk").getResponseName());
|
|
|
|
message.flagged = "2".equals(response.get(Field.get("flagStatus").getResponseName()));
|
2010-07-05 09:48:43 -04:00
|
|
|
message.draft = "9".equals(response.get(Field.get("messageFlags").getResponseName())) || "8".equals(response.get(Field.get("messageFlags").getResponseName()));
|
2010-07-05 05:58:18 -04:00
|
|
|
String lastVerbExecuted = response.get(Field.get("lastVerbExecuted").getResponseName());
|
2010-07-01 12:12:57 -04:00
|
|
|
message.answered = "102".equals(lastVerbExecuted) || "103".equals(lastVerbExecuted);
|
|
|
|
message.forwarded = "104".equals(lastVerbExecuted);
|
2010-07-05 05:58:18 -04:00
|
|
|
message.date = response.get(Field.get("date").getResponseName());
|
|
|
|
message.deleted = "1".equals(response.get(Field.get("deleted").getResponseName()));
|
2010-07-01 12:12:57 -04:00
|
|
|
|
|
|
|
if (LOGGER.isDebugEnabled()) {
|
|
|
|
StringBuilder buffer = new StringBuilder();
|
|
|
|
buffer.append("Message");
|
|
|
|
if (message.imapUid != 0) {
|
|
|
|
buffer.append(" IMAP uid: ").append(message.imapUid);
|
|
|
|
}
|
|
|
|
if (message.uid != null) {
|
|
|
|
buffer.append(" uid: ").append(message.uid);
|
|
|
|
}
|
2010-07-05 05:58:18 -04:00
|
|
|
buffer.append(" ItemId: ").append(message.itemId.id);
|
|
|
|
buffer.append(" ChangeKey: ").append(message.itemId.changeKey);
|
2010-07-01 12:12:57 -04:00
|
|
|
LOGGER.debug(buffer.toString());
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2010-06-08 18:21:53 -04:00
|
|
|
@Override
|
2010-07-01 12:12:57 -04:00
|
|
|
public MessageList searchMessages(String folderPath, Set<String> attributes, Condition condition) throws IOException {
|
|
|
|
MessageList messages = new MessageList();
|
|
|
|
List<EWSMethod.Item> responses = searchItems(folderPath, attributes, condition, FolderQueryTraversal.SHALLOW);
|
|
|
|
|
|
|
|
for (EWSMethod.Item response : responses) {
|
|
|
|
Message message = buildMessage(response);
|
|
|
|
message.messageList = messages;
|
|
|
|
messages.add(message);
|
|
|
|
}
|
|
|
|
Collections.sort(messages);
|
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<EWSMethod.Item> searchItems(String folderPath, Set<String> attributes, Condition condition, FolderQueryTraversal folderQueryTraversal) throws IOException {
|
2010-07-05 05:58:18 -04:00
|
|
|
FindItemMethod findItemMethod = new FindItemMethod(folderQueryTraversal, BaseShape.ID_ONLY, getFolderId(folderPath));
|
2010-07-01 12:12:57 -04:00
|
|
|
for (String attribute : attributes) {
|
2010-07-05 05:58:18 -04:00
|
|
|
findItemMethod.addAdditionalProperty(Field.get(attribute));
|
2010-07-01 12:12:57 -04:00
|
|
|
}
|
2010-07-01 12:37:05 -04:00
|
|
|
if (condition != null && !condition.isEmpty()) {
|
|
|
|
findItemMethod.setSearchExpression((SearchExpression) condition);
|
|
|
|
}
|
2010-07-01 12:12:57 -04:00
|
|
|
executeMethod(findItemMethod);
|
|
|
|
return findItemMethod.getResponseItems();
|
2010-06-08 18:21:53 -04:00
|
|
|
}
|
|
|
|
|
2010-06-07 18:30:23 -04:00
|
|
|
protected static class MultiCondition extends ExchangeSession.MultiCondition implements SearchExpression {
|
2010-06-07 17:17:07 -04:00
|
|
|
protected MultiCondition(Operator operator, Condition... condition) {
|
|
|
|
super(operator, condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void appendTo(StringBuilder buffer) {
|
2010-07-01 12:37:05 -04:00
|
|
|
int actualConditionCount = 0;
|
2010-06-07 17:17:07 -04:00
|
|
|
for (Condition condition : conditions) {
|
2010-07-01 12:37:05 -04:00
|
|
|
if (!condition.isEmpty()) {
|
|
|
|
actualConditionCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (actualConditionCount > 0) {
|
|
|
|
if (actualConditionCount > 1) {
|
|
|
|
buffer.append("<t:").append(operator.toString()).append('>');
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Condition condition : conditions) {
|
|
|
|
condition.appendTo(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actualConditionCount > 1) {
|
|
|
|
buffer.append("</t:").append(operator.toString()).append('>');
|
|
|
|
}
|
2010-06-07 17:17:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-07 18:30:23 -04:00
|
|
|
protected static class NotCondition extends ExchangeSession.NotCondition implements SearchExpression {
|
|
|
|
protected NotCondition(Condition condition) {
|
|
|
|
super(condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void appendTo(StringBuilder buffer) {
|
|
|
|
buffer.append("<t:Not>");
|
|
|
|
condition.appendTo(buffer);
|
|
|
|
buffer.append("</t:Not>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
|
2010-06-07 18:30:23 -04:00
|
|
|
protected static class AttributeCondition extends ExchangeSession.AttributeCondition implements SearchExpression {
|
2010-06-17 18:37:33 -04:00
|
|
|
protected ContainmentMode containmentMode;
|
|
|
|
protected ContainmentComparison containmentComparison;
|
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
protected AttributeCondition(String attributeName, Operator operator, String value) {
|
|
|
|
super(attributeName, operator, value);
|
|
|
|
}
|
|
|
|
|
2010-06-17 18:37:33 -04:00
|
|
|
protected AttributeCondition(String attributeName, Operator operator, String value,
|
|
|
|
ContainmentMode containmentMode, ContainmentComparison containmentComparison) {
|
|
|
|
super(attributeName, operator, value);
|
|
|
|
this.containmentMode = containmentMode;
|
|
|
|
this.containmentComparison = containmentComparison;
|
|
|
|
}
|
|
|
|
|
2010-06-08 18:21:53 -04:00
|
|
|
protected FieldURI getFieldURI(String attributeName) {
|
2010-07-05 05:58:18 -04:00
|
|
|
FieldURI fieldURI = Field.get(attributeName);
|
2010-06-08 18:21:53 -04:00
|
|
|
if (fieldURI == null) {
|
|
|
|
throw new IllegalArgumentException("Unknown field: " + attributeName);
|
|
|
|
}
|
|
|
|
return fieldURI;
|
|
|
|
}
|
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
public void appendTo(StringBuilder buffer) {
|
2010-06-17 18:37:33 -04:00
|
|
|
buffer.append("<t:").append(operator.toString());
|
|
|
|
if (containmentMode != null) {
|
|
|
|
containmentMode.appendTo(buffer);
|
|
|
|
}
|
|
|
|
if (containmentComparison != null) {
|
|
|
|
containmentComparison.appendTo(buffer);
|
|
|
|
}
|
|
|
|
buffer.append('>');
|
2010-06-08 18:21:53 -04:00
|
|
|
getFieldURI(attributeName).appendTo(buffer);
|
2010-06-07 17:17:07 -04:00
|
|
|
|
|
|
|
buffer.append("<t:FieldURIOrConstant><t:Constant Value=\"");
|
|
|
|
buffer.append(StringUtil.xmlEncode(value));
|
|
|
|
buffer.append("\"/></t:FieldURIOrConstant>");
|
|
|
|
|
|
|
|
buffer.append("</t:").append(operator.toString()).append('>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-08 18:21:53 -04:00
|
|
|
protected static class HeaderCondition extends AttributeCondition {
|
|
|
|
|
|
|
|
protected HeaderCondition(String attributeName, Operator operator, String value) {
|
|
|
|
super(attributeName, operator, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected FieldURI getFieldURI(String attributeName) {
|
|
|
|
return new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.InternetHeaders, attributeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-06-30 17:36:13 -04:00
|
|
|
protected static class IsNullCondition implements ExchangeSession.Condition, SearchExpression {
|
2010-06-08 18:21:53 -04:00
|
|
|
protected String attributeName;
|
|
|
|
|
2010-06-08 06:44:47 -04:00
|
|
|
protected IsNullCondition(String attributeName) {
|
2010-06-08 18:21:53 -04:00
|
|
|
this.attributeName = attributeName;
|
2010-06-08 06:44:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void appendTo(StringBuilder buffer) {
|
|
|
|
buffer.append("<t:Not><t:Exists>");
|
2010-07-05 05:58:18 -04:00
|
|
|
Field.get(attributeName).appendTo(buffer);
|
2010-06-08 06:44:47 -04:00
|
|
|
buffer.append("</t:Exists></t:Not>");
|
|
|
|
}
|
2010-07-01 10:23:31 -04:00
|
|
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-08 06:44:47 -04:00
|
|
|
}
|
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
@Override
|
2010-06-30 17:36:13 -04:00
|
|
|
public ExchangeSession.MultiCondition and(Condition... condition) {
|
2010-06-07 17:17:07 -04:00
|
|
|
return new MultiCondition(Operator.And, condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-06-30 17:36:13 -04:00
|
|
|
public ExchangeSession.MultiCondition or(Condition... condition) {
|
2010-06-07 17:17:07 -04:00
|
|
|
return new MultiCondition(Operator.Or, condition);
|
|
|
|
}
|
|
|
|
|
2010-06-07 18:30:23 -04:00
|
|
|
@Override
|
2010-06-08 06:44:47 -04:00
|
|
|
public Condition not(Condition condition) {
|
2010-06-07 18:30:23 -04:00
|
|
|
return new NotCondition(condition);
|
|
|
|
}
|
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
@Override
|
2010-06-08 06:44:47 -04:00
|
|
|
public Condition equals(String attributeName, String value) {
|
2010-06-07 17:17:07 -04:00
|
|
|
return new AttributeCondition(attributeName, Operator.IsEqualTo, value);
|
|
|
|
}
|
|
|
|
|
2010-06-16 08:06:59 -04:00
|
|
|
@Override
|
|
|
|
public Condition equals(String attributeName, int value) {
|
|
|
|
return new AttributeCondition(attributeName, Operator.IsEqualTo, String.valueOf(value));
|
|
|
|
}
|
|
|
|
|
2010-06-08 18:21:53 -04:00
|
|
|
@Override
|
|
|
|
public Condition headerEquals(String headerName, String value) {
|
|
|
|
return new HeaderCondition(headerName, Operator.IsEqualTo, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Condition gte(String attributeName, String value) {
|
|
|
|
return new AttributeCondition(attributeName, Operator.IsGreaterThanOrEqualTo, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Condition lt(String attributeName, String value) {
|
|
|
|
return new AttributeCondition(attributeName, Operator.IsLessThan, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Condition gt(String attributeName, String value) {
|
|
|
|
return new AttributeCondition(attributeName, Operator.IsGreaterThan, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-06-21 16:37:20 -04:00
|
|
|
public Condition contains(String attributeName, String value) {
|
2010-06-17 18:37:33 -04:00
|
|
|
return new AttributeCondition(attributeName, Operator.Contains, value, ContainmentMode.Substring, ContainmentComparison.IgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Condition startsWith(String attributeName, String value) {
|
|
|
|
return new AttributeCondition(attributeName, Operator.Contains, value, ContainmentMode.Prefixed, ContainmentComparison.IgnoreCase);
|
2010-06-08 18:21:53 -04:00
|
|
|
}
|
|
|
|
|
2010-06-08 06:44:47 -04:00
|
|
|
@Override
|
|
|
|
public Condition isNull(String attributeName) {
|
|
|
|
return new IsNullCondition(attributeName);
|
|
|
|
}
|
|
|
|
|
2010-06-08 18:21:53 -04:00
|
|
|
@Override
|
|
|
|
public Condition isTrue(String attributeName) {
|
|
|
|
return new AttributeCondition(attributeName, Operator.IsEqualTo, "True");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Condition isFalse(String attributeName) {
|
|
|
|
return new AttributeCondition(attributeName, Operator.IsEqualTo, "False");
|
|
|
|
}
|
|
|
|
|
2010-06-10 16:47:55 -04:00
|
|
|
protected static final HashSet<FieldURI> FOLDER_PROPERTIES = new HashSet<FieldURI>();
|
2010-06-15 10:51:49 -04:00
|
|
|
|
2010-06-10 16:47:55 -04:00
|
|
|
static {
|
|
|
|
FOLDER_PROPERTIES.add(ExtendedFieldURI.PR_URL_COMP_NAME);
|
|
|
|
FOLDER_PROPERTIES.add(ExtendedFieldURI.PR_LAST_MODIFICATION_TIME);
|
|
|
|
FOLDER_PROPERTIES.add(ExtendedFieldURI.PR_CONTAINER_CLASS);
|
|
|
|
FOLDER_PROPERTIES.add(ExtendedFieldURI.PR_LOCAL_COMMIT_TIME_MAX);
|
|
|
|
FOLDER_PROPERTIES.add(ExtendedFieldURI.PR_CONTENT_UNREAD);
|
|
|
|
FOLDER_PROPERTIES.add(ExtendedFieldURI.PR_SUBFOLDERS);
|
|
|
|
}
|
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
protected Folder buildFolder(EWSMethod.Item item) {
|
|
|
|
Folder folder = new Folder();
|
2010-06-29 10:15:50 -04:00
|
|
|
folder.folderId = new FolderId(item.get("FolderId"), item.get("ChangeKey"));
|
|
|
|
folder.displayName = item.get(ExtendedFieldURI.PR_URL_COMP_NAME.getPropertyTag());
|
2010-06-07 18:30:23 -04:00
|
|
|
folder.folderClass = item.get(ExtendedFieldURI.PR_CONTAINER_CLASS.getPropertyTag());
|
2010-06-07 17:17:07 -04:00
|
|
|
folder.etag = item.get(ExtendedFieldURI.PR_LAST_MODIFICATION_TIME.getPropertyTag());
|
2010-06-10 16:47:55 -04:00
|
|
|
folder.ctag = item.get(ExtendedFieldURI.PR_LOCAL_COMMIT_TIME_MAX.getPropertyTag());
|
|
|
|
folder.unreadCount = item.getInt(ExtendedFieldURI.PR_CONTENT_UNREAD.getPropertyTag());
|
|
|
|
folder.hasChildren = item.getBoolean(ExtendedFieldURI.PR_SUBFOLDERS.getPropertyTag());
|
2010-06-07 17:17:07 -04:00
|
|
|
// noInferiors not implemented
|
|
|
|
return folder;
|
2010-06-02 04:46:46 -04:00
|
|
|
}
|
|
|
|
|
2010-06-07 05:07:31 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
2010-06-07 17:17:07 -04:00
|
|
|
public List<ExchangeSession.Folder> getSubFolders(String folderPath, Condition condition, boolean recursive) throws IOException {
|
|
|
|
List<ExchangeSession.Folder> folders = new ArrayList<ExchangeSession.Folder>();
|
|
|
|
appendSubFolders(folders, folderPath, getFolderId(folderPath), condition, recursive);
|
|
|
|
return folders;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void appendSubFolders(List<ExchangeSession.Folder> folders,
|
|
|
|
String parentFolderPath, FolderId parentFolderId,
|
|
|
|
Condition condition, boolean recursive) throws IOException {
|
2010-06-10 16:47:55 -04:00
|
|
|
FindFolderMethod findFolderMethod = new FindFolderMethod(FolderQueryTraversal.SHALLOW,
|
2010-06-15 10:51:49 -04:00
|
|
|
BaseShape.ID_ONLY, parentFolderId, FOLDER_PROPERTIES, (SearchExpression) condition);
|
2010-06-29 10:15:50 -04:00
|
|
|
executeMethod(findFolderMethod);
|
2010-06-07 17:17:07 -04:00
|
|
|
for (EWSMethod.Item item : findFolderMethod.getResponseItems()) {
|
|
|
|
Folder folder = buildFolder(item);
|
|
|
|
if (parentFolderPath.length() > 0) {
|
|
|
|
folder.folderPath = parentFolderPath + '/' + item.get(ExtendedFieldURI.PR_URL_COMP_NAME.getPropertyTag());
|
2010-06-08 06:44:47 -04:00
|
|
|
} else if (folderIdMap.get(folder.folderId.value) != null) {
|
|
|
|
folder.folderPath = folderIdMap.get(folder.folderId.value);
|
2010-06-07 17:17:07 -04:00
|
|
|
} else {
|
|
|
|
folder.folderPath = item.get(ExtendedFieldURI.PR_URL_COMP_NAME.getPropertyTag());
|
|
|
|
}
|
|
|
|
folders.add(folder);
|
|
|
|
if (recursive && folder.hasChildren) {
|
|
|
|
appendSubFolders(folders, folder.folderPath, folder.folderId, condition, recursive);
|
|
|
|
}
|
|
|
|
}
|
2010-06-07 05:07:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
2010-06-30 17:36:13 -04:00
|
|
|
public ExchangeSession.Folder getFolder(String folderPath) throws IOException {
|
|
|
|
return internalGetFolder(folderPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get folder by path.
|
|
|
|
*
|
|
|
|
* @param folderPath folder path
|
|
|
|
* @return folder object
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
protected EwsExchangeSession.Folder internalGetFolder(String folderPath) throws IOException {
|
2010-06-10 16:47:55 -04:00
|
|
|
GetFolderMethod getFolderMethod = new GetFolderMethod(BaseShape.ID_ONLY, getFolderId(folderPath), FOLDER_PROPERTIES);
|
2010-06-29 10:15:50 -04:00
|
|
|
executeMethod(getFolderMethod);
|
2010-06-07 05:07:31 -04:00
|
|
|
EWSMethod.Item item = getFolderMethod.getResponseItem();
|
2010-06-07 17:17:07 -04:00
|
|
|
Folder folder = null;
|
2010-06-07 05:07:31 -04:00
|
|
|
if (item != null) {
|
2010-06-07 17:17:07 -04:00
|
|
|
folder = buildFolder(item);
|
|
|
|
folder.folderPath = folderPath;
|
2010-06-07 05:07:31 -04:00
|
|
|
}
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
2010-06-09 05:52:12 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
2010-06-29 10:15:50 -04:00
|
|
|
public void createFolder(String folderPath, String folderClass) throws IOException {
|
|
|
|
FolderPath path = new FolderPath(folderPath);
|
|
|
|
EWSMethod.Item folder = new EWSMethod.Item();
|
|
|
|
folder.type = "Folder";
|
|
|
|
folder.put("DisplayName", path.folderName);
|
|
|
|
folder.put("FolderClass", folderClass);
|
|
|
|
CreateFolderMethod createFolderMethod = new CreateFolderMethod(getFolderId(path.parentPath), folder);
|
|
|
|
executeMethod(createFolderMethod);
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
2010-06-29 10:15:50 -04:00
|
|
|
public void deleteFolder(String folderPath) throws IOException {
|
|
|
|
FolderId folderId = getFolderIdIfExists(folderPath);
|
|
|
|
if (folderId != null) {
|
|
|
|
DeleteFolderMethod deleteFolderMethod = new DeleteFolderMethod(folderId);
|
|
|
|
executeMethod(deleteFolderMethod);
|
|
|
|
} else {
|
2010-06-30 17:36:13 -04:00
|
|
|
LOGGER.debug("Folder " + folderPath + " not found");
|
2010-06-29 10:15:50 -04:00
|
|
|
}
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
2010-07-05 05:58:18 -04:00
|
|
|
public void copyMessage(ExchangeSession.Message message, String targetFolder) throws IOException {
|
2010-07-05 11:03:35 -04:00
|
|
|
CopyItemMethod copyItemMethod = new CopyItemMethod(((EwsExchangeSession.Message) message).itemId, getFolderId(targetFolder));
|
|
|
|
executeMethod(copyItemMethod);
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
2010-06-29 10:15:50 -04:00
|
|
|
public void moveFolder(String folderPath, String targetFolderPath) throws IOException {
|
|
|
|
FolderPath path = new FolderPath(folderPath);
|
|
|
|
FolderPath targetPath = new FolderPath(targetFolderPath);
|
|
|
|
FolderId folderId = getFolderId(folderPath);
|
|
|
|
FolderId toFolderId = getFolderId(targetPath.parentPath);
|
|
|
|
toFolderId.changeKey = null;
|
|
|
|
// move folder
|
|
|
|
if (!path.parentPath.equals(targetPath.parentPath)) {
|
|
|
|
MoveFolderMethod moveFolderMethod = new MoveFolderMethod(folderId, toFolderId);
|
|
|
|
executeMethod(moveFolderMethod);
|
|
|
|
}
|
|
|
|
// rename folder
|
|
|
|
if (!path.folderName.equals(targetPath.folderName)) {
|
|
|
|
Set<FieldUpdate> updates = new HashSet<FieldUpdate>();
|
|
|
|
updates.add(new FieldUpdate(UnindexedFieldURI.FOLDER_DISPLAYNAME, targetPath.folderName));
|
|
|
|
UpdateFolderMethod updateFolderMethod = new UpdateFolderMethod(folderId, updates);
|
|
|
|
executeMethod(updateFolderMethod);
|
|
|
|
}
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
2010-07-05 05:58:18 -04:00
|
|
|
protected void moveToTrash(ExchangeSession.Message message) throws IOException {
|
2010-07-07 05:20:42 -04:00
|
|
|
MoveItemMethod moveItemMethod = new MoveItemMethod(((EwsExchangeSession.Message) message).itemId, getFolderId(TRASH));
|
|
|
|
executeMethod(moveItemMethod);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected class Contact extends ExchangeSession.Contact {
|
|
|
|
// item id
|
|
|
|
ItemId itemId;
|
|
|
|
|
|
|
|
protected Contact(EWSMethod.Item response) throws URIException {
|
|
|
|
itemId = new ItemId(response.get("ItemId"), response.get("ChangeKey"));
|
|
|
|
|
|
|
|
permanentUrl = response.get(Field.get("permanenturl").getResponseName());
|
|
|
|
etag = response.get(Field.get("etag").getResponseName());
|
|
|
|
displayName = response.get(Field.get("displayname").getResponseName());
|
|
|
|
for (String attributeName : CONTACT_ATTRIBUTES) {
|
|
|
|
String value = response.get(Field.get("attributeName").getResponseName());
|
|
|
|
if (value != null) {
|
|
|
|
if ("bday".equals(attributeName) || "lastmodified".equals(attributeName)) {
|
|
|
|
try {
|
|
|
|
value = ExchangeSession.getZuluDateFormat().format(ExchangeSession.getExchangeZuluDateFormatMillisecond().parse(value));
|
|
|
|
} catch (ParseException e) {
|
|
|
|
LOGGER.warn("Invalid date: " + value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
put(attributeName, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public Contact(String folderPath, String itemName, Map<String, String> properties, String etag, String noneMatch) {
|
|
|
|
super(folderPath, itemName, properties, etag, noneMatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Set<FieldUpdate> buildProperties() throws IOException {
|
|
|
|
HashSet<FieldUpdate> list = new HashSet<FieldUpdate>();
|
|
|
|
for (Map.Entry<String, String> entry : entrySet()) {
|
|
|
|
list.add(Field.createFieldUpdate(entry.getKey(), entry.getValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create or update contact
|
|
|
|
*
|
|
|
|
* @return action result
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public ItemResult createOrUpdate() throws IOException {
|
|
|
|
|
|
|
|
EWSMethod.Item item = new EWSMethod.Item();
|
|
|
|
item.type = "Contact";
|
|
|
|
item.setFieldUpdates(buildProperties());
|
|
|
|
// TODO: handle etag and noneMatch
|
|
|
|
CreateItemMethod createItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, getFolderId(folderPath), item);
|
|
|
|
executeMethod(createItemMethod);
|
|
|
|
|
|
|
|
// TODO: detect create/update
|
|
|
|
int status = createItemMethod.getStatusCode();
|
|
|
|
if (status == HttpURLConnection.HTTP_OK) {
|
|
|
|
if (etag != null) {
|
|
|
|
LOGGER.debug("Updated event " + getHref());
|
|
|
|
} else {
|
|
|
|
LOGGER.warn("Overwritten event " + getHref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ItemResult itemResult = new ItemResult();
|
|
|
|
itemResult.status = status;
|
|
|
|
// TODO: get etag
|
|
|
|
// itemResult.etag = ???
|
|
|
|
|
|
|
|
return itemResult;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected class Event extends ExchangeSession.Event {
|
|
|
|
// item id
|
|
|
|
ItemId itemId;
|
|
|
|
|
|
|
|
protected Event(EWSMethod.Item response) throws URIException {
|
|
|
|
itemId = new ItemId(response.get("ItemId"), response.get("ChangeKey"));
|
|
|
|
|
|
|
|
permanentUrl = response.get(Field.get("permanenturl").getResponseName());
|
|
|
|
etag = response.get(Field.get("etag").getResponseName());
|
|
|
|
displayName = response.get(Field.get("displayname").getResponseName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public Event(String folderPath, String itemName, String contentClass, String itemBody, String etag, String noneMatch) {
|
|
|
|
super(folderPath, itemName, contentClass, itemBody, etag, noneMatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ItemResult createOrUpdate(byte[] content) throws IOException {
|
|
|
|
EWSMethod.Item item = new EWSMethod.Item();
|
|
|
|
item.type = "Message";
|
|
|
|
item.mimeContent = Base64.encodeBase64(content);
|
|
|
|
// TODO: handle etag and noneMatch
|
|
|
|
CreateItemMethod createItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, getFolderId(folderPath), item);
|
|
|
|
executeMethod(createItemMethod);
|
|
|
|
|
|
|
|
// TODO: detect create/update
|
|
|
|
int status = createItemMethod.getStatusCode();
|
|
|
|
if (status == HttpURLConnection.HTTP_OK) {
|
|
|
|
if (etag != null) {
|
|
|
|
LOGGER.debug("Updated event " + getHref());
|
|
|
|
} else {
|
|
|
|
LOGGER.warn("Overwritten event " + getHref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ItemResult itemResult = new ItemResult();
|
|
|
|
itemResult.status = status;
|
|
|
|
// TODO: get etag
|
|
|
|
// itemResult.etag = ???
|
|
|
|
|
|
|
|
return itemResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getBody() throws HttpException {
|
|
|
|
String result;
|
|
|
|
LOGGER.debug("Get event: " + permanentUrl);
|
|
|
|
try {
|
|
|
|
byte[] content = getContent(itemId);
|
|
|
|
result = getICS(new ByteArrayInputStream(content));
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw buildHttpException(e);
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
throw buildHttpException(e);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
2010-06-15 10:51:49 -04:00
|
|
|
@Override
|
2010-07-07 05:20:42 -04:00
|
|
|
public List<ExchangeSession.Contact> searchContacts(String folderPath, Set<String> attributes, Condition condition) throws IOException {
|
|
|
|
List<ExchangeSession.Contact> contacts = new ArrayList<ExchangeSession.Contact>();
|
|
|
|
List<EWSMethod.Item> responses = searchItems(folderPath, attributes,
|
|
|
|
and(equals("outlookmessageclass", "IPM.Contact"), condition),
|
|
|
|
FolderQueryTraversal.SHALLOW);
|
|
|
|
|
|
|
|
for (EWSMethod.Item response : responses) {
|
|
|
|
contacts.add(new Contact(response));
|
|
|
|
}
|
|
|
|
return contacts;
|
2010-06-15 10:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-07-07 05:20:42 -04:00
|
|
|
protected List<ExchangeSession.Event> searchEvents(String folderPath, Set<String> attributes, Condition condition) throws IOException {
|
|
|
|
List<ExchangeSession.Event> events = new ArrayList<ExchangeSession.Event>();
|
|
|
|
List<EWSMethod.Item> responses = searchItems(folderPath, attributes,
|
|
|
|
condition,
|
|
|
|
FolderQueryTraversal.SHALLOW);
|
|
|
|
for (EWSMethod.Item response : responses) {
|
|
|
|
events.add(new Event(response));
|
|
|
|
}
|
|
|
|
|
|
|
|
return events;
|
2010-06-15 10:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Item getItem(String folderPath, String itemName) throws IOException {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2010-07-07 05:20:42 -04:00
|
|
|
@Override
|
|
|
|
public ContactPhoto getContactPhoto(ExchangeSession.Contact contact) throws IOException {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2010-06-16 09:30:36 -04:00
|
|
|
@Override
|
|
|
|
public int deleteItem(String folderPath, String itemName) throws IOException {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int sendEvent(String icsBody) throws IOException {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:51:49 -04:00
|
|
|
@Override
|
2010-07-06 05:46:55 -04:00
|
|
|
protected ItemResult internalCreateOrUpdateContact(String folderPath, String itemName, Map<String, String> properties, String etag, String noneMatch) throws IOException {
|
2010-07-07 05:20:42 -04:00
|
|
|
return new Contact(folderPath, itemName, properties, etag, noneMatch).createOrUpdate();
|
2010-06-15 10:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-07-06 05:46:55 -04:00
|
|
|
protected ItemResult internalCreateOrUpdateEvent(String folderPath, String itemName, String contentClass, String icsBody, String etag, String noneMatch) throws IOException {
|
2010-07-07 05:20:42 -04:00
|
|
|
return new Event(folderPath, itemName, contentClass, icsBody, etag, noneMatch).createOrUpdate();
|
2010-06-15 10:51:49 -04:00
|
|
|
}
|
|
|
|
|
2010-06-19 17:35:09 -04:00
|
|
|
@Override
|
2010-06-21 09:52:09 -04:00
|
|
|
public boolean isSharedFolder(String folderPath) {
|
|
|
|
throw new UnsupportedOperationException();
|
2010-06-19 17:35:09 -04:00
|
|
|
}
|
|
|
|
|
2010-06-16 17:43:11 -04:00
|
|
|
@Override
|
|
|
|
protected void loadVtimezone() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2010-06-07 05:07:31 -04:00
|
|
|
|
|
|
|
private FolderId getFolderId(String folderPath) throws IOException {
|
2010-06-29 10:15:50 -04:00
|
|
|
FolderId folderId = getFolderIdIfExists(folderPath);
|
|
|
|
if (folderId == null) {
|
|
|
|
throw new DavMailException("EXCEPTION_FOLDER_NOT_FOUND", folderPath);
|
|
|
|
}
|
|
|
|
return folderId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private FolderId getFolderIdIfExists(String folderPath) throws IOException {
|
2010-06-07 18:30:23 -04:00
|
|
|
String[] folderNames;
|
|
|
|
FolderId currentFolderId;
|
2010-06-08 06:44:47 -04:00
|
|
|
if (folderPath.startsWith(PUBLIC_ROOT)) {
|
|
|
|
currentFolderId = DistinguishedFolderId.PUBLICFOLDERSROOT;
|
2010-06-07 18:30:23 -04:00
|
|
|
folderNames = folderPath.substring(PUBLIC_ROOT.length()).split("/");
|
2010-06-08 06:44:47 -04:00
|
|
|
} else if (folderPath.startsWith(INBOX)) {
|
|
|
|
currentFolderId = DistinguishedFolderId.INBOX;
|
|
|
|
folderNames = folderPath.substring(INBOX.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(CALENDAR)) {
|
|
|
|
currentFolderId = DistinguishedFolderId.CALENDAR;
|
|
|
|
folderNames = folderPath.substring(CALENDAR.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(CONTACTS)) {
|
|
|
|
currentFolderId = DistinguishedFolderId.CONTACTS;
|
|
|
|
folderNames = folderPath.substring(CONTACTS.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(SENT)) {
|
|
|
|
currentFolderId = DistinguishedFolderId.SENTITEMS;
|
|
|
|
folderNames = folderPath.substring(SENT.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(DRAFTS)) {
|
|
|
|
currentFolderId = DistinguishedFolderId.DRAFTS;
|
|
|
|
folderNames = folderPath.substring(DRAFTS.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(TRASH)) {
|
|
|
|
currentFolderId = DistinguishedFolderId.DELETEDITEMS;
|
|
|
|
folderNames = folderPath.substring(TRASH.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(JUNK)) {
|
|
|
|
currentFolderId = DistinguishedFolderId.JUNKEMAIL;
|
|
|
|
folderNames = folderPath.substring(JUNK.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(UNSENT)) {
|
|
|
|
currentFolderId = DistinguishedFolderId.OUTBOX;
|
|
|
|
folderNames = folderPath.substring(UNSENT.length()).split("/");
|
2010-06-07 18:30:23 -04:00
|
|
|
} else {
|
2010-06-08 06:44:47 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.MSGFOLDERROOT;
|
|
|
|
folderNames = folderPath.split("/");
|
2010-06-07 18:30:23 -04:00
|
|
|
}
|
2010-06-07 05:07:31 -04:00
|
|
|
for (String folderName : folderNames) {
|
2010-06-08 06:44:47 -04:00
|
|
|
if (folderName.length() > 0) {
|
2010-06-07 05:07:31 -04:00
|
|
|
currentFolderId = getSubFolderByName(currentFolderId, folderName);
|
2010-06-29 10:15:50 -04:00
|
|
|
if (currentFolderId == null) {
|
|
|
|
break;
|
|
|
|
}
|
2010-06-07 05:07:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return currentFolderId;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected FolderId getSubFolderByName(FolderId parentFolderId, String folderName) throws IOException {
|
2010-06-29 10:15:50 -04:00
|
|
|
FolderId folderId = null;
|
2010-06-07 05:07:31 -04:00
|
|
|
FindFolderMethod findFolderMethod = new FindFolderMethod(
|
|
|
|
FolderQueryTraversal.SHALLOW,
|
|
|
|
BaseShape.ID_ONLY,
|
|
|
|
parentFolderId,
|
2010-06-10 16:47:55 -04:00
|
|
|
FOLDER_PROPERTIES,
|
2010-06-07 05:07:31 -04:00
|
|
|
new TwoOperandExpression(TwoOperandExpression.Operator.IsEqualTo,
|
|
|
|
ExtendedFieldURI.PR_URL_COMP_NAME, folderName)
|
|
|
|
);
|
2010-06-29 10:15:50 -04:00
|
|
|
executeMethod(findFolderMethod);
|
|
|
|
EWSMethod.Item item = findFolderMethod.getResponseItem();
|
|
|
|
if (item != null) {
|
|
|
|
folderId = new FolderId(item.get("FolderId"), item.get("ChangeKey"));
|
|
|
|
}
|
|
|
|
return folderId;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int executeMethod(EWSMethod ewsMethod) throws IOException {
|
|
|
|
int status;
|
2010-06-07 17:17:07 -04:00
|
|
|
try {
|
2010-06-29 10:15:50 -04:00
|
|
|
status = httpClient.executeMethod(ewsMethod);
|
|
|
|
ewsMethod.checkSuccess();
|
2010-06-07 17:17:07 -04:00
|
|
|
} finally {
|
2010-06-29 10:15:50 -04:00
|
|
|
ewsMethod.releaseConnection();
|
2010-06-08 06:44:47 -04:00
|
|
|
}
|
2010-06-29 10:15:50 -04:00
|
|
|
return status;
|
2010-06-07 05:07:31 -04:00
|
|
|
}
|
|
|
|
|
2010-06-02 04:46:46 -04:00
|
|
|
}
|
2010-07-07 05:20:42 -04:00
|
|
|
|