Fix : Send 401 Unauthorized on authentication failure on Caldav connections instead of 500 internal server error to let client prompt user for the right password

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@239 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2008-12-19 13:01:14 +00:00
parent 34f85b6714
commit 0f7c9339c5
2 changed files with 14 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import davmail.exchange.ExchangeSessionFactory;
import davmail.tray.DavGatewayTray;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.auth.AuthenticationException;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
@ -109,7 +110,11 @@ public class CaldavConnection extends AbstractConnection {
if (session == null) {
// first check network connectivity
ExchangeSessionFactory.checkConfig();
try {
session = ExchangeSessionFactory.getInstance(userName, password);
} catch (AuthenticationException e) {
sendErr(HttpStatus.SC_UNAUTHORIZED, e.getMessage());
}
}
handleRequest(command, path, headers, content);
}

View File

@ -3,6 +3,7 @@ package davmail.exchange;
import davmail.Settings;
import davmail.http.DavGatewayHttpClientFacade;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthenticationException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
@ -316,8 +317,9 @@ public class ExchangeSession {
}
int status = method.getStatusCode();
// User may be authenticated, get various session information
if (status != HttpStatus.SC_OK) {
if (status == HttpStatus.SC_UNAUTHORIZED) {
throw new AuthenticationException("Authentication failed: invalid user or password");
} else if (status != HttpStatus.SC_OK) {
HttpException ex = new HttpException();
ex.setReasonCode(status);
ex.setReason(method.getStatusText());
@ -328,9 +330,9 @@ public class ExchangeSession {
if (queryString != null && queryString.contains("reason=2")) {
method.releaseConnection();
if (poolKey.userName != null && poolKey.userName.contains("\\")) {
throw new HttpException("Authentication failed: invalid user or password");
throw new AuthenticationException("Authentication failed: invalid user or password");
} else {
throw new HttpException("Authentication failed: invalid user or password, " +
throw new AuthenticationException("Authentication failed: invalid user or password, " +
"retry with domain\\user");
}
}
@ -349,6 +351,9 @@ public class ExchangeSession {
wdr.setPath(URIUtil.getPath(inboxUrl));
} catch (AuthenticationException exc) {
LOGGER.error(exc.toString());
throw exc;
} catch (IOException exc) {
StringBuffer message = new StringBuffer();
message.append("DavMail login exception: ");