1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-08-13 16:53:51 -04:00

Another NTLM fix: activate NTLM only on 401 unauthorized in executeGetMethod

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@857 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-11-25 00:39:08 +00:00
parent bcda464f20
commit 849ba11a5e

View File

@ -25,6 +25,7 @@ import davmail.exception.HttpForbiddenException;
import davmail.exception.HttpNotFoundException;
import davmail.ui.tray.DavGatewayTray;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
@ -41,6 +42,9 @@ import org.apache.log4j.Logger;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Create HttpClient instance according to DavGateway Settings
@ -117,6 +121,12 @@ public final class DavGatewayHttpClientFacade {
httpClient.setHttpConnectionManager(multiThreadedHttpConnectionManager);
}
ArrayList<String> authPrefs = new ArrayList<String>();
authPrefs.add(AuthPolicy.DIGEST);
authPrefs.add(AuthPolicy.BASIC);
// exclude NTLM authentication scheme
httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy");
String proxyHost = null;
int proxyPort = 0;
@ -355,6 +365,19 @@ public final class DavGatewayHttpClientFacade {
return status;
}
private static boolean hasNTLM(HttpClient httpClient) {
Object authPrefs = httpClient.getParams().getParameter(AuthPolicy.AUTH_SCHEME_PRIORITY);
return authPrefs instanceof List<?> && ((Collection) authPrefs).contains(AuthPolicy.NTLM);
}
private static void addNTLM(HttpClient httpClient) {
ArrayList<String> authPrefs = new ArrayList<String>();
authPrefs.add(AuthPolicy.NTLM);
authPrefs.add(AuthPolicy.DIGEST);
authPrefs.add(AuthPolicy.BASIC);
httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
}
/**
* Execute Get method, do not follow redirects.
*
@ -367,6 +390,12 @@ public final class DavGatewayHttpClientFacade {
// do not follow redirects in expired sessions
method.setFollowRedirects(followRedirects);
int status = httpClient.executeMethod(method);
if (status == HttpStatus.SC_UNAUTHORIZED & !hasNTLM(httpClient)) {
method.releaseConnection();
LOGGER.debug("Received unauthorized at "+method.getURI()+", retrying with NTLM");
addNTLM(httpClient);
status = httpClient.executeMethod(method);
}
if (status != HttpStatus.SC_OK) {
LOGGER.warn("GET failed with status " + status + " at " + method.getURI() + ": " + method.getResponseBodyAsString());
throw new DavMailException("EXCEPTION_GET_FAILED", status, method.getURI());