1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-14 03:32:22 -05:00

IMAP: try to support public folders hosted on a separate server (302 redirect on PROPFIND)

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@878 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-12-09 21:13:43 +00:00
parent f78a5ab913
commit ce6906f666
2 changed files with 44 additions and 14 deletions

View File

@ -166,6 +166,7 @@ public class ExchangeSession {
private String draftsUrl;
private String calendarUrl;
private String contactsUrl;
private String publicFolderUrl;
/**
* Base user mailboxes path (used to select folder)
@ -232,7 +233,7 @@ public class ExchangeSession {
buildMailPath(method);
// got base http mailbox http url
// get base http mailbox http urls
getWellKnownFolders();
} catch (DavMailAuthenticationException exc) {
@ -642,13 +643,24 @@ public class ExchangeSession {
draftsUrl = getURIPropertyIfExists(properties, "drafts", URN_SCHEMAS_HTTPMAIL);
calendarUrl = getURIPropertyIfExists(properties, "calendar", URN_SCHEMAS_HTTPMAIL);
contactsUrl = getURIPropertyIfExists(properties, "contacts", URN_SCHEMAS_HTTPMAIL);
try {
PropFindMethod propFindMethod = new PropFindMethod("/public", CONTENT_TAG, 0);
DavGatewayHttpClientFacade.executeMethod(httpClient, propFindMethod);
publicFolderUrl = propFindMethod.getURI().getURI();
} catch (IOException e) {
LOGGER.warn("Public folders not available: " + (e.getMessage() == null ? e : e.getMessage()));
// default public folder path
publicFolderUrl = "/public";
}
LOGGER.debug("Inbox URL : " + inboxUrl +
" Trash URL : " + deleteditemsUrl +
" Sent URL : " + sentitemsUrl +
" Send URL : " + sendmsgUrl +
" Drafts URL : " + draftsUrl +
" Calendar URL : " + calendarUrl +
" Contacts URL : " + contactsUrl
" Contacts URL : " + contactsUrl +
" Public folder URL : " + publicFolderUrl
);
} catch (IOException e) {
LOGGER.error(e.getMessage());
@ -737,7 +749,7 @@ public class ExchangeSession {
message.messageId = message.messageId.substring(1, message.messageId.length() - 1);
}
LOGGER.debug("Found message IMAP uid: "+message.imapUid+" href: " + responseEntity.getHref()+" permanenturl:"+message.permanentUrl);
LOGGER.debug("Found message IMAP uid: " + message.imapUid + " href: " + responseEntity.getHref() + " permanenturl:" + message.permanentUrl);
return message;
}
@ -1086,7 +1098,7 @@ public class ExchangeSession {
} else if (folderName.startsWith("calendar")) {
folderPath = folderName.replaceFirst("calendar", calendarUrl);
} else if (folderName.startsWith("public")) {
folderPath = '/' + folderName;
folderPath = publicFolderUrl + folderName.substring("public".length());
// absolute folder path
} else if (folderName.startsWith("/")) {
folderPath = folderName;
@ -1536,7 +1548,7 @@ public class ExchangeSession {
}
isoWriter.flush();
} catch (HttpServerErrorException e) {
LOGGER.warn("Unable to retrieve message at: "+messageUrl);
LOGGER.warn("Unable to retrieve message at: " + messageUrl);
throw e;
} finally {
if (reader != null) {
@ -1770,7 +1782,7 @@ public class ExchangeSession {
}
protected HttpException buildHttpException(Exception e) {
String message = "Unable to get event "+getName()+" at " + permanentUrl + ": " + e.getMessage();
String message = "Unable to get event " + getName() + " at " + permanentUrl + ": " + e.getMessage();
LOGGER.warn(message);
return new HttpException(message);
}
@ -2803,7 +2815,11 @@ public class ExchangeSession {
*/
public String getCmdBasePath() {
if (mailPath == null) {
return "/public/";
if (publicFolderUrl == null) {
return "/public/";
} else {
return publicFolderUrl + '/';
}
} else {
return mailPath;
}
@ -3216,7 +3232,7 @@ public class ExchangeSession {
} else {
endDate = icalDateFormat.parse(endDateValue);
}
freebusyUrl = "/public/?cmd=freebusy" +
freebusyUrl = publicFolderUrl + "/?cmd=freebusy" +
"&start=" + exchangeZuluDateFormat.format(startDate) +
"&end=" + exchangeZuluDateFormat.format(endDate) +
"&interval=" + FREE_BUSY_INTERVAL +

View File

@ -20,10 +20,7 @@ package davmail.http;
import davmail.BundleMessage;
import davmail.Settings;
import davmail.exception.DavMailException;
import davmail.exception.HttpForbiddenException;
import davmail.exception.HttpNotFoundException;
import davmail.exception.HttpServerErrorException;
import davmail.exception.*;
import davmail.ui.tray.DavGatewayTray;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthPolicy;
@ -326,6 +323,15 @@ public final class DavGatewayHttpClientFacade {
try {
int status = httpClient.executeMethod(method);
// need to follow redirects (once) on public folders
if (isRedirect(status)) {
method.releaseConnection();
URI targetUri = new URI(method.getResponseHeader("Location").getValue(), true);
checkExpiredSession(targetUri.getQuery());
method.setURI(targetUri);
status = httpClient.executeMethod(method);
}
if (status != HttpStatus.SC_MULTI_STATUS) {
throw buildHttpException(method);
}
@ -382,9 +388,9 @@ public final class DavGatewayHttpClientFacade {
// do not follow redirects in expired sessions
method.setFollowRedirects(followRedirects);
int status = httpClient.executeMethod(method);
if (status == HttpStatus.SC_UNAUTHORIZED & !hasNTLM(httpClient)) {
if (status == HttpStatus.SC_UNAUTHORIZED & !hasNTLM(httpClient)) {
method.releaseConnection();
LOGGER.debug("Received unauthorized at "+method.getURI()+", retrying with NTLM");
LOGGER.debug("Received unauthorized at " + method.getURI() + ", retrying with NTLM");
addNTLM(httpClient);
status = httpClient.executeMethod(method);
}
@ -395,6 +401,7 @@ public final class DavGatewayHttpClientFacade {
// check for expired session
if (followRedirects) {
String queryString = method.getQueryString();
checkExpiredSession(queryString);
if (queryString != null && queryString.contains("reason=2")) {
LOGGER.warn("GET failed, session expired at " + method.getURI() + ": " + method.getResponseBodyAsString());
throw DavGatewayHttpClientFacade.buildHttpException(method);
@ -402,6 +409,13 @@ public final class DavGatewayHttpClientFacade {
}
}
private static void checkExpiredSession(String queryString) throws DavMailAuthenticationException {
if (queryString != null && queryString.contains("reason=2")) {
LOGGER.warn("Request failed, session expired (reason=2) ");
throw new DavMailAuthenticationException("EXCEPTION_SESSION_EXPIRED");
}
}
/**
* Build Http Exception from methode status
*