2008-12-06 19:29:11 -05:00
|
|
|
|
2008-12-16 18:34:01 -05:00
|
|
|
package com.android.email.mail.transport;
|
2008-12-06 19:29:11 -05:00
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedOutputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.SocketAddress;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.security.GeneralSecurityException;
|
2009-01-01 03:56:19 -05:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2008-12-06 19:29:11 -05:00
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
import javax.net.ssl.SSLException;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.parsers.SAXParser;
|
|
|
|
import javax.xml.parsers.SAXParserFactory;
|
|
|
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.client.CookieStore;
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
import org.xml.sax.XMLReader;
|
|
|
|
|
|
|
|
import android.util.Config;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2008-12-16 18:34:01 -05:00
|
|
|
import com.android.email.Email;
|
|
|
|
import com.android.email.PeekableInputStream;
|
|
|
|
import com.android.email.codec.binary.Base64;
|
|
|
|
import com.android.email.mail.Address;
|
|
|
|
import com.android.email.mail.AuthenticationFailedException;
|
|
|
|
import com.android.email.mail.Folder;
|
|
|
|
import com.android.email.mail.Message;
|
|
|
|
import com.android.email.mail.MessagingException;
|
|
|
|
import com.android.email.mail.Transport;
|
|
|
|
import com.android.email.mail.CertificateValidationException;
|
|
|
|
import com.android.email.mail.Message.RecipientType;
|
|
|
|
import com.android.email.mail.store.TrustManagerFactory;
|
|
|
|
import com.android.email.mail.store.WebDavStore;
|
|
|
|
import com.android.email.mail.store.WebDavStore.HttpGeneric;
|
2009-01-17 21:43:15 -05:00
|
|
|
import com.android.email.mail.store.WebDavStore.DataSet;
|
2008-12-16 18:34:01 -05:00
|
|
|
import com.android.email.mail.store.WebDavStore.WebDavHandler;
|
2008-12-06 19:29:11 -05:00
|
|
|
|
|
|
|
public class WebDavTransport extends Transport {
|
|
|
|
public static final int CONNECTION_SECURITY_NONE = 0;
|
|
|
|
public static final int CONNECTION_SECURITY_TLS_OPTIONAL = 1;
|
|
|
|
public static final int CONNECTION_SECURITY_TLS_REQUIRED = 2;
|
|
|
|
public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3;
|
|
|
|
public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4;
|
|
|
|
|
|
|
|
String host;
|
|
|
|
int mPort;
|
|
|
|
private int mConnectionSecurity;
|
|
|
|
private String mUsername; /* Stores the username for authentications */
|
|
|
|
private String mPassword; /* Stores the password for authentications */
|
|
|
|
private String mUrl; /* Stores the base URL for the server */
|
|
|
|
|
|
|
|
boolean mSecure;
|
|
|
|
Socket mSocket;
|
|
|
|
PeekableInputStream mIn;
|
|
|
|
OutputStream mOut;
|
|
|
|
private WebDavStore store;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* webdav://user:password@server:port CONNECTION_SECURITY_NONE
|
|
|
|
* webdav+tls://user:password@server:port CONNECTION_SECURITY_TLS_OPTIONAL
|
|
|
|
* webdav+tls+://user:password@server:port CONNECTION_SECURITY_TLS_REQUIRED
|
|
|
|
* webdav+ssl+://user:password@server:port CONNECTION_SECURITY_SSL_REQUIRED
|
|
|
|
* webdav+ssl://user:password@server:port CONNECTION_SECURITY_SSL_OPTIONAL
|
|
|
|
*
|
|
|
|
* @param _uri
|
|
|
|
*/
|
|
|
|
public WebDavTransport(String _uri) throws MessagingException {
|
|
|
|
store = new WebDavStore(_uri);
|
2008-12-16 18:34:01 -05:00
|
|
|
Log.d(Email.LOG_TAG, ">>> New WebDavTransport creation complete");
|
2008-12-06 19:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void open() throws MessagingException {
|
2008-12-16 18:34:01 -05:00
|
|
|
Log.d(Email.LOG_TAG, ">>> open called on WebDavTransport ");
|
2009-01-27 00:39:41 -05:00
|
|
|
store.getHttpClient();
|
2008-12-06 19:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// public void sendMessage(Message message) throws MessagingException {
|
|
|
|
// Address[] from = message.getFrom();
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public String generateTempURI(String subject) {
|
|
|
|
String encodedSubject = URLEncoder.encode(subject);
|
2009-01-05 20:31:57 -05:00
|
|
|
return store.getUrl() + "/drafts/" + encodedSubject + ".eml";
|
2008-12-06 19:29:11 -05:00
|
|
|
}
|
|
|
|
public String generateSendURI() {
|
2009-01-05 20:31:57 -05:00
|
|
|
return store.getUrl() + "/##DavMailSubmissionURI##/";
|
2008-12-06 19:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(Message message) throws MessagingException {
|
2008-12-16 18:34:01 -05:00
|
|
|
Log.d(Email.LOG_TAG, ">>> sendMessage called.");
|
2008-12-06 19:29:11 -05:00
|
|
|
|
2009-01-01 03:56:19 -05:00
|
|
|
DefaultHttpClient httpclient;
|
2009-01-27 00:39:41 -05:00
|
|
|
httpclient = store.getHttpClient();
|
2008-12-06 19:29:11 -05:00
|
|
|
HttpGeneric httpmethod;
|
|
|
|
HttpResponse response;
|
|
|
|
StringEntity bodyEntity;
|
|
|
|
int statusCode;
|
|
|
|
String subject;
|
|
|
|
ByteArrayOutputStream out;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
subject = message.getSubject();
|
|
|
|
} catch (MessagingException e) {
|
2008-12-16 18:34:01 -05:00
|
|
|
Log.e(Email.LOG_TAG, "MessagingException while retrieving Subject: " + e);
|
2008-12-06 19:29:11 -05:00
|
|
|
subject = "";
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
out = new ByteArrayOutputStream(message.getSize());
|
|
|
|
} catch (MessagingException e) {
|
2008-12-16 18:34:01 -05:00
|
|
|
Log.e(Email.LOG_TAG, "MessagingException while getting size of message: " + e);
|
2008-12-06 19:29:11 -05:00
|
|
|
out = new ByteArrayOutputStream();
|
|
|
|
}
|
|
|
|
open();
|
|
|
|
message.writeTo(
|
|
|
|
new EOLConvertingOutputStream(
|
|
|
|
new BufferedOutputStream(out, 1024)));
|
|
|
|
|
|
|
|
bodyEntity = new StringEntity(out.toString(), "UTF-8");
|
|
|
|
bodyEntity.setContentType("message/rfc822");
|
|
|
|
|
|
|
|
httpmethod = store.new HttpGeneric(generateTempURI(subject));
|
|
|
|
httpmethod.setMethod("PUT");
|
|
|
|
httpmethod.setEntity(bodyEntity);
|
|
|
|
|
|
|
|
response = httpclient.execute(httpmethod);
|
|
|
|
statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
|
|
|
if (statusCode < 200 ||
|
|
|
|
statusCode > 300) {
|
2009-01-05 20:31:57 -05:00
|
|
|
throw new IOException("Sending Message: Error while trying to upload to drafts folder: "+
|
|
|
|
response.getStatusLine().toString()+ "\n\n"+
|
|
|
|
WebDavStore.getHttpRequestResponse(bodyEntity, response.getEntity()));
|
2008-12-06 19:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
httpmethod = store.new HttpGeneric(generateTempURI(subject));
|
|
|
|
httpmethod.setMethod("MOVE");
|
|
|
|
httpmethod.setHeader("Destination", generateSendURI());
|
|
|
|
|
|
|
|
response = httpclient.execute(httpmethod);
|
|
|
|
statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
|
|
|
if (statusCode < 200 ||
|
|
|
|
statusCode > 300) {
|
2009-01-05 20:31:57 -05:00
|
|
|
throw new IOException("Sending Message: Error while trying to move finished draft to Outbox: "+
|
|
|
|
response.getStatusLine().toString()+ "\n\n"+
|
|
|
|
WebDavStore.getHttpRequestResponse(null, response.getEntity()));
|
2008-12-06 19:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (UnsupportedEncodingException uee) {
|
2009-01-05 20:31:57 -05:00
|
|
|
Log.e(Email.LOG_TAG, "UnsupportedEncodingException in sendMessage() " + uee);
|
2008-12-06 19:29:11 -05:00
|
|
|
} catch (IOException ioe) {
|
2009-01-05 20:31:57 -05:00
|
|
|
Log.e(Email.LOG_TAG, "IOException in sendMessage() " + ioe);
|
|
|
|
throw new MessagingException("Unable to send message"+ioe.getMessage(), ioe);
|
2008-12-06 19:29:11 -05:00
|
|
|
}
|
2008-12-16 18:34:01 -05:00
|
|
|
Log.d(Email.LOG_TAG, ">>> getMessageCount finished");
|
2008-12-06 19:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|