mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-13 19:22:22 -05:00
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:
parent
7897bcc77f
commit
e883fad25e
@ -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);
|
||||
|
40
src/java/davmail/http/DavMailCookieSpec.java
Normal file
40
src/java/davmail/http/DavMailCookieSpec.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user