1
0
mirror of https://github.com/moparisthebest/davmail synced 2025-02-28 09:21:49 -05:00

Dav: disable galFind on error

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1347 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-08-16 21:50:18 +00:00
parent 8c30891d0c
commit 9e824fc524
2 changed files with 179 additions and 161 deletions

View File

@ -623,7 +623,8 @@ public abstract class ExchangeSession {
*/
public abstract MessageList searchMessages(String folderName, Set<String> attributes, Condition condition) throws IOException;
protected enum Operator {
@SuppressWarnings({"JavaDoc"})
public enum Operator {
Or, And, Not, IsEqualTo,
IsGreaterThan, IsGreaterThanOrEqualTo,
IsLessThan, IsLessThanOrEqualTo,
@ -676,10 +677,20 @@ public abstract class ExchangeSession {
return false;
}
/**
* Get attribute name.
*
* @return attribute name
*/
public String getAttributeName() {
return attributeName;
}
/**
* Condition value.
*
* @return value
*/
public String getValue() {
return value;
}
@ -703,10 +714,20 @@ public abstract class ExchangeSession {
}
}
/**
* Conditions list.
*
* @return conditions
*/
public List<Condition> getConditions() {
return conditions;
}
/**
* Condition operator.
*
* @return operator
*/
public Operator getOperator() {
return operator;
}
@ -1742,6 +1763,11 @@ public abstract class ExchangeSession {
return name;
}
/**
* Set contact name
*
* @param name contact name
*/
public void setName(String name) {
this.itemName = name;
}
@ -2037,10 +2063,10 @@ public abstract class ExchangeSession {
} else {
// reset body
description = "";
String status = vCalendar.getAttendeeStatus();
if (status != null) {
writer.writeHeader("Subject", BundleMessage.format(status)+ subject);
writer.writeHeader("Subject", BundleMessage.format(status) + subject);
} else {
writer.writeHeader("Subject", subject);
}
@ -2567,47 +2593,6 @@ public abstract class ExchangeSession {
return result;
}
/**
* Get current Exchange alias name from mailbox name
*
* @return user name
*/
protected String getAliasFromMailPath() {
if (mailPath == null) {
return null;
}
int index = mailPath.lastIndexOf('/', mailPath.length() - 2);
if (index >= 0 && mailPath.endsWith("/")) {
return mailPath.substring(index + 1, mailPath.length() - 1);
} else {
LOGGER.warn(new BundleMessage("EXCEPTION_INVALID_MAIL_PATH", mailPath));
return null;
}
}
/**
* Get user alias from mailbox display name over Webdav.
*
* @return user alias
*/
public String getAliasFromMailboxDisplayName() {
if (mailPath == null) {
return null;
}
String displayName = null;
try {
Folder rootFolder = getFolder("");
if (rootFolder == null) {
LOGGER.warn(new BundleMessage("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath));
} else {
displayName = rootFolder.displayName;
}
} catch (IOException e) {
LOGGER.warn(new BundleMessage("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath));
}
return displayName;
}
/**
* Test if folderPath is inside user mailbox.
*
@ -2634,91 +2619,6 @@ public abstract class ExchangeSession {
}
}
/**
* Get user email from global address list (galfind).
*
* @param alias user alias
* @return user email
*/
public String getEmail(String alias) {
String emailResult = null;
if (alias != null) {
GetMethod getMethod = null;
String path = null;
try {
path = getCmdBasePath() + "?Cmd=galfind&AN=" + URIUtil.encodeWithinQuery(alias);
getMethod = new GetMethod(path);
DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod, true);
Map<String, Map<String, String>> results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN");
Map<String, String> result = results.get(alias.toLowerCase());
if (result != null) {
emailResult = result.get("EM");
}
} catch (IOException e) {
LOGGER.debug("GET " + path + " failed: " + e + ' ' + e.getMessage());
} finally {
if (getMethod != null) {
getMethod.releaseConnection();
}
}
}
return emailResult;
}
/**
* Determine user email through various means.
*
* @param hostName Exchange server host name for last failover
*/
public void buildEmail(String hostName) {
// first try to get email from login name
alias = getAliasFromLogin();
email = getEmail(alias);
// failover: use mailbox name as alias
if (email == null) {
alias = getAliasFromMailPath();
email = getEmail(alias);
}
// another failover : get alias from mailPath display name
if (email == null) {
alias = getAliasFromMailboxDisplayName();
email = getEmail(alias);
}
if (email == null) {
// failover : get email from Exchange 2007 Options page
alias = getAliasFromOptions();
email = getEmail(alias);
// failover: get email from options
if (alias != null && email == null) {
email = getEmailFromOptions();
}
}
if (email == null) {
LOGGER.debug("Unable to get user email with alias " + getAliasFromLogin()
+ " or " + getAliasFromMailPath()
+ " or " + getAliasFromOptions()
);
// last failover: build email from domain name and mailbox display name
StringBuilder buffer = new StringBuilder();
// most reliable alias
alias = getAliasFromMailboxDisplayName();
if (alias == null) {
alias = getAliasFromLogin();
}
if (alias != null) {
buffer.append(alias);
if (alias.indexOf('@') < 0) {
buffer.append('@');
int dotIndex = hostName.indexOf('.');
if (dotIndex >= 0) {
buffer.append(hostName.substring(dotIndex + 1));
}
}
}
email = buffer.toString();
}
}
static final String MAILBOX_BASE = "/cn=";
protected String getAliasFromOptions() {
@ -2811,26 +2711,14 @@ public abstract class ExchangeSession {
}
/**
* Search users in global address book
* Search global address list
*
* @param searchAttribute exchange search attribute
* @param searchValue search value
* @return List of users
* @param condition search filter
* @param returningAttributes returning attributes
* @param sizeLimit size limit
* @return matching contacts from gal
* @throws IOException on error
*/
public Map<String, Map<String, String>> galFind(String searchAttribute, String searchValue) throws IOException {
Map<String, Map<String, String>> results;
GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery(getCmdBasePath() + "?Cmd=galfind&" + searchAttribute + '=' + searchValue));
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod, true);
results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN");
} finally {
getMethod.releaseConnection();
}
LOGGER.debug("galfind " + searchAttribute + '=' + searchValue + ": " + results.size() + " result(s)");
return results;
}
public abstract Map<String, Contact> galFind(Condition condition, Set<String> returningAttributes, int sizeLimit) throws IOException;
/**

View File

@ -235,10 +235,32 @@ public class DavExchangeSession extends ExchangeSession {
GALFIND_ATTRIBUTE_MAP.put("roomnumber", "office");
}
boolean disableGalFind;
protected Map<String, Map<String, String>> galFind(String query) throws IOException {
Map<String, Map<String, String>> results;
String path = getCmdBasePath() + "?Cmd=galfind" + query;
GetMethod getMethod = new GetMethod(path);
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod, true);
results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN");
} catch (IOException e) {
LOGGER.debug("GET " + path + " failed: " + e + ' ' + e.getMessage());
disableGalFind = true;
throw e;
} finally {
getMethod.releaseConnection();
}
return results;
}
@Override
public Map<String, ExchangeSession.Contact> galFind(Condition condition, Set<String> returningAttributes, int sizeLimit) throws IOException {
Map<String, ExchangeSession.Contact> contacts = new HashMap<String, ExchangeSession.Contact>();
if (condition instanceof MultiCondition) {
if (disableGalFind) {
// do nothing
} else if (condition instanceof MultiCondition) {
List<Condition> conditions = ((ExchangeSession.MultiCondition) condition).getConditions();
Operator operator = ((ExchangeSession.MultiCondition) condition).getOperator();
if (operator == Operator.Or) {
@ -259,8 +281,6 @@ public class DavExchangeSession extends ExchangeSession {
if (searchAttribute != null) {
String searchValue = ((ExchangeSession.AttributeCondition) condition).getValue();
StringBuilder query = new StringBuilder();
query.append(getCmdBasePath());
query.append("?Cmd=galfind");
if ("EM".equals(searchAttribute)) {
// mail search, split
int atIndex = searchValue.indexOf('@');
@ -272,22 +292,15 @@ public class DavExchangeSession extends ExchangeSession {
int dotIndex = searchValue.indexOf('.');
if (dotIndex >= 0) {
// assume mail starts with firstname
query.append("&FN=").append(searchValue.substring(0, dotIndex));
query.append("&LN=").append(searchValue.substring(dotIndex + 1));
query.append("&FN=").append(URIUtil.encodeWithinQuery(searchValue.substring(0, dotIndex)));
query.append("&LN=").append(URIUtil.encodeWithinQuery(searchValue.substring(dotIndex + 1)));
} else {
query.append("&FN=").append(searchValue);
query.append("&FN=").append(URIUtil.encodeWithinQuery(searchValue));
}
} else {
query.append('&').append(searchAttribute).append('=').append(searchValue);
}
GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery(query.toString()));
Map<String, Map<String, String>> results;
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod, true);
results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN");
} finally {
getMethod.releaseConnection();
query.append('&').append(searchAttribute).append('=').append(URIUtil.encodeWithinQuery(searchValue));
}
Map<String, Map<String, String>> results = galFind(query.toString());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(query.toString() + ": " + results.size() + " result(s)");
}
@ -456,6 +469,123 @@ public class DavExchangeSession extends ExchangeSession {
}
}
/**
* Determine user email through various means.
*
* @param hostName Exchange server host name for last failover
*/
public void buildEmail(String hostName) {
// first try to get email from login name
alias = getAliasFromLogin();
email = getEmail(alias);
// failover: use mailbox name as alias
if (email == null) {
alias = getAliasFromMailPath();
email = getEmail(alias);
}
// another failover : get alias from mailPath display name
if (email == null) {
alias = getAliasFromMailboxDisplayName();
email = getEmail(alias);
}
if (email == null) {
// failover : get email from Exchange 2007 Options page
alias = getAliasFromOptions();
email = getEmail(alias);
// failover: get email from options
if (alias != null && email == null) {
email = getEmailFromOptions();
}
}
if (email == null) {
LOGGER.debug("Unable to get user email with alias " + getAliasFromLogin()
+ " or " + getAliasFromMailPath()
+ " or " + getAliasFromOptions()
);
// last failover: build email from domain name and mailbox display name
StringBuilder buffer = new StringBuilder();
// most reliable alias
alias = getAliasFromMailboxDisplayName();
if (alias == null) {
alias = getAliasFromLogin();
}
if (alias != null) {
buffer.append(alias);
if (alias.indexOf('@') < 0) {
buffer.append('@');
int dotIndex = hostName.indexOf('.');
if (dotIndex >= 0) {
buffer.append(hostName.substring(dotIndex + 1));
}
}
}
email = buffer.toString();
}
}
/**
* Get user alias from mailbox display name over Webdav.
*
* @return user alias
*/
public String getAliasFromMailboxDisplayName() {
if (mailPath == null) {
return null;
}
String displayName = null;
try {
Folder rootFolder = getFolder("");
if (rootFolder == null) {
LOGGER.warn(new BundleMessage("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath));
} else {
displayName = rootFolder.displayName;
}
} catch (IOException e) {
LOGGER.warn(new BundleMessage("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath));
}
return displayName;
}
/**
* Get current Exchange alias name from mailbox name
*
* @return user name
*/
protected String getAliasFromMailPath() {
if (mailPath == null) {
return null;
}
int index = mailPath.lastIndexOf('/', mailPath.length() - 2);
if (index >= 0 && mailPath.endsWith("/")) {
return mailPath.substring(index + 1, mailPath.length() - 1);
} else {
LOGGER.warn(new BundleMessage("EXCEPTION_INVALID_MAIL_PATH", mailPath));
return null;
}
}
/**
* Get user email from global address list (galfind).
*
* @param alias user alias
* @return user email
*/
public String getEmail(String alias) {
String emailResult = null;
if (alias != null) {
try {
Map<String, Map<String, String>> results = galFind("&AN=" + URIUtil.encodeWithinQuery(alias));
Map<String, String> result = results.get(alias.toLowerCase());
if (result != null) {
emailResult = result.get("EM");
}
} catch (IOException e) {
LOGGER.debug("getEmail("+alias+") failed");
}
}
return emailResult;
}
protected String getURIPropertyIfExists(DavPropertySet properties, String alias) throws URIException {
DavProperty property = properties.get(Field.getPropertyName(alias));
if (property == null) {