diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index d21e6cd2..7ff76ec1 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -3032,24 +3032,6 @@ public abstract class ExchangeSession { */ public abstract boolean isSharedFolder(String folderPath); - /** - * Build base path for cmd commands (galfind, gallookup). - * This does not work with freebusy, which requires /public/ - * - * @return cmd base path - */ - public String getCmdBasePath() { - if (mailPath == null) { - if (publicFolderUrl == null) { - return "/public/"; - } else { - return publicFolderUrl + '/'; - } - } else { - return mailPath; - } - } - static final String MAILBOX_BASE = "/cn="; protected void getEmailAndAliasFromOptions() { diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index 8e14eaf7..af36fdb0 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -203,6 +203,22 @@ public class DavExchangeSession extends ExchangeSession { return !getFolderPath(folderPath).toLowerCase().startsWith(mailPath.toLowerCase()); } + /** + * Build base path for cmd commands (galfind, gallookup). + * + * @return cmd base path + */ + public String getCmdBasePath() { + if (PUBLIC_ROOT.equals(publicFolderUrl)) { + // public folder is not available => try to use mailbox path + // Note: This does not work with freebusy, which requires /public/ + return mailPath; + } else { + // use public folder url + return publicFolderUrl; + } + } + /** * LDAP to Exchange Criteria Map */ @@ -448,6 +464,8 @@ public class DavExchangeSession extends ExchangeSession { @Override protected void buildSessionInfo(HttpMethod method) throws DavMailException { + checkPublicFolder(); + buildMailPath(method); // get base http mailbox http urls @@ -677,6 +695,35 @@ public class DavExchangeSession extends ExchangeSession { } } + protected void checkPublicFolder() { + + Cookie[] currentCookies = httpClient.getState().getCookies(); + // check public folder access + try { + publicFolderUrl = httpClient.getHostConfiguration().getHostURL()+PUBLIC_ROOT; + DavPropertyNameSet davPropertyNameSet = new DavPropertyNameSet(); + davPropertyNameSet.add(Field.getPropertyName("displayname")); + PropFindMethod propFindMethod = new PropFindMethod(publicFolderUrl, davPropertyNameSet, 0); + try { + DavGatewayHttpClientFacade.executeMethod(httpClient, propFindMethod); + } catch (IOException e) { + // workaround for NTLM authentication only on /public + if (!DavGatewayHttpClientFacade.hasNTLM(httpClient)) { + DavGatewayHttpClientFacade.addNTLM(httpClient); + DavGatewayHttpClientFacade.executeMethod(httpClient, propFindMethod); + } + } + // update public folder URI + publicFolderUrl = propFindMethod.getURI().getURI(); + } catch (IOException e) { + // restore cookies on error + httpClient.getState().addCookies(currentCookies); + LOGGER.warn("Public folders not available: " + (e.getMessage() == null ? e : e.getMessage())); + // default public folder path + publicFolderUrl = PUBLIC_ROOT; + } + } + protected void getWellKnownFolders() throws DavMailException { // Retrieve well known URLs MultiStatusResponse[] responses; @@ -705,39 +752,6 @@ public class DavExchangeSession extends ExchangeSession { outboxName = getFolderName(outboxUrl); // junk folder not available over webdav - // default public folder path - publicFolderUrl = PUBLIC_ROOT; - - Cookie[] currentCookies = httpClient.getState().getCookies(); - // check public folder access - try { - if (inboxUrl != null) { - // try to build full public URI from inboxUrl - URI publicUri = new URI(inboxUrl, false); - publicUri.setPath(PUBLIC_ROOT); - publicFolderUrl = publicUri.getURI(); - } - DavPropertyNameSet davPropertyNameSet = new DavPropertyNameSet(); - davPropertyNameSet.add(Field.getPropertyName("displayname")); - PropFindMethod propFindMethod = new PropFindMethod(publicFolderUrl, davPropertyNameSet, 0); - try { - DavGatewayHttpClientFacade.executeMethod(httpClient, propFindMethod); - } catch (IOException e) { - // workaround for NTLM authentication only on /public - if (!DavGatewayHttpClientFacade.hasNTLM(httpClient)) { - DavGatewayHttpClientFacade.addNTLM(httpClient); - DavGatewayHttpClientFacade.executeMethod(httpClient, propFindMethod); - } - } - // update public folder URI - publicFolderUrl = propFindMethod.getURI().getURI(); - } catch (IOException e) { - // restore cookies on error - httpClient.getState().addCookies(currentCookies); - LOGGER.warn("Public folders not available: " + (e.getMessage() == null ? e : e.getMessage())); - publicFolderUrl = "/public"; - } - LOGGER.debug("Inbox URL: " + inboxUrl + " Trash URL: " + deleteditemsUrl + " Sent URL: " + sentitemsUrl +