1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 19:22: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;
/**
* 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

@ -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);
try {
contentInputStream = getContentInputStream(message.messageUrl);
} catch (HttpNotFoundException e) {
LOGGER.debug("Message not found at: " + message.messageUrl + ", retrying with permanenturl");
contentInputStream = getContentInputStream(message.permanentUrl);
}
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 {
try {
reader.close();
} catch (IOException e) {
LOGGER.warn("Error closing message input stream", e);
}
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,12 +1691,14 @@ public class DavExchangeSession extends ExchangeSession {
}
protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
String zuluDateValue;
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
*/
@ -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,12 +1116,14 @@ public class EwsExchangeSession extends ExchangeSession {
}
protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
String zuluDateValue;
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;
}