1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 11:12:22 -05:00

EWS: move WebDav message write and delete to DavExchangeSession

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1085 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-06-09 09:52:12 +00:00
parent 38f74f5c75
commit 0394dda045
3 changed files with 187 additions and 58 deletions

View File

@ -601,14 +601,33 @@ public abstract class ExchangeSession {
*/
public abstract void updateMessage(Message message, Map<String, String> properties) throws IOException;
/**
* Delete Exchange message.
*
* @param message Exchange message
* @throws IOException on error
*/
public abstract void deleteMessage(Message message) throws IOException;
/**
* Send message to recipients, properties contains bcc recipients and other non MIME flags.
* @param properties additional message properties
*
* @param properties additional message properties
* @param messageBody MIME message body
* @throws IOException on error
*/
public abstract void sendMessage(HashMap<String, String> properties, String 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;
protected static final List<String> POP_MESSAGE_ATTRIBUTES = new ArrayList<String>();
static {
@ -1345,40 +1364,8 @@ public abstract class ExchangeSession {
* @throws IOException on error
*/
public void write(OutputStream os, boolean doubleDot) throws IOException {
BufferedReader reader = getContentReader(this);
try {
write(os, messageUrl, doubleDot);
} catch (HttpNotFoundException e) {
LOGGER.debug("Message not found at: " + messageUrl + ", retrying with permanenturl");
write(os, permanentUrl, doubleDot);
}
}
protected boolean isGzipEncoded(HttpMethod method) {
Header[] contentEncodingHeaders = method.getResponseHeaders("Content-Encoding");
if (contentEncodingHeaders != null) {
for (Header header : contentEncodingHeaders) {
if ("gzip".equals(header.getValue())) {
return true;
}
}
}
return false;
}
protected void write(OutputStream os, String url, boolean doubleDot) throws IOException {
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 = null;
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true);
if (isGzipEncoded(method)) {
reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(method.getResponseBodyAsStream())));
} else {
reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
}
OutputStreamWriter isoWriter = new OutputStreamWriter(os);
String line;
while ((line = reader.readLine()) != null) {
@ -1406,27 +1393,12 @@ public abstract class ExchangeSession {
isoWriter.write((char) 10);
}
isoWriter.flush();
} catch (HttpException e) {
LOGGER.warn("Unable to retrieve message at: " + messageUrl);
if (Settings.getBooleanProperty("davmail.deleteBroken")
&& method.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
LOGGER.warn("Deleting broken message at: " + messageUrl + " permanentUrl: " + permanentUrl);
try {
this.delete();
} catch (IOException ioe) {
LOGGER.warn("Unable to delete broken message at: " + permanentUrl);
}
}
throw e;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
LOGGER.warn("Error closing message input stream", e);
}
try {
reader.close();
} catch (IOException e) {
LOGGER.warn("Error closing message input stream", e);
}
method.releaseConnection();
}
}
@ -1515,7 +1487,7 @@ public abstract class ExchangeSession {
*/
public void delete() throws IOException {
LOGGER.debug("Delete " + permanentUrl + " (" + messageUrl + ")");
DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, permanentUrl);
deleteMessage(this);
}
/**

View File

@ -19,13 +19,16 @@
package davmail.exchange.dav;
import davmail.BundleMessage;
import davmail.Settings;
import davmail.exception.DavMailAuthenticationException;
import davmail.exception.DavMailException;
import davmail.exception.HttpNotFoundException;
import davmail.exchange.ExchangeSession;
import davmail.http.DavGatewayHttpClientFacade;
import davmail.ui.tray.DavGatewayTray;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
@ -36,13 +39,12 @@ import org.apache.jackrabbit.webdav.client.methods.PropPatchMethod;
import org.apache.jackrabbit.webdav.property.*;
import org.apache.jackrabbit.webdav.xml.Namespace;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.net.NoRouteToHostException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.*;
import java.util.zip.GZIPInputStream;
/**
* Webdav Exchange adapter.
@ -733,6 +735,15 @@ public class DavExchangeSession extends ExchangeSession {
}
}
/**
* @inheritDoc
*/
@Override
public void deleteMessage(Message message) throws IOException {
LOGGER.debug("Delete " + message.permanentUrl + " (" + message.messageUrl + ")");
DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, message.permanentUrl);
}
/**
* @inheritDoc
*/
@ -750,6 +761,76 @@ public class DavExchangeSession extends ExchangeSession {
}
}
protected boolean isGzipEncoded(HttpMethod method) {
Header[] contentEncodingHeaders = method.getResponseHeaders("Content-Encoding");
if (contentEncodingHeaders != null) {
for (Header header : contentEncodingHeaders) {
if ("gzip".equals(header.getValue())) {
return true;
}
}
}
return false;
}
/**
* @inheritDoc
*/
@Override
protected BufferedReader getContentReader(Message message) throws IOException {
BufferedReader reader = null;
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(message.permanentUrl));
method.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
method.setRequestHeader("Translate", "f");
method.setRequestHeader("Accept-Encoding", "gzip");
BufferedReader reader = null;
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true);
InputStreamReader inputStreamReader;
if (isGzipEncoded(method)) {
inputStreamReader = new InputStreamReader(new GZIPInputStream(method.getResponseBodyAsStream()));
} else {
inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream());
}
reader = new BufferedReader(inputStreamReader) {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
method.releaseConnection();
}
}
};
} 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);
}
}
throw e;
}
return reader;
}
/**
* @inheritDoc
*/

View File

@ -27,7 +27,10 @@ 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.IOException;
import java.net.NoRouteToHostException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -52,6 +55,11 @@ public class EwsExchangeSession extends ExchangeSession {
super(url, userName, password);
}
@Override
public boolean isExpired() throws NoRouteToHostException, UnknownHostException {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
protected void buildSessionInfo(HttpMethod method) throws DavMailException {
// nothing to do, mailPath not used in EWS mode
@ -86,9 +94,37 @@ public class EwsExchangeSession extends ExchangeSession {
}
}
@Override
public void createMessage(String folderPath, String messageName, HashMap<String, String> properties, String messageBody) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void updateMessage(Message message, Map<String, String> properties) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void deleteMessage(Message message) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void sendMessage(HashMap<String, String> properties, String messageBody) throws IOException {
throw new UnsupportedOperationException();
}
@Override
protected BufferedReader getContentReader(Message message) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public MessageList searchMessages(String folderName, List<String> attributes, Condition condition) throws IOException {
// TODO
throw new UnsupportedOperationException();
}
@ -319,6 +355,46 @@ public class EwsExchangeSession extends ExchangeSession {
return folder;
}
/**
* @inheritDoc
*/
@Override
public void createFolder(String folderName, String folderClass) throws IOException {
throw new UnsupportedOperationException();
}
/**
* @inheritDoc
*/
@Override
public void deleteFolder(String folderName) throws IOException {
throw new UnsupportedOperationException();
}
/**
* @inheritDoc
*/
@Override
public void copyMessage(Message message, String targetFolder) throws IOException {
throw new UnsupportedOperationException();
}
/**
* @inheritDoc
*/
@Override
public void moveFolder(String folderName, String targetName) throws IOException {
throw new UnsupportedOperationException();
}
/**
* @inheritDoc
*/
@Override
protected void moveToTrash(Message message) throws IOException {
throw new UnsupportedOperationException();
}
private FolderId getFolderId(String folderPath) throws IOException {
String[] folderNames;