mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-14 03:32:22 -05:00
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
This commit is contained in:
parent
98c52a5beb
commit
f3913ba8fc
@ -84,37 +84,7 @@ public final class Settings {
|
|||||||
isFirstStart = true;
|
isFirstStart = true;
|
||||||
|
|
||||||
// first start : set default values, ports above 1024 for unix/linux
|
// first start : set default values, ports above 1024 for unix/linux
|
||||||
SETTINGS.put("davmail.url", "http://exchangeServer/exchange/");
|
setDefaultSettings();
|
||||||
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", "");
|
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -131,6 +101,44 @@ public final class Settings {
|
|||||||
updateLoggingConfig();
|
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
|
* Return DavMail log file path
|
||||||
*
|
*
|
||||||
|
@ -138,8 +138,9 @@ public class ExchangeSession {
|
|||||||
private String alias;
|
private String alias;
|
||||||
private final HttpClient httpClient;
|
private final HttpClient httpClient;
|
||||||
|
|
||||||
private final ExchangeSessionFactory.PoolKey poolKey;
|
private final String userName;
|
||||||
|
private final String password;
|
||||||
|
|
||||||
private boolean disableGalLookup;
|
private boolean disableGalLookup;
|
||||||
private static final String YYYY_MM_DD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss";
|
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'";
|
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.
|
* 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
|
* @throws IOException on error
|
||||||
*/
|
*/
|
||||||
ExchangeSession(ExchangeSessionFactory.PoolKey poolKey) throws IOException {
|
ExchangeSession(String url, String userName, String password) throws IOException {
|
||||||
this.poolKey = poolKey;
|
this.userName = userName;
|
||||||
|
this.password = password;
|
||||||
try {
|
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
|
// avoid 401 roundtrips
|
||||||
httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
|
httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
|
||||||
|
|
||||||
// get webmail root url
|
// get webmail root url
|
||||||
// providing credentials
|
// providing credentials
|
||||||
// manually follow redirect
|
// manually follow redirect
|
||||||
HttpMethod method = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, poolKey.url);
|
HttpMethod method = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, url);
|
||||||
|
|
||||||
if (isBasicAuthentication) {
|
if (isBasicAuthentication) {
|
||||||
int status = method.getStatusCode();
|
int status = method.getStatusCode();
|
||||||
@ -187,7 +191,7 @@ public class ExchangeSession {
|
|||||||
throw DavGatewayHttpClientFacade.buildHttpException(method);
|
throw DavGatewayHttpClientFacade.buildHttpException(method);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
method = formLogin(httpClient, method, poolKey.userName, poolKey.password);
|
method = formLogin(httpClient, method, userName, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
buildMailPath(method);
|
buildMailPath(method);
|
||||||
@ -272,7 +276,7 @@ public class ExchangeSession {
|
|||||||
*/
|
*/
|
||||||
public boolean checkCredentials(String userName, String password) {
|
public boolean checkCredentials(String userName, String password) {
|
||||||
return userName != null && password != null
|
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();
|
String queryString = logonMethod.getQueryString();
|
||||||
if (queryString != null && queryString.contains("reason=2")) {
|
if (queryString != null && queryString.contains("reason=2")) {
|
||||||
logonMethod.releaseConnection();
|
logonMethod.releaseConnection();
|
||||||
if (poolKey.userName != null && poolKey.userName.contains("\\")) {
|
if (this.userName != null && this.userName.contains("\\")) {
|
||||||
throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED");
|
throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED");
|
||||||
} else {
|
} else {
|
||||||
throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED_RETRY");
|
throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED_RETRY");
|
||||||
@ -2416,12 +2420,12 @@ public class ExchangeSession {
|
|||||||
*/
|
*/
|
||||||
protected String getAliasFromLogin() {
|
protected String getAliasFromLogin() {
|
||||||
// Exchange 2007 : userName is login without domain
|
// Exchange 2007 : userName is login without domain
|
||||||
String userName = poolKey.userName;
|
String result = this.userName;
|
||||||
int index = userName.indexOf('\\');
|
int index = result.indexOf('\\');
|
||||||
if (index >= 0) {
|
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 exchangeZuluDateFormat = getExchangeZuluDateFormat();
|
||||||
SimpleDateFormat icalDateFormat = getZuluDateFormat();
|
SimpleDateFormat icalDateFormat = getZuluDateFormat();
|
||||||
|
|
||||||
String url;
|
String freebusyUrl;
|
||||||
Date startDate;
|
Date startDate;
|
||||||
Date endDate;
|
Date endDate;
|
||||||
try {
|
try {
|
||||||
@ -2888,7 +2892,7 @@ public class ExchangeSession {
|
|||||||
} else {
|
} else {
|
||||||
endDate = icalDateFormat.parse(endDateValue);
|
endDate = icalDateFormat.parse(endDateValue);
|
||||||
}
|
}
|
||||||
url = "/public/?cmd=freebusy" +
|
freebusyUrl = "/public/?cmd=freebusy" +
|
||||||
"&start=" + exchangeZuluDateFormat.format(startDate) +
|
"&start=" + exchangeZuluDateFormat.format(startDate) +
|
||||||
"&end=" + exchangeZuluDateFormat.format(endDate) +
|
"&end=" + exchangeZuluDateFormat.format(endDate) +
|
||||||
"&interval=" + FREE_BUSY_INTERVAL +
|
"&interval=" + FREE_BUSY_INTERVAL +
|
||||||
@ -2898,7 +2902,7 @@ public class ExchangeSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FreeBusy freeBusy = null;
|
FreeBusy freeBusy = null;
|
||||||
GetMethod getMethod = new GetMethod(url);
|
GetMethod getMethod = new GetMethod(freebusyUrl);
|
||||||
getMethod.setRequestHeader("Content-Type", "text/xml");
|
getMethod.setRequestHeader("Content-Type", "text/xml");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -103,7 +103,7 @@ public final class ExchangeSessionFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
session = new ExchangeSession(poolKey);
|
session = new ExchangeSession(poolKey.url, poolKey.userName, poolKey.password);
|
||||||
ExchangeSession.LOGGER.debug("Created new session: " + session);
|
ExchangeSession.LOGGER.debug("Created new session: " + session);
|
||||||
}
|
}
|
||||||
// successfull login, put session in cache
|
// successfull login, put session in cache
|
||||||
|
Loading…
Reference in New Issue
Block a user