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;
|
2010-07-21 11:39:51 -04:00
|
|
|
import davmail.exception.HttpNotFoundException;
|
2010-06-02 04:46:46 -04:00
|
|
|
import davmail.exchange.ExchangeSession;
|
2010-08-17 09:39:14 -04:00
|
|
|
import davmail.exchange.VCalendar;
|
2010-09-21 10:02:44 -04:00
|
|
|
import davmail.exchange.VProperty;
|
2010-06-07 17:17:07 -04:00
|
|
|
import davmail.http.DavGatewayHttpClientFacade;
|
2010-07-19 18:01:11 -04:00
|
|
|
import davmail.util.IOUtil;
|
2010-06-07 17:17:07 -04:00
|
|
|
import davmail.util.StringUtil;
|
2010-07-05 05:58:18 -04:00
|
|
|
import org.apache.commons.codec.binary.Base64;
|
2010-08-26 10:12:57 -04:00
|
|
|
import org.apache.commons.httpclient.*;
|
|
|
|
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
|
2010-07-27 06:26:19 -04:00
|
|
|
import org.apache.commons.httpclient.methods.GetMethod;
|
2010-08-26 10:12:57 -04:00
|
|
|
import org.apache.commons.httpclient.methods.PostMethod;
|
2010-06-02 04:46:46 -04:00
|
|
|
|
2010-08-19 19:20:31 -04:00
|
|
|
import javax.mail.MessagingException;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
2010-08-17 09:39:14 -04:00
|
|
|
import java.io.BufferedReader;
|
2010-08-19 19:20:31 -04:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2010-07-19 18:01:11 -04:00
|
|
|
import java.io.IOException;
|
2010-08-17 09:39:14 -04:00
|
|
|
import java.io.InputStreamReader;
|
2010-07-07 05:20:42 -04:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.text.ParseException;
|
2010-07-27 09:19:28 -04:00
|
|
|
import java.text.SimpleDateFormat;
|
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-12-08 16:39:55 -05:00
|
|
|
protected static final int PAGE_SIZE = 100;
|
|
|
|
|
2010-12-17 08:06:11 -05:00
|
|
|
protected static final Set<String> MESSAGE_TYPES = new HashSet<String>();
|
2010-12-08 16:39:55 -05:00
|
|
|
|
2010-12-07 17:07:01 -05:00
|
|
|
static {
|
|
|
|
MESSAGE_TYPES.add("Message");
|
|
|
|
MESSAGE_TYPES.add("CalendarItem");
|
|
|
|
|
|
|
|
MESSAGE_TYPES.add("MeetingMessage");
|
|
|
|
MESSAGE_TYPES.add("MeetingRequest");
|
|
|
|
MESSAGE_TYPES.add("MeetingResponse");
|
|
|
|
MESSAGE_TYPES.add("MeetingCancellation");
|
|
|
|
|
|
|
|
// exclude types from IMAP
|
|
|
|
//MESSAGE_TYPES.add("Item");
|
|
|
|
//MESSAGE_TYPES.add("PostItem");
|
|
|
|
//MESSAGE_TYPES.add("Contact");
|
|
|
|
//MESSAGE_TYPES.add("DistributionList");
|
|
|
|
//MESSAGE_TYPES.add("Task");
|
|
|
|
}
|
|
|
|
|
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-07-19 05:22:42 -04:00
|
|
|
protected final String parentPath;
|
|
|
|
protected final String folderName;
|
2010-06-29 10:15:50 -04:00
|
|
|
|
|
|
|
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-10-30 15:13:46 -04:00
|
|
|
@Override
|
|
|
|
protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password) throws IOException {
|
|
|
|
LOGGER.debug("Form based authentication detected");
|
|
|
|
|
|
|
|
HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod);
|
|
|
|
if (logonMethod == null) {
|
|
|
|
LOGGER.debug("Authentication form not found at " + initmethod.getURI() + ", will try direct EWS access");
|
|
|
|
} else {
|
|
|
|
logonMethod = postLogonMethod(httpClient, logonMethod, userName, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
return logonMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-01 10:51:38 -04:00
|
|
|
/**
|
|
|
|
* Check endpoint url.
|
|
|
|
*
|
|
|
|
* @param endPointUrl endpoint url
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
protected void checkEndPointUrl(String endPointUrl) throws IOException {
|
|
|
|
HttpMethod getMethod = new GetMethod(endPointUrl);
|
|
|
|
getMethod.setFollowRedirects(false);
|
|
|
|
try {
|
|
|
|
int status = DavGatewayHttpClientFacade.executeNoRedirect(httpClient, getMethod);
|
2010-10-30 15:13:46 -04:00
|
|
|
if (status == HttpStatus.SC_UNAUTHORIZED) {
|
|
|
|
throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED");
|
|
|
|
} else if (status != HttpStatus.SC_MOVED_TEMPORARILY) {
|
2010-09-01 10:51:38 -04:00
|
|
|
throw DavGatewayHttpClientFacade.buildHttpException(getMethod);
|
|
|
|
}
|
|
|
|
// check Location
|
|
|
|
Header locationHeader = getMethod.getResponseHeader("Location");
|
|
|
|
if (locationHeader == null || !"/ews/services.wsdl".equalsIgnoreCase(locationHeader.getValue())) {
|
|
|
|
throw new IOException("Ews endpoint not available at " + getMethod.getURI().toString());
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.debug(e.getMessage());
|
|
|
|
throw e;
|
|
|
|
} finally {
|
|
|
|
getMethod.releaseConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-02 04:46:46 -04:00
|
|
|
@Override
|
|
|
|
protected void buildSessionInfo(HttpMethod method) throws DavMailException {
|
2010-10-27 17:52:33 -04:00
|
|
|
// no need to check logon method body
|
2010-10-30 15:13:46 -04:00
|
|
|
if (method != null) {
|
|
|
|
method.releaseConnection();
|
|
|
|
// need to retrieve email and alias
|
|
|
|
getEmailAndAliasFromOptions();
|
|
|
|
} else {
|
|
|
|
// OWA authentication failed, get email address from login
|
|
|
|
if (userName.indexOf('@') >= 0) {
|
|
|
|
// userName is email address
|
|
|
|
email = userName;
|
|
|
|
alias = userName.substring(0, userName.indexOf('@'));
|
|
|
|
}
|
|
|
|
}
|
2010-10-27 17:52:33 -04:00
|
|
|
|
2010-08-26 10:12:57 -04:00
|
|
|
if (email == null || alias == null) {
|
|
|
|
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
|
|
|
|
}
|
|
|
|
currentMailboxPath = "/users/" + email.toLowerCase();
|
|
|
|
|
2010-06-02 04:46:46 -04:00
|
|
|
// nothing to do, mailPath not used in EWS mode
|
2010-06-07 17:17:07 -04:00
|
|
|
// check EWS access
|
|
|
|
try {
|
2010-09-01 10:51:38 -04:00
|
|
|
checkEndPointUrl("/ews/exchange.asmx");
|
2010-11-10 17:16:59 -05:00
|
|
|
// workaround for Exchange bug: send fake request
|
|
|
|
internalGetFolder("");
|
2010-06-07 17:17:07 -04:00
|
|
|
} catch (IOException e) {
|
2010-08-26 10:12:57 -04:00
|
|
|
try {
|
|
|
|
// failover, try to retrieve EWS url from autodiscover
|
2010-09-01 10:51:38 -04:00
|
|
|
checkEndPointUrl(getEwsUrlFromAutoDiscover());
|
2010-11-10 17:16:59 -05:00
|
|
|
// workaround for Exchange bug: send fake request
|
|
|
|
internalGetFolder("");
|
2010-08-26 10:12:57 -04:00
|
|
|
} catch (IOException e2) {
|
2010-11-09 17:36:10 -05:00
|
|
|
// autodiscover failed and initial exception was authentication failure => throw original exception
|
|
|
|
if (e instanceof DavMailAuthenticationException) {
|
2010-12-07 17:07:01 -05:00
|
|
|
throw (DavMailAuthenticationException) e;
|
2010-11-09 17:36:10 -05:00
|
|
|
}
|
2010-08-26 10:12:57 -04:00
|
|
|
LOGGER.error(e2.getMessage());
|
|
|
|
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
|
|
|
|
}
|
2010-06-07 17:17:07 -04:00
|
|
|
}
|
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-07-08 19:26:41 -04:00
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
}
|
|
|
|
|
2010-08-26 10:12:57 -04:00
|
|
|
protected static class AutoDiscoverMethod extends PostMethod {
|
2010-09-02 16:19:47 -04:00
|
|
|
AutoDiscoverMethod(String autodiscoverHost, String userEmail) {
|
2010-09-06 18:55:29 -04:00
|
|
|
super("https://" + autodiscoverHost + "/autodiscover/autodiscover.xml");
|
2010-09-02 16:19:47 -04:00
|
|
|
setAutoDiscoverRequestEntity(userEmail);
|
|
|
|
}
|
|
|
|
|
2010-08-26 10:12:57 -04:00
|
|
|
AutoDiscoverMethod(String userEmail) {
|
|
|
|
super("/autodiscover/autodiscover.xml");
|
2010-09-02 16:19:47 -04:00
|
|
|
setAutoDiscoverRequestEntity(userEmail);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setAutoDiscoverRequestEntity(String userEmail) {
|
2010-08-26 10:12:57 -04:00
|
|
|
String body = "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\">" +
|
|
|
|
"<Request>" +
|
|
|
|
"<EMailAddress>" + userEmail + "</EMailAddress>" +
|
|
|
|
"<AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>" +
|
|
|
|
"</Request>" +
|
|
|
|
"</Autodiscover>";
|
|
|
|
setRequestEntity(new ByteArrayRequestEntity(body.getBytes(), "text/xml"));
|
|
|
|
}
|
|
|
|
|
|
|
|
String ewsUrl;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void processResponseBody(HttpState httpState, HttpConnection httpConnection) {
|
|
|
|
Header contentTypeHeader = getResponseHeader("Content-Type");
|
2010-09-01 10:51:38 -04:00
|
|
|
if (contentTypeHeader != null &&
|
|
|
|
("text/xml; charset=utf-8".equals(contentTypeHeader.getValue())
|
|
|
|
|| "text/html; charset=utf-8".equals(contentTypeHeader.getValue())
|
|
|
|
)) {
|
2010-08-26 10:12:57 -04:00
|
|
|
BufferedReader autodiscoverReader = null;
|
|
|
|
try {
|
|
|
|
autodiscoverReader = new BufferedReader(new InputStreamReader(getResponseBodyAsStream()));
|
|
|
|
String line;
|
|
|
|
// find ews url
|
|
|
|
while ((line = autodiscoverReader.readLine()) != null
|
|
|
|
&& (line.indexOf("<EwsUrl>") == -1)
|
|
|
|
&& (line.indexOf("</EwsUrl>") == -1)) {
|
|
|
|
}
|
|
|
|
if (line != null) {
|
|
|
|
ewsUrl = line.substring(line.indexOf("<EwsUrl>") + 8, line.indexOf("</EwsUrl>"));
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.debug(e);
|
|
|
|
} finally {
|
|
|
|
if (autodiscoverReader != null) {
|
|
|
|
try {
|
|
|
|
autodiscoverReader.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.debug(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getEwsUrlFromAutoDiscover() throws DavMailAuthenticationException {
|
|
|
|
String ewsUrl;
|
2010-09-02 16:19:47 -04:00
|
|
|
try {
|
|
|
|
ewsUrl = getEwsUrlFromAutoDiscover(null);
|
|
|
|
} catch (IOException e) {
|
|
|
|
try {
|
|
|
|
ewsUrl = getEwsUrlFromAutoDiscover("autodiscover." + email.substring(email.indexOf('@') + 1));
|
|
|
|
} catch (IOException e2) {
|
|
|
|
LOGGER.error(e2.getMessage());
|
|
|
|
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ewsUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getEwsUrlFromAutoDiscover(String autodiscoverHostname) throws IOException {
|
|
|
|
String ewsUrl;
|
|
|
|
AutoDiscoverMethod autoDiscoverMethod;
|
|
|
|
if (autodiscoverHostname != null) {
|
|
|
|
autoDiscoverMethod = new AutoDiscoverMethod(autodiscoverHostname, email);
|
|
|
|
} else {
|
|
|
|
autoDiscoverMethod = new AutoDiscoverMethod(email);
|
|
|
|
}
|
2010-08-26 10:12:57 -04:00
|
|
|
try {
|
|
|
|
int status = DavGatewayHttpClientFacade.executeNoRedirect(httpClient, autoDiscoverMethod);
|
|
|
|
if (status != HttpStatus.SC_OK) {
|
|
|
|
throw DavGatewayHttpClientFacade.buildHttpException(autoDiscoverMethod);
|
|
|
|
}
|
|
|
|
ewsUrl = autoDiscoverMethod.ewsUrl;
|
2010-09-02 16:19:47 -04:00
|
|
|
|
|
|
|
// update host name
|
|
|
|
DavGatewayHttpClientFacade.setClientHost(httpClient, ewsUrl);
|
|
|
|
|
2010-08-26 10:12:57 -04:00
|
|
|
if (ewsUrl == null) {
|
|
|
|
throw new IOException("Ews url not found");
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
autoDiscoverMethod.releaseConnection();
|
|
|
|
}
|
|
|
|
return ewsUrl;
|
|
|
|
}
|
|
|
|
|
2010-07-05 05:58:18 -04:00
|
|
|
class Message extends ExchangeSession.Message {
|
|
|
|
// message item id
|
|
|
|
ItemId itemId;
|
2010-08-20 05:12:29 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getPermanentId() {
|
|
|
|
return itemId.id;
|
|
|
|
}
|
2010-07-05 05:58:18 -04:00
|
|
|
}
|
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
|
|
|
|
*/
|
2010-09-15 18:49:19 -04:00
|
|
|
protected List<FieldUpdate> buildProperties(Map<String, String> properties) {
|
|
|
|
ArrayList<FieldUpdate> list = new ArrayList<FieldUpdate>();
|
2010-07-05 05:58:18 -04:00
|
|
|
for (Map.Entry<String, String> entry : properties.entrySet()) {
|
|
|
|
if ("read".equals(entry.getKey())) {
|
2010-07-09 08:52:45 -04:00
|
|
|
list.add(Field.createFieldUpdate("read", Boolean.toString("1".equals(entry.getValue()))));
|
2010-07-05 05:58:18 -04:00
|
|
|
} 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 ("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-08-19 19:20:31 -04:00
|
|
|
public void createMessage(String folderPath, String messageName, HashMap<String, String> properties, MimeMessage mimeMessage) throws IOException {
|
2010-07-05 05:58:18 -04:00
|
|
|
EWSMethod.Item item = new EWSMethod.Item();
|
|
|
|
item.type = "Message";
|
2010-08-26 10:12:57 -04:00
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
2010-08-19 19:20:31 -04:00
|
|
|
try {
|
|
|
|
mimeMessage.writeTo(baos);
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
throw new IOException(e.getMessage());
|
|
|
|
}
|
|
|
|
baos.close();
|
|
|
|
item.mimeContent = Base64.encodeBase64(baos.toByteArray());
|
2010-07-12 02:42:09 -04:00
|
|
|
|
2010-09-15 18:49:19 -04:00
|
|
|
List<FieldUpdate> fieldUpdates = buildProperties(properties);
|
2010-07-05 05:58:18 -04:00
|
|
|
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
|
|
|
}
|
2010-07-25 16:36:52 -04:00
|
|
|
fieldUpdates.add(Field.createFieldUpdate("urlcompname", messageName));
|
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-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,
|
2010-07-10 18:42:48 -04:00
|
|
|
SendMeetingInvitationsOrCancellations.SendToNone,
|
2010-07-05 11:03:35 -04:00
|
|
|
((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-23 04:58:39 -04:00
|
|
|
LOGGER.debug("Delete " + message.permanentUrl);
|
2010-08-17 11:42:20 -04:00
|
|
|
DeleteItemMethod deleteItemMethod = new DeleteItemMethod(((EwsExchangeSession.Message) message).itemId, DeleteType.HardDelete, SendMeetingCancellations.SendToNone);
|
2010-07-05 09:48:43 -04:00
|
|
|
executeMethod(deleteItemMethod);
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-07-12 12:35:46 -04:00
|
|
|
public void sendMessage(byte[] messageBody) throws IOException {
|
2010-07-11 18:50:20 -04:00
|
|
|
EWSMethod.Item item = new EWSMethod.Item();
|
|
|
|
item.type = "Message";
|
2010-07-12 12:35:46 -04:00
|
|
|
item.mimeContent = Base64.encodeBase64(messageBody);
|
2010-07-12 02:21:31 -04:00
|
|
|
|
|
|
|
CreateItemMethod createItemMethod = new CreateItemMethod(MessageDisposition.SendAndSaveCopy, getFolderId(SENT), item);
|
2010-07-11 18:50:20 -04:00
|
|
|
executeMethod(createItemMethod);
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
2010-08-19 19:20:31 -04:00
|
|
|
@Override
|
2010-08-20 05:12:29 -04:00
|
|
|
public void sendMessage(MimeMessage mimeMessage) throws IOException {
|
2010-08-19 19:20:31 -04:00
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
2010-08-20 05:12:29 -04:00
|
|
|
try {
|
|
|
|
mimeMessage.writeTo(baos);
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
throw new IOException(e.getMessage());
|
|
|
|
}
|
2010-08-19 19:20:31 -04:00
|
|
|
sendMessage(baos.toByteArray());
|
|
|
|
}
|
|
|
|
|
2010-07-05 06:23:51 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected byte[] getContent(ExchangeSession.Message message) throws IOException {
|
2010-07-07 05:20:42 -04:00
|
|
|
return getContent(((EwsExchangeSession.Message) message).itemId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-31 18:52:45 -04:00
|
|
|
* Get item content.
|
2010-07-09 17:25:18 -04:00
|
|
|
*
|
2010-07-09 08:52:45 -04:00
|
|
|
* @param itemId EWS item id
|
|
|
|
* @return item content as byte array
|
|
|
|
* @throws IOException on error
|
2010-07-07 05:20:42 -04:00
|
|
|
*/
|
|
|
|
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);
|
2010-09-16 17:33:20 -04:00
|
|
|
return getItemMethod.getMimeContent();
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
2010-07-22 07:54:18 -04:00
|
|
|
protected Message buildMessage(EWSMethod.Item response) throws DavMailException {
|
2010-07-01 12:12:57 -04:00
|
|
|
Message message = new Message();
|
|
|
|
|
2010-07-05 05:58:18 -04:00
|
|
|
// get item id
|
2010-07-07 08:55:13 -04:00
|
|
|
message.itemId = new ItemId(response);
|
2010-07-05 05:58:18 -04:00
|
|
|
|
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-27 06:54:00 -04:00
|
|
|
message.draft = (response.getInt(Field.get("messageFlags").getResponseName()) & 8) != 0;
|
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-20 12:33:58 -04:00
|
|
|
message.date = convertDateFromExchange(response.get(Field.get("date").getResponseName()));
|
2010-07-05 05:58:18 -04:00
|
|
|
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();
|
2010-07-27 16:34:43 -04:00
|
|
|
List<EWSMethod.Item> responses = searchItems(folderPath, attributes, condition, FolderQueryTraversal.SHALLOW, 0);
|
2010-07-01 12:12:57 -04:00
|
|
|
|
|
|
|
for (EWSMethod.Item response : responses) {
|
2010-12-07 17:07:01 -05:00
|
|
|
if (MESSAGE_TYPES.contains(response.type)) {
|
|
|
|
Message message = buildMessage(response);
|
|
|
|
message.messageList = messages;
|
|
|
|
messages.add(message);
|
|
|
|
}
|
2010-07-01 12:12:57 -04:00
|
|
|
}
|
|
|
|
Collections.sort(messages);
|
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
|
2010-07-27 16:34:43 -04:00
|
|
|
protected List<EWSMethod.Item> searchItems(String folderPath, Set<String> attributes, Condition condition, FolderQueryTraversal folderQueryTraversal, int maxCount) throws IOException {
|
2010-12-08 16:39:55 -05:00
|
|
|
int offset = 0;
|
|
|
|
List<EWSMethod.Item> results = new ArrayList<EWSMethod.Item>();
|
|
|
|
FindItemMethod findItemMethod;
|
|
|
|
do {
|
|
|
|
int fetchCount = PAGE_SIZE;
|
|
|
|
if (maxCount > 0) {
|
|
|
|
fetchCount = Math.min(PAGE_SIZE, maxCount - offset);
|
|
|
|
}
|
|
|
|
findItemMethod = new FindItemMethod(folderQueryTraversal, BaseShape.ID_ONLY, getFolderId(folderPath), offset, fetchCount);
|
|
|
|
for (String attribute : attributes) {
|
|
|
|
findItemMethod.addAdditionalProperty(Field.get(attribute));
|
|
|
|
}
|
|
|
|
if (condition != null && !condition.isEmpty()) {
|
|
|
|
findItemMethod.setSearchExpression((SearchExpression) condition);
|
|
|
|
}
|
|
|
|
executeMethod(findItemMethod);
|
|
|
|
results.addAll(findItemMethod.getResponseItems());
|
|
|
|
offset = results.size();
|
|
|
|
} while (!(findItemMethod.includesLastItemInRange || (maxCount > 0 && offset == maxCount)));
|
|
|
|
return results;
|
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-08-07 14:59:49 -04:00
|
|
|
protected FieldURI getFieldURI() {
|
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-08-07 14:59:49 -04:00
|
|
|
protected Operator getOperator() {
|
|
|
|
return operator;
|
|
|
|
}
|
|
|
|
|
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-08-07 14:59:49 -04:00
|
|
|
FieldURI fieldURI = getFieldURI();
|
2010-07-25 10:36:27 -04:00
|
|
|
fieldURI.appendTo(buffer);
|
2010-06-07 17:17:07 -04:00
|
|
|
|
2010-08-17 08:20:20 -04:00
|
|
|
if (operator != Operator.Contains) {
|
2010-08-09 16:15:28 -04:00
|
|
|
buffer.append("<t:FieldURIOrConstant>");
|
|
|
|
}
|
|
|
|
buffer.append("<t:Constant Value=\"");
|
2010-07-25 10:36:27 -04:00
|
|
|
// encode urlcompname
|
|
|
|
if (fieldURI instanceof ExtendedFieldURI && "0x10f3".equals(((ExtendedFieldURI) fieldURI).propertyTag)) {
|
|
|
|
buffer.append(StringUtil.xmlEncode(StringUtil.encodeUrlcompname(value)));
|
2010-09-06 18:55:29 -04:00
|
|
|
} else if (fieldURI instanceof ExtendedFieldURI
|
|
|
|
&& ((ExtendedFieldURI) fieldURI).propertyType == ExtendedFieldURI.PropertyType.Integer) {
|
|
|
|
// check value
|
|
|
|
try {
|
|
|
|
Integer.parseInt(value);
|
|
|
|
buffer.append(value);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
// invalid value, replace with 0
|
|
|
|
buffer.append('0');
|
|
|
|
}
|
2010-07-25 10:36:27 -04:00
|
|
|
} else {
|
|
|
|
buffer.append(StringUtil.xmlEncode(value));
|
|
|
|
}
|
2010-08-09 16:15:28 -04:00
|
|
|
buffer.append("\"/>");
|
2010-08-17 08:20:20 -04:00
|
|
|
if (operator != Operator.Contains) {
|
2010-08-09 16:15:28 -04:00
|
|
|
buffer.append("</t:FieldURIOrConstant>");
|
|
|
|
}
|
2010-06-07 17:17:07 -04:00
|
|
|
|
|
|
|
buffer.append("</t:").append(operator.toString()).append('>');
|
|
|
|
}
|
2010-08-07 14:59:49 -04:00
|
|
|
|
2010-08-07 17:32:37 -04:00
|
|
|
public boolean isMatch(ExchangeSession.Contact contact) {
|
|
|
|
String lowerCaseValue = value.toLowerCase();
|
|
|
|
|
|
|
|
String actualValue = contact.get(attributeName);
|
|
|
|
if (actualValue == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
actualValue = actualValue.toLowerCase();
|
|
|
|
if (operator == Operator.IsEqualTo) {
|
2010-08-09 16:15:28 -04:00
|
|
|
return lowerCaseValue.equals(actualValue);
|
2010-08-07 17:32:37 -04:00
|
|
|
} else {
|
|
|
|
return operator == Operator.Contains && ((containmentMode.equals(ContainmentMode.Substring) && actualValue.contains(lowerCaseValue)) ||
|
|
|
|
(containmentMode.equals(ContainmentMode.Prefixed) && actualValue.startsWith(lowerCaseValue)));
|
|
|
|
}
|
2010-08-07 14:59:49 -04:00
|
|
|
}
|
2010-08-07 17:32:37 -04:00
|
|
|
|
2010-06-07 17:17:07 -04:00
|
|
|
}
|
|
|
|
|
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
|
2010-08-07 14:59:49 -04:00
|
|
|
protected FieldURI getFieldURI() {
|
2010-06-08 18:21:53 -04:00
|
|
|
return new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.InternetHeaders, attributeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-06-30 17:36:13 -04:00
|
|
|
protected static class IsNullCondition implements ExchangeSession.Condition, SearchExpression {
|
2010-07-19 05:22:42 -04:00
|
|
|
protected final String attributeName;
|
2010-06-08 18:21:53 -04:00
|
|
|
|
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-08-07 17:32:37 -04:00
|
|
|
public boolean isMatch(ExchangeSession.Contact contact) {
|
|
|
|
String actualValue = contact.get(attributeName);
|
|
|
|
return actualValue == null;
|
|
|
|
}
|
|
|
|
|
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-07-27 09:19:28 -04:00
|
|
|
public Condition isEqualTo(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
|
2010-07-27 09:19:28 -04:00
|
|
|
public Condition isEqualTo(String attributeName, int value) {
|
2010-06-16 08:06:59 -04:00
|
|
|
return new AttributeCondition(attributeName, Operator.IsEqualTo, String.valueOf(value));
|
|
|
|
}
|
|
|
|
|
2010-06-08 18:21:53 -04:00
|
|
|
@Override
|
2010-07-27 09:19:28 -04:00
|
|
|
public Condition headerIsEqualTo(String headerName, String value) {
|
2010-06-08 18:21:53 -04:00
|
|
|
return new HeaderCondition(headerName, Operator.IsEqualTo, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Condition gte(String attributeName, String value) {
|
|
|
|
return new AttributeCondition(attributeName, Operator.IsGreaterThanOrEqualTo, value);
|
|
|
|
}
|
|
|
|
|
2010-07-22 19:10:24 -04:00
|
|
|
@Override
|
|
|
|
public Condition lte(String attributeName, String value) {
|
2010-07-25 15:55:51 -04:00
|
|
|
return new AttributeCondition(attributeName, Operator.IsLessThanOrEqualTo, value);
|
2010-07-22 19:10:24 -04:00
|
|
|
}
|
|
|
|
|
2010-06-08 18:21:53 -04:00
|
|
|
@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) {
|
2010-07-09 19:53:28 -04:00
|
|
|
return new AttributeCondition(attributeName, Operator.IsEqualTo, "true");
|
2010-06-08 18:21:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Condition isFalse(String attributeName) {
|
2010-07-09 19:53:28 -04:00
|
|
|
return new AttributeCondition(attributeName, Operator.IsEqualTo, "false");
|
2010-06-08 18:21:53 -04:00
|
|
|
}
|
|
|
|
|
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 {
|
2010-07-20 05:10:39 -04:00
|
|
|
FOLDER_PROPERTIES.add(Field.get("urlcompname"));
|
2010-07-27 06:39:53 -04:00
|
|
|
FOLDER_PROPERTIES.add(Field.get("folderDisplayName"));
|
2010-07-20 05:10:39 -04:00
|
|
|
FOLDER_PROPERTIES.add(Field.get("lastmodified"));
|
|
|
|
FOLDER_PROPERTIES.add(Field.get("folderclass"));
|
|
|
|
FOLDER_PROPERTIES.add(Field.get("ctag"));
|
|
|
|
FOLDER_PROPERTIES.add(Field.get("unread"));
|
|
|
|
FOLDER_PROPERTIES.add(Field.get("hassubs"));
|
2010-07-23 04:58:39 -04:00
|
|
|
FOLDER_PROPERTIES.add(Field.get("uidNext"));
|
|
|
|
FOLDER_PROPERTIES.add(Field.get("highestUid"));
|
2010-06-10 16:47:55 -04:00
|
|
|
}
|
|
|
|
|
2010-07-28 13:11:18 -04:00
|
|
|
protected Folder buildFolder(String mailbox, EWSMethod.Item item) {
|
2010-06-07 17:17:07 -04:00
|
|
|
Folder folder = new Folder();
|
2010-07-28 13:11:18 -04:00
|
|
|
folder.folderId = new FolderId(mailbox, item);
|
2010-07-27 06:39:53 -04:00
|
|
|
folder.displayName = item.get(Field.get("folderDisplayName").getResponseName());
|
2010-07-20 05:10:39 -04:00
|
|
|
folder.folderClass = item.get(Field.get("folderclass").getResponseName());
|
|
|
|
folder.etag = item.get(Field.get("lastmodified").getResponseName());
|
|
|
|
folder.ctag = item.get(Field.get("ctag").getResponseName());
|
|
|
|
folder.unreadCount = item.getInt(Field.get("unread").getResponseName());
|
|
|
|
folder.hasChildren = item.getBoolean(Field.get("hassubs").getResponseName());
|
2010-06-07 17:17:07 -04:00
|
|
|
// noInferiors not implemented
|
2010-07-23 04:58:39 -04:00
|
|
|
folder.uidNext = item.getInt(Field.get("uidNext").getResponseName());
|
2010-06-07 17:17:07 -04:00
|
|
|
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 {
|
2010-07-09 19:53:28 -04:00
|
|
|
String baseFolderPath = folderPath;
|
|
|
|
if (baseFolderPath.startsWith("/users/")) {
|
|
|
|
int index = baseFolderPath.indexOf('/', "/users/".length());
|
|
|
|
if (index >= 0) {
|
2010-07-12 02:21:31 -04:00
|
|
|
baseFolderPath = baseFolderPath.substring(index + 1);
|
2010-07-09 19:53:28 -04:00
|
|
|
}
|
|
|
|
}
|
2010-06-07 17:17:07 -04:00
|
|
|
List<ExchangeSession.Folder> folders = new ArrayList<ExchangeSession.Folder>();
|
2010-07-09 19:53:28 -04:00
|
|
|
appendSubFolders(folders, baseFolderPath, getFolderId(folderPath), condition, recursive);
|
2010-06-07 17:17:07 -04:00
|
|
|
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()) {
|
2010-07-28 13:11:18 -04:00
|
|
|
Folder folder = buildFolder(parentFolderId.mailbox, item);
|
2010-06-07 17:17:07 -04:00
|
|
|
if (parentFolderPath.length() > 0) {
|
2010-07-29 11:42:07 -04:00
|
|
|
folder.folderPath = parentFolderPath + '/' + item.get(Field.get("folderDisplayName").getResponseName());
|
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 {
|
2010-07-29 11:42:07 -04:00
|
|
|
folder.folderPath = item.get(Field.get("folderDisplayName").getResponseName());
|
2010-06-07 17:17:07 -04:00
|
|
|
}
|
|
|
|
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-07-28 13:11:18 -04:00
|
|
|
FolderId folderId = getFolderId(folderPath);
|
|
|
|
GetFolderMethod getFolderMethod = new GetFolderMethod(BaseShape.ID_ONLY, folderId, 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-08-17 09:39:14 -04:00
|
|
|
Folder folder;
|
2010-06-07 05:07:31 -04:00
|
|
|
if (item != null) {
|
2010-07-28 13:11:18 -04:00
|
|
|
folder = buildFolder(folderId.mailbox, item);
|
2010-06-07 17:17:07 -04:00
|
|
|
folder.folderPath = folderPath;
|
2010-08-16 18:07:59 -04:00
|
|
|
} else {
|
2010-08-18 06:33:25 -04:00
|
|
|
throw new HttpNotFoundException("Folder " + folderPath + " not found");
|
2010-06-07 05:07:31 -04:00
|
|
|
}
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
2010-06-09 05:52:12 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
@Override
|
2010-07-25 10:36:27 -04:00
|
|
|
public int createFolder(String folderPath, String folderClass, Map<String, String> properties) throws IOException {
|
2010-06-29 10:15:50 -04:00
|
|
|
FolderPath path = new FolderPath(folderPath);
|
|
|
|
EWSMethod.Item folder = new EWSMethod.Item();
|
|
|
|
folder.type = "Folder";
|
|
|
|
folder.put("FolderClass", folderClass);
|
2010-08-18 06:33:25 -04:00
|
|
|
folder.put("DisplayName", path.folderName);
|
2010-07-24 18:23:14 -04:00
|
|
|
// TODO: handle properties
|
2010-06-29 10:15:50 -04:00
|
|
|
CreateFolderMethod createFolderMethod = new CreateFolderMethod(getFolderId(path.parentPath), folder);
|
|
|
|
executeMethod(createFolderMethod);
|
2010-07-24 18:23:14 -04:00
|
|
|
return HttpStatus.SC_CREATED;
|
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)) {
|
2010-09-15 18:49:19 -04:00
|
|
|
ArrayList<FieldUpdate> updates = new ArrayList<FieldUpdate>();
|
2010-07-20 05:10:39 -04:00
|
|
|
updates.add(new FieldUpdate(Field.get("folderDisplayName"), targetPath.folderName));
|
2010-06-29 10:15:50 -04:00
|
|
|
UpdateFolderMethod updateFolderMethod = new UpdateFolderMethod(folderId, updates);
|
|
|
|
executeMethod(updateFolderMethod);
|
|
|
|
}
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
2010-09-07 17:09:53 -04:00
|
|
|
@Override
|
|
|
|
public void moveItem(String sourcePath, String targetPath) throws IOException {
|
|
|
|
FolderPath sourceFolderPath = new FolderPath(sourcePath);
|
|
|
|
Item item = getItem(sourceFolderPath.parentPath, sourceFolderPath.folderName);
|
|
|
|
FolderPath targetFolderPath = new FolderPath(targetPath);
|
|
|
|
FolderId toFolderId = getFolderId(targetFolderPath.parentPath);
|
2010-09-17 06:13:00 -04:00
|
|
|
MoveItemMethod moveItemMethod = new MoveItemMethod(((Event) item).itemId, toFolderId);
|
2010-09-07 17:09:53 -04:00
|
|
|
executeMethod(moveItemMethod);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2010-07-19 05:22:42 -04:00
|
|
|
protected Contact(EWSMethod.Item response) throws DavMailException {
|
2010-07-07 08:55:13 -04:00
|
|
|
itemId = new ItemId(response);
|
2010-07-07 05:20:42 -04:00
|
|
|
|
|
|
|
permanentUrl = response.get(Field.get("permanenturl").getResponseName());
|
|
|
|
etag = response.get(Field.get("etag").getResponseName());
|
|
|
|
displayName = response.get(Field.get("displayname").getResponseName());
|
2010-07-09 17:25:18 -04:00
|
|
|
itemName = response.get(Field.get("urlcompname").getResponseName());
|
2010-07-27 13:08:39 -04:00
|
|
|
// workaround for missing urlcompname in Exchange 2010
|
|
|
|
if (itemName == null) {
|
|
|
|
itemName = StringUtil.base64ToUrl(itemId.id) + ".EML";
|
|
|
|
}
|
2010-07-07 05:20:42 -04:00
|
|
|
for (String attributeName : CONTACT_ATTRIBUTES) {
|
2010-07-07 08:29:41 -04:00
|
|
|
String value = response.get(Field.get(attributeName).getResponseName());
|
2010-08-09 16:15:28 -04:00
|
|
|
if (value != null && value.length() > 0) {
|
2010-07-20 12:33:58 -04:00
|
|
|
if ("bday".equals(attributeName) || "anniversary".equals(attributeName) || "lastmodified".equals(attributeName) || "datereceived".equals(attributeName)) {
|
|
|
|
value = convertDateFromExchange(value);
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
|
|
|
put(attributeName, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2010-07-09 17:25:18 -04:00
|
|
|
protected Contact(String folderPath, String itemName, Map<String, String> properties, String etag, String noneMatch) {
|
2010-07-07 05:20:42 -04:00
|
|
|
super(folderPath, itemName, properties, etag, noneMatch);
|
|
|
|
}
|
|
|
|
|
2010-08-07 14:59:49 -04:00
|
|
|
/**
|
|
|
|
* Empty constructor for GalFind
|
|
|
|
*/
|
2010-08-07 17:32:37 -04:00
|
|
|
protected Contact() {
|
2010-08-07 14:59:49 -04:00
|
|
|
}
|
|
|
|
|
2010-09-21 09:12:19 -04:00
|
|
|
protected void buildProperties(List<FieldUpdate> updates) {
|
2010-07-07 05:20:42 -04:00
|
|
|
for (Map.Entry<String, String> entry : entrySet()) {
|
2010-07-17 06:45:21 -04:00
|
|
|
if ("photo".equals(entry.getKey())) {
|
2010-09-21 09:12:19 -04:00
|
|
|
updates.add(Field.createFieldUpdate("haspicture", "true"));
|
|
|
|
} else if (!entry.getKey().startsWith("email") && !entry.getKey().startsWith("smtpemail")) {
|
|
|
|
updates.add(Field.createFieldUpdate(entry.getKey(), entry.getValue()));
|
2010-07-17 06:45:21 -04:00
|
|
|
}
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
2010-09-21 09:12:19 -04:00
|
|
|
// handle email addresses
|
|
|
|
IndexedFieldUpdate emailFieldUpdate = null;
|
|
|
|
for (Map.Entry<String, String> entry : entrySet()) {
|
2010-09-24 18:44:15 -04:00
|
|
|
if (entry.getKey().startsWith("smtpemail") && entry.getValue() != null) {
|
2010-09-21 09:12:19 -04:00
|
|
|
if (emailFieldUpdate == null) {
|
|
|
|
emailFieldUpdate = new IndexedFieldUpdate("EmailAddresses");
|
|
|
|
}
|
|
|
|
emailFieldUpdate.addFieldValue(Field.createFieldUpdate(entry.getKey(), entry.getValue()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (emailFieldUpdate != null) {
|
|
|
|
updates.add(emailFieldUpdate);
|
|
|
|
}
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create or update contact
|
|
|
|
*
|
|
|
|
* @return action result
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public ItemResult createOrUpdate() throws IOException {
|
2010-07-17 06:45:21 -04:00
|
|
|
String photo = get("photo");
|
|
|
|
|
2010-07-09 08:52:45 -04:00
|
|
|
ItemResult itemResult = new ItemResult();
|
|
|
|
EWSMethod createOrUpdateItemMethod;
|
|
|
|
|
|
|
|
// first try to load existing event
|
|
|
|
String currentEtag = null;
|
|
|
|
ItemId currentItemId = null;
|
2010-07-18 19:36:25 -04:00
|
|
|
FileAttachment currentFileAttachment = null;
|
2010-07-27 13:08:39 -04:00
|
|
|
EWSMethod.Item currentItem = getEwsItem(folderPath, itemName);
|
|
|
|
if (currentItem != null) {
|
|
|
|
currentItemId = new ItemId(currentItem);
|
|
|
|
currentEtag = currentItem.get(Field.get("etag").getResponseName());
|
2010-07-18 19:36:25 -04:00
|
|
|
|
|
|
|
// load current picture
|
|
|
|
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, currentItemId, false);
|
|
|
|
getItemMethod.addAdditionalProperty(Field.get("attachments"));
|
|
|
|
executeMethod(getItemMethod);
|
|
|
|
EWSMethod.Item item = getItemMethod.getResponseItem();
|
|
|
|
if (item != null) {
|
|
|
|
currentFileAttachment = item.getAttachmentByName("ContactPicture.jpg");
|
|
|
|
}
|
2010-07-09 08:52:45 -04:00
|
|
|
}
|
|
|
|
if ("*".equals(noneMatch)) {
|
|
|
|
// create requested
|
|
|
|
if (currentItemId != null) {
|
|
|
|
itemResult.status = HttpStatus.SC_PRECONDITION_FAILED;
|
|
|
|
return itemResult;
|
|
|
|
}
|
|
|
|
} else if (etag != null) {
|
|
|
|
// update requested
|
|
|
|
if (currentItemId == null || !etag.equals(currentEtag)) {
|
|
|
|
itemResult.status = HttpStatus.SC_PRECONDITION_FAILED;
|
|
|
|
return itemResult;
|
|
|
|
}
|
|
|
|
}
|
2010-07-07 05:20:42 -04:00
|
|
|
|
2010-09-21 09:12:19 -04:00
|
|
|
List<FieldUpdate> properties = new ArrayList<FieldUpdate>();
|
2010-07-09 08:52:45 -04:00
|
|
|
if (currentItemId != null) {
|
2010-09-21 09:12:19 -04:00
|
|
|
buildProperties(properties);
|
2010-07-09 08:52:45 -04:00
|
|
|
// update
|
|
|
|
createOrUpdateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
|
|
|
|
ConflictResolution.AlwaysOverwrite,
|
2010-07-10 18:42:48 -04:00
|
|
|
SendMeetingInvitationsOrCancellations.SendToNone,
|
2010-07-27 15:28:27 -04:00
|
|
|
currentItemId, properties);
|
2010-07-09 08:52:45 -04:00
|
|
|
} else {
|
|
|
|
// create
|
|
|
|
EWSMethod.Item newItem = new EWSMethod.Item();
|
|
|
|
newItem.type = "Contact";
|
2010-07-27 15:28:27 -04:00
|
|
|
// force urlcompname on create
|
|
|
|
properties.add(Field.createFieldUpdate("urlcompname", convertItemNameToEML(itemName)));
|
2010-09-21 09:12:19 -04:00
|
|
|
buildProperties(properties);
|
2010-07-27 15:28:27 -04:00
|
|
|
newItem.setFieldUpdates(properties);
|
2010-07-09 08:52:45 -04:00
|
|
|
createOrUpdateItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, getFolderId(folderPath), newItem);
|
|
|
|
}
|
|
|
|
executeMethod(createOrUpdateItemMethod);
|
2010-07-07 05:20:42 -04:00
|
|
|
|
2010-07-09 08:52:45 -04:00
|
|
|
itemResult.status = createOrUpdateItemMethod.getStatusCode();
|
|
|
|
if (itemResult.status == HttpURLConnection.HTTP_OK) {
|
2010-07-09 17:25:18 -04:00
|
|
|
//noinspection VariableNotUsedInsideIf
|
2010-07-10 18:42:48 -04:00
|
|
|
if (etag == null) {
|
2010-07-09 08:52:45 -04:00
|
|
|
itemResult.status = HttpStatus.SC_CREATED;
|
2010-07-20 09:25:15 -04:00
|
|
|
LOGGER.debug("Created contact " + getHref());
|
2010-07-07 05:20:42 -04:00
|
|
|
} else {
|
2010-07-20 09:25:15 -04:00
|
|
|
LOGGER.debug("Updated contact " + getHref());
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
2010-07-21 12:42:46 -04:00
|
|
|
} else {
|
|
|
|
return itemResult;
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
2010-07-09 08:52:45 -04:00
|
|
|
|
|
|
|
ItemId newItemId = new ItemId(createOrUpdateItemMethod.getResponseItem());
|
2010-07-17 06:45:21 -04:00
|
|
|
|
2010-07-28 17:10:58 -04:00
|
|
|
// disable contact picture handling on Exchange 2007
|
2010-07-29 11:42:07 -04:00
|
|
|
if ("Exchange2010".equals(serverVersion)) {
|
2010-07-28 17:10:58 -04:00
|
|
|
// first delete current picture
|
|
|
|
if (currentFileAttachment != null) {
|
|
|
|
DeleteAttachmentMethod deleteAttachmentMethod = new DeleteAttachmentMethod(currentFileAttachment.attachmentId);
|
|
|
|
executeMethod(deleteAttachmentMethod);
|
|
|
|
}
|
2010-07-18 19:36:25 -04:00
|
|
|
|
2010-07-28 17:10:58 -04:00
|
|
|
if (photo != null) {
|
|
|
|
// convert image to jpeg
|
|
|
|
byte[] resizedImageBytes = IOUtil.resizeImage(Base64.decodeBase64(photo.getBytes()), 90);
|
2010-07-17 17:34:14 -04:00
|
|
|
|
2010-07-28 17:10:58 -04:00
|
|
|
FileAttachment attachment = new FileAttachment("ContactPicture.jpg", "image/jpeg", new String(Base64.encodeBase64(resizedImageBytes)));
|
|
|
|
if ("Exchange2010".equals(serverVersion)) {
|
|
|
|
attachment.setIsContactPhoto(true);
|
|
|
|
}
|
|
|
|
// update photo attachment
|
|
|
|
CreateAttachmentMethod createAttachmentMethod = new CreateAttachmentMethod(newItemId, attachment);
|
|
|
|
executeMethod(createAttachmentMethod);
|
2010-07-27 08:58:23 -04:00
|
|
|
}
|
2010-07-17 06:45:21 -04:00
|
|
|
}
|
|
|
|
|
2010-07-09 08:52:45 -04:00
|
|
|
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, newItemId, false);
|
2010-07-10 18:42:48 -04:00
|
|
|
getItemMethod.addAdditionalProperty(Field.get("etag"));
|
2010-07-09 08:52:45 -04:00
|
|
|
executeMethod(getItemMethod);
|
|
|
|
itemResult.etag = getItemMethod.getResponseItem().get(Field.get("etag").getResponseName());
|
2010-07-07 05:20:42 -04:00
|
|
|
|
|
|
|
return itemResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected class Event extends ExchangeSession.Event {
|
|
|
|
// item id
|
|
|
|
ItemId itemId;
|
2010-10-27 17:52:33 -04:00
|
|
|
String type;
|
2010-07-07 05:20:42 -04:00
|
|
|
|
2010-07-22 07:54:18 -04:00
|
|
|
protected Event(EWSMethod.Item response) {
|
2010-07-07 08:55:13 -04:00
|
|
|
itemId = new ItemId(response);
|
2010-07-07 05:20:42 -04:00
|
|
|
|
2010-10-27 17:52:33 -04:00
|
|
|
type = response.type;
|
|
|
|
|
2010-07-07 05:20:42 -04:00
|
|
|
permanentUrl = response.get(Field.get("permanenturl").getResponseName());
|
|
|
|
etag = response.get(Field.get("etag").getResponseName());
|
|
|
|
displayName = response.get(Field.get("displayname").getResponseName());
|
2010-08-24 16:27:41 -04:00
|
|
|
subject = response.get(Field.get("subject").getResponseName());
|
2010-07-09 19:53:28 -04:00
|
|
|
itemName = response.get(Field.get("urlcompname").getResponseName());
|
2010-07-27 13:08:39 -04:00
|
|
|
// workaround for missing urlcompname in Exchange 2010
|
|
|
|
if (itemName == null) {
|
|
|
|
itemName = StringUtil.base64ToUrl(itemId.id) + ".EML";
|
|
|
|
}
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2010-07-31 18:52:45 -04:00
|
|
|
protected Event(String folderPath, String itemName, String contentClass, String itemBody, String etag, String noneMatch) throws IOException {
|
2010-07-07 05:20:42 -04:00
|
|
|
super(folderPath, itemName, contentClass, itemBody, etag, noneMatch);
|
|
|
|
}
|
2010-07-07 08:55:13 -04:00
|
|
|
|
2010-07-10 18:42:48 -04:00
|
|
|
@Override
|
|
|
|
public ItemResult createOrUpdate() throws IOException {
|
2010-09-15 18:49:19 -04:00
|
|
|
byte[] itemContent = Base64.encodeBase64(vCalendar.toString().getBytes("UTF-8"));
|
2010-07-07 05:20:42 -04:00
|
|
|
|
2010-07-09 19:53:28 -04:00
|
|
|
ItemResult itemResult = new ItemResult();
|
|
|
|
EWSMethod createOrUpdateItemMethod;
|
|
|
|
|
|
|
|
// first try to load existing event
|
|
|
|
String currentEtag = null;
|
|
|
|
ItemId currentItemId = null;
|
2010-07-27 13:12:44 -04:00
|
|
|
EWSMethod.Item currentItem = getEwsItem(folderPath, itemName);
|
|
|
|
if (currentItem != null) {
|
|
|
|
currentItemId = new ItemId(currentItem);
|
|
|
|
currentEtag = currentItem.get(Field.get("etag").getResponseName());
|
2010-08-18 06:33:25 -04:00
|
|
|
LOGGER.debug("Existing item found with etag: " + currentEtag + " id: " + currentItemId.id);
|
2010-07-09 19:53:28 -04:00
|
|
|
}
|
|
|
|
if ("*".equals(noneMatch)) {
|
|
|
|
// create requested
|
|
|
|
if (currentItemId != null) {
|
|
|
|
itemResult.status = HttpStatus.SC_PRECONDITION_FAILED;
|
|
|
|
return itemResult;
|
|
|
|
}
|
|
|
|
} else if (etag != null) {
|
|
|
|
// update requested
|
|
|
|
if (currentItemId == null || !etag.equals(currentEtag)) {
|
|
|
|
itemResult.status = HttpStatus.SC_PRECONDITION_FAILED;
|
|
|
|
return itemResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentItemId != null) {
|
2010-08-17 12:45:20 -04:00
|
|
|
/*Set<FieldUpdate> updates = new HashSet<FieldUpdate>();
|
2010-09-17 06:13:00 -04:00
|
|
|
// TODO: update properties instead of brute force delete/add
|
2010-08-17 12:45:20 -04:00
|
|
|
updates.add(new FieldUpdate(Field.get("mimeContent"), new String(Base64.encodeBase64(itemContent))));
|
2010-07-09 19:53:28 -04:00
|
|
|
// update
|
|
|
|
createOrUpdateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
|
2010-08-17 12:45:20 -04:00
|
|
|
ConflictResolution.AutoResolve,
|
2010-07-10 18:42:48 -04:00
|
|
|
SendMeetingInvitationsOrCancellations.SendToNone,
|
2010-08-17 12:45:20 -04:00
|
|
|
currentItemId, updates);*/
|
|
|
|
// hard method: delete/create on update
|
|
|
|
DeleteItemMethod deleteItemMethod = new DeleteItemMethod(currentItemId, DeleteType.HardDelete, SendMeetingCancellations.SendToNone);
|
|
|
|
executeMethod(deleteItemMethod);
|
|
|
|
} //else {
|
2010-08-18 06:33:25 -04:00
|
|
|
// create
|
|
|
|
EWSMethod.Item newItem = new EWSMethod.Item();
|
|
|
|
newItem.type = "CalendarItem";
|
|
|
|
newItem.mimeContent = itemContent;
|
2010-09-15 18:49:19 -04:00
|
|
|
ArrayList<FieldUpdate> updates = new ArrayList<FieldUpdate>();
|
|
|
|
if (!vCalendar.hasVAlarm()) {
|
|
|
|
updates.add(Field.createFieldUpdate("reminderset", "false"));
|
|
|
|
}
|
2010-09-16 05:27:06 -04:00
|
|
|
//updates.add(Field.createFieldUpdate("outlookmessageclass", "IPM.Appointment"));
|
2010-08-18 06:33:25 -04:00
|
|
|
// force urlcompname
|
|
|
|
updates.add(Field.createFieldUpdate("urlcompname", convertItemNameToEML(itemName)));
|
2010-09-17 06:13:00 -04:00
|
|
|
if (vCalendar.isMeeting() && vCalendar.isMeetingOrganizer()) {
|
2010-09-16 05:27:06 -04:00
|
|
|
updates.add(Field.createFieldUpdate("apptstateflags", "1"));
|
|
|
|
} else {
|
|
|
|
updates.add(Field.createFieldUpdate("apptstateflags", "0"));
|
|
|
|
}
|
2010-09-23 03:31:18 -04:00
|
|
|
// handle mozilla alarm
|
|
|
|
String xMozLastack = vCalendar.getFirstVeventPropertyValue("X-MOZ-LASTACK");
|
|
|
|
if (xMozLastack != null) {
|
2010-10-27 17:52:33 -04:00
|
|
|
updates.add(Field.createFieldUpdate("xmozlastack", xMozLastack));
|
2010-09-23 03:31:18 -04:00
|
|
|
}
|
|
|
|
String xMozSnoozeTime = vCalendar.getFirstVeventPropertyValue("X-MOZ-SNOOZE-TIME");
|
|
|
|
if (xMozSnoozeTime != null) {
|
2010-10-27 17:52:33 -04:00
|
|
|
updates.add(Field.createFieldUpdate("xmozsnoozetime", xMozSnoozeTime));
|
2010-09-23 03:31:18 -04:00
|
|
|
}
|
2010-09-21 09:12:19 -04:00
|
|
|
|
|
|
|
if (vCalendar.isMeeting()) {
|
|
|
|
VCalendar.Recipients recipients = vCalendar.getRecipients(false);
|
|
|
|
if (recipients.attendees != null) {
|
|
|
|
updates.add(Field.createFieldUpdate("to", recipients.attendees));
|
|
|
|
}
|
|
|
|
if (recipients.optionalAttendees != null) {
|
|
|
|
updates.add(Field.createFieldUpdate("cc", recipients.optionalAttendees));
|
|
|
|
}
|
|
|
|
if (recipients.organizer != null && !vCalendar.isMeetingOrganizer()) {
|
|
|
|
updates.add(Field.createFieldUpdate("from", recipients.optionalAttendees));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-17 06:13:00 -04:00
|
|
|
// patch allday date values
|
2010-09-17 06:44:56 -04:00
|
|
|
if (vCalendar.isCdoAllDay()) {
|
|
|
|
updates.add(Field.createFieldUpdate("dtstart", convertCalendarDateToExchange(vCalendar.getFirstVeventPropertyValue("DTSTART"))));
|
|
|
|
updates.add(Field.createFieldUpdate("dtend", convertCalendarDateToExchange(vCalendar.getFirstVeventPropertyValue("DTEND"))));
|
|
|
|
}
|
2010-09-21 09:12:19 -04:00
|
|
|
updates.add(Field.createFieldUpdate("busystatus", "BUSY".equals(vCalendar.getFirstVeventPropertyValue("X-MICROSOFT-CDO-BUSYSTATUS")) ? "Busy" : "Free"));
|
2010-09-17 06:13:00 -04:00
|
|
|
if (vCalendar.isCdoAllDay()) {
|
|
|
|
if ("Exchange2010".equals(serverVersion)) {
|
|
|
|
updates.add(Field.createFieldUpdate("starttimezone", vCalendar.getVTimezone().getPropertyValue("TZID")));
|
|
|
|
} else {
|
|
|
|
updates.add(Field.createFieldUpdate("meetingtimezone", vCalendar.getVTimezone().getPropertyValue("TZID")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-18 06:33:25 -04:00
|
|
|
newItem.setFieldUpdates(updates);
|
|
|
|
createOrUpdateItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, SendMeetingInvitations.SendToNone, getFolderId(folderPath), newItem);
|
2010-08-17 12:45:20 -04:00
|
|
|
//}
|
2010-07-09 19:53:28 -04:00
|
|
|
|
|
|
|
executeMethod(createOrUpdateItemMethod);
|
|
|
|
|
|
|
|
itemResult.status = createOrUpdateItemMethod.getStatusCode();
|
|
|
|
if (itemResult.status == HttpURLConnection.HTTP_OK) {
|
|
|
|
//noinspection VariableNotUsedInsideIf
|
2010-08-17 12:45:20 -04:00
|
|
|
if (currentItemId == null) {
|
2010-07-09 19:53:28 -04:00
|
|
|
itemResult.status = HttpStatus.SC_CREATED;
|
2010-07-07 05:20:42 -04:00
|
|
|
LOGGER.debug("Updated event " + getHref());
|
|
|
|
} else {
|
|
|
|
LOGGER.warn("Overwritten event " + getHref());
|
|
|
|
}
|
|
|
|
}
|
2010-07-09 19:53:28 -04:00
|
|
|
|
|
|
|
ItemId newItemId = new ItemId(createOrUpdateItemMethod.getResponseItem());
|
|
|
|
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, newItemId, false);
|
2010-07-10 18:42:48 -04:00
|
|
|
getItemMethod.addAdditionalProperty(Field.get("etag"));
|
2010-07-09 19:53:28 -04:00
|
|
|
executeMethod(getItemMethod);
|
|
|
|
itemResult.etag = getItemMethod.getResponseItem().get(Field.get("etag").getResponseName());
|
2010-07-07 05:20:42 -04:00
|
|
|
|
|
|
|
return itemResult;
|
2010-07-09 19:53:28 -04:00
|
|
|
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-07-31 18:52:45 -04:00
|
|
|
public byte[] getEventContent() throws IOException {
|
|
|
|
byte[] content;
|
|
|
|
if (LOGGER.isDebugEnabled()) {
|
2010-09-16 17:33:20 -04:00
|
|
|
LOGGER.debug("Get event: " + itemName);
|
2010-07-31 18:52:45 -04:00
|
|
|
}
|
2010-07-07 05:20:42 -04:00
|
|
|
try {
|
2010-09-16 17:33:20 -04:00
|
|
|
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, true);
|
2010-10-27 17:52:33 -04:00
|
|
|
if (!"Message".equals(type)) {
|
|
|
|
getItemMethod.addAdditionalProperty(Field.get("reminderset"));
|
|
|
|
getItemMethod.addAdditionalProperty(Field.get("calendaruid"));
|
|
|
|
getItemMethod.addAdditionalProperty(Field.get("requiredattendees"));
|
|
|
|
getItemMethod.addAdditionalProperty(Field.get("optionalattendees"));
|
|
|
|
getItemMethod.addAdditionalProperty(Field.get("xmozlastack"));
|
|
|
|
getItemMethod.addAdditionalProperty(Field.get("xmozsnoozetime"));
|
|
|
|
}
|
2010-09-21 10:02:44 -04:00
|
|
|
|
2010-09-16 17:33:20 -04:00
|
|
|
executeMethod(getItemMethod);
|
|
|
|
content = getItemMethod.getMimeContent();
|
2010-09-17 06:13:00 -04:00
|
|
|
VCalendar localVCalendar = new VCalendar(content, email, getVTimezone());
|
2010-09-16 17:33:20 -04:00
|
|
|
// remove additional reminder
|
|
|
|
if (!"true".equals(getItemMethod.getResponseItem().get(Field.get("reminderset").getResponseName()))) {
|
2010-09-17 06:13:00 -04:00
|
|
|
localVCalendar.removeVAlarm();
|
2010-09-16 17:33:20 -04:00
|
|
|
}
|
|
|
|
String calendaruid = getItemMethod.getResponseItem().get(Field.get("calendaruid").getResponseName());
|
2010-09-17 06:13:00 -04:00
|
|
|
if (calendaruid != null) {
|
|
|
|
localVCalendar.setFirstVeventPropertyValue("UID", calendaruid);
|
2010-09-16 17:33:20 -04:00
|
|
|
}
|
2010-09-21 10:02:44 -04:00
|
|
|
List<EWSMethod.Attendee> attendees = getItemMethod.getResponseItem().getAttendees();
|
|
|
|
if (attendees != null) {
|
|
|
|
for (EWSMethod.Attendee attendee : attendees) {
|
2010-10-27 17:52:33 -04:00
|
|
|
VProperty attendeeProperty = new VProperty("ATTENDEE", "mailto:" + attendee.email);
|
2010-09-21 10:02:44 -04:00
|
|
|
attendeeProperty.addParam("CN", attendee.name);
|
|
|
|
attendeeProperty.addParam("PARTSTAT", attendee.partstat);
|
|
|
|
attendeeProperty.addParam("ROLE", attendee.role);
|
|
|
|
localVCalendar.addFirstVeventProperty(attendeeProperty);
|
|
|
|
}
|
|
|
|
}
|
2010-09-23 03:31:18 -04:00
|
|
|
// restore mozilla alarm status
|
|
|
|
localVCalendar.setFirstVeventPropertyValue("X-MOZ-LASTACK",
|
|
|
|
getItemMethod.getResponseItem().get(Field.get("xmozlastack").getResponseName()));
|
|
|
|
localVCalendar.setFirstVeventPropertyValue("X-MOZ-SNOOZE-TIME",
|
|
|
|
getItemMethod.getResponseItem().get(Field.get("xmozsnoozetime").getResponseName()));
|
2010-09-17 06:13:00 -04:00
|
|
|
content = localVCalendar.toString().getBytes("UTF-8");
|
2010-09-16 17:33:20 -04:00
|
|
|
|
2010-07-07 05:20:42 -04:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw buildHttpException(e);
|
|
|
|
}
|
2010-07-31 18:52:45 -04:00
|
|
|
return content;
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
2010-06-09 05:52:12 -04:00
|
|
|
}
|
|
|
|
|
2010-06-15 10:51:49 -04:00
|
|
|
@Override
|
2010-07-27 16:34:43 -04:00
|
|
|
public List<ExchangeSession.Contact> searchContacts(String folderPath, Set<String> attributes, Condition condition, int maxCount) throws IOException {
|
2010-07-07 05:20:42 -04:00
|
|
|
List<ExchangeSession.Contact> contacts = new ArrayList<ExchangeSession.Contact>();
|
2010-07-09 17:25:18 -04:00
|
|
|
List<EWSMethod.Item> responses = searchItems(folderPath, attributes, condition,
|
2010-07-27 16:34:43 -04:00
|
|
|
FolderQueryTraversal.SHALLOW, maxCount);
|
2010-07-07 05:20:42 -04:00
|
|
|
|
|
|
|
for (EWSMethod.Item response : responses) {
|
|
|
|
contacts.add(new Contact(response));
|
|
|
|
}
|
|
|
|
return contacts;
|
2010-06-15 10:51:49 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 19:43:56 -04:00
|
|
|
@Override
|
|
|
|
public List<ExchangeSession.Event> getEventMessages(String folderPath) throws IOException {
|
|
|
|
return searchEvents(folderPath, ITEM_PROPERTIES,
|
|
|
|
and(startsWith("outlookmessageclass", "IPM.Schedule.Meeting."),
|
|
|
|
or(isNull("processed"), isFalse("processed"))));
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:51:49 -04:00
|
|
|
@Override
|
2010-07-22 19:10:24 -04:00
|
|
|
public List<ExchangeSession.Event> searchEvents(String folderPath, Set<String> attributes, Condition condition) throws IOException {
|
2010-07-07 05:20:42 -04:00
|
|
|
List<ExchangeSession.Event> events = new ArrayList<ExchangeSession.Event>();
|
|
|
|
List<EWSMethod.Item> responses = searchItems(folderPath, attributes,
|
|
|
|
condition,
|
2010-07-27 16:34:43 -04:00
|
|
|
FolderQueryTraversal.SHALLOW, 0);
|
2010-07-07 05:20:42 -04:00
|
|
|
for (EWSMethod.Item response : responses) {
|
2010-10-27 17:52:33 -04:00
|
|
|
Event event = new Event(response);
|
|
|
|
if ("Message".equals(event.type)) {
|
|
|
|
// need to check body
|
|
|
|
try {
|
|
|
|
event.getEventContent();
|
|
|
|
events.add(event);
|
|
|
|
} catch (HttpException e) {
|
2010-10-30 15:13:46 -04:00
|
|
|
LOGGER.warn("Ignore invalid event " + event.getHref());
|
2010-10-27 17:52:33 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
events.add(event);
|
|
|
|
}
|
|
|
|
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return events;
|
2010-06-15 10:51:49 -04:00
|
|
|
}
|
|
|
|
|
2010-07-07 08:29:41 -04:00
|
|
|
protected static final HashSet<String> EVENT_REQUEST_PROPERTIES = new HashSet<String>();
|
|
|
|
|
|
|
|
static {
|
|
|
|
EVENT_REQUEST_PROPERTIES.add("permanenturl");
|
|
|
|
EVENT_REQUEST_PROPERTIES.add("etag");
|
|
|
|
EVENT_REQUEST_PROPERTIES.add("displayname");
|
2010-08-24 16:27:41 -04:00
|
|
|
EVENT_REQUEST_PROPERTIES.add("subject");
|
2010-07-08 19:26:41 -04:00
|
|
|
EVENT_REQUEST_PROPERTIES.add("urlcompname");
|
2010-07-07 08:29:41 -04:00
|
|
|
}
|
|
|
|
|
2010-07-27 13:08:39 -04:00
|
|
|
protected EWSMethod.Item getEwsItem(String folderPath, String itemName) throws IOException {
|
|
|
|
EWSMethod.Item item = null;
|
|
|
|
String urlcompname = convertItemNameToEML(itemName);
|
|
|
|
// workaround for missing urlcompname in Exchange 2010
|
|
|
|
if (isItemId(urlcompname)) {
|
|
|
|
ItemId itemId = new ItemId(StringUtil.urlToBase64(urlcompname.substring(0, 152)));
|
|
|
|
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, false);
|
|
|
|
for (String attribute : EVENT_REQUEST_PROPERTIES) {
|
|
|
|
getItemMethod.addAdditionalProperty(Field.get(attribute));
|
|
|
|
}
|
|
|
|
executeMethod(getItemMethod);
|
|
|
|
item = getItemMethod.getResponseItem();
|
|
|
|
} else {
|
2010-07-27 16:34:43 -04:00
|
|
|
List<EWSMethod.Item> responses = searchItems(folderPath, EVENT_REQUEST_PROPERTIES, isEqualTo("urlcompname", urlcompname), FolderQueryTraversal.SHALLOW, 0);
|
2010-07-27 13:08:39 -04:00
|
|
|
if (!responses.isEmpty()) {
|
|
|
|
item = responses.get(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2010-07-07 08:29:41 -04:00
|
|
|
|
2010-06-15 10:51:49 -04:00
|
|
|
@Override
|
|
|
|
public Item getItem(String folderPath, String itemName) throws IOException {
|
2010-07-27 13:08:39 -04:00
|
|
|
EWSMethod.Item item = getEwsItem(folderPath, itemName);
|
|
|
|
if (item == null) {
|
2010-07-19 06:36:37 -04:00
|
|
|
throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND");
|
2010-07-07 08:29:41 -04:00
|
|
|
}
|
2010-07-27 13:08:39 -04:00
|
|
|
|
|
|
|
String itemType = item.type;
|
2010-07-07 08:29:41 -04:00
|
|
|
if ("Contact".equals(itemType)) {
|
|
|
|
// retrieve Contact properties
|
2010-07-27 13:08:39 -04:00
|
|
|
ItemId itemId = new ItemId(item);
|
|
|
|
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, false);
|
|
|
|
for (String attribute : CONTACT_ATTRIBUTES) {
|
|
|
|
getItemMethod.addAdditionalProperty(Field.get(attribute));
|
|
|
|
}
|
|
|
|
executeMethod(getItemMethod);
|
|
|
|
item = getItemMethod.getResponseItem();
|
|
|
|
if (item == null) {
|
2010-07-22 07:54:18 -04:00
|
|
|
throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND");
|
2010-07-20 09:25:15 -04:00
|
|
|
}
|
2010-07-27 13:08:39 -04:00
|
|
|
return new Contact(item);
|
2010-07-07 08:29:41 -04:00
|
|
|
} else if ("CalendarItem".equals(itemType)
|
2010-10-27 17:52:33 -04:00
|
|
|
|| "MeetingRequest".equals(itemType)
|
|
|
|
// VTODOs appear as Messages
|
|
|
|
|| "Message".equals(itemType)) {
|
2010-07-27 13:08:39 -04:00
|
|
|
return new Event(item);
|
2010-07-07 08:29:41 -04:00
|
|
|
} else {
|
2010-07-19 06:36:37 -04:00
|
|
|
throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND");
|
2010-07-07 08:29:41 -04:00
|
|
|
}
|
2010-07-27 13:08:39 -04:00
|
|
|
|
2010-06-15 10:51:49 -04:00
|
|
|
}
|
|
|
|
|
2010-07-07 05:20:42 -04:00
|
|
|
@Override
|
|
|
|
public ContactPhoto getContactPhoto(ExchangeSession.Contact contact) throws IOException {
|
2010-07-17 06:45:21 -04:00
|
|
|
ContactPhoto contactPhoto = null;
|
|
|
|
|
2010-07-17 17:34:14 -04:00
|
|
|
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, ((EwsExchangeSession.Contact) contact).itemId, false);
|
2010-07-17 06:45:21 -04:00
|
|
|
getItemMethod.addAdditionalProperty(Field.get("attachments"));
|
|
|
|
executeMethod(getItemMethod);
|
|
|
|
EWSMethod.Item item = getItemMethod.getResponseItem();
|
|
|
|
if (item != null) {
|
|
|
|
FileAttachment attachment = item.getAttachmentByName("ContactPicture.jpg");
|
|
|
|
if (attachment != null) {
|
|
|
|
// get attachment content
|
|
|
|
GetAttachmentMethod getAttachmentMethod = new GetAttachmentMethod(attachment.attachmentId);
|
|
|
|
executeMethod(getAttachmentMethod);
|
|
|
|
|
|
|
|
contactPhoto = new ContactPhoto();
|
|
|
|
contactPhoto.content = getAttachmentMethod.getResponseItem().get("Content");
|
2010-07-23 04:58:39 -04:00
|
|
|
if (attachment.contentType == null) {
|
|
|
|
contactPhoto.contentType = "image/jpeg";
|
|
|
|
} else {
|
|
|
|
contactPhoto.contentType = attachment.contentType;
|
|
|
|
}
|
|
|
|
|
2010-07-17 06:45:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return contactPhoto;
|
2010-07-07 05:20:42 -04:00
|
|
|
}
|
|
|
|
|
2010-06-16 09:30:36 -04:00
|
|
|
@Override
|
2010-07-20 05:10:39 -04:00
|
|
|
public void deleteItem(String folderPath, String itemName) throws IOException {
|
2010-07-27 16:34:43 -04:00
|
|
|
EWSMethod.Item item = getEwsItem(folderPath, itemName);
|
|
|
|
if (item != null) {
|
2010-08-17 11:42:20 -04:00
|
|
|
DeleteItemMethod deleteItemMethod = new DeleteItemMethod(new ItemId(item), DeleteType.HardDelete, SendMeetingCancellations.SendToNone);
|
2010-07-07 08:55:13 -04:00
|
|
|
executeMethod(deleteItemMethod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-07-20 05:10:39 -04:00
|
|
|
public void processItem(String folderPath, String itemName) throws IOException {
|
2010-07-27 16:34:43 -04:00
|
|
|
EWSMethod.Item item = getEwsItem(folderPath, itemName);
|
|
|
|
if (item != null) {
|
2010-07-07 08:55:13 -04:00
|
|
|
HashMap<String, String> localProperties = new HashMap<String, String>();
|
|
|
|
localProperties.put("processed", "1");
|
|
|
|
localProperties.put("read", "1");
|
|
|
|
UpdateItemMethod updateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
|
|
|
|
ConflictResolution.AlwaysOverwrite,
|
2010-07-10 18:42:48 -04:00
|
|
|
SendMeetingInvitationsOrCancellations.SendToNone,
|
2010-07-27 16:34:43 -04:00
|
|
|
new ItemId(item), buildProperties(localProperties));
|
2010-07-07 08:55:13 -04:00
|
|
|
executeMethod(updateItemMethod);
|
|
|
|
}
|
2010-06-16 09:30:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int sendEvent(String icsBody) throws IOException {
|
2010-07-28 02:45:00 -04:00
|
|
|
String itemName = UUID.randomUUID().toString() + ".EML";
|
|
|
|
byte[] mimeContent = new Event(DRAFTS, itemName, "urn:content-classes:calendarmessage", icsBody, null, null).createMimeContent();
|
|
|
|
if (mimeContent == null) {
|
|
|
|
// no recipients, cancel
|
|
|
|
return HttpStatus.SC_NO_CONTENT;
|
|
|
|
} else {
|
|
|
|
sendMessage(mimeContent);
|
|
|
|
return HttpStatus.SC_OK;
|
|
|
|
}
|
2010-06-16 09:30:36 -04:00
|
|
|
}
|
|
|
|
|
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) {
|
2010-07-28 13:11:18 -04:00
|
|
|
return folderPath.startsWith("/") && !folderPath.toLowerCase().startsWith(currentMailboxPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String getFreeBusyData(String attendee, String start, String end, int interval) throws IOException {
|
|
|
|
GetUserAvailabilityMethod getUserAvailabilityMethod = new GetUserAvailabilityMethod(attendee, start, end, interval);
|
|
|
|
executeMethod(getUserAvailabilityMethod);
|
|
|
|
return getUserAvailabilityMethod.getMergedFreeBusy();
|
2010-06-19 17:35:09 -04:00
|
|
|
}
|
|
|
|
|
2010-06-16 17:43:11 -04:00
|
|
|
@Override
|
|
|
|
protected void loadVtimezone() {
|
2010-08-18 06:33:25 -04:00
|
|
|
|
2010-08-17 09:39:14 -04:00
|
|
|
try {
|
2010-08-18 06:33:25 -04:00
|
|
|
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 {
|
2010-09-16 17:33:20 -04:00
|
|
|
timezoneId = getTimezoneidFromOptions();
|
2010-08-18 06:33:25 -04:00
|
|
|
}
|
|
|
|
if (timezoneId != null) {
|
|
|
|
createCalendarFolder("davmailtemp", null);
|
|
|
|
EWSMethod.Item item = new EWSMethod.Item();
|
|
|
|
item.type = "CalendarItem";
|
|
|
|
if ("Exchange2010".equals(serverVersion)) {
|
2010-11-24 17:45:34 -05:00
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
|
|
|
|
dateFormatter.setTimeZone(GMT_TIMEZONE);
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
item.put("Start", dateFormatter.format(cal.getTime()));
|
|
|
|
cal.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
|
item.put("End", dateFormatter.format(cal.getTime()));
|
2010-08-18 06:33:25 -04:00
|
|
|
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");
|
|
|
|
}
|
2010-08-17 09:39:14 -04:00
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.warn("Unable to get VTIMEZONE info: " + e, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2010-12-12 17:52:04 -05:00
|
|
|
&& (line.indexOf("tblTmZn") == -1)
|
|
|
|
&& (line.indexOf("selTmZn") == -1)) {
|
2010-08-17 09:39:14 -04:00
|
|
|
}
|
|
|
|
if (line != null) {
|
2010-12-12 17:52:04 -05:00
|
|
|
if (line.indexOf("tblTmZn") >= 0) {
|
|
|
|
int start = line.indexOf("oV=\"") + 4;
|
|
|
|
int end = line.indexOf('\"', start);
|
|
|
|
result = line.substring(start, end);
|
|
|
|
} else {
|
|
|
|
int end = line.lastIndexOf("\" selected>");
|
|
|
|
int start = line.lastIndexOf('\"', end - 1);
|
|
|
|
result = line.substring(start+1, end);
|
|
|
|
}
|
2010-08-17 09:39:14 -04:00
|
|
|
}
|
|
|
|
} 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;
|
2010-06-16 17:43:11 -04:00
|
|
|
}
|
|
|
|
|
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) {
|
2010-07-22 07:54:18 -04:00
|
|
|
throw new HttpNotFoundException("Folder '" + folderPath + "' not found");
|
2010-06-29 10:15:50 -04:00
|
|
|
}
|
|
|
|
return folderId;
|
|
|
|
}
|
|
|
|
|
2010-07-28 13:11:18 -04:00
|
|
|
protected static final String USERS_ROOT = "/users/";
|
|
|
|
|
|
|
|
protected FolderId getFolderIdIfExists(String folderPath) throws IOException {
|
2010-07-27 13:08:39 -04:00
|
|
|
String lowerCaseFolderPath = folderPath.toLowerCase();
|
2010-07-28 13:11:18 -04:00
|
|
|
if (currentMailboxPath.equals(lowerCaseFolderPath)) {
|
|
|
|
return getSubFolderIdIfExists(null, "");
|
|
|
|
} else if (lowerCaseFolderPath.startsWith(currentMailboxPath + '/')) {
|
|
|
|
return getSubFolderIdIfExists(null, folderPath.substring(currentMailboxPath.length() + 1));
|
|
|
|
} else if (folderPath.startsWith("/users/")) {
|
|
|
|
int slashIndex = folderPath.indexOf('/', USERS_ROOT.length());
|
|
|
|
String mailbox;
|
|
|
|
String subFolderPath;
|
|
|
|
if (slashIndex >= 0) {
|
|
|
|
mailbox = folderPath.substring(USERS_ROOT.length(), slashIndex);
|
2010-07-28 17:10:58 -04:00
|
|
|
subFolderPath = folderPath.substring(slashIndex + 1);
|
2010-07-28 13:11:18 -04:00
|
|
|
} else {
|
|
|
|
mailbox = folderPath.substring(USERS_ROOT.length());
|
|
|
|
subFolderPath = "";
|
2010-07-27 13:08:39 -04:00
|
|
|
}
|
2010-07-28 13:11:18 -04:00
|
|
|
return getSubFolderIdIfExists(mailbox, subFolderPath);
|
|
|
|
} else {
|
|
|
|
return getSubFolderIdIfExists(null, folderPath);
|
2010-07-09 17:25:18 -04:00
|
|
|
}
|
2010-07-28 13:11:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected FolderId getSubFolderIdIfExists(String mailbox, String folderPath) throws IOException {
|
|
|
|
String[] folderNames;
|
|
|
|
FolderId currentFolderId;
|
|
|
|
|
2010-07-09 17:25:18 -04:00
|
|
|
if (folderPath.startsWith(PUBLIC_ROOT)) {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.publicfoldersroot);
|
2010-06-07 18:30:23 -04:00
|
|
|
folderNames = folderPath.substring(PUBLIC_ROOT.length()).split("/");
|
2010-07-08 19:26:41 -04:00
|
|
|
} else if (folderPath.startsWith(INBOX) || folderPath.startsWith(LOWER_CASE_INBOX)) {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.inbox);
|
2010-06-08 06:44:47 -04:00
|
|
|
folderNames = folderPath.substring(INBOX.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(CALENDAR)) {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.calendar);
|
2010-06-08 06:44:47 -04:00
|
|
|
folderNames = folderPath.substring(CALENDAR.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(CONTACTS)) {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.contacts);
|
2010-06-08 06:44:47 -04:00
|
|
|
folderNames = folderPath.substring(CONTACTS.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(SENT)) {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.sentitems);
|
2010-06-08 06:44:47 -04:00
|
|
|
folderNames = folderPath.substring(SENT.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(DRAFTS)) {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.drafts);
|
2010-06-08 06:44:47 -04:00
|
|
|
folderNames = folderPath.substring(DRAFTS.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(TRASH)) {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.deleteditems);
|
2010-06-08 06:44:47 -04:00
|
|
|
folderNames = folderPath.substring(TRASH.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(JUNK)) {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.junkemail);
|
2010-06-08 06:44:47 -04:00
|
|
|
folderNames = folderPath.substring(JUNK.length()).split("/");
|
|
|
|
} else if (folderPath.startsWith(UNSENT)) {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.outbox);
|
2010-06-08 06:44:47 -04:00
|
|
|
folderNames = folderPath.substring(UNSENT.length()).split("/");
|
2010-06-07 18:30:23 -04:00
|
|
|
} else {
|
2010-07-28 13:11:18 -04:00
|
|
|
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.msgfolderroot);
|
2010-06-08 06:44:47 -04:00
|
|
|
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,
|
2010-07-27 06:39:53 -04:00
|
|
|
Field.get("folderDisplayName"), folderName)
|
2010-06-07 05:07:31 -04:00
|
|
|
);
|
2010-06-29 10:15:50 -04:00
|
|
|
executeMethod(findFolderMethod);
|
|
|
|
EWSMethod.Item item = findFolderMethod.getResponseItem();
|
|
|
|
if (item != null) {
|
2010-07-28 13:11:18 -04:00
|
|
|
folderId = new FolderId(parentFolderId.mailbox, item);
|
2010-06-29 10:15:50 -04:00
|
|
|
}
|
|
|
|
return folderId;
|
|
|
|
}
|
|
|
|
|
2010-07-20 05:10:39 -04:00
|
|
|
protected void executeMethod(EWSMethod ewsMethod) throws IOException {
|
2010-06-07 17:17:07 -04:00
|
|
|
try {
|
2010-07-27 08:58:23 -04:00
|
|
|
ewsMethod.setServerVersion(serverVersion);
|
2010-07-20 05:10:39 -04:00
|
|
|
httpClient.executeMethod(ewsMethod);
|
2010-07-27 08:58:23 -04:00
|
|
|
if (serverVersion == null) {
|
|
|
|
serverVersion = ewsMethod.getServerVersion();
|
|
|
|
}
|
2010-06-29 10:15:50 -04:00
|
|
|
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-07 05:07:31 -04:00
|
|
|
}
|
|
|
|
|
2010-08-07 14:59:49 -04:00
|
|
|
protected static final HashMap<String, String> GALFIND_ATTRIBUTE_MAP = new HashMap<String, String>();
|
|
|
|
|
|
|
|
static {
|
2010-09-06 18:55:29 -04:00
|
|
|
GALFIND_ATTRIBUTE_MAP.put("imapUid", "Name");
|
2010-08-07 14:59:49 -04:00
|
|
|
GALFIND_ATTRIBUTE_MAP.put("cn", "DisplayName");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("givenName", "GivenName");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("sn", "Surname");
|
2010-12-07 15:49:23 -05:00
|
|
|
GALFIND_ATTRIBUTE_MAP.put("smtpemail1", "EmailAddress1");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("smtpemail2", "EmailAddress2");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("smtpemail3", "EmailAddress3");
|
2010-11-03 18:31:36 -04:00
|
|
|
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("roomnumber", "OfficeLocation");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("street", "BusinessStreet");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("l", "BusinessCity");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("o", "CompanyName");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("postalcode", "BusinessPostalCode");
|
2010-11-07 17:01:49 -05:00
|
|
|
GALFIND_ATTRIBUTE_MAP.put("st", "BusinessState");
|
2010-12-07 15:25:50 -05:00
|
|
|
GALFIND_ATTRIBUTE_MAP.put("co", "BusinessCountryOrRegion");
|
2010-11-03 18:31:36 -04:00
|
|
|
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("manager", "Manager");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("middlename", "Initials");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("title", "JobTitle");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("department", "Department");
|
|
|
|
|
2010-11-07 17:01:49 -05:00
|
|
|
GALFIND_ATTRIBUTE_MAP.put("otherTelephone", "OtherTelephone");
|
2010-11-03 18:31:36 -04:00
|
|
|
GALFIND_ATTRIBUTE_MAP.put("telephoneNumber", "BusinessPhone");
|
2010-11-07 17:01:49 -05:00
|
|
|
GALFIND_ATTRIBUTE_MAP.put("mobile", "MobilePhone");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("facsimiletelephonenumber", "BusinessFax");
|
|
|
|
GALFIND_ATTRIBUTE_MAP.put("secretarycn", "AssistantName");
|
2010-11-03 18:31:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static final HashSet<String> IGNORE_ATTRIBUTE_SET = new HashSet<String>();
|
2010-12-07 17:07:01 -05:00
|
|
|
|
2010-11-03 18:31:36 -04:00
|
|
|
static {
|
2010-12-07 17:07:01 -05:00
|
|
|
IGNORE_ATTRIBUTE_SET.add("ContactSource");
|
|
|
|
IGNORE_ATTRIBUTE_SET.add("Culture");
|
|
|
|
IGNORE_ATTRIBUTE_SET.add("AssistantPhone");
|
2010-08-07 14:59:49 -04:00
|
|
|
}
|
|
|
|
|
2010-08-07 17:32:37 -04:00
|
|
|
protected Contact buildGalfindContact(EWSMethod.Item response) {
|
|
|
|
Contact contact = new Contact();
|
2010-08-09 16:15:28 -04:00
|
|
|
contact.setName(response.get("Name"));
|
2010-09-06 18:55:29 -04:00
|
|
|
contact.put("imapUid", response.get("Name"));
|
2010-11-07 17:38:27 -05:00
|
|
|
contact.put("uid", response.get("Name"));
|
2010-11-03 18:31:36 -04:00
|
|
|
if (LOGGER.isDebugEnabled()) {
|
|
|
|
for (String key : response.keySet()) {
|
|
|
|
if (!IGNORE_ATTRIBUTE_SET.contains(key) && !GALFIND_ATTRIBUTE_MAP.containsValue(key)) {
|
2010-12-07 17:07:01 -05:00
|
|
|
LOGGER.debug("Unsupported ResolveNames in " + contact.getName() + " response attribute: " + key + " value: " + response.get(key));
|
2010-11-03 18:31:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-07 17:32:37 -04:00
|
|
|
for (Map.Entry<String, String> entry : GALFIND_ATTRIBUTE_MAP.entrySet()) {
|
|
|
|
String attributeValue = response.get(entry.getValue());
|
|
|
|
if (attributeValue != null) {
|
|
|
|
contact.put(entry.getKey(), attributeValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contact;
|
|
|
|
}
|
|
|
|
|
2010-08-17 09:39:14 -04:00
|
|
|
@Override
|
2010-08-09 18:19:15 -04:00
|
|
|
public Map<String, ExchangeSession.Contact> galFind(Condition condition, Set<String> returningAttributes, int sizeLimit) throws IOException {
|
2010-08-07 17:32:37 -04:00
|
|
|
Map<String, ExchangeSession.Contact> contacts = new HashMap<String, ExchangeSession.Contact>();
|
|
|
|
if (condition instanceof MultiCondition) {
|
2010-08-17 09:39:14 -04:00
|
|
|
List<Condition> conditions = ((ExchangeSession.MultiCondition) condition).getConditions();
|
|
|
|
Operator operator = ((ExchangeSession.MultiCondition) condition).getOperator();
|
2010-08-07 17:32:37 -04:00
|
|
|
if (operator == Operator.Or) {
|
|
|
|
for (Condition innerCondition : conditions) {
|
2010-08-09 18:19:15 -04:00
|
|
|
contacts.putAll(galFind(innerCondition, returningAttributes, sizeLimit));
|
2010-08-07 17:32:37 -04:00
|
|
|
}
|
|
|
|
} else if (operator == Operator.And && !conditions.isEmpty()) {
|
2010-08-09 18:19:15 -04:00
|
|
|
Map<String, ExchangeSession.Contact> innerContacts = galFind(conditions.get(0), returningAttributes, sizeLimit);
|
2010-08-07 17:32:37 -04:00
|
|
|
for (ExchangeSession.Contact contact : innerContacts.values()) {
|
|
|
|
if (condition.isMatch(contact)) {
|
2010-08-09 16:15:28 -04:00
|
|
|
contacts.put(contact.getName().toLowerCase(), contact);
|
2010-08-07 17:32:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (condition instanceof AttributeCondition) {
|
2010-08-17 09:39:14 -04:00
|
|
|
String mappedAttributeName = GALFIND_ATTRIBUTE_MAP.get(((ExchangeSession.AttributeCondition) condition).getAttributeName());
|
2010-08-07 14:59:49 -04:00
|
|
|
if (mappedAttributeName != null) {
|
2010-08-17 09:39:14 -04:00
|
|
|
String value = ((ExchangeSession.AttributeCondition) condition).getValue().toLowerCase();
|
2010-08-07 14:59:49 -04:00
|
|
|
Operator operator = ((AttributeCondition) condition).getOperator();
|
|
|
|
String searchValue = value;
|
|
|
|
if (mappedAttributeName.startsWith("EmailAddress")) {
|
|
|
|
searchValue = "smtp:" + searchValue;
|
|
|
|
}
|
|
|
|
if (operator == Operator.IsEqualTo) {
|
|
|
|
searchValue = '=' + searchValue;
|
|
|
|
}
|
|
|
|
ResolveNamesMethod resolveNamesMethod = new ResolveNamesMethod(searchValue);
|
|
|
|
executeMethod(resolveNamesMethod);
|
|
|
|
List<EWSMethod.Item> responses = resolveNamesMethod.getResponseItems();
|
2010-10-21 16:20:40 -04:00
|
|
|
if (LOGGER.isDebugEnabled()) {
|
2010-10-27 17:52:33 -04:00
|
|
|
LOGGER.debug("ResolveNames(" + searchValue + ") returned " + responses.size() + " results");
|
2010-10-21 16:20:40 -04:00
|
|
|
}
|
2010-08-07 14:59:49 -04:00
|
|
|
for (EWSMethod.Item response : responses) {
|
2010-08-07 17:32:37 -04:00
|
|
|
Contact contact = buildGalfindContact(response);
|
|
|
|
if (condition.isMatch(contact)) {
|
2010-08-09 16:15:28 -04:00
|
|
|
contacts.put(contact.getName().toLowerCase(), contact);
|
2010-08-07 14:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contacts;
|
|
|
|
}
|
|
|
|
|
2010-07-20 12:33:58 -04:00
|
|
|
protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
|
2010-07-22 07:54:18 -04:00
|
|
|
String zuluDateValue = null;
|
|
|
|
if (exchangeDateValue != null) {
|
|
|
|
try {
|
|
|
|
zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormat().parse(exchangeDateValue));
|
|
|
|
} catch (ParseException e) {
|
|
|
|
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue);
|
|
|
|
}
|
2010-07-09 17:25:18 -04:00
|
|
|
}
|
|
|
|
return zuluDateValue;
|
|
|
|
}
|
|
|
|
|
2010-09-17 06:13:00 -04:00
|
|
|
protected String convertCalendarDateToExchange(String vcalendarDateValue) throws DavMailException {
|
|
|
|
String zuluDateValue = null;
|
|
|
|
if (vcalendarDateValue != null) {
|
|
|
|
try {
|
2010-12-12 17:52:04 -05:00
|
|
|
SimpleDateFormat dateParser;
|
|
|
|
if (vcalendarDateValue.length() == 8) {
|
|
|
|
dateParser = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
|
|
|
|
} else {
|
|
|
|
dateParser = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH);
|
|
|
|
}
|
2010-09-17 06:13:00 -04:00
|
|
|
dateParser.setTimeZone(GMT_TIMEZONE);
|
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
|
|
|
|
dateFormatter.setTimeZone(GMT_TIMEZONE);
|
|
|
|
zuluDateValue = dateFormatter.format(dateParser.parse(vcalendarDateValue));
|
|
|
|
} catch (ParseException e) {
|
|
|
|
throw new DavMailException("EXCEPTION_INVALID_DATE", vcalendarDateValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zuluDateValue;
|
|
|
|
}
|
|
|
|
|
2010-07-27 09:19:28 -04:00
|
|
|
/**
|
|
|
|
* Format date to exchange search format.
|
|
|
|
*
|
|
|
|
* @param date date object
|
|
|
|
* @return formatted search date
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String formatSearchDate(Date date) {
|
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat(YYYY_MM_DD_T_HHMMSS_Z, Locale.ENGLISH);
|
|
|
|
dateFormatter.setTimeZone(GMT_TIMEZONE);
|
|
|
|
return dateFormatter.format(date);
|
|
|
|
}
|
|
|
|
|
2010-07-27 13:08:39 -04:00
|
|
|
protected static boolean isItemId(String itemName) {
|
2010-07-28 02:45:00 -04:00
|
|
|
return itemName.length() == 156;
|
2010-07-27 13:08:39 -04:00
|
|
|
}
|
2010-06-02 04:46:46 -04:00
|
|
|
}
|
2010-07-07 05:20:42 -04:00
|
|
|
|