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; 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 * Get raw MIME message content
* *
@ -2615,7 +2606,6 @@ public abstract class ExchangeSession {
* *
* @param folderPath Exchange folder path * @param folderPath Exchange folder path
* @param itemName item name * @param itemName item name
* @return HTTP status
* @throws IOException on error * @throws IOException on error
*/ */
public abstract void deleteItem(String folderPath, String itemName) throws IOException; 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 putmethod = new PutMethod(messageUrl);
putmethod.setRequestHeader("Translate", "f"); putmethod.setRequestHeader("Translate", "f");
putmethod.setRequestHeader("Content-Type", "message/rfc822"); putmethod.setRequestHeader("Content-Type", "message/rfc822");
try { try {
// use same encoding as client socket reader // use same encoding as client socket reader
putmethod.setRequestEntity(new ByteArrayRequestEntity(messageBody)); putmethod.setRequestEntity(new ByteArrayRequestEntity(messageBody));
@ -1583,62 +1583,55 @@ public class DavExchangeSession extends ExchangeSession {
@Override @Override
protected byte[] getContent(Message message) throws IOException { protected byte[] getContent(Message message) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedReader reader = getContentReader(message); InputStream contentInputStream;
try { 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 { try {
reader.close(); contentInputStream = getContentInputStream(message.messageUrl);
} catch (IOException e) { } catch (HttpNotFoundException e) {
LOGGER.warn("Error closing message input stream", 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(); return baos.toByteArray();
} }
/** protected InputStream getContentInputStream(String url) throws IOException {
* @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 {
final GetMethod method = new GetMethod(URIUtil.encodePath(url)); final GetMethod method = new GetMethod(URIUtil.encodePath(url));
method.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); method.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
method.setRequestHeader("Translate", "f"); method.setRequestHeader("Translate", "f");
method.setRequestHeader("Accept-Encoding", "gzip"); method.setRequestHeader("Accept-Encoding", "gzip");
BufferedReader reader; InputStream inputStream;
try { try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true); DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true);
InputStreamReader inputStreamReader;
if (isGzipEncoded(method)) { if (isGzipEncoded(method)) {
inputStreamReader = new InputStreamReader(new GZIPInputStream(method.getResponseBodyAsStream())); inputStream = new GZIPInputStream(method.getResponseBodyAsStream());
} else { } 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 @Override
public void close() throws IOException { public void close() throws IOException {
try { try {
@ -1651,19 +1644,10 @@ public class DavExchangeSession extends ExchangeSession {
} catch (HttpException e) { } catch (HttpException e) {
method.releaseConnection(); method.releaseConnection();
LOGGER.warn("Unable to retrieve message at: " + message.messageUrl); LOGGER.warn("Unable to retrieve message at: " + url);
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);
}
}
throw e; throw e;
} }
return reader; return inputStream;
} }
/** /**
@ -1707,11 +1691,13 @@ public class DavExchangeSession extends ExchangeSession {
} }
protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException { protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
String zuluDateValue; String zuluDateValue = null;
try { if (exchangeDateValue != null) {
zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormatMillisecond().parse(exchangeDateValue)); try {
} catch (ParseException e) { zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormatMillisecond().parse(exchangeDateValue));
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue); } catch (ParseException e) {
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue);
}
} }
return zuluDateValue; 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.HttpStatus;
import org.apache.commons.httpclient.methods.HeadMethod; import org.apache.commons.httpclient.methods.HeadMethod;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.NoRouteToHostException; import java.net.NoRouteToHostException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@ -208,15 +205,6 @@ public class EwsExchangeSession extends ExchangeSession {
executeMethod(createItemMethod); 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 * @inheritDoc
*/ */
@ -238,7 +226,7 @@ public class EwsExchangeSession extends ExchangeSession {
return getItemMethod.getMimeContent(); return getItemMethod.getMimeContent();
} }
protected Message buildMessage(EWSMethod.Item response) throws DavMailException { protected Message buildMessage(EWSMethod.Item response) throws DavMailException {
Message message = new Message(); Message message = new Message();
// get item id // get item id
@ -796,7 +784,7 @@ public class EwsExchangeSession extends ExchangeSession {
// item id // item id
ItemId itemId; ItemId itemId;
protected Event(EWSMethod.Item response) { protected Event(EWSMethod.Item response) {
itemId = new ItemId(response); itemId = new ItemId(response);
permanentUrl = response.get(Field.get("permanenturl").getResponseName()); permanentUrl = response.get(Field.get("permanenturl").getResponseName());
@ -950,9 +938,9 @@ public class EwsExchangeSession extends ExchangeSession {
String itemType = responses.get(0).type; String itemType = responses.get(0).type;
if ("Contact".equals(itemType)) { if ("Contact".equals(itemType)) {
// retrieve Contact properties // 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()) { if (contacts.isEmpty()) {
throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND"); throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND");
} }
return contacts.get(0); return contacts.get(0);
} else if ("CalendarItem".equals(itemType) } else if ("CalendarItem".equals(itemType)
@ -1044,7 +1032,7 @@ public class EwsExchangeSession extends ExchangeSession {
private FolderId getFolderId(String folderPath) throws IOException { private FolderId getFolderId(String folderPath) throws IOException {
FolderId folderId = getFolderIdIfExists(folderPath); FolderId folderId = getFolderIdIfExists(folderPath);
if (folderId == null) { if (folderId == null) {
throw new HttpNotFoundException("Folder '"+folderPath+"' not found"); throw new HttpNotFoundException("Folder '" + folderPath + "' not found");
} }
return folderId; return folderId;
} }
@ -1056,7 +1044,7 @@ public class EwsExchangeSession extends ExchangeSession {
if (currentMailboxPath.equals(folderPath)) { if (currentMailboxPath.equals(folderPath)) {
return DistinguishedFolderId.MSGFOLDERROOT; return DistinguishedFolderId.MSGFOLDERROOT;
} else if (folderPath.startsWith(currentMailboxPath + '/')) { } else if (folderPath.startsWith(currentMailboxPath + '/')) {
return getFolderIdIfExists(folderPath.substring(currentMailboxPath.length()+1)); return getFolderIdIfExists(folderPath.substring(currentMailboxPath.length() + 1));
} }
if (folderPath.startsWith(PUBLIC_ROOT)) { if (folderPath.startsWith(PUBLIC_ROOT)) {
currentFolderId = DistinguishedFolderId.PUBLICFOLDERSROOT; currentFolderId = DistinguishedFolderId.PUBLICFOLDERSROOT;
@ -1128,11 +1116,13 @@ public class EwsExchangeSession extends ExchangeSession {
} }
protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException { protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
String zuluDateValue; String zuluDateValue = null;
try { if (exchangeDateValue != null) {
zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormat().parse(exchangeDateValue)); try {
} catch (ParseException e) { zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormat().parse(exchangeDateValue));
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue); } catch (ParseException e) {
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue);
}
} }
return zuluDateValue; return zuluDateValue;
} }