Improve exception handling

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@519 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-04-15 15:20:06 +00:00
parent 08cf32b5f3
commit bdb41fbc11
5 changed files with 41 additions and 59 deletions

View File

@ -27,6 +27,10 @@ import java.util.*;
* Handle a caldav connection.
*/
public class CaldavConnection extends AbstractConnection {
/**
* Maximum keep alive time in seconds
*/
protected static final int MAX_KEEP_ALIVE_TIME = 300;
protected final Logger wireLogger = Logger.getLogger(this.getClass());
protected boolean closed = false;
@ -86,8 +90,8 @@ public class CaldavConnection extends AbstractConnection {
} catch (NumberFormatException e) {
throw new IOException("Invalid Keep-Alive: " + keepAliveValue);
}
if (keepAlive > 300) {
keepAlive = 300;
if (keepAlive > MAX_KEEP_ALIVE_TIME) {
keepAlive = MAX_KEEP_ALIVE_TIME;
}
client.setSoTimeout(keepAlive * 1000);
DavGatewayTray.debug("Set socket timeout to " + keepAlive + " seconds");
@ -106,37 +110,31 @@ public class CaldavConnection extends AbstractConnection {
break;
}
tokens = new StringTokenizer(line);
if (tokens.hasMoreTokens()) {
String command = tokens.nextToken();
Map<String, String> headers = parseHeaders();
if (tokens.hasMoreTokens()) {
String path = URIUtil.decode(tokens.nextToken());
String content = getContent(headers.get("content-length"));
setSocketTimeout(headers.get("keep-alive"));
// client requested connection close
closed = "close".equals(headers.get("connection"));
if ("OPTIONS".equals(command)) {
sendOptions();
} else if (!headers.containsKey("authorization")) {
String command = tokens.nextToken();
Map<String, String> headers = parseHeaders();
String path = URIUtil.decode(tokens.nextToken());
String content = getContent(headers.get("content-length"));
setSocketTimeout(headers.get("keep-alive"));
// client requested connection close
closed = "close".equals(headers.get("connection"));
if ("OPTIONS".equals(command)) {
sendOptions();
} else if (!headers.containsKey("authorization")) {
sendUnauthorized();
} else {
decodeCredentials(headers.get("authorization"));
// authenticate only once
if (session == null) {
// first check network connectivity
ExchangeSessionFactory.checkConfig();
try {
session = ExchangeSessionFactory.getInstance(userName, password);
} catch (AuthenticationException e) {
sendUnauthorized();
} else {
decodeCredentials(headers.get("authorization"));
// authenticate only once
if (session == null) {
// first check network connectivity
ExchangeSessionFactory.checkConfig();
try {
session = ExchangeSessionFactory.getInstance(userName, password);
} catch (AuthenticationException e) {
sendUnauthorized();
}
}
if (session != null) {
handleRequest(command, path, headers, content);
}
}
} else {
sendErr(HttpStatus.SC_NOT_IMPLEMENTED, "Invalid URI");
}
if (session != null) {
handleRequest(command, path, headers, content);
}
}
@ -147,12 +145,8 @@ public class CaldavConnection extends AbstractConnection {
DavGatewayTray.debug("Closing connection on timeout");
} catch (SocketException e) {
DavGatewayTray.debug("Connection closed");
} catch (IOException e) {
if (e.getMessage() != null) {
DavGatewayTray.error(e.getMessage(), e);
} else {
DavGatewayTray.error(e);
}
} catch (Exception e) {
DavGatewayTray.error(e);
try {
sendErr(e);
} catch (IOException e2) {
@ -176,19 +170,9 @@ public class CaldavConnection extends AbstractConnection {
sendRoot(request);
} else if (request.isGet() && request.isRoot()) {
sendGetRoot();
// deprecated url, will be removed
} else if (request.isPath(1, "calendar")) {
StringBuilder message = new StringBuilder();
message.append("/calendar no longer supported, recreate calendar with /users/")
.append(session.getEmail()).append("/calendar");
DavGatewayTray.error(message.toString());
sendErr(HttpStatus.SC_BAD_REQUEST, message.toString());
// /principals/users namespace
} else if (request.isPath(1, "principals") && request.isPath(2, "users")) {
handleUserPrincipals(request);
// users root
} else if (request.isPath(1, "users")) {
// principal request
if (request.isPropFind() && request.isPathLength(3)) {
sendUserRoot(request);
} else {
@ -204,9 +188,7 @@ public class CaldavConnection extends AbstractConnection {
protected void handleUserPrincipals(CaldavRequest request) throws IOException {
if (request.isPropFind() && request.isPathLength(4)) {
sendPrincipal(request, request.getPathElement(3));
} else if (request.isPropFind() && request.isPathLength(4)) {
sendPrincipal(request, request.getPathElement(3));
// send back principal on search
// send back principal on search
} else if (request.isReport() && request.isPathLength(3)) {
sendPrincipal(request, session.getEmail());
} else {

View File

@ -371,7 +371,7 @@ public class LdapConnection extends AbstractConnection {
DavGatewayTray.debug("Connection closed");
} catch (SocketTimeoutException e) {
DavGatewayTray.debug("Closing connection on timeout");
} catch (IOException e) {
} catch (Exception e) {
DavGatewayTray.error(e);
try {
sendErr(0, LDAP_REP_BIND, e);

View File

@ -228,7 +228,7 @@ public class PopConnection extends AbstractConnection {
os.flush();
}
} catch (IOException e) {
} catch (Exception e) {
DavGatewayTray.error(e);
try {
sendERR(e.getMessage());

View File

@ -136,10 +136,10 @@ public class SmtpConnection extends AbstractConnection {
} catch (SocketException e) {
DavGatewayTray.debug("Connection closed");
} catch (IOException e) {
DavGatewayTray.error(e.getMessage());
} catch (Exception e) {
DavGatewayTray.error(e);
try {
sendClient("500 " + e.getMessage());
sendClient("500 " + ((e.getMessage()==null)?e:e.getMessage()));
} catch (IOException e2) {
DavGatewayTray.debug("Exception sending error to client", e2);
}

View File

@ -55,10 +55,7 @@ public class DavGatewayTray {
}
protected static void displayMessage(String message, Exception e, Priority priority) {
LOGGER.log(priority, message, e);
if (davGatewayTray != null
&& (!(e instanceof NetworkDownException) || isActive())) {
StringBuilder buffer = new StringBuilder();
StringBuilder buffer = new StringBuilder();
if (message != null) {
buffer.append(message).append(" ");
}
@ -67,6 +64,9 @@ public class DavGatewayTray {
} else {
buffer.append(e.toString());
}
LOGGER.log(priority, buffer.toString(), e);
if (davGatewayTray != null
&& (!(e instanceof NetworkDownException) || isActive())) {
davGatewayTray.displayMessage(buffer.toString(), priority);
}
if (davGatewayTray != null && e instanceof NetworkDownException) {