From d4cdc76721103285be6620558fdf6db92492ee05 Mon Sep 17 00:00:00 2001 From: mguessan Date: Mon, 7 Dec 2009 09:35:25 +0000 Subject: [PATCH] IMAP: Improve error handling, do not fail on message retrieval error, just send error message git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@870 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../exception/HttpServerErrorException.java | 35 +++++++++++++++++++ .../http/DavGatewayHttpClientFacade.java | 7 ++-- src/java/davmail/imap/ImapConnection.java | 15 ++++++-- src/java/davmailmessages.properties | 1 - 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/java/davmail/exception/HttpServerErrorException.java diff --git a/src/java/davmail/exception/HttpServerErrorException.java b/src/java/davmail/exception/HttpServerErrorException.java new file mode 100644 index 00000000..170b9839 --- /dev/null +++ b/src/java/davmail/exception/HttpServerErrorException.java @@ -0,0 +1,35 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2009 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.exception; + +import org.apache.commons.httpclient.HttpException; + +/** + * HttpException with 500 internal server error status. + */ +public class HttpServerErrorException extends HttpException { + /** + * HttpException with 500 internal server error status. + * + * @param message exception message + */ + public HttpServerErrorException(String message) { + super(message); + } +} diff --git a/src/java/davmail/http/DavGatewayHttpClientFacade.java b/src/java/davmail/http/DavGatewayHttpClientFacade.java index 58430ff6..3925415d 100644 --- a/src/java/davmail/http/DavGatewayHttpClientFacade.java +++ b/src/java/davmail/http/DavGatewayHttpClientFacade.java @@ -23,6 +23,7 @@ import davmail.Settings; import davmail.exception.DavMailException; import davmail.exception.HttpForbiddenException; import davmail.exception.HttpNotFoundException; +import davmail.exception.HttpServerErrorException; import davmail.ui.tray.DavGatewayTray; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.auth.AuthPolicy; @@ -389,14 +390,14 @@ public final class DavGatewayHttpClientFacade { } 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()); + throw DavGatewayHttpClientFacade.buildHttpException(method); } // check for expired session if (followRedirects) { String queryString = method.getQueryString(); if (queryString != null && queryString.contains("reason=2")) { LOGGER.warn("GET failed, session expired at " + method.getURI() + ": " + method.getResponseBodyAsString()); - throw new DavMailException("EXCEPTION_GET_FAILED", status, method.getURI()); + throw DavGatewayHttpClientFacade.buildHttpException(method); } } } @@ -424,6 +425,8 @@ public final class DavGatewayHttpClientFacade { return new HttpForbiddenException(message.toString()); } else if (status == HttpStatus.SC_NOT_FOUND) { return new HttpNotFoundException(message.toString()); + } else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { + return new HttpServerErrorException(message.toString()); } else { return new HttpException(message.toString()); } diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index 9aca3ca8..e8ef1e0e 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -257,7 +257,12 @@ public class ImapConnection extends AbstractConnection { while (uidRangeIterator.hasNext()) { DavGatewayTray.switchIcon(); ExchangeSession.Message message = uidRangeIterator.next(); - handleFetch(message, uidRangeIterator.currentIndex, parameters); + try { + handleFetch(message, uidRangeIterator.currentIndex, parameters); + } catch (IOException e) { + DavGatewayTray.log(e); + sendClient(commandId + " NO Unable to retrieve message: "+e.getMessage()); + } } sendClient(commandId + " OK UID FETCH completed"); } @@ -319,7 +324,13 @@ public class ImapConnection extends AbstractConnection { while (rangeIterator.hasNext()) { DavGatewayTray.switchIcon(); ExchangeSession.Message message = rangeIterator.next(); - handleFetch(message, rangeIterator.currentIndex, parameters); + try { + handleFetch(message, rangeIterator.currentIndex, parameters); + } catch (IOException e) { + DavGatewayTray.log(e); + sendClient(commandId + " NO Unable to retrieve message: "+e.getMessage()); + } + } sendClient(commandId + " OK FETCH completed"); } diff --git a/src/java/davmailmessages.properties b/src/java/davmailmessages.properties index df369e32..35f4433c 100644 --- a/src/java/davmailmessages.properties +++ b/src/java/davmailmessages.properties @@ -25,7 +25,6 @@ EXCEPTION_INVALID_REQUEST=Invalid request: {0} EXCEPTION_INVALID_SEARCH_PARAMETERS=Invalid search parameters: {0} EXCEPTION_NETWORK_DOWN=All network interfaces down or host unreachable ! EXCEPTION_UNABLE_TO_CREATE_MESSAGE=Unable to create message {0}: {1}{2}{3} -EXCEPTION_GET_FAILED=Get request failed with status {0} at {1} EXCEPTION_UNABLE_TO_GET_FOLDER=Unable to get folder at {0} EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER=Unable to get mail folder at {0} EXCEPTION_UNABLE_TO_GET_PROPERTY=Unable to get property {0}