Dav: refactor getContentReader and fix regression on null date value

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1252 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-07-22 11:54:18 +00:00
parent bc4e2fd539
commit 87aa40b7d1
3 changed files with 55 additions and 89 deletions

View File

@ -518,15 +518,6 @@ public abstract class ExchangeSession {
*/
public abstract void sendMessage(byte[] messageBody) throws IOException;
/**
* Create message MIME body reader.
*
* @param message Exchange message
* @return message body reader
* @throws IOException on error
*/
protected abstract BufferedReader getContentReader(Message message) throws IOException;
/**
* Get raw MIME message content
*
@ -2615,7 +2606,6 @@ public abstract class ExchangeSession {
*
* @param folderPath Exchange folder path
* @param itemName item name
* @return HTTP status
* @throws IOException on error
*/
public abstract void deleteItem(String folderPath, String itemName) throws IOException;

View File

@ -1510,7 +1510,7 @@ public class DavExchangeSession extends ExchangeSession {
PutMethod putmethod = new PutMethod(messageUrl);
putmethod.setRequestHeader("Translate", "f");
putmethod.setRequestHeader("Content-Type", "message/rfc822");
try {
// use same encoding as client socket reader
putmethod.setRequestEntity(new ByteArrayRequestEntity(messageBody));
@ -1583,62 +1583,55 @@ public class DavExchangeSession extends ExchangeSession {
@Override
protected byte[] getContent(Message message) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedReader reader = getContentReader(message);
InputStream contentInputStream;
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos);
String line;
boolean first = true;
while ((line = reader.readLine()) != null) {
if (first) {
first = false;
} else {
outputStreamWriter.write((char) 13);
outputStreamWriter.write((char) 10);
}
outputStreamWriter.write(line);
}
outputStreamWriter.flush();
} finally {
try {
reader.close();
} catch (IOException e) {
LOGGER.warn("Error closing message input stream", e);
contentInputStream = getContentInputStream(message.messageUrl);
} catch (HttpNotFoundException e) {
LOGGER.debug("Message not found at: " + message.messageUrl + ", retrying with permanenturl");
contentInputStream = getContentInputStream(message.permanentUrl);
}
} catch (HttpException e) {
// other exception
if (Settings.getBooleanProperty("davmail.deleteBroken")) {
LOGGER.warn("Deleting broken message at: " + message.messageUrl + " permanentUrl: " + message.permanentUrl);
try {
message.delete();
} catch (IOException ioe) {
LOGGER.warn("Unable to delete broken message at: " + message.permanentUrl);
}
}
throw e;
}
try {
IOUtil.write(contentInputStream, baos);
} finally {
contentInputStream.close();
}
return baos.toByteArray();
}
/**
* @inheritDoc
*/
@Override
protected BufferedReader getContentReader(Message message) throws IOException {
BufferedReader reader;
try {
reader = getContentReader(message, message.messageUrl);
} catch (HttpNotFoundException e) {
LOGGER.debug("Message not found at: " + message.messageUrl + ", retrying with permanenturl");
reader = getContentReader(message, message.permanentUrl);
}
return reader;
}
protected BufferedReader getContentReader(Message message, String url) throws IOException {
protected InputStream getContentInputStream(String url) throws IOException {
final GetMethod method = new GetMethod(URIUtil.encodePath(url));
method.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
method.setRequestHeader("Translate", "f");
method.setRequestHeader("Accept-Encoding", "gzip");
BufferedReader reader;
InputStream inputStream;
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true);
InputStreamReader inputStreamReader;
if (isGzipEncoded(method)) {
inputStreamReader = new InputStreamReader(new GZIPInputStream(method.getResponseBodyAsStream()));
inputStream = new GZIPInputStream(method.getResponseBodyAsStream());
} else {
inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream());
inputStream = method.getResponseBodyAsStream();
}
reader = new BufferedReader(inputStreamReader) {
inputStream = new FilterInputStream(inputStream) {
@Override
public int read() throws IOException {
return super.read();
}
@Override
public void close() throws IOException {
try {
@ -1651,19 +1644,10 @@ public class DavExchangeSession extends ExchangeSession {
} catch (HttpException e) {
method.releaseConnection();
LOGGER.warn("Unable to retrieve message at: " + message.messageUrl);
if (Settings.getBooleanProperty("davmail.deleteBroken")
&& method.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
LOGGER.warn("Deleting broken message at: " + message.messageUrl + " permanentUrl: " + message.permanentUrl);
try {
message.delete();
} catch (IOException ioe) {
LOGGER.warn("Unable to delete broken message at: " + message.permanentUrl);
}
}
LOGGER.warn("Unable to retrieve message at: " + url);
throw e;
}
return reader;
return inputStream;
}
/**
@ -1707,11 +1691,13 @@ public class DavExchangeSession extends ExchangeSession {
}
protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
String zuluDateValue;
try {
zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormatMillisecond().parse(exchangeDateValue));
} catch (ParseException e) {
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue);
String zuluDateValue = null;
if (exchangeDateValue != null) {
try {
zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormatMillisecond().parse(exchangeDateValue));
} catch (ParseException e) {
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue);
}
}
return zuluDateValue;
}

View File

@ -31,10 +31,7 @@ import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.HeadMethod;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.NoRouteToHostException;
import java.net.UnknownHostException;
@ -208,15 +205,6 @@ public class EwsExchangeSession extends ExchangeSession {
executeMethod(createItemMethod);
}
/**
* @inheritDoc
*/
@Override
protected BufferedReader getContentReader(ExchangeSession.Message message) throws IOException {
byte[] content = getContent(message);
return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(content)));
}
/**
* @inheritDoc
*/
@ -238,7 +226,7 @@ public class EwsExchangeSession extends ExchangeSession {
return getItemMethod.getMimeContent();
}
protected Message buildMessage(EWSMethod.Item response) throws DavMailException {
protected Message buildMessage(EWSMethod.Item response) throws DavMailException {
Message message = new Message();
// get item id
@ -796,7 +784,7 @@ public class EwsExchangeSession extends ExchangeSession {
// item id
ItemId itemId;
protected Event(EWSMethod.Item response) {
protected Event(EWSMethod.Item response) {
itemId = new ItemId(response);
permanentUrl = response.get(Field.get("permanenturl").getResponseName());
@ -950,9 +938,9 @@ public class EwsExchangeSession extends ExchangeSession {
String itemType = responses.get(0).type;
if ("Contact".equals(itemType)) {
// retrieve Contact properties
List<ExchangeSession.Contact> contacts = searchContacts(folderPath, CONTACT_ATTRIBUTES, equals("urlcompname", urlcompname));
List<ExchangeSession.Contact> contacts = searchContacts(folderPath, CONTACT_ATTRIBUTES, equals("urlcompname", urlcompname));
if (contacts.isEmpty()) {
throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND");
throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND");
}
return contacts.get(0);
} else if ("CalendarItem".equals(itemType)
@ -1044,7 +1032,7 @@ public class EwsExchangeSession extends ExchangeSession {
private FolderId getFolderId(String folderPath) throws IOException {
FolderId folderId = getFolderIdIfExists(folderPath);
if (folderId == null) {
throw new HttpNotFoundException("Folder '"+folderPath+"' not found");
throw new HttpNotFoundException("Folder '" + folderPath + "' not found");
}
return folderId;
}
@ -1056,7 +1044,7 @@ public class EwsExchangeSession extends ExchangeSession {
if (currentMailboxPath.equals(folderPath)) {
return DistinguishedFolderId.MSGFOLDERROOT;
} else if (folderPath.startsWith(currentMailboxPath + '/')) {
return getFolderIdIfExists(folderPath.substring(currentMailboxPath.length()+1));
return getFolderIdIfExists(folderPath.substring(currentMailboxPath.length() + 1));
}
if (folderPath.startsWith(PUBLIC_ROOT)) {
currentFolderId = DistinguishedFolderId.PUBLICFOLDERSROOT;
@ -1128,11 +1116,13 @@ public class EwsExchangeSession extends ExchangeSession {
}
protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
String zuluDateValue;
try {
zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormat().parse(exchangeDateValue));
} catch (ParseException e) {
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue);
String zuluDateValue = null;
if (exchangeDateValue != null) {
try {
zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormat().parse(exchangeDateValue));
} catch (ParseException e) {
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue);
}
}
return zuluDateValue;
}