mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-14 11:42:23 -05:00
Failover for Exchange 2007 current user alias retrieval: try to get it from the options page
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@425 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
8ab20013a7
commit
ba874940d8
@ -292,11 +292,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();
|
buildEmail(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();
|
buildEmail(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);
|
||||||
}
|
}
|
||||||
@ -1675,7 +1675,7 @@ public class ExchangeSession {
|
|||||||
*/
|
*/
|
||||||
protected String getAliasFromMailPath() throws IOException {
|
protected String getAliasFromMailPath() throws IOException {
|
||||||
if (mailPath == null) {
|
if (mailPath == null) {
|
||||||
throw new IOException("Empty mail path");
|
return null;
|
||||||
}
|
}
|
||||||
int index = mailPath.lastIndexOf("/", mailPath.length() - 2);
|
int index = mailPath.lastIndexOf("/", mailPath.length() - 2);
|
||||||
if (index >= 0 && mailPath.endsWith("/")) {
|
if (index >= 0 && mailPath.endsWith("/")) {
|
||||||
@ -1687,6 +1687,7 @@ public class ExchangeSession {
|
|||||||
|
|
||||||
public String getEmail(String alias) throws IOException {
|
public String getEmail(String alias) throws IOException {
|
||||||
String emailResult = null;
|
String emailResult = null;
|
||||||
|
if (alias != null) {
|
||||||
GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + alias);
|
GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + alias);
|
||||||
try {
|
try {
|
||||||
int status = wdr.retrieveSessionInstance().executeMethod(getMethod);
|
int status = wdr.retrieveSessionInstance().executeMethod(getMethod);
|
||||||
@ -1702,10 +1703,11 @@ public class ExchangeSession {
|
|||||||
} finally {
|
} finally {
|
||||||
getMethod.releaseConnection();
|
getMethod.releaseConnection();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return emailResult;
|
return emailResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buildEmail() throws IOException {
|
public void buildEmail(String methodPath) throws IOException {
|
||||||
// first try to get email from login name
|
// first try to get email from login name
|
||||||
email = getEmail(getAliasFromLogin());
|
email = getEmail(getAliasFromLogin());
|
||||||
// failover: use mailbox name as alias
|
// failover: use mailbox name as alias
|
||||||
@ -1713,8 +1715,50 @@ public class ExchangeSession {
|
|||||||
email = getEmail(getAliasFromMailPath());
|
email = getEmail(getAliasFromMailPath());
|
||||||
}
|
}
|
||||||
if (email == null) {
|
if (email == null) {
|
||||||
throw new IOException("Unable to get user email with alias " + getAliasFromLogin() + " or " + getAliasFromMailPath());
|
// failover : get email from Exchange 2007 Options page
|
||||||
|
email = getEmail(getAliasFromOptions(methodPath));
|
||||||
}
|
}
|
||||||
|
if (email == null) {
|
||||||
|
throw new IOException("Unable to get user email with alias " + getAliasFromLogin()
|
||||||
|
+ " or " + getAliasFromMailPath()
|
||||||
|
+ " or " + getAliasFromOptions(methodPath)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getAliasFromOptions(String path) {
|
||||||
|
String result = null;
|
||||||
|
// get user mail URL from html body
|
||||||
|
BufferedReader optionsPageReader = null;
|
||||||
|
GetMethod optionsMethod = new GetMethod(path + "?ae=Options&t=About");
|
||||||
|
try {
|
||||||
|
wdr.retrieveSessionInstance().executeMethod(optionsMethod);
|
||||||
|
optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream()));
|
||||||
|
String line;
|
||||||
|
// find mailbox full name
|
||||||
|
final String MAILBOX_BASE = "cn=recipients/cn=";
|
||||||
|
//noinspection StatementWithEmptyBody
|
||||||
|
while ((line = optionsPageReader.readLine()) != null && line.toLowerCase().indexOf(MAILBOX_BASE) == -1) {
|
||||||
|
}
|
||||||
|
if (line != null) {
|
||||||
|
int start = line.toLowerCase().indexOf(MAILBOX_BASE) + MAILBOX_BASE.length();
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user