Add custom cookie policy to support extended host name

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1335 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-08-13 19:58:51 +00:00
parent 7897bcc77f
commit e883fad25e
2 changed files with 64 additions and 8 deletions

View File

@ -26,6 +26,7 @@ import davmail.util.StringUtil;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
@ -68,9 +69,28 @@ public final class DavGatewayHttpClientFacade {
DavGatewayHttpClientFacade.start();
}
// register custom cookie policy
static {
CookiePolicy.registerCookieSpec("DavMailCookieSpec", DavMailCookieSpec.class);
}
private DavGatewayHttpClientFacade() {
}
/**
* Create basic http client with default params.
*
* @return HttpClient instance
*/
private static HttpClient getBaseInstance() {
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, IE_USER_AGENT);
httpClient.getParams().setParameter(HttpClientParams.MAX_REDIRECTS, MAX_REDIRECTS);
httpClient.getParams().setCookiePolicy("DavMailCookieSpec");
return httpClient;
}
/**
* Create a configured HttpClient instance.
*
@ -80,9 +100,7 @@ public final class DavGatewayHttpClientFacade {
*/
public static HttpClient getInstance(String url) throws DavMailException {
// create an HttpClient instance
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, IE_USER_AGENT);
httpClient.getParams().setParameter(HttpClientParams.MAX_REDIRECTS, MAX_REDIRECTS);
HttpClient httpClient = getBaseInstance();
configureClient(httpClient, url);
return httpClient;
}
@ -97,9 +115,7 @@ public final class DavGatewayHttpClientFacade {
* @throws DavMailException on error
*/
public static HttpClient getInstance(String url, String userName, String password) throws DavMailException {
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, IE_USER_AGENT);
httpClient.getParams().setParameter(HttpClientParams.MAX_REDIRECTS, MAX_REDIRECTS);
HttpClient httpClient = getBaseInstance();
configureClient(httpClient, url);
// some Exchange servers redirect to a different host for freebusy, use wide auth scope
AuthScope authScope = new AuthScope(null, -1);
@ -320,7 +336,7 @@ public final class DavGatewayHttpClientFacade {
* @param httpClient http client instance
* @param path <i>encoded</i> searched folder path
* @param searchRequest (SQL like) search request
* @param maxCount max item count
* @param maxCount max item count
* @return Responses enumeration
* @throws IOException on error
*/
@ -344,7 +360,7 @@ public final class DavGatewayHttpClientFacade {
};
searchMethod.setRequestEntity(new StringRequestEntity(searchBody, "text/xml", "UTF-8"));
if (maxCount > 0) {
searchMethod.addRequestHeader("Range", "rows=0-"+(maxCount-1));
searchMethod.addRequestHeader("Range", "rows=0-" + (maxCount - 1));
}
return executeMethod(httpClient, searchMethod);
}

View File

@ -0,0 +1,40 @@
/*
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
* Copyright (C) 2010 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.
*/
package davmail.http;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.cookie.MalformedCookieException;
import org.apache.commons.httpclient.cookie.RFC2109Spec;
/**
* Custom CookieSpec to allow extended domain names.
*/
public class DavMailCookieSpec extends RFC2109Spec {
public void validate(String host, int port, String path,
boolean secure, final Cookie cookie) throws MalformedCookieException {
String hostWithoutDomain = host.substring(0, host.length()
- cookie.getDomain().length());
if (hostWithoutDomain.indexOf('.') != -1) {
// discard additional host name part
super.validate(host.substring(hostWithoutDomain.indexOf('.')+1), port, path, secure, cookie);
} else {
super.validate(host, port, path, secure, cookie);
}
}
}