1
0
mirror of https://github.com/moparisthebest/davmail synced 2025-01-07 03:38:05 -05:00

Enable preemptive authentication to avoid most 401 requestsFix current user email retrieval: use login as alias, failover to mailbox name as alias

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@281 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-01-08 09:58:31 +00:00
parent 47a0aef919
commit 4e0dfc1a68
3 changed files with 59 additions and 33 deletions

View File

@ -213,24 +213,24 @@ public class CaldavConnection extends AbstractConnection {
sendCalendar(request, depth, paths[2]); sendCalendar(request, depth, paths[2]);
} else if ("REPORT".equals(command) && "users".equals(paths[1]) && paths.length == 4 && "calendar".equals(paths[3]) } else if ("REPORT".equals(command) && "users".equals(paths[1]) && paths.length == 4 && "calendar".equals(paths[3])
// only current user for now // only current user for now
&& session.getEmail().equals(paths[2])) { && session.getEmail().equalsIgnoreCase(paths[2])) {
reportCalendar(request); reportCalendar(request);
} else if ("PUT".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3]) } else if ("PUT".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3])
// only current user for now // only current user for now
&& session.getEmail().equals(paths[2])) { && session.getEmail().equalsIgnoreCase(paths[2])) {
String etag = headers.get("if-match"); String etag = headers.get("if-match");
int status = session.createOrUpdateEvent(paths[4], body, etag); int status = session.createOrUpdateEvent(paths[4], body, etag);
sendHttpResponse(status, true); sendHttpResponse(status, true);
} else if ("DELETE".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3]) } else if ("DELETE".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3])
// only current user for now // only current user for now
&& session.getEmail().equals(paths[2])) { && session.getEmail().equalsIgnoreCase(paths[2])) {
int status = session.deleteEvent(paths[4]); int status = session.deleteEvent(paths[4]);
sendHttpResponse(status, true); sendHttpResponse(status, true);
} else if ("GET".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3]) } else if ("GET".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3])
// only current user for now // only current user for now
&& session.getEmail().equals(paths[2])) { && session.getEmail().equalsIgnoreCase(paths[2])) {
ExchangeSession.Event event = session.getEvent(paths[4]); ExchangeSession.Event event = session.getEvent(paths[4]);
sendHttpResponse(HttpStatus.SC_OK, null, "text/calendar;charset=UTF-8", event.getICS(), true); sendHttpResponse(HttpStatus.SC_OK, null, "text/calendar;charset=UTF-8", event.getICS(), true);
@ -408,7 +408,10 @@ public class CaldavConnection extends AbstractConnection {
buffer.append("<D:multistatus xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">\n"); buffer.append("<D:multistatus xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">\n");
appendCalendar(buffer, principal, request); appendCalendar(buffer, principal, request);
if (depth == 1) { if (depth == 1) {
appendEventsResponses(buffer, request, session.getAllEvents()); DavGatewayTray.debug("Searching calendar events...");
List<ExchangeSession.Event> events = session.getAllEvents();
DavGatewayTray.debug("Found "+events.size()+" calendar events");
appendEventsResponses(buffer, request, events);
} }
buffer.append("</D:multistatus>\n"); buffer.append("</D:multistatus>\n");
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true); sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true);

View File

@ -275,17 +275,16 @@ 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);
// get user name from mailPath and build Email buildEmail();
buildEmail(getUserName());
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(getUserName()); buildEmail();
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);
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Error parsing main page at " + method.getPath()); LOGGER.error("Error parsing main page at " + method.getPath(), e);
} finally { } finally {
if (mainPageReader != null) { if (mainPageReader != null) {
try { try {
@ -300,6 +299,9 @@ public class ExchangeSession {
if (mailPath == null) { if (mailPath == null) {
throw new AuthenticationException("Unable to build mail path, authentication failed: password expired ?"); throw new AuthenticationException("Unable to build mail path, authentication failed: password expired ?");
} }
if (email == null) {
throw new AuthenticationException("Unable to get email, authentication failed: password expired ?");
}
} }
void login() throws IOException { void login() throws IOException {
@ -1119,13 +1121,12 @@ public class ExchangeSession {
} }
/** /**
* Get current Exchange user name * Get current Exchange alias name from login name
* *
* @return user name * @return user name
* @throws java.io.IOException on error * @throws java.io.IOException on error
*/ */
protected String getUserName() throws IOException { protected String getAliasFromLogin() throws IOException {
if (mailPath == null) {
// Exchange 2007 : userName is login without domain // Exchange 2007 : userName is login without domain
String userName = poolKey.userName; String userName = poolKey.userName;
int index = userName.indexOf('\\'); int index = userName.indexOf('\\');
@ -1133,7 +1134,18 @@ public class ExchangeSession {
userName = userName.substring(index + 1); userName = userName.substring(index + 1);
} }
return userName; return userName;
} else { }
/**
* Get current Exchange alias name from mailbox name
*
* @return user name
* @throws java.io.IOException on error
*/
protected String getAliasFromMailPath() throws IOException {
if (mailPath == null) {
throw new IOException("Empty mail path");
}
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);
@ -1141,28 +1153,39 @@ public class ExchangeSession {
throw new IOException("Invalid mail path: " + mailPath); throw new IOException("Invalid mail path: " + mailPath);
} }
} }
}
public void buildEmail(String userName) throws IOException { public String getEmail(String alias) throws IOException {
GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + userName); String emailResult = null;
GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + alias);
try { try {
int status = wdr.retrieveSessionInstance().executeMethod(getMethod); int status = wdr.retrieveSessionInstance().executeMethod(getMethod);
if (status != HttpStatus.SC_OK) { if (status != HttpStatus.SC_OK) {
throw new IOException("Unable to get user email from: " + getMethod.getPath()); throw new IOException("Unable to get user email from: " + getMethod.getPath());
} }
Map<String, Map<String, String>> results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN"); Map<String, Map<String, String>> results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN");
Map<String, String> result = results.get(userName); Map<String, String> result = results.get(alias.toLowerCase());
if (result != null) { if (result != null) {
email = result.get("EM"); emailResult = result.get("EM");
} }
} finally { } finally {
getMethod.releaseConnection(); getMethod.releaseConnection();
} }
if (email == null) { return emailResult;
throw new IOException("Unable to get user email for " + userName + " at " + getMethod.getPath());
} }
public void buildEmail() throws IOException {
// first try to get email from login name
email = getEmail(getAliasFromLogin());
// failover: use mailbox name as alias
if (email == null) {
email = getEmail(getAliasFromMailPath());
}
if (email == null) {
throw new IOException("Unable to get user email with alias " + getAliasFromLogin() + " or " + getAliasFromMailPath());
}
// normalize email
email = email.toLowerCase();
} }
/** /**

View File

@ -67,7 +67,7 @@ public final class XMLStreamUtil {
if (event == XMLStreamConstants.START_ELEMENT && rowName.equals(reader.getLocalName())) { if (event == XMLStreamConstants.START_ELEMENT && rowName.equals(reader.getLocalName())) {
item = new HashMap<String, String>(); item = new HashMap<String, String>();
} else if (event == XMLStreamConstants.END_ELEMENT && rowName.equals(reader.getLocalName())) { } else if (event == XMLStreamConstants.END_ELEMENT && rowName.equals(reader.getLocalName())) {
results.put(item.get(idName),item); results.put(item.get(idName).toLowerCase(),item);
item = null; item = null;
} else if (event == XMLStreamConstants.START_ELEMENT && item != null) { } else if (event == XMLStreamConstants.START_ELEMENT && item != null) {
currentElement = reader.getLocalName(); currentElement = reader.getLocalName();