diff --git a/src/java/davmail/caldav/CaldavConnection.java b/src/java/davmail/caldav/CaldavConnection.java index d09bf337..23604360 100644 --- a/src/java/davmail/caldav/CaldavConnection.java +++ b/src/java/davmail/caldav/CaldavConnection.java @@ -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("\n"); appendCalendar(buffer, principal, request); if (depth == 1) { - appendEventsResponses(buffer, request, session.getAllEvents()); + DavGatewayTray.debug("Searching calendar events..."); + List events = session.getAllEvents(); + DavGatewayTray.debug("Found "+events.size()+" calendar events"); + appendEventsResponses(buffer, request, events); } buffer.append("\n"); sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true); diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index b532f22e..ce3be226 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -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> results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN"); - Map result = results.get(userName); + Map 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(); } /** diff --git a/src/java/davmail/exchange/XMLStreamUtil.java b/src/java/davmail/exchange/XMLStreamUtil.java index a4f74e11..5789cb44 100644 --- a/src/java/davmail/exchange/XMLStreamUtil.java +++ b/src/java/davmail/exchange/XMLStreamUtil.java @@ -67,7 +67,7 @@ public final class XMLStreamUtil { if (event == XMLStreamConstants.START_ELEMENT && rowName.equals(reader.getLocalName())) { item = new HashMap(); } 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();