2009-07-21 04:39:18 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2009 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.
|
|
|
|
*/
|
2008-11-03 20:47:10 -05:00
|
|
|
package davmail.http;
|
|
|
|
|
2009-04-23 10:54:06 -04:00
|
|
|
import davmail.BundleMessage;
|
2009-08-21 05:58:19 -04:00
|
|
|
import davmail.Settings;
|
2009-08-18 14:15:05 -04:00
|
|
|
import davmail.exception.DavMailException;
|
2009-09-25 05:13:02 -04:00
|
|
|
import davmail.exception.HttpForbiddenException;
|
|
|
|
import davmail.exception.HttpNotFoundException;
|
2009-04-03 03:34:45 -04:00
|
|
|
import davmail.ui.tray.DavGatewayTray;
|
2008-12-08 07:59:47 -05:00
|
|
|
import org.apache.commons.httpclient.*;
|
2009-04-01 11:51:12 -04:00
|
|
|
import org.apache.commons.httpclient.auth.AuthPolicy;
|
|
|
|
import org.apache.commons.httpclient.auth.AuthScope;
|
2009-03-18 13:26:33 -04:00
|
|
|
import org.apache.commons.httpclient.methods.DeleteMethod;
|
2009-04-01 11:51:12 -04:00
|
|
|
import org.apache.commons.httpclient.methods.GetMethod;
|
|
|
|
import org.apache.commons.httpclient.methods.StringRequestEntity;
|
2009-04-07 08:46:26 -04:00
|
|
|
import org.apache.commons.httpclient.params.HttpClientParams;
|
2009-08-21 05:58:19 -04:00
|
|
|
import org.apache.commons.httpclient.params.HttpMethodParams;
|
2009-04-01 11:51:12 -04:00
|
|
|
import org.apache.jackrabbit.webdav.DavException;
|
|
|
|
import org.apache.jackrabbit.webdav.MultiStatusResponse;
|
|
|
|
import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
|
|
|
|
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
|
2009-08-21 05:58:19 -04:00
|
|
|
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
|
2009-09-17 17:40:47 -04:00
|
|
|
import org.apache.log4j.Logger;
|
2008-11-03 20:47:10 -05:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2009-04-01 11:51:12 -04:00
|
|
|
import java.util.ArrayList;
|
2008-11-03 20:47:10 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create HttpClient instance according to DavGateway Settings
|
|
|
|
*/
|
2008-12-17 10:27:56 -05:00
|
|
|
public final class DavGatewayHttpClientFacade {
|
2009-09-17 17:40:47 -04:00
|
|
|
static final Logger LOGGER = Logger.getLogger("davmail.http.DavGatewayHttpClientFacade");
|
|
|
|
|
2009-04-16 18:20:30 -04:00
|
|
|
static final String IE_USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
|
|
|
|
static final int MAX_REDIRECTS = 10;
|
2009-09-18 05:27:00 -04:00
|
|
|
static final Object LOCK = new Object();
|
|
|
|
private static MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager;
|
2008-12-08 07:59:47 -05:00
|
|
|
|
2009-04-16 18:20:30 -04:00
|
|
|
static final long ONE_MINUTE = 60000;
|
2009-04-01 18:59:04 -04:00
|
|
|
|
2009-09-18 05:27:00 -04:00
|
|
|
private static Thread httpConnectionManagerThread;
|
2009-04-01 18:59:04 -04:00
|
|
|
|
2008-11-30 13:05:36 -05:00
|
|
|
static {
|
2008-12-08 08:49:21 -05:00
|
|
|
DavGatewayHttpClientFacade.start();
|
2008-11-30 13:05:36 -05:00
|
|
|
// force XML response with Internet Explorer header
|
|
|
|
System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
|
|
|
|
}
|
|
|
|
|
2008-12-02 05:20:46 -05:00
|
|
|
private DavGatewayHttpClientFacade() {
|
|
|
|
}
|
|
|
|
|
2008-11-03 20:47:10 -05:00
|
|
|
/**
|
|
|
|
* Create a configured HttpClient instance.
|
|
|
|
*
|
|
|
|
* @return httpClient
|
|
|
|
*/
|
|
|
|
public static HttpClient getInstance() {
|
|
|
|
// create an HttpClient instance
|
|
|
|
HttpClient httpClient = new HttpClient();
|
2009-04-01 11:51:12 -04:00
|
|
|
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, IE_USER_AGENT);
|
2009-04-07 08:46:26 -04:00
|
|
|
httpClient.getParams().setParameter(HttpClientParams.MAX_REDIRECTS, MAX_REDIRECTS);
|
2008-11-03 20:47:10 -05:00
|
|
|
configureClient(httpClient);
|
|
|
|
return httpClient;
|
|
|
|
}
|
|
|
|
|
2009-08-21 05:58:19 -04:00
|
|
|
/**
|
|
|
|
* Build an HttpClient instance for the provided url and credentials.
|
|
|
|
*
|
|
|
|
* @param url http(s) url
|
|
|
|
* @param userName user name
|
|
|
|
* @param password user password
|
|
|
|
* @return HttpClient instance
|
|
|
|
* @throws DavMailException on error
|
|
|
|
*/
|
2009-08-18 14:15:05 -04:00
|
|
|
public static HttpClient getInstance(String url, String userName, String password) throws DavMailException {
|
2009-03-18 13:26:33 -04:00
|
|
|
HttpClient httpClient = new HttpClient();
|
2009-04-01 11:51:12 -04:00
|
|
|
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, IE_USER_AGENT);
|
2009-04-07 08:46:26 -04:00
|
|
|
httpClient.getParams().setParameter(HttpClientParams.MAX_REDIRECTS, MAX_REDIRECTS);
|
2009-03-18 13:26:33 -04:00
|
|
|
HostConfiguration hostConfig = httpClient.getHostConfiguration();
|
2009-08-18 14:15:05 -04:00
|
|
|
try {
|
2009-08-21 05:58:19 -04:00
|
|
|
URI httpURI = new URI(url, true);
|
2009-08-18 14:15:05 -04:00
|
|
|
hostConfig.setHost(httpURI);
|
|
|
|
AuthScope authScope = new AuthScope(httpURI.getHost(), httpURI.getPort(), AuthScope.ANY_REALM);
|
|
|
|
httpClient.getState().setCredentials(authScope, new UsernamePasswordCredentials(userName, password));
|
|
|
|
} catch (URIException e) {
|
|
|
|
throw new DavMailException("LOG_INVALID_URL", url);
|
|
|
|
}
|
2009-03-18 13:26:33 -04:00
|
|
|
configureClient(httpClient);
|
|
|
|
return httpClient;
|
|
|
|
}
|
|
|
|
|
2008-11-03 20:47:10 -05:00
|
|
|
/**
|
|
|
|
* Update http client configuration (proxy)
|
|
|
|
*
|
|
|
|
* @param httpClient current Http client
|
|
|
|
*/
|
|
|
|
public static void configureClient(HttpClient httpClient) {
|
2009-09-18 05:27:00 -04:00
|
|
|
synchronized (LOCK) {
|
|
|
|
httpClient.setHttpConnectionManager(multiThreadedHttpConnectionManager);
|
|
|
|
}
|
2008-11-30 13:05:36 -05:00
|
|
|
|
2009-08-13 16:55:09 -04:00
|
|
|
ArrayList<String> authPrefs = new ArrayList<String>();
|
|
|
|
authPrefs.add(AuthPolicy.DIGEST);
|
|
|
|
authPrefs.add(AuthPolicy.BASIC);
|
2009-08-14 03:57:12 -04:00
|
|
|
// exclude the NTLM authentication scheme
|
2009-08-13 16:55:09 -04:00
|
|
|
httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
|
|
|
|
|
2008-11-03 20:47:10 -05:00
|
|
|
boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy");
|
|
|
|
String proxyHost = null;
|
|
|
|
int proxyPort = 0;
|
|
|
|
String proxyUser = null;
|
|
|
|
String proxyPassword = null;
|
|
|
|
|
2008-12-08 09:35:16 -05:00
|
|
|
if (enableProxy) {
|
2008-11-03 20:47:10 -05:00
|
|
|
proxyHost = Settings.getProperty("davmail.proxyHost");
|
|
|
|
proxyPort = Settings.getIntProperty("davmail.proxyPort");
|
|
|
|
proxyUser = Settings.getProperty("davmail.proxyUser");
|
|
|
|
proxyPassword = Settings.getProperty("davmail.proxyPassword");
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure proxy
|
|
|
|
if (proxyHost != null && proxyHost.length() > 0) {
|
|
|
|
httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
|
|
|
|
if (proxyUser != null && proxyUser.length() > 0) {
|
|
|
|
|
2009-04-01 11:51:12 -04:00
|
|
|
AuthScope authScope = new AuthScope(proxyHost, proxyPort, AuthScope.ANY_REALM);
|
|
|
|
|
2009-04-01 17:31:44 -04:00
|
|
|
// detect ntlm authentication (windows domain name in user name)
|
2009-04-16 17:16:40 -04:00
|
|
|
int backslashindex = proxyUser.indexOf('\\');
|
2008-11-03 20:47:10 -05:00
|
|
|
if (backslashindex > 0) {
|
2009-04-01 11:51:12 -04:00
|
|
|
httpClient.getState().setProxyCredentials(authScope,
|
2008-11-03 20:47:10 -05:00
|
|
|
new NTCredentials(proxyUser.substring(backslashindex + 1),
|
|
|
|
proxyPassword, null,
|
|
|
|
proxyUser.substring(0, backslashindex)));
|
|
|
|
} else {
|
2009-04-01 11:51:12 -04:00
|
|
|
httpClient.getState().setProxyCredentials(authScope,
|
2008-11-03 20:47:10 -05:00
|
|
|
new UsernamePasswordCredentials(proxyUser, proxyPassword));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Http Status code for the given URL
|
|
|
|
*
|
|
|
|
* @param url url string
|
|
|
|
* @return HttpStatus code
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public static int getHttpStatus(String url) throws IOException {
|
|
|
|
int status = 0;
|
|
|
|
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance();
|
|
|
|
HttpMethod testMethod = new GetMethod(url);
|
2008-11-04 05:37:36 -05:00
|
|
|
testMethod.setDoAuthentication(false);
|
2008-11-03 20:47:10 -05:00
|
|
|
try {
|
|
|
|
status = httpClient.executeMethod(testMethod);
|
|
|
|
} finally {
|
|
|
|
testMethod.releaseConnection();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2009-08-21 05:58:19 -04:00
|
|
|
/**
|
|
|
|
* Check if status is a redirect (various 30x values).
|
|
|
|
*
|
|
|
|
* @param status Http status
|
|
|
|
* @return true if status is a redirect
|
|
|
|
*/
|
2008-11-03 20:47:10 -05:00
|
|
|
public static boolean isRedirect(int status) {
|
|
|
|
return status == HttpStatus.SC_MOVED_PERMANENTLY
|
|
|
|
|| status == HttpStatus.SC_MOVED_TEMPORARILY
|
|
|
|
|| status == HttpStatus.SC_SEE_OTHER
|
|
|
|
|| status == HttpStatus.SC_TEMPORARY_REDIRECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute given url, manually follow redirects.
|
|
|
|
* Workaround for HttpClient bug (GET full URL over HTTPS and proxy)
|
|
|
|
*
|
|
|
|
* @param httpClient HttpClient instance
|
|
|
|
* @param url url string
|
|
|
|
* @return executed method
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public static HttpMethod executeFollowRedirects(HttpClient httpClient, String url) throws IOException {
|
|
|
|
HttpMethod method = new GetMethod(url);
|
2008-12-01 07:38:49 -05:00
|
|
|
method.setFollowRedirects(false);
|
|
|
|
return executeFollowRedirects(httpClient, method);
|
|
|
|
}
|
|
|
|
|
2009-08-21 05:58:19 -04:00
|
|
|
/**
|
|
|
|
* Execute method with httpClient, follow 30x redirects.
|
|
|
|
*
|
|
|
|
* @param httpClient Http client instance
|
|
|
|
* @param method Http method
|
|
|
|
* @return last http method after redirects
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2008-12-01 07:38:49 -05:00
|
|
|
public static HttpMethod executeFollowRedirects(HttpClient httpClient, HttpMethod method) throws IOException {
|
2009-04-16 17:52:17 -04:00
|
|
|
HttpMethod currentMethod = method;
|
2008-11-03 20:47:10 -05:00
|
|
|
try {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_EXECUTE_FOLLOW_REDIRECTS", currentMethod.getURI()));
|
2009-04-16 17:52:17 -04:00
|
|
|
httpClient.executeMethod(currentMethod);
|
|
|
|
Header location = currentMethod.getResponseHeader("Location");
|
2008-11-03 20:47:10 -05:00
|
|
|
int redirectCount = 0;
|
|
|
|
while (redirectCount++ < 10
|
|
|
|
&& location != null
|
2009-04-16 17:52:17 -04:00
|
|
|
&& isRedirect(currentMethod.getStatusCode())) {
|
|
|
|
currentMethod.releaseConnection();
|
|
|
|
currentMethod = new GetMethod(location.getValue());
|
|
|
|
currentMethod.setFollowRedirects(false);
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_EXECUTE_FOLLOW_REDIRECTS_COUNT", currentMethod.getURI(), redirectCount));
|
2009-04-16 17:52:17 -04:00
|
|
|
httpClient.executeMethod(currentMethod);
|
|
|
|
location = currentMethod.getResponseHeader("Location");
|
2008-11-03 20:47:10 -05:00
|
|
|
}
|
2009-04-16 17:52:17 -04:00
|
|
|
if (location != null && isRedirect(currentMethod.getStatusCode())) {
|
|
|
|
currentMethod.releaseConnection();
|
2008-11-03 20:47:10 -05:00
|
|
|
throw new HttpException("Maximum redirections reached");
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2009-04-16 17:52:17 -04:00
|
|
|
currentMethod.releaseConnection();
|
2008-11-03 20:47:10 -05:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
// caller will need to release connection
|
2009-04-16 17:52:17 -04:00
|
|
|
return currentMethod;
|
2008-11-03 20:47:10 -05:00
|
|
|
}
|
2008-12-08 08:49:21 -05:00
|
|
|
|
2008-12-17 10:27:56 -05:00
|
|
|
/**
|
2009-03-18 13:26:33 -04:00
|
|
|
* Execute webdav search method.
|
|
|
|
*
|
|
|
|
* @param httpClient http client instance
|
|
|
|
* @param path <i>encoded</i> searched folder path
|
2008-12-17 10:27:56 -05:00
|
|
|
* @param searchRequest (SQL like) search request
|
2009-03-18 13:26:33 -04:00
|
|
|
* @return Responses enumeration
|
2008-12-17 10:27:56 -05:00
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2009-04-01 11:51:12 -04:00
|
|
|
public static MultiStatusResponse[] executeSearchMethod(HttpClient httpClient, String path, String searchRequest) throws IOException {
|
2008-12-17 10:27:56 -05:00
|
|
|
String searchBody = "<?xml version=\"1.0\"?>\n" +
|
|
|
|
"<d:searchrequest xmlns:d=\"DAV:\">\n" +
|
2009-03-18 18:19:28 -04:00
|
|
|
" <d:sql>" + searchRequest.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">") + "</d:sql>\n" +
|
2008-12-17 10:27:56 -05:00
|
|
|
"</d:searchrequest>";
|
2009-04-01 11:51:12 -04:00
|
|
|
DavMethodBase searchMethod = new DavMethodBase(path) {
|
|
|
|
|
2009-08-11 12:55:35 -04:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
2009-04-01 11:51:12 -04:00
|
|
|
return "SEARCH";
|
|
|
|
}
|
|
|
|
|
2009-08-11 12:55:35 -04:00
|
|
|
@Override
|
|
|
|
protected boolean isSuccess(int statusCode) {
|
2009-04-01 11:51:12 -04:00
|
|
|
return statusCode == 207;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
searchMethod.setRequestEntity(new StringRequestEntity(searchBody, "text/xml", "UTF-8"));
|
2009-03-18 13:26:33 -04:00
|
|
|
return executeMethod(httpClient, searchMethod);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute webdav propfind method.
|
|
|
|
*
|
|
|
|
* @param httpClient http client instance
|
|
|
|
* @param path <i>encoded</i> searched folder path
|
|
|
|
* @param depth propfind request depth
|
|
|
|
* @param properties propfind requested properties
|
|
|
|
* @return Responses enumeration
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2009-04-01 11:51:12 -04:00
|
|
|
public static MultiStatusResponse[] executePropFindMethod(HttpClient httpClient, String path, int depth, DavPropertyNameSet properties) throws IOException {
|
|
|
|
PropFindMethod propFindMethod = new PropFindMethod(path, properties, depth);
|
2009-03-18 13:26:33 -04:00
|
|
|
return executeMethod(httpClient, propFindMethod);
|
|
|
|
}
|
|
|
|
|
2009-08-21 05:58:19 -04:00
|
|
|
/**
|
|
|
|
* Execute a delete method on the given path with httpClient.
|
|
|
|
*
|
|
|
|
* @param httpClient Http client instance
|
|
|
|
* @param path Path to be deleted
|
|
|
|
* @return Http status
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2009-03-18 13:26:33 -04:00
|
|
|
public static int executeDeleteMethod(HttpClient httpClient, String path) throws IOException {
|
|
|
|
DeleteMethod deleteMethod = new DeleteMethod(path);
|
2009-09-17 17:40:47 -04:00
|
|
|
deleteMethod.setFollowRedirects(false);
|
2009-03-18 13:26:33 -04:00
|
|
|
|
|
|
|
int status = executeHttpMethod(httpClient, deleteMethod);
|
|
|
|
// do not throw error if already deleted
|
|
|
|
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_NOT_FOUND) {
|
|
|
|
throw DavGatewayHttpClientFacade.buildHttpException(deleteMethod);
|
|
|
|
}
|
2009-03-18 18:24:25 -04:00
|
|
|
return HttpStatus.SC_OK;
|
2009-03-18 13:26:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute webdav request.
|
|
|
|
*
|
|
|
|
* @param httpClient http client instance
|
|
|
|
* @param method webdav method
|
|
|
|
* @return Responses enumeration
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2009-04-01 11:51:12 -04:00
|
|
|
public static MultiStatusResponse[] executeMethod(HttpClient httpClient, DavMethodBase method) throws IOException {
|
|
|
|
MultiStatusResponse[] responses = null;
|
2008-12-17 10:27:56 -05:00
|
|
|
try {
|
2009-03-18 13:26:33 -04:00
|
|
|
int status = httpClient.executeMethod(method);
|
|
|
|
|
|
|
|
if (status != HttpStatus.SC_MULTI_STATUS) {
|
|
|
|
throw buildHttpException(method);
|
2008-12-17 10:27:56 -05:00
|
|
|
}
|
2009-04-01 11:51:12 -04:00
|
|
|
responses = method.getResponseBodyAsMultiStatus().getResponses();
|
2009-03-18 13:26:33 -04:00
|
|
|
|
2009-04-01 11:51:12 -04:00
|
|
|
} catch (DavException e) {
|
|
|
|
throw new IOException(e.getMessage());
|
2009-03-18 13:26:33 -04:00
|
|
|
} finally {
|
|
|
|
method.releaseConnection();
|
|
|
|
}
|
2009-04-01 11:51:12 -04:00
|
|
|
return responses;
|
2009-03-18 13:26:33 -04:00
|
|
|
}
|
2008-12-17 10:27:56 -05:00
|
|
|
|
2009-08-21 05:58:19 -04:00
|
|
|
/**
|
|
|
|
* Execute method with httpClient.
|
|
|
|
*
|
|
|
|
* @param httpClient Http client instance
|
|
|
|
* @param method Http method
|
|
|
|
* @return Http status
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2009-03-18 13:26:33 -04:00
|
|
|
public static int executeHttpMethod(HttpClient httpClient, HttpMethod method) throws IOException {
|
|
|
|
int status = 0;
|
|
|
|
try {
|
|
|
|
status = httpClient.executeMethod(method);
|
2008-12-17 10:27:56 -05:00
|
|
|
} finally {
|
2009-03-18 13:26:33 -04:00
|
|
|
method.releaseConnection();
|
2008-12-17 10:27:56 -05:00
|
|
|
}
|
2009-03-18 13:26:33 -04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2009-09-17 17:40:47 -04:00
|
|
|
/**
|
|
|
|
* Execute Get method, do not follow redirects.
|
|
|
|
*
|
|
|
|
* @param httpClient Http client instance
|
|
|
|
* @param method Http method
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public static void executeGetMethod(HttpClient httpClient, GetMethod method) throws IOException {
|
|
|
|
// do not follow redirects in expired sessions
|
|
|
|
method.setFollowRedirects(false);
|
|
|
|
int status = httpClient.executeMethod(method);
|
|
|
|
if (status != HttpStatus.SC_OK) {
|
2009-09-18 05:27:00 -04:00
|
|
|
LOGGER.warn("GET failed with status " + status + " at " + method.getURI() + ": " + method.getResponseBodyAsString());
|
2009-09-17 17:40:47 -04:00
|
|
|
throw new DavMailException("EXCEPTION_GET_FAILED", status, method.getURI());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-18 13:26:33 -04:00
|
|
|
/**
|
|
|
|
* Build Http Exception from methode status
|
|
|
|
*
|
|
|
|
* @param method Http Method
|
|
|
|
* @return Http Exception
|
|
|
|
*/
|
|
|
|
public static HttpException buildHttpException(HttpMethod method) {
|
2009-08-11 12:55:35 -04:00
|
|
|
int status = method.getStatusCode();
|
|
|
|
// 440 means forbidden on Exchange
|
|
|
|
if (status == 440) {
|
2009-09-21 17:50:47 -04:00
|
|
|
status = HttpStatus.SC_FORBIDDEN;
|
2009-08-11 12:55:35 -04:00
|
|
|
}
|
2009-09-21 17:50:47 -04:00
|
|
|
StringBuilder message = new StringBuilder();
|
|
|
|
message.append(status).append(' ').append(method.getStatusText());
|
|
|
|
try {
|
|
|
|
message.append(" at ").append(method.getURI().getURI());
|
|
|
|
} catch (URIException e) {
|
|
|
|
message.append(method.getPath());
|
|
|
|
}
|
2009-09-25 05:13:02 -04:00
|
|
|
if (status == HttpStatus.SC_FORBIDDEN) {
|
|
|
|
return new HttpForbiddenException(message.toString());
|
|
|
|
} else if (status == HttpStatus.SC_NOT_FOUND) {
|
|
|
|
return new HttpNotFoundException(message.toString());
|
|
|
|
} else {
|
|
|
|
return new HttpException(message.toString());
|
|
|
|
}
|
2008-12-17 10:27:56 -05:00
|
|
|
}
|
|
|
|
|
2009-08-21 05:58:19 -04:00
|
|
|
/**
|
|
|
|
* Stop HttpConnectionManager.
|
|
|
|
*/
|
2008-12-08 08:49:21 -05:00
|
|
|
public static void stop() {
|
2009-09-18 05:27:00 -04:00
|
|
|
synchronized (LOCK) {
|
|
|
|
if (multiThreadedHttpConnectionManager != null) {
|
|
|
|
if (httpConnectionManagerThread != null) {
|
|
|
|
httpConnectionManagerThread.interrupt();
|
|
|
|
}
|
|
|
|
multiThreadedHttpConnectionManager.shutdown();
|
|
|
|
multiThreadedHttpConnectionManager = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class HttpConnectionManagerThread extends Thread {
|
|
|
|
HttpConnectionManagerThread() {
|
|
|
|
super(HttpConnectionManager.class.getSimpleName());
|
|
|
|
setDaemon(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
boolean terminated = false;
|
|
|
|
while (!terminated) {
|
|
|
|
try {
|
|
|
|
sleep(ONE_MINUTE);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
terminated = true;
|
|
|
|
}
|
|
|
|
synchronized (LOCK) {
|
|
|
|
if (multiThreadedHttpConnectionManager != null) {
|
|
|
|
multiThreadedHttpConnectionManager.closeIdleConnections(ONE_MINUTE);
|
|
|
|
}
|
|
|
|
}
|
2009-04-01 18:59:04 -04:00
|
|
|
}
|
2008-12-08 08:49:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-21 05:58:19 -04:00
|
|
|
/**
|
2009-09-17 05:22:19 -04:00
|
|
|
* Create and start a new HttpConnectionManager, close idle connections every minute.
|
2009-08-21 05:58:19 -04:00
|
|
|
*/
|
2008-12-08 08:49:21 -05:00
|
|
|
public static void start() {
|
2009-09-18 05:27:00 -04:00
|
|
|
synchronized (LOCK) {
|
|
|
|
if (multiThreadedHttpConnectionManager == null) {
|
|
|
|
multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
|
|
|
|
multiThreadedHttpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(100);
|
|
|
|
httpConnectionManagerThread = new HttpConnectionManagerThread();
|
|
|
|
httpConnectionManagerThread.start();
|
|
|
|
}
|
2008-12-08 08:49:21 -05:00
|
|
|
}
|
|
|
|
}
|
2008-11-03 20:47:10 -05:00
|
|
|
}
|