mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-13 19:22:22 -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:
parent
47a0aef919
commit
4e0dfc1a68
@ -213,24 +213,24 @@ public class CaldavConnection extends AbstractConnection {
|
||||
sendCalendar(request, depth, paths[2]);
|
||||
} else if ("REPORT".equals(command) && "users".equals(paths[1]) && paths.length == 4 && "calendar".equals(paths[3])
|
||||
// only current user for now
|
||||
&& session.getEmail().equals(paths[2])) {
|
||||
&& session.getEmail().equalsIgnoreCase(paths[2])) {
|
||||
reportCalendar(request);
|
||||
|
||||
} else if ("PUT".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3])
|
||||
// only current user for now
|
||||
&& session.getEmail().equals(paths[2])) {
|
||||
&& session.getEmail().equalsIgnoreCase(paths[2])) {
|
||||
String etag = headers.get("if-match");
|
||||
int status = session.createOrUpdateEvent(paths[4], body, etag);
|
||||
sendHttpResponse(status, true);
|
||||
|
||||
} else if ("DELETE".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3])
|
||||
// only current user for now
|
||||
&& session.getEmail().equals(paths[2])) {
|
||||
&& session.getEmail().equalsIgnoreCase(paths[2])) {
|
||||
int status = session.deleteEvent(paths[4]);
|
||||
sendHttpResponse(status, true);
|
||||
} else if ("GET".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3])
|
||||
// only current user for now
|
||||
&& session.getEmail().equals(paths[2])) {
|
||||
&& session.getEmail().equalsIgnoreCase(paths[2])) {
|
||||
ExchangeSession.Event event = session.getEvent(paths[4]);
|
||||
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");
|
||||
appendCalendar(buffer, principal, request);
|
||||
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");
|
||||
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true);
|
||||
|
@ -275,17 +275,16 @@ public class ExchangeSession {
|
||||
URL baseURL = new URL(mailBoxBaseHref);
|
||||
mailPath = baseURL.getPath();
|
||||
LOGGER.debug("Base href found in body, mailPath is " + mailPath);
|
||||
// get user name from mailPath and build Email
|
||||
buildEmail(getUserName());
|
||||
buildEmail();
|
||||
LOGGER.debug("Current user email is " + email);
|
||||
} else {
|
||||
// failover for Exchange 2007 : build standard mailbox link with email
|
||||
buildEmail(getUserName());
|
||||
buildEmail();
|
||||
mailPath = "/exchange/" + email + "/";
|
||||
LOGGER.debug("Current user email is " + email + ", mailPath is " + mailPath);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error parsing main page at " + method.getPath());
|
||||
LOGGER.error("Error parsing main page at " + method.getPath(), e);
|
||||
} finally {
|
||||
if (mainPageReader != null) {
|
||||
try {
|
||||
@ -300,6 +299,9 @@ public class ExchangeSession {
|
||||
if (mailPath == null) {
|
||||
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 {
|
||||
@ -1119,50 +1121,71 @@ public class ExchangeSession {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current Exchange user name
|
||||
* Get current Exchange alias name from login name
|
||||
*
|
||||
* @return user name
|
||||
* @throws java.io.IOException on error
|
||||
*/
|
||||
protected String getUserName() throws IOException {
|
||||
protected String getAliasFromLogin() throws IOException {
|
||||
// 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
// 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;
|
||||
throw new IOException("Empty mail path");
|
||||
}
|
||||
int index = mailPath.lastIndexOf("/", mailPath.length() - 2);
|
||||
if (index >= 0 && mailPath.endsWith("/")) {
|
||||
return mailPath.substring(index + 1, mailPath.length() - 1);
|
||||
} else {
|
||||
int index = mailPath.lastIndexOf("/", mailPath.length() - 2);
|
||||
if (index >= 0 && mailPath.endsWith("/")) {
|
||||
return mailPath.substring(index + 1, mailPath.length() - 1);
|
||||
} else {
|
||||
throw new IOException("Invalid mail path: " + mailPath);
|
||||
}
|
||||
throw new IOException("Invalid mail path: " + mailPath);
|
||||
}
|
||||
}
|
||||
|
||||
public void buildEmail(String userName) throws IOException {
|
||||
GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + userName);
|
||||
public String getEmail(String alias) throws IOException {
|
||||
String emailResult = null;
|
||||
GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + alias);
|
||||
try {
|
||||
int status = wdr.retrieveSessionInstance().executeMethod(getMethod);
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
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, String> result = results.get(userName);
|
||||
Map<String, String> result = results.get(alias.toLowerCase());
|
||||
if (result != null) {
|
||||
email = result.get("EM");
|
||||
emailResult = result.get("EM");
|
||||
}
|
||||
|
||||
|
||||
} finally {
|
||||
getMethod.releaseConnection();
|
||||
}
|
||||
if (email == null) {
|
||||
throw new IOException("Unable to get user email for " + userName + " at " + getMethod.getPath());
|
||||
}
|
||||
return emailResult;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -67,7 +67,7 @@ public final class XMLStreamUtil {
|
||||
if (event == XMLStreamConstants.START_ELEMENT && rowName.equals(reader.getLocalName())) {
|
||||
item = new HashMap<String, String>();
|
||||
} 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;
|
||||
} else if (event == XMLStreamConstants.START_ELEMENT && item != null) {
|
||||
currentElement = reader.getLocalName();
|
||||
|
Loading…
Reference in New Issue
Block a user