mirror of
https://github.com/moparisthebest/davmail
synced 2025-01-12 22:18:11 -05:00
Refactor email and alias retrieval: always use options page with Exchange 2007
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1375 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
3ecc0aa80e
commit
c5722222bd
@ -2629,8 +2629,12 @@ public abstract class ExchangeSession {
|
||||
* @return user name
|
||||
*/
|
||||
protected String getAliasFromLogin() {
|
||||
// Exchange 2007 : userName is login without domain
|
||||
// login is email, not alias
|
||||
if (this.userName.indexOf('@') >= 0) {
|
||||
return null;
|
||||
}
|
||||
String result = this.userName;
|
||||
// remove domain name
|
||||
int index = result.indexOf('\\');
|
||||
if (index >= 0) {
|
||||
result = result.substring(index + 1);
|
||||
@ -2666,8 +2670,7 @@ public abstract class ExchangeSession {
|
||||
|
||||
static final String MAILBOX_BASE = "/cn=";
|
||||
|
||||
protected String getAliasFromOptions() {
|
||||
String result = null;
|
||||
protected void getEmailAndAliasFromOptions() {
|
||||
// get user mail URL from html body
|
||||
BufferedReader optionsPageReader = null;
|
||||
GetMethod optionsMethod = new GetMethod("/owa/?ae=Options&t=About");
|
||||
@ -2675,14 +2678,22 @@ public abstract class ExchangeSession {
|
||||
DavGatewayHttpClientFacade.executeGetMethod(httpClient, optionsMethod, false);
|
||||
optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream()));
|
||||
String line;
|
||||
// find mailbox full name
|
||||
//noinspection StatementWithEmptyBody
|
||||
while ((line = optionsPageReader.readLine()) != null && line.toLowerCase().indexOf(MAILBOX_BASE) == -1) {
|
||||
|
||||
// find email and alias
|
||||
while ((line = optionsPageReader.readLine()) != null
|
||||
&& (line.indexOf('[') == -1
|
||||
|| line.indexOf('@') == -1
|
||||
|| line.indexOf(']') == -1)
|
||||
&& line.toLowerCase().indexOf(MAILBOX_BASE) == -1) {
|
||||
}
|
||||
if (line != null) {
|
||||
int start = line.toLowerCase().lastIndexOf(MAILBOX_BASE) + MAILBOX_BASE.length();
|
||||
int end = line.indexOf('<', start);
|
||||
result = line.substring(start, end);
|
||||
alias = line.substring(start, end);
|
||||
start = line.indexOf('[') + 1;
|
||||
end = line.indexOf(']', start);
|
||||
email = line.substring(start, end);
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error parsing options page at " + optionsMethod.getPath());
|
||||
@ -2697,44 +2708,6 @@ public abstract class ExchangeSession {
|
||||
optionsMethod.releaseConnection();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getEmailFromOptions() {
|
||||
String result = null;
|
||||
// get user mail URL from html body
|
||||
BufferedReader optionsPageReader = null;
|
||||
GetMethod optionsMethod = new GetMethod("/owa/?ae=Options&t=About");
|
||||
try {
|
||||
DavGatewayHttpClientFacade.executeGetMethod(httpClient, optionsMethod, false);
|
||||
optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream()));
|
||||
String line;
|
||||
// find email
|
||||
//noinspection StatementWithEmptyBody
|
||||
while ((line = optionsPageReader.readLine()) != null
|
||||
&& (line.indexOf('[') == -1
|
||||
|| line.indexOf('@') == -1
|
||||
|| line.indexOf(']') == -1)) {
|
||||
}
|
||||
if (line != null) {
|
||||
int start = line.indexOf('[') + 1;
|
||||
int end = line.indexOf(']', start);
|
||||
result = line.substring(start, end);
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -425,33 +425,31 @@ public class DavExchangeSession extends ExchangeSession {
|
||||
|
||||
static final String BASE_HREF = "<base href=\"";
|
||||
|
||||
protected void buildMailPath(HttpMethod method) throws DavMailAuthenticationException {
|
||||
// find base url
|
||||
String line;
|
||||
|
||||
/**
|
||||
* Exchange 2003: get mailPath from welcome page
|
||||
*
|
||||
* @param method
|
||||
* @return
|
||||
*/
|
||||
protected String getMailpathFromWelcomePage(HttpMethod method) {
|
||||
String welcomePageMailPath = null;
|
||||
// get user mail URL from html body (multi frame)
|
||||
BufferedReader mainPageReader = null;
|
||||
try {
|
||||
mainPageReader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
|
||||
//noinspection StatementWithEmptyBody
|
||||
String line;
|
||||
while ((line = mainPageReader.readLine()) != null && line.toLowerCase().indexOf(BASE_HREF) == -1) {
|
||||
}
|
||||
if (line != null) {
|
||||
// Exchange 2003
|
||||
int start = line.toLowerCase().indexOf(BASE_HREF) + BASE_HREF.length();
|
||||
int end = line.indexOf('\"', start);
|
||||
String mailBoxBaseHref = line.substring(start, end);
|
||||
URL baseURL = new URL(mailBoxBaseHref);
|
||||
mailPath = baseURL.getPath();
|
||||
LOGGER.debug("Base href found in body, mailPath is " + mailPath);
|
||||
buildEmail(method.getURI().getHost());
|
||||
LOGGER.debug("Current user email is " + email);
|
||||
} else {
|
||||
// failover for Exchange 2007 : build standard mailbox link with email
|
||||
buildEmail(method.getURI().getHost());
|
||||
mailPath = "/exchange/" + email + '/';
|
||||
LOGGER.debug("Current user email is " + email + ", mailPath is " + mailPath);
|
||||
welcomePageMailPath = baseURL.getPath();
|
||||
LOGGER.debug("Base href found in body, mailPath is " + welcomePageMailPath);
|
||||
}
|
||||
rootPath = mailPath.substring(0, mailPath.lastIndexOf('/', mailPath.length() - 2) + 1);
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error parsing main page at " + method.getPath(), e);
|
||||
} finally {
|
||||
@ -464,11 +462,33 @@ public class DavExchangeSession extends ExchangeSession {
|
||||
}
|
||||
method.releaseConnection();
|
||||
}
|
||||
return welcomePageMailPath;
|
||||
}
|
||||
|
||||
protected void buildMailPath(HttpMethod method) throws DavMailAuthenticationException {
|
||||
// get mailPath from welcome page on Exchange 2003
|
||||
mailPath = getMailpathFromWelcomePage(method);
|
||||
|
||||
//noinspection VariableNotUsedInsideIf
|
||||
if (mailPath != null) {
|
||||
// Exchange 2003
|
||||
try {
|
||||
buildEmail(method.getURI().getHost());
|
||||
} catch (URIException uriException) {
|
||||
LOGGER.warn(uriException);
|
||||
}
|
||||
} else {
|
||||
// Exchange 2007 : get alias and email from options page
|
||||
getEmailAndAliasFromOptions();;
|
||||
// build standard mailbox link with email
|
||||
mailPath = "/exchange/" + email + '/';
|
||||
}
|
||||
|
||||
if (mailPath == null || email == null) {
|
||||
throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED_PASSWORD_EXPIRED");
|
||||
}
|
||||
LOGGER.debug("Current user email is " + email + ", mailPath is " + mailPath);
|
||||
rootPath = mailPath.substring(0, mailPath.lastIndexOf('/', mailPath.length() - 2) + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -477,51 +497,52 @@ public class DavExchangeSession extends ExchangeSession {
|
||||
* @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
|
||||
String mailBoxPath = getMailboxPath();
|
||||
// mailPath contains either alias or email
|
||||
if (mailBoxPath.indexOf('@') >= 0) {
|
||||
email = mailBoxPath;
|
||||
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));
|
||||
} else {
|
||||
// use mailbox name as alias
|
||||
alias = mailBoxPath;
|
||||
email = getEmail(alias);
|
||||
if (email == null) {
|
||||
// failover: try to get email from login name
|
||||
alias = getAliasFromLogin();
|
||||
email = getEmail(alias);
|
||||
}
|
||||
// another failover : get alias from mailPath display name
|
||||
if (email == null) {
|
||||
alias = getAliasFromMailboxDisplayName();
|
||||
email = getEmail(alias);
|
||||
}
|
||||
if (email == null) {
|
||||
LOGGER.debug("Unable to get user email with alias " + mailBoxPath
|
||||
+ " or " + getAliasFromLogin()
|
||||
+ " or " +getAliasFromMailboxDisplayName()
|
||||
);
|
||||
// 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();
|
||||
}
|
||||
email = buffer.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -553,7 +574,7 @@ public class DavExchangeSession extends ExchangeSession {
|
||||
*
|
||||
* @return user name
|
||||
*/
|
||||
protected String getAliasFromMailPath() {
|
||||
protected String getMailboxPath() {
|
||||
if (mailPath == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -97,8 +97,7 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
}
|
||||
|
||||
// also need to retrieve email and alias
|
||||
alias = getAliasFromOptions();
|
||||
email = getEmailFromOptions();
|
||||
getEmailAndAliasFromOptions();
|
||||
if (email == null || alias == null) {
|
||||
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user