From f3913ba8fc05a9ec5537f255c9e6c22099cc00aa Mon Sep 17 00:00:00 2001 From: mguessan Date: Wed, 30 Sep 2009 21:54:53 +0000 Subject: [PATCH] Refactor ExchangeSession to allow independent session creation. git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@763 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/Settings.java | 70 +++++++++++-------- .../davmail/exchange/ExchangeSession.java | 42 ++++++----- .../exchange/ExchangeSessionFactory.java | 2 +- 3 files changed, 63 insertions(+), 51 deletions(-) diff --git a/src/java/davmail/Settings.java b/src/java/davmail/Settings.java index 823477bd..dd21c79b 100644 --- a/src/java/davmail/Settings.java +++ b/src/java/davmail/Settings.java @@ -84,37 +84,7 @@ public final class Settings { isFirstStart = true; // first start : set default values, ports above 1024 for unix/linux - SETTINGS.put("davmail.url", "http://exchangeServer/exchange/"); - SETTINGS.put("davmail.popPort", "1110"); - SETTINGS.put("davmail.imapPort", "1143"); - SETTINGS.put("davmail.smtpPort", "1025"); - SETTINGS.put("davmail.caldavPort", "1080"); - SETTINGS.put("davmail.ldapPort", "1389"); - SETTINGS.put("davmail.keepDelay", "30"); - SETTINGS.put("davmail.sentKeepDelay", "90"); - SETTINGS.put("davmail.caldavPastDelay", "90"); - SETTINGS.put("davmail.allowRemote", Boolean.FALSE.toString()); - SETTINGS.put("davmail.bindAddress", ""); - SETTINGS.put("davmail.enableProxy", Boolean.FALSE.toString()); - SETTINGS.put("davmail.proxyHost", ""); - SETTINGS.put("davmail.proxyPort", ""); - SETTINGS.put("davmail.proxyUser", ""); - SETTINGS.put("davmail.proxyPassword", ""); - SETTINGS.put("davmail.server", Boolean.FALSE.toString()); - SETTINGS.put("davmail.server.certificate.hash", ""); - SETTINGS.put("davmail.ssl.keystoreType", ""); - SETTINGS.put("davmail.ssl.keystoreFile", ""); - SETTINGS.put("davmail.ssl.keystorePass", ""); - SETTINGS.put("davmail.ssl.keyPass", ""); - SETTINGS.put("davmail.ssl.pkcs11Library", ""); - SETTINGS.put("davmail.ssl.pkcs11Config", ""); - - // logging - SETTINGS.put("log4j.rootLogger", Level.WARN.toString()); - SETTINGS.put("log4j.logger.davmail", Level.DEBUG.toString()); - SETTINGS.put("log4j.logger.httpclient.wire", Level.WARN.toString()); - SETTINGS.put("log4j.logger.org.apache.commons.httpclient", Level.WARN.toString()); - SETTINGS.put("davmail.logFilePath", ""); + setDefaultSettings(); save(); } } catch (IOException e) { @@ -131,6 +101,44 @@ public final class Settings { updateLoggingConfig(); } + /** + * Set all settings to default values. + * Ports above 1024 for unix/linux + */ + public static void setDefaultSettings() { + SETTINGS.put("davmail.url", "http://exchangeServer/exchange/"); + SETTINGS.put("davmail.popPort", "1110"); + SETTINGS.put("davmail.imapPort", "1143"); + SETTINGS.put("davmail.smtpPort", "1025"); + SETTINGS.put("davmail.caldavPort", "1080"); + SETTINGS.put("davmail.ldapPort", "1389"); + SETTINGS.put("davmail.keepDelay", "30"); + SETTINGS.put("davmail.sentKeepDelay", "90"); + SETTINGS.put("davmail.caldavPastDelay", "90"); + SETTINGS.put("davmail.allowRemote", Boolean.FALSE.toString()); + SETTINGS.put("davmail.bindAddress", ""); + SETTINGS.put("davmail.enableProxy", Boolean.FALSE.toString()); + SETTINGS.put("davmail.proxyHost", ""); + SETTINGS.put("davmail.proxyPort", ""); + SETTINGS.put("davmail.proxyUser", ""); + SETTINGS.put("davmail.proxyPassword", ""); + SETTINGS.put("davmail.server", Boolean.FALSE.toString()); + SETTINGS.put("davmail.server.certificate.hash", ""); + SETTINGS.put("davmail.ssl.keystoreType", ""); + SETTINGS.put("davmail.ssl.keystoreFile", ""); + SETTINGS.put("davmail.ssl.keystorePass", ""); + SETTINGS.put("davmail.ssl.keyPass", ""); + SETTINGS.put("davmail.ssl.pkcs11Library", ""); + SETTINGS.put("davmail.ssl.pkcs11Config", ""); + + // logging + SETTINGS.put("log4j.rootLogger", Level.WARN.toString()); + SETTINGS.put("log4j.logger.davmail", Level.DEBUG.toString()); + SETTINGS.put("log4j.logger.httpclient.wire", Level.WARN.toString()); + SETTINGS.put("log4j.logger.org.apache.commons.httpclient", Level.WARN.toString()); + SETTINGS.put("davmail.logFilePath", ""); + } + /** * Return DavMail log file path * diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 39e8c3ed..19e1547e 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -138,8 +138,9 @@ public class ExchangeSession { private String alias; private final HttpClient httpClient; - private final ExchangeSessionFactory.PoolKey poolKey; - + private final String userName; + private final String password; + private boolean disableGalLookup; private static final String YYYY_MM_DD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss"; private static final String YYYYMMDD_T_HHMMSS_Z = "yyyyMMdd'T'HHmmss'Z'"; @@ -157,24 +158,27 @@ public class ExchangeSession { /** * Create an exchange session for the given URL. - * The session is not actually established until a call to login() + * The session is established for given userName and password * - * @param poolKey session pool key + * @param url Exchange url + * @param userName user login name + * @param password user password * @throws IOException on error */ - ExchangeSession(ExchangeSessionFactory.PoolKey poolKey) throws IOException { - this.poolKey = poolKey; + ExchangeSession(String url, String userName, String password) throws IOException { + this.userName = userName; + this.password = password; try { - boolean isBasicAuthentication = isBasicAuthentication(poolKey.url); + boolean isBasicAuthentication = isBasicAuthentication(url); - httpClient = DavGatewayHttpClientFacade.getInstance(poolKey.url, poolKey.userName, poolKey.password); + httpClient = DavGatewayHttpClientFacade.getInstance(url, userName, password); // avoid 401 roundtrips httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true); // get webmail root url // providing credentials // manually follow redirect - HttpMethod method = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, poolKey.url); + HttpMethod method = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, url); if (isBasicAuthentication) { int status = method.getStatusCode(); @@ -187,7 +191,7 @@ public class ExchangeSession { throw DavGatewayHttpClientFacade.buildHttpException(method); } } else { - method = formLogin(httpClient, method, poolKey.userName, poolKey.password); + method = formLogin(httpClient, method, userName, password); } buildMailPath(method); @@ -272,7 +276,7 @@ public class ExchangeSession { */ public boolean checkCredentials(String userName, String password) { return userName != null && password != null - && userName.equals(poolKey.userName) && password.equals(poolKey.password); + && userName.equals(this.userName) && password.equals(this.password); } /** @@ -467,7 +471,7 @@ public class ExchangeSession { String queryString = logonMethod.getQueryString(); if (queryString != null && queryString.contains("reason=2")) { logonMethod.releaseConnection(); - if (poolKey.userName != null && poolKey.userName.contains("\\")) { + if (this.userName != null && this.userName.contains("\\")) { throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED"); } else { throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED_RETRY"); @@ -2416,12 +2420,12 @@ public class ExchangeSession { */ protected String getAliasFromLogin() { // Exchange 2007 : userName is login without domain - String userName = poolKey.userName; - int index = userName.indexOf('\\'); + String result = this.userName; + int index = result.indexOf('\\'); if (index >= 0) { - userName = userName.substring(index + 1); + result = result.substring(index + 1); } - return userName; + return result; } /** @@ -2874,7 +2878,7 @@ public class ExchangeSession { SimpleDateFormat exchangeZuluDateFormat = getExchangeZuluDateFormat(); SimpleDateFormat icalDateFormat = getZuluDateFormat(); - String url; + String freebusyUrl; Date startDate; Date endDate; try { @@ -2888,7 +2892,7 @@ public class ExchangeSession { } else { endDate = icalDateFormat.parse(endDateValue); } - url = "/public/?cmd=freebusy" + + freebusyUrl = "/public/?cmd=freebusy" + "&start=" + exchangeZuluDateFormat.format(startDate) + "&end=" + exchangeZuluDateFormat.format(endDate) + "&interval=" + FREE_BUSY_INTERVAL + @@ -2898,7 +2902,7 @@ public class ExchangeSession { } FreeBusy freeBusy = null; - GetMethod getMethod = new GetMethod(url); + GetMethod getMethod = new GetMethod(freebusyUrl); getMethod.setRequestHeader("Content-Type", "text/xml"); try { diff --git a/src/java/davmail/exchange/ExchangeSessionFactory.java b/src/java/davmail/exchange/ExchangeSessionFactory.java index 6f795bd9..3db177e2 100644 --- a/src/java/davmail/exchange/ExchangeSessionFactory.java +++ b/src/java/davmail/exchange/ExchangeSessionFactory.java @@ -103,7 +103,7 @@ public final class ExchangeSessionFactory { } if (session == null) { - session = new ExchangeSession(poolKey); + session = new ExchangeSession(poolKey.url, poolKey.userName, poolKey.password); ExchangeSession.LOGGER.debug("Created new session: " + session); } // successfull login, put session in cache