1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-14 03:32:22 -05:00

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

@ -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); try {
String line; contentInputStream = getContentInputStream(message.messageUrl);
boolean first = true; } catch (HttpNotFoundException e) {
while ((line = reader.readLine()) != null) { LOGGER.debug("Message not found at: " + message.messageUrl + ", retrying with permanenturl");
if (first) { contentInputStream = getContentInputStream(message.permanentUrl);
first = false;
} else {
outputStreamWriter.write((char) 13);
outputStreamWriter.write((char) 10);
} }
outputStreamWriter.write(line); } 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);
} }
outputStreamWriter.flush(); }
throw e;
}
try {
IOUtil.write(contentInputStream, baos);
} finally { } finally {
try { contentInputStream.close();
reader.close();
} catch (IOException e) {
LOGGER.warn("Error closing message input stream", e);
}
} }
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,12 +1691,14 @@ public class DavExchangeSession extends ExchangeSession {
} }
protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException { protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
String zuluDateValue; String zuluDateValue = null;
if (exchangeDateValue != null) {
try { try {
zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormatMillisecond().parse(exchangeDateValue)); zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormatMillisecond().parse(exchangeDateValue));
} catch (ParseException e) { } catch (ParseException e) {
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue); 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
*/ */
@ -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,12 +1116,14 @@ public class EwsExchangeSession extends ExchangeSession {
} }
protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException { protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
String zuluDateValue; String zuluDateValue = null;
if (exchangeDateValue != null) {
try { try {
zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormat().parse(exchangeDateValue)); zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormat().parse(exchangeDateValue));
} catch (ParseException e) { } catch (ParseException e) {
throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue); throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue);
} }
}
return zuluDateValue; return zuluDateValue;
} }