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,6 +84,28 @@ 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
|
||||||
|
setDefaultSettings();
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_LOAD_SETTINGS"), e);
|
||||||
|
} finally {
|
||||||
|
if (fileInputStream != null) {
|
||||||
|
try {
|
||||||
|
fileInputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
DavGatewayTray.debug(new BundleMessage("LOG_ERROR_CLOGING_CONFIG_FILE"), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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.url", "http://exchangeServer/exchange/");
|
||||||
SETTINGS.put("davmail.popPort", "1110");
|
SETTINGS.put("davmail.popPort", "1110");
|
||||||
SETTINGS.put("davmail.imapPort", "1143");
|
SETTINGS.put("davmail.imapPort", "1143");
|
||||||
@ -115,20 +137,6 @@ public final class Settings {
|
|||||||
SETTINGS.put("log4j.logger.httpclient.wire", Level.WARN.toString());
|
SETTINGS.put("log4j.logger.httpclient.wire", Level.WARN.toString());
|
||||||
SETTINGS.put("log4j.logger.org.apache.commons.httpclient", Level.WARN.toString());
|
SETTINGS.put("log4j.logger.org.apache.commons.httpclient", Level.WARN.toString());
|
||||||
SETTINGS.put("davmail.logFilePath", "");
|
SETTINGS.put("davmail.logFilePath", "");
|
||||||
save();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_LOAD_SETTINGS"), e);
|
|
||||||
} finally {
|
|
||||||
if (fileInputStream != null) {
|
|
||||||
try {
|
|
||||||
fileInputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
DavGatewayTray.debug(new BundleMessage("LOG_ERROR_CLOGING_CONFIG_FILE"), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
updateLoggingConfig();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -138,7 +138,8 @@ 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";
|
||||||
@ -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