mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-13 19:22:22 -05:00
Improve connection pool handling: do not pool simple checkConfig and getVersion connections.
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1596 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
6f01bd98fd
commit
13132245c4
@ -158,9 +158,12 @@ public abstract class ExchangeSession {
|
||||
public ExchangeSession(String url, String userName, String password) throws IOException {
|
||||
this.userName = userName;
|
||||
try {
|
||||
boolean isBasicAuthentication = isBasicAuthentication(url);
|
||||
httpClient = DavGatewayHttpClientFacade.getInstance(url);
|
||||
// set private connection pool
|
||||
DavGatewayHttpClientFacade.createMultiThreadedHttpConnectionManager(httpClient);
|
||||
boolean isBasicAuthentication = isBasicAuthentication(httpClient, url);
|
||||
|
||||
httpClient = DavGatewayHttpClientFacade.getInstance(url, userName, password);
|
||||
DavGatewayHttpClientFacade.setCredentials(httpClient, userName, password);
|
||||
|
||||
// get webmail root url
|
||||
// providing credentials
|
||||
@ -280,11 +283,12 @@ public abstract class ExchangeSession {
|
||||
* Test authentication mode : form based or basic.
|
||||
*
|
||||
* @param url exchange base URL
|
||||
* @param httpClient httpClient instance
|
||||
* @return true if basic authentication detected
|
||||
* @throws IOException unable to connect to exchange
|
||||
*/
|
||||
protected boolean isBasicAuthentication(String url) throws IOException {
|
||||
return DavGatewayHttpClientFacade.getHttpStatus(url) == HttpStatus.SC_UNAUTHORIZED;
|
||||
protected boolean isBasicAuthentication(HttpClient httpClient, String url) throws IOException {
|
||||
return DavGatewayHttpClientFacade.getHttpStatus(httpClient, url) == HttpStatus.SC_UNAUTHORIZED;
|
||||
}
|
||||
|
||||
protected String getAbsoluteUri(HttpMethod method, String path) throws URIException {
|
||||
|
@ -108,21 +108,16 @@ public final class DavGatewayHttpClientFacade {
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an HttpClient instance for the provided url and credentials.
|
||||
* Set credentials on HttpClient instance.
|
||||
*
|
||||
* @param url http(s) url
|
||||
* @param httpClient httpClient instance
|
||||
* @param userName user name
|
||||
* @param password user password
|
||||
* @return HttpClient instance
|
||||
* @throws DavMailException on error
|
||||
*/
|
||||
public static HttpClient getInstance(String url, String userName, String password) throws DavMailException {
|
||||
HttpClient httpClient = getBaseInstance();
|
||||
configureClient(httpClient, url);
|
||||
public static void setCredentials(HttpClient httpClient, String userName, String password) {
|
||||
// some Exchange servers redirect to a different host for freebusy, use wide auth scope
|
||||
AuthScope authScope = new AuthScope(null, -1);
|
||||
httpClient.getState().setCredentials(authScope, new NTCredentials(userName, password, "", ""));
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -152,8 +147,6 @@ public final class DavGatewayHttpClientFacade {
|
||||
public static void configureClient(HttpClient httpClient, String url) throws DavMailException {
|
||||
setClientHost(httpClient, url);
|
||||
|
||||
httpClient.setHttpConnectionManager(createConnectionManager());
|
||||
|
||||
if (!needNTLM) {
|
||||
ArrayList<String> authPrefs = new ArrayList<String>();
|
||||
authPrefs.add(AuthPolicy.DIGEST);
|
||||
@ -222,13 +215,13 @@ public final class DavGatewayHttpClientFacade {
|
||||
/**
|
||||
* Get Http Status code for the given URL
|
||||
*
|
||||
* @param httpClient httpClient instance
|
||||
* @param url url string
|
||||
* @return HttpStatus code
|
||||
* @throws IOException on error
|
||||
*/
|
||||
public static int getHttpStatus(String url) throws IOException {
|
||||
public static int getHttpStatus(HttpClient httpClient, String url) throws IOException {
|
||||
int status = 0;
|
||||
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance(url);
|
||||
HttpMethod testMethod = new GetMethod(url);
|
||||
testMethod.setDoAuthentication(false);
|
||||
try {
|
||||
@ -497,8 +490,7 @@ public final class DavGatewayHttpClientFacade {
|
||||
public static void addNTLM(HttpClient httpClient) {
|
||||
// disable preemptive authentication
|
||||
httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, false);
|
||||
// NTLM authentication uses persistent connections, use private connection manager
|
||||
httpClient.setHttpConnectionManager(createConnectionManager());
|
||||
|
||||
// register the jcifs based NTLMv2 implementation
|
||||
AuthPolicy.registerAuthScheme(AuthPolicy.NTLM, NTLMv2Scheme.class);
|
||||
|
||||
@ -674,14 +666,19 @@ public final class DavGatewayHttpClientFacade {
|
||||
}
|
||||
}
|
||||
|
||||
private static MultiThreadedHttpConnectionManager createConnectionManager() {
|
||||
/**
|
||||
* Create and set connection pool.
|
||||
*
|
||||
* @param httpClient httpClient instance
|
||||
*/
|
||||
public static void createMultiThreadedHttpConnectionManager(HttpClient httpClient) {
|
||||
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
|
||||
connectionManager.getParams().setDefaultMaxConnectionsPerHost(100);
|
||||
connectionManager.getParams().setConnectionTimeout(10000);
|
||||
synchronized (LOCK) {
|
||||
httpConnectionManagerThread.addConnectionManager(connectionManager);
|
||||
}
|
||||
return connectionManager;
|
||||
httpClient.setHttpConnectionManager(connectionManager);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user