Finished initial refactoring of fetch(). Some slight formatting and cleanup changes as well.

This commit is contained in:
Matthew Brace 2008-12-29 00:25:19 +00:00
parent 34be0269f8
commit 72c4095ceb
1 changed files with 118 additions and 90 deletions

View File

@ -2,12 +2,14 @@ package com.android.email.mail.store;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
@ -34,6 +36,7 @@ import com.android.email.mail.Store;
import com.android.email.mail.internet.MimeBodyPart;
import com.android.email.mail.internet.MimeMessage;
import com.android.email.mail.internet.TextBody;
import com.android.email.mail.transport.EOLConvertingOutputStream;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
@ -230,13 +233,10 @@ public class WebDavStore extends Store {
buffer.append("<?xml version='1.0' ?>");
buffer.append("<a:searchrequest xmlns:a='DAV:'><a:sql>\r\n");
buffer.append("SELECT \"DAV:ishidden\"\r\n");
// buffer.append(" FROM \"\"\r\n");
buffer.append(" FROM SCOPE('deep traversal of \""+this.mUrl+"\"')\r\n");
buffer.append(" WHERE \"DAV:ishidden\"=False AND \"DAV:isfolder\"=True\r\n");
buffer.append("</a:sql></a:searchrequest>\r\n");
return buffer.toString();
}
private String getMessageCountXml(String messageState) {
@ -869,6 +869,15 @@ public class WebDavStore extends Store {
fetchEnvelope(messages, listener);
}
if (fp.contains(FetchProfile.Item.BODY_SANE)) {
fetchMessages(messages, listener, FETCH_BODY_SANE_SUGGESTED_SIZE / 76);
}
if (fp.contains(FetchProfile.Item.BODY)) {
fetchMessages(messages, listener, -1);
}
if (fp.contains(FetchProfile.Item.STRUCTURE)) {
for (int i = 0, count = messages.length; i < count; i++) {
if (!(messages[i] instanceof WebDavMessage)) {
throw new MessagingException("WebDavStore fetch called with non-WebDavMessage");
@ -879,30 +888,48 @@ public class WebDavStore extends Store {
listener.messageStarted(wdMessage.getUid(), i, count);
}
/**
* Set the body to null if it's asking for the structure because
* we don't support it yet.
*/
if (fp.contains(FetchProfile.Item.STRUCTURE)) {
wdMessage.setBody(null);
if (listener != null) {
listener.messageFinished(wdMessage, i, count);
}
}
}
}
/**
* Message fetching that we can pull as a stream
* Fetches the full messages or up to lines lines and passes them to the message parser.
*/
if (fp.contains(FetchProfile.Item.BODY) ||
fp.contains(FetchProfile.Item.BODY_SANE)) {
private void fetchMessages(Message[] messages, MessageRetrievalListener listener, int lines) throws MessagingException {
DefaultHttpClient httpclient = new DefaultHttpClient();
InputStream istream = null;
InputStream resultStream = null;
HttpGet httpget;
HttpEntity entity;
HttpResponse response;
/**
* We can't hand off to processRequest() since we need the stream to parse.
*/
if (needAuth()) {
authenticate();
}
if (WebDavStore.this.mAuthenticated == false ||
WebDavStore.this.mAuthCookies == null) {
throw new MessagingException("Error during authentication in fetchMessages().");
}
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
for (int i = 0, count = messages.length; i < count; i++) {
WebDavMessage wdMessage;
int statusCode = 0;
try {
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
if (!(messages[i] instanceof WebDavMessage)) {
throw new MessagingException("WebDavStore fetch called with non-WebDavMessage");
}
wdMessage = (WebDavMessage) messages[i];
if (listener != null) {
listener.messageStarted(wdMessage.getUid(), i, count);
}
/**
* If fetch is called outside of the initial list (ie, a locally stored
@ -915,7 +942,11 @@ public class WebDavStore extends Store {
}
}
httpget = new HttpGet(new URI(wdMessage.getUrl()));
try {
HttpGet httpget = new HttpGet(new URI(wdMessage.getUrl()));
HttpResponse response;
HttpEntity entity;
httpget.setHeader("translate", "f");
response = httpclient.execute(httpget);
@ -928,24 +959,25 @@ public class WebDavStore extends Store {
}
entity = response.getEntity();
if (entity != null) {
InputStream istream = null;
StringBuffer buffer = new StringBuffer();
String tempText = new String();
String resultText = new String();
String bodyBoundary = "";
BufferedReader reader;
int totalLines = FETCH_BODY_SANE_SUGGESTED_SIZE / 76;
int lines = 0;
int currentLines = 0;
istream = entity.getContent();
if (fp.contains(FetchProfile.Item.BODY_SANE)) {
if (lines != -1) {
reader = new BufferedReader(new InputStreamReader(istream), 8192);
while ((tempText = reader.readLine()) != null &&
(lines < totalLines)) {
(currentLines < lines)) {
buffer.append(tempText+"\r\n");
lines++;
currentLines++;
}
istream.close();
@ -954,7 +986,6 @@ public class WebDavStore extends Store {
}
wdMessage.parse(istream);
}
} catch (IllegalArgumentException iae) {
@ -964,7 +995,6 @@ public class WebDavStore extends Store {
} catch (IOException ioe) {
Log.e(Email.LOG_TAG, "Non-success response code loading message, response code was " + statusCode);
}
}
if (listener != null) {
listener.messageFinished(wdMessage, i, count);
@ -1189,20 +1219,18 @@ public class WebDavStore extends Store {
@Override
public void appendMessages(Message[] messages) throws MessagingException {
appendMessages(messages, false);
Log.e(Email.LOG_TAG, "appendMessages() not implmented");
}
public void appendMessages(Message[] messages, boolean copy) throws MessagingException {
}
@Override
public void copyMessages(Message[] msgs, Folder folder) throws MessagingException {
Log.e(Email.LOG_TAG, "copyMessages() not implemented");
}
@Override
public Message[] expunge() throws MessagingException {
/** Do nothing, deletes occur as soon as the call is made rather than flags on the message */
return null;
}