1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 19:22:22 -05:00

Fix : use email instead of mailbox name for webdav path with Exchange 2007

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@255 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2008-12-25 22:16:24 +00:00
parent 2107f7a223
commit d0ebab2d7f

View File

@ -78,6 +78,7 @@ public class ExchangeSession {
* Base user mailboxes path (used to select folder) * Base user mailboxes path (used to select folder)
*/ */
private String mailPath; private String mailPath;
private String email;
private String currentFolderUrl; private String currentFolderUrl;
private WebdavResource wdr = null; private WebdavResource wdr = null;
@ -256,8 +257,7 @@ public class ExchangeSession {
return logonMethod; return logonMethod;
} }
protected String getMailPath(HttpMethod method) { protected void buildMailPath(HttpMethod method) throws HttpException {
String result = 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 {
@ -273,10 +273,13 @@ public class ExchangeSession {
int end = line.indexOf("\"", start); int end = line.indexOf("\"", start);
String mailBoxBaseHref = line.substring(start, end); String mailBoxBaseHref = line.substring(start, end);
URL baseURL = new URL(mailBoxBaseHref); URL baseURL = new URL(mailBoxBaseHref);
result = baseURL.getPath(); mailPath = baseURL.getPath();
// get user name from mailPath and build Email
buildEmail(getUserName());
} else { } else {
// failover for Exchange 2007 : try to get mailbox from options // failover for Exchange 2007 : build standard mailbox link with email
result = getMailPathFromOptions(method.getPath()); buildEmail(getUserName());
mailPath = "/exchange/" + email + "/";
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Error parsing main page at " + method.getPath()); LOGGER.error("Error parsing main page at " + method.getPath());
@ -291,42 +294,9 @@ public class ExchangeSession {
method.releaseConnection(); method.releaseConnection();
} }
return result; if (mailPath == null) {
throw new AuthenticationException(poolKey.url + " not found in body, authentication failed: password expired ?");
} }
protected String getMailPathFromOptions(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 = "/exchange/" + 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;
} }
void login() throws IOException { void login() throws IOException {
@ -384,11 +354,7 @@ public class ExchangeSession {
} }
} }
mailPath = getMailPath(method); buildMailPath(method);
if (mailPath == null) {
throw new HttpException(poolKey.url + " not found in body, authentication failed: password expired ?");
}
// got base http mailbox http url // got base http mailbox http url
wdr.setPath(mailPath); wdr.setPath(mailPath);
@ -1135,7 +1101,16 @@ public class ExchangeSession {
* @return user name * @return user name
* @throws java.io.IOException on error * @throws java.io.IOException on error
*/ */
public String getUserName() throws IOException { protected String getUserName() throws IOException {
if (mailPath == null) {
// Exchange 2007 : userName is login without domain
String userName = poolKey.userName;
int index = userName.indexOf('\\');
if (index >= 0) {
userName = userName.substring(index + 1);
}
return userName;
} else {
int index = mailPath.lastIndexOf("/", mailPath.length() - 2); int index = mailPath.lastIndexOf("/", mailPath.length() - 2);
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);
@ -1143,16 +1118,10 @@ public class ExchangeSession {
throw new IOException("Invalid mail path: " + mailPath); throw new IOException("Invalid mail path: " + mailPath);
} }
} }
}
/** public void buildEmail(String userName) throws IOException {
* Get current user email GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + userName);
*
* @return user email
* @throws java.io.IOException on error
*/
public String getEmail() throws IOException {
String email = null;
GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + getUserName());
try { try {
int status = wdr.retrieveSessionInstance().executeMethod(getMethod); int status = wdr.retrieveSessionInstance().executeMethod(getMethod);
if (status != HttpStatus.SC_OK) { if (status != HttpStatus.SC_OK) {
@ -1163,9 +1132,18 @@ public class ExchangeSession {
getMethod.releaseConnection(); getMethod.releaseConnection();
} }
if (email == null) { if (email == null) {
throw new IOException("Unable to get user email from: " + getMethod.getPath()); throw new IOException("Unable to get user email for " + userName + " at " + getMethod.getPath());
} }
}
/**
* Get current user email
*
* @return user email
* @throws java.io.IOException on error
*/
public String getEmail() throws IOException {
return email; return email;
} }