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

Refactor email retrieval : do not throw IOException in failovers

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@555 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-05-07 08:45:00 +00:00
parent 0d49f19903
commit 54f07e822b

View File

@ -392,13 +392,14 @@ public class ExchangeSession {
} }
protected void buildMailPath(HttpMethod method) throws DavMailAuthenticationException { protected void buildMailPath(HttpMethod method) throws DavMailAuthenticationException {
// find base url
final String BASE_HREF = "<base href=\"";
String line = null;
// get user mail URL from html body (multi frame) // get user mail URL from html body (multi frame)
BufferedReader mainPageReader = null; BufferedReader mainPageReader = null;
try { try {
mainPageReader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); mainPageReader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
String line;
// find base url
final String BASE_HREF = "<base href=\"";
//noinspection StatementWithEmptyBody //noinspection StatementWithEmptyBody
while ((line = mainPageReader.readLine()) != null && line.toLowerCase().indexOf(BASE_HREF) == -1) { while ((line = mainPageReader.readLine()) != null && line.toLowerCase().indexOf(BASE_HREF) == -1) {
} }
@ -409,11 +410,11 @@ public class ExchangeSession {
URL baseURL = new URL(mailBoxBaseHref); URL baseURL = new URL(mailBoxBaseHref);
mailPath = baseURL.getPath(); mailPath = baseURL.getPath();
LOGGER.debug("Base href found in body, mailPath is " + mailPath); LOGGER.debug("Base href found in body, mailPath is " + mailPath);
buildEmail(method); buildEmail(method.getURI().getHost(), method.getPath());
LOGGER.debug("Current user email is " + email); LOGGER.debug("Current user email is " + email);
} else { } else {
// failover for Exchange 2007 : build standard mailbox link with email // failover for Exchange 2007 : build standard mailbox link with email
buildEmail(method); buildEmail(method.getURI().getHost(), method.getPath());
mailPath = "/exchange/" + email + '/'; mailPath = "/exchange/" + email + '/';
LOGGER.debug("Current user email is " + email + ", mailPath is " + mailPath); LOGGER.debug("Current user email is " + email + ", mailPath is " + mailPath);
} }
@ -430,6 +431,7 @@ public class ExchangeSession {
method.releaseConnection(); method.releaseConnection();
} }
if (mailPath == null || email == null) { if (mailPath == null || email == null) {
throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED_PASSWORD_EXPIRED"); throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED_PASSWORD_EXPIRED");
} }
@ -1697,9 +1699,8 @@ public class ExchangeSession {
* Get current Exchange alias name from mailbox name * Get current Exchange alias name from mailbox name
* *
* @return user name * @return user name
* @throws IOException on error
*/ */
protected String getAliasFromMailPath() throws IOException { protected String getAliasFromMailPath() {
if (mailPath == null) { if (mailPath == null) {
return null; return null;
} }
@ -1707,21 +1708,27 @@ public class ExchangeSession {
if (index >= 0 && mailPath.endsWith("/")) { if (index >= 0 && mailPath.endsWith("/")) {
return mailPath.substring(index + 1, mailPath.length() - 1); return mailPath.substring(index + 1, mailPath.length() - 1);
} else { } else {
throw new DavMailException("EXCEPTION_INVALID_MAIL_PATH", mailPath); LOGGER.warn(new BundleMessage("EXCEPTION_INVALID_MAIL_PATH", mailPath));
return null;
} }
} }
public String getAliasFromMailboxDisplayName() throws IOException { public String getAliasFromMailboxDisplayName() {
if (mailPath == null) { if (mailPath == null) {
return null; return null;
} }
String displayName; String displayName = null;
MultiStatusResponse[] responses = DavGatewayHttpClientFacade.executePropFindMethod( try {
httpClient, URIUtil.encodePath(mailPath), 0, DISPLAY_NAME); MultiStatusResponse[] responses = DavGatewayHttpClientFacade.executePropFindMethod(
if (responses.length == 0) { httpClient, URIUtil.encodePath(mailPath), 0, DISPLAY_NAME);
throw new DavMailException("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER"); if (responses.length == 0) {
LOGGER.warn(new BundleMessage("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER"));
} else {
displayName = getPropertyIfExists(responses[0].getProperties(HttpStatus.SC_OK), "displayname", Namespace.getNamespace("DAV:"));
}
} catch (IOException e) {
LOGGER.warn(new BundleMessage("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER"));
} }
displayName = getPropertyIfExists(responses[0].getProperties(HttpStatus.SC_OK), "displayname", Namespace.getNamespace("DAV:"));
return displayName; return displayName;
} }
@ -1761,12 +1768,14 @@ public class ExchangeSession {
} }
} }
public String getEmail(String alias) throws IOException { public String getEmail(String alias) {
String emailResult = null; String emailResult = null;
if (alias != null) { if (alias != null) {
String path = getCmdBasePath() + "?Cmd=galfind&AN=" + URIUtil.encodeWithinQuery(alias); GetMethod getMethod = null;
GetMethod getMethod = new GetMethod(path); String path = null;
try { try {
path = getCmdBasePath() + "?Cmd=galfind&AN=" + URIUtil.encodeWithinQuery(alias);
getMethod = new GetMethod(path);
int status = httpClient.executeMethod(getMethod); int status = httpClient.executeMethod(getMethod);
if (status != HttpStatus.SC_OK) { if (status != HttpStatus.SC_OK) {
throw new DavMailException("EXCEPTION_UNABLE_TO_GET_EMAIL", getMethod.getPath()); throw new DavMailException("EXCEPTION_UNABLE_TO_GET_EMAIL", getMethod.getPath());
@ -1776,16 +1785,18 @@ public class ExchangeSession {
if (result != null) { if (result != null) {
emailResult = result.get("EM"); emailResult = result.get("EM");
} }
} catch (HttpException e) { } catch (IOException e) {
LOGGER.debug("GET " + path + " failed: " + e); LOGGER.debug("GET " + path + " failed: " + e + ' ' + e.getMessage());
} finally { } finally {
getMethod.releaseConnection(); if (getMethod != null) {
getMethod.releaseConnection();
}
} }
} }
return emailResult; return emailResult;
} }
public void buildEmail(HttpMethod method) throws IOException { public void buildEmail(String hostName, String methodPath) {
// first try to get email from login name // first try to get email from login name
alias = getAliasFromLogin(); alias = getAliasFromLogin();
email = getEmail(alias); email = getEmail(alias);
@ -1801,13 +1812,13 @@ public class ExchangeSession {
} }
if (email == null) { if (email == null) {
// failover : get email from Exchange 2007 Options page // failover : get email from Exchange 2007 Options page
alias = getAliasFromOptions(method.getPath()); alias = getAliasFromOptions(methodPath);
email = getEmail(alias); email = getEmail(alias);
} }
if (email == null) { if (email == null) {
LOGGER.debug("Unable to get user email with alias " + getAliasFromLogin() LOGGER.debug("Unable to get user email with alias " + getAliasFromLogin()
+ " or " + getAliasFromMailPath() + " or " + getAliasFromMailPath()
+ " or " + getAliasFromOptions(method.getPath()) + " or " + getAliasFromOptions(methodPath)
); );
// last failover: build email from domain name and mailbox display name // last failover: build email from domain name and mailbox display name
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
@ -1819,7 +1830,6 @@ public class ExchangeSession {
if (alias != null) { if (alias != null) {
buffer.append(alias); buffer.append(alias);
buffer.append('@'); buffer.append('@');
String hostName = method.getURI().getHost();
int dotIndex = hostName.indexOf('.'); int dotIndex = hostName.indexOf('.');
if (dotIndex >= 0) { if (dotIndex >= 0) {
buffer.append(hostName.substring(dotIndex + 1)); buffer.append(hostName.substring(dotIndex + 1));