diff --git a/src/java/davmail/exception/WebdavNotAvailableException.java b/src/java/davmail/exception/WebdavNotAvailableException.java new file mode 100644 index 00000000..fabd8209 --- /dev/null +++ b/src/java/davmail/exception/WebdavNotAvailableException.java @@ -0,0 +1,34 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2010 Mickael Guessant + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.exception; + +/** + * Exchange 2007 with Webdav disabled will trigger this exception. + */ +public class WebdavNotAvailableException extends DavMailException { + /** + * Create a DavMail exception with the given BundleMessage key and arguments. + * + * @param key message key + * @param arguments message values + */ + public WebdavNotAvailableException(String key, Object... arguments) { + super(key, arguments); + } +} diff --git a/src/java/davmail/exchange/ExchangeSessionFactory.java b/src/java/davmail/exchange/ExchangeSessionFactory.java index 74628f21..1d2870c6 100644 --- a/src/java/davmail/exchange/ExchangeSessionFactory.java +++ b/src/java/davmail/exchange/ExchangeSessionFactory.java @@ -22,6 +22,7 @@ import davmail.BundleMessage; import davmail.Settings; import davmail.exception.DavMailAuthenticationException; import davmail.exception.DavMailException; +import davmail.exception.WebdavNotAvailableException; import davmail.exchange.dav.DavExchangeSession; import davmail.exchange.ews.EwsExchangeSession; import davmail.http.DavGatewayHttpClientFacade; @@ -114,7 +115,16 @@ public final class ExchangeSessionFactory { if (Settings.getBooleanProperty("davmail.enableEws")) { session = new EwsExchangeSession(poolKey.url, poolKey.userName, poolKey.password); } else { - session = new DavExchangeSession(poolKey.url, poolKey.userName, poolKey.password); + try { + session = new DavExchangeSession(poolKey.url, poolKey.userName, poolKey.password); + } catch (WebdavNotAvailableException e) { + ExchangeSession.LOGGER.debug(e.getMessage()+", retry with EWS"); + session = new EwsExchangeSession(poolKey.url, poolKey.userName, poolKey.password); + // success, enable EWS flag + ExchangeSession.LOGGER.debug("EWS found, changing davmail.enableEws setting"); + Settings.setProperty("davmail.enableEws", "true"); + Settings.save(); + } } ExchangeSession.LOGGER.debug("Created new session: " + session); } diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index 590383e8..37d01f27 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -662,7 +662,7 @@ public class DavExchangeSession extends ExchangeSession { responses = DavGatewayHttpClientFacade.executePropFindMethod( httpClient, URIUtil.encodePath(mailPath), 0, WELL_KNOWN_FOLDERS); if (responses.length == 0) { - throw new DavMailException("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath); + throw new WebdavNotAvailableException("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath); } DavPropertySet properties = responses[0].getProperties(HttpStatus.SC_OK); inboxUrl = getURIPropertyIfExists(properties, "inbox"); @@ -728,7 +728,7 @@ public class DavExchangeSession extends ExchangeSession { ); } catch (IOException e) { LOGGER.error(e.getMessage()); - throw new DavMailAuthenticationException("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath); + throw new WebdavNotAvailableException("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath); } }