mirror of https://github.com/moparisthebest/k-9
Refactored how the HTTP calls were to reduce the growing volume of duplicated code.
This commit is contained in:
parent
60b03f3dc2
commit
2dede98463
|
@ -156,100 +156,56 @@ public class WebDavStore extends Store {
|
||||||
@Override
|
@Override
|
||||||
public Folder[] getPersonalNamespaces() throws MessagingException {
|
public Folder[] getPersonalNamespaces() throws MessagingException {
|
||||||
ArrayList<Folder> folderList = new ArrayList<Folder>();
|
ArrayList<Folder> folderList = new ArrayList<Folder>();
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
HashMap<String, String> headers = new HashMap<String, String>();
|
||||||
HttpEntity responseEntity;
|
ParsedDataSet dataset = new ParsedDataSet();
|
||||||
HttpGeneric httpmethod;
|
|
||||||
HttpResponse response;
|
|
||||||
StringEntity messageEntity;
|
|
||||||
String messageBody;
|
String messageBody;
|
||||||
int status_code;
|
String[] folderUrls;
|
||||||
|
int urlLength;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We have to check authentication here so we have the proper URL stored
|
||||||
|
*/
|
||||||
if (needAuth()) {
|
if (needAuth()) {
|
||||||
authenticate();
|
authenticate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
messageBody = getFolderListXml();
|
||||||
|
headers.put("Brief", "t");
|
||||||
|
|
||||||
if (this.mAuthenticated == false ||
|
dataset = processRequest(this.mUrl, "SEARCH", messageBody, headers);
|
||||||
this.mAuthCookies == null) {
|
|
||||||
return folderList.toArray(new Folder[] {});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
folderUrls = dataset.getHrefs();
|
||||||
/** Set up and execute the request */
|
urlLength = folderUrls.length;
|
||||||
httpclient.setCookieStore(this.mAuthCookies);
|
|
||||||
messageBody = getFolderListXml();
|
|
||||||
messageEntity = new StringEntity(messageBody);
|
|
||||||
messageEntity.setContentType("text/xml");
|
|
||||||
|
|
||||||
httpmethod = new HttpGeneric(this.mUrl);// + "/Exchange/" + this.mUsername);
|
|
||||||
httpmethod.setMethod("SEARCH");
|
|
||||||
httpmethod.setEntity(messageEntity);
|
|
||||||
httpmethod.setHeader("Brief", "t");
|
|
||||||
|
|
||||||
response = httpclient.execute(httpmethod);
|
for (int i = 0; i < urlLength; i++) {
|
||||||
status_code = response.getStatusLine().getStatusCode();
|
String[] urlParts = folderUrls[i].split("/");
|
||||||
|
String folderName = urlParts[urlParts.length - 1];
|
||||||
if (status_code < 200 ||
|
String fullPathName = "";
|
||||||
status_code > 300) {
|
WebDavFolder wdFolder;
|
||||||
throw new IOException("Error getting folder listing");
|
|
||||||
}
|
|
||||||
|
|
||||||
responseEntity = response.getEntity();
|
|
||||||
|
|
||||||
if (responseEntity != null) {
|
|
||||||
/** Parse the returned data */
|
|
||||||
try {
|
|
||||||
InputStream istream = responseEntity.getContent();
|
|
||||||
SAXParserFactory spf = SAXParserFactory.newInstance();
|
|
||||||
SAXParser sp = spf.newSAXParser();
|
|
||||||
|
|
||||||
XMLReader xr = sp.getXMLReader();
|
|
||||||
|
|
||||||
WebDavHandler myHandler = new WebDavHandler();
|
|
||||||
xr.setContentHandler(myHandler);
|
|
||||||
|
|
||||||
xr.parse(new InputSource(istream));
|
|
||||||
|
|
||||||
ParsedDataSet dataset = myHandler.getDataSet();
|
|
||||||
|
|
||||||
String[] folderUrls = dataset.getHrefs();
|
|
||||||
int urlLength = folderUrls.length;
|
|
||||||
|
|
||||||
for (int i = 0; i < urlLength; i++) {
|
|
||||||
String[] urlParts = folderUrls[i].split("/");
|
|
||||||
String folderName = urlParts[urlParts.length - 1];
|
|
||||||
String fullPathName = "";
|
|
||||||
WebDavFolder wdFolder;
|
|
||||||
|
|
||||||
if (folderName.equalsIgnoreCase(Email.INBOX)) {
|
if (folderName.equalsIgnoreCase(Email.INBOX)) {
|
||||||
folderName = "INBOX";
|
folderName = "INBOX";
|
||||||
} else {
|
} else {
|
||||||
for (int j = 5, count = urlParts.length; j < count; j++) {
|
for (int j = 5, count = urlParts.length; j < count; j++) {
|
||||||
if (j != 5) {
|
if (j != 5) {
|
||||||
fullPathName = fullPathName + "/" + urlParts[j];
|
fullPathName = fullPathName + "/" + urlParts[j];
|
||||||
} else {
|
} else {
|
||||||
fullPathName = urlParts[j];
|
fullPathName = urlParts[j];
|
||||||
}
|
|
||||||
}
|
|
||||||
folderName = java.net.URLDecoder.decode(fullPathName, "UTF-8");
|
|
||||||
}
|
|
||||||
|
|
||||||
wdFolder = new WebDavFolder(folderName);
|
|
||||||
wdFolder.setUrl(folderUrls[i]);
|
|
||||||
folderList.add(wdFolder);
|
|
||||||
this.mFolderList.put(folderName, wdFolder);
|
|
||||||
//folderList.add(getFolder(java.net.URLDecoder.decode(folderName, "UTF-8")));
|
|
||||||
}
|
}
|
||||||
} catch (SAXException se) {
|
}
|
||||||
Log.e(Email.LOG_TAG, "Error with SAXParser " + se);
|
try {
|
||||||
} catch (ParserConfigurationException pce) {
|
folderName = java.net.URLDecoder.decode(fullPathName, "UTF-8");
|
||||||
Log.e(Email.LOG_TAG, "Error with SAXParser " + pce);
|
} catch (UnsupportedEncodingException uee) {
|
||||||
|
/** If we don't support UTF-8 there's a problem, don't decode it then */
|
||||||
|
folderName = fullPathName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (UnsupportedEncodingException uee) {
|
|
||||||
Log.e(Email.LOG_TAG, "Error with encoding " + uee);
|
wdFolder = new WebDavFolder(folderName);
|
||||||
} catch (IOException ioe) {
|
wdFolder.setUrl(folderUrls[i]);
|
||||||
Log.e(Email.LOG_TAG, "IOException " + ioe);
|
folderList.add(wdFolder);
|
||||||
}
|
this.mFolderList.put(folderName, wdFolder);
|
||||||
|
}
|
||||||
|
|
||||||
return folderList.toArray(new WebDavFolder[] {});
|
return folderList.toArray(new WebDavFolder[] {});
|
||||||
}
|
}
|
||||||
|
@ -519,13 +475,101 @@ public class WebDavStore extends Store {
|
||||||
public CookieStore getAuthCookies() {
|
public CookieStore getAuthCookies() {
|
||||||
return mAuthCookies;
|
return mAuthCookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAlias() {
|
public String getAlias() {
|
||||||
return alias;
|
return alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUrl() {
|
public String getUrl() {
|
||||||
return mUrl;
|
return mUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs an httprequest to the supplied url using the supplied method.
|
||||||
|
* messageBody and headers are optional as not all requests will need them.
|
||||||
|
* There are two signatures to support calls that don't require parsing of the response.
|
||||||
|
*/
|
||||||
|
private ParsedDataSet processRequest(String url, String method, String messageBody, HashMap<String, String> headers) {
|
||||||
|
return processRequest(url, method, messageBody, headers, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ParsedDataSet processRequest(String url, String method, String messageBody, HashMap<String, String> headers, boolean needsParsing) {
|
||||||
|
ParsedDataSet dataset = new ParsedDataSet();
|
||||||
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
|
||||||
|
if (url == null ||
|
||||||
|
method == null) {
|
||||||
|
return dataset;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needAuth()) {
|
||||||
|
authenticate();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.mAuthenticated == false ||
|
||||||
|
this.mAuthCookies == null) {
|
||||||
|
Log.e(Email.LOG_TAG, "Error during authentication");
|
||||||
|
return dataset;
|
||||||
|
}
|
||||||
|
|
||||||
|
httpclient.setCookieStore(this.mAuthCookies);
|
||||||
|
try {
|
||||||
|
int statusCode = -1;
|
||||||
|
StringEntity messageEntity;
|
||||||
|
HttpGeneric httpmethod = new HttpGeneric(url);
|
||||||
|
HttpResponse response;
|
||||||
|
HttpEntity entity;
|
||||||
|
|
||||||
|
if (messageBody != null) {
|
||||||
|
messageEntity = new StringEntity(messageBody);
|
||||||
|
messageEntity.setContentType("text/xml");
|
||||||
|
httpmethod.setEntity(messageEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String headerName : headers.keySet()) {
|
||||||
|
httpmethod.setHeader(headerName, headers.get(headerName));
|
||||||
|
}
|
||||||
|
|
||||||
|
httpmethod.setMethod(method);
|
||||||
|
|
||||||
|
response = httpclient.execute(httpmethod);
|
||||||
|
statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
|
||||||
|
if (statusCode < 200 ||
|
||||||
|
statusCode > 300) {
|
||||||
|
throw new IOException("Error processing request, returned HTTP Response Code was " + statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
entity = response.getEntity();
|
||||||
|
|
||||||
|
if (entity != null &&
|
||||||
|
needsParsing) {
|
||||||
|
try {
|
||||||
|
InputStream istream = entity.getContent();
|
||||||
|
SAXParserFactory spf = SAXParserFactory.newInstance();
|
||||||
|
SAXParser sp = spf.newSAXParser();
|
||||||
|
XMLReader xr = sp.getXMLReader();
|
||||||
|
WebDavHandler myHandler = new WebDavHandler();
|
||||||
|
|
||||||
|
xr.setContentHandler(myHandler);
|
||||||
|
xr.parse(new InputSource(istream));
|
||||||
|
|
||||||
|
dataset = myHandler.getDataSet();
|
||||||
|
} catch (SAXException se) {
|
||||||
|
Log.e(Email.LOG_TAG, "SAXException in processRequest() " + se);
|
||||||
|
} catch (ParserConfigurationException pce) {
|
||||||
|
Log.e(Email.LOG_TAG, "ParserConfigurationException in processRequest() " + pce);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (UnsupportedEncodingException uee) {
|
||||||
|
Log.e(Email.LOG_TAG, "UnsupportedEncodingException: " + uee);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
Log.e(Email.LOG_TAG, "IOException: " + ioe);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dataset;
|
||||||
|
}
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Helper and Inner classes
|
* Helper and Inner classes
|
||||||
*/
|
*/
|
||||||
|
@ -609,14 +653,9 @@ public class WebDavStore extends Store {
|
||||||
private int getMessageCount(boolean read, CookieStore authCookies) {
|
private int getMessageCount(boolean read, CookieStore authCookies) {
|
||||||
String isRead;
|
String isRead;
|
||||||
int messageCount = 0;
|
int messageCount = 0;
|
||||||
|
ParsedDataSet dataset = new ParsedDataSet();
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
HashMap<String, String> headers = new HashMap<String, String>();
|
||||||
HttpGeneric httpmethod;
|
|
||||||
HttpResponse response;
|
|
||||||
HttpEntity responseEntity;
|
|
||||||
StringEntity bodyEntity;
|
|
||||||
String messageBody;
|
String messageBody;
|
||||||
int statusCode;
|
|
||||||
|
|
||||||
if (read) {
|
if (read) {
|
||||||
isRead = new String("True");
|
isRead = new String("True");
|
||||||
|
@ -624,57 +663,13 @@ public class WebDavStore extends Store {
|
||||||
isRead = new String("False");
|
isRead = new String("False");
|
||||||
}
|
}
|
||||||
|
|
||||||
httpclient.setCookieStore(authCookies);
|
|
||||||
|
|
||||||
messageBody = getMessageCountXml(isRead);
|
messageBody = getMessageCountXml(isRead);
|
||||||
|
headers.put("Brief", "t");
|
||||||
try {
|
dataset = processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);
|
||||||
bodyEntity = new StringEntity(messageBody);
|
if (dataset != null) {
|
||||||
bodyEntity.setContentType("text/xml");
|
messageCount = dataset.getMessageCount();
|
||||||
|
|
||||||
httpmethod = new HttpGeneric(this.mFolderUrl);
|
|
||||||
httpmethod.setMethod("SEARCH");
|
|
||||||
httpmethod.setEntity(bodyEntity);
|
|
||||||
httpmethod.setHeader("Brief", "t");
|
|
||||||
|
|
||||||
response = httpclient.execute(httpmethod);
|
|
||||||
statusCode = response.getStatusLine().getStatusCode();
|
|
||||||
|
|
||||||
if (statusCode < 200 ||
|
|
||||||
statusCode > 300) {
|
|
||||||
throw new IOException("Error getting message count, status code was " + statusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
responseEntity = response.getEntity();
|
|
||||||
|
|
||||||
if (responseEntity != null) {
|
|
||||||
try {
|
|
||||||
ParsedDataSet dataset = new ParsedDataSet();
|
|
||||||
InputStream istream = responseEntity.getContent();
|
|
||||||
SAXParserFactory spf = SAXParserFactory.newInstance();
|
|
||||||
SAXParser sp = spf.newSAXParser();
|
|
||||||
|
|
||||||
XMLReader xr = sp.getXMLReader();
|
|
||||||
WebDavHandler myHandler = new WebDavHandler();
|
|
||||||
xr.setContentHandler(myHandler);
|
|
||||||
|
|
||||||
xr.parse(new InputSource(istream));
|
|
||||||
|
|
||||||
dataset = myHandler.getDataSet();
|
|
||||||
messageCount = dataset.getMessageCount();
|
|
||||||
|
|
||||||
istream.close();
|
|
||||||
} catch (SAXException se) {
|
|
||||||
Log.e(Email.LOG_TAG, "SAXException in getMessageCount " + se);
|
|
||||||
} catch (ParserConfigurationException pce) {
|
|
||||||
Log.e(Email.LOG_TAG, "ParserConfigurationException in getMessageCount " + pce);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException uee) {
|
|
||||||
Log.e(Email.LOG_TAG, "UnsupportedEncodingException in getMessageCount() " + uee);
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.e(Email.LOG_TAG, "IOException in getMessageCount() " + ioe);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return messageCount;
|
return messageCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -740,10 +735,12 @@ public class WebDavStore extends Store {
|
||||||
@Override
|
@Override
|
||||||
public Message[] getMessages(int start, int end, MessageRetrievalListener listener)
|
public Message[] getMessages(int start, int end, MessageRetrievalListener listener)
|
||||||
throws MessagingException {
|
throws MessagingException {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
|
||||||
ArrayList<Message> messages = new ArrayList<Message>();
|
ArrayList<Message> messages = new ArrayList<Message>();
|
||||||
String[] uids;
|
String[] uids;
|
||||||
|
ParsedDataSet dataset = new ParsedDataSet();
|
||||||
|
HashMap<String, String> headers = new HashMap<String, String>();
|
||||||
|
int uidsLength = -1;
|
||||||
|
|
||||||
String messageBody;
|
String messageBody;
|
||||||
int prevStart = start;
|
int prevStart = start;
|
||||||
|
|
||||||
|
@ -765,75 +762,27 @@ public class WebDavStore extends Store {
|
||||||
return messages.toArray(new Message[] {});
|
return messages.toArray(new Message[] {});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Retrieve and parse the XML entity for our messages */
|
|
||||||
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
|
|
||||||
messageBody = getMessagesXml();
|
messageBody = getMessagesXml();
|
||||||
|
|
||||||
try {
|
headers.put("Brief", "t");
|
||||||
int status_code = -1;
|
headers.put("Range", "rows=" + start + "-" + end);
|
||||||
StringEntity messageEntity = new StringEntity(messageBody);
|
dataset = processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);
|
||||||
HttpGeneric httpmethod = new HttpGeneric(this.mFolderUrl);
|
|
||||||
HttpResponse response;
|
|
||||||
HttpEntity entity;
|
|
||||||
|
|
||||||
messageEntity.setContentType("text/xml");
|
|
||||||
httpmethod.setMethod("SEARCH");
|
|
||||||
httpmethod.setEntity(messageEntity);
|
|
||||||
httpmethod.setHeader("Brief", "t");
|
|
||||||
httpmethod.setHeader("Range", "rows=" + start + "-" + end);
|
|
||||||
|
|
||||||
response = httpclient.execute(httpmethod);
|
uids = dataset.getUids();
|
||||||
status_code = response.getStatusLine().getStatusCode();
|
HashMap<String, String> uidToUrl = dataset.getUidToUrl();
|
||||||
|
uidsLength = uids.length;
|
||||||
|
|
||||||
if (status_code < 200 ||
|
for (int i = 0; i < uidsLength; i++) {
|
||||||
status_code > 300) {
|
if (listener != null) {
|
||||||
throw new IOException("Error getting messages, returned HTTP Response code " + status_code);
|
listener.messageStarted(uids[i], i, uidsLength);
|
||||||
}
|
}
|
||||||
|
WebDavMessage message = new WebDavMessage(uids[i], this);
|
||||||
entity = response.getEntity();
|
message.setUrl(uidToUrl.get(uids[i]));
|
||||||
|
messages.add(message);
|
||||||
if (entity != null) {
|
|
||||||
try {
|
|
||||||
InputStream istream = entity.getContent();
|
|
||||||
SAXParserFactory spf = SAXParserFactory.newInstance();
|
|
||||||
SAXParser sp = spf.newSAXParser();
|
|
||||||
XMLReader xr = sp.getXMLReader();
|
|
||||||
WebDavHandler myHandler = new WebDavHandler();
|
|
||||||
ParsedDataSet dataset;
|
|
||||||
int uidsLength = 0;
|
|
||||||
int urlsLength = 0;
|
|
||||||
|
|
||||||
xr.setContentHandler(myHandler);
|
|
||||||
xr.parse(new InputSource(istream));
|
|
||||||
|
|
||||||
dataset = myHandler.getDataSet();
|
|
||||||
|
|
||||||
uids = dataset.getUids();
|
|
||||||
HashMap<String, String> uidToUrl = dataset.getUidToUrl();
|
|
||||||
uidsLength = uids.length;
|
|
||||||
|
|
||||||
for (int i = 0; i < uidsLength; i++) {
|
|
||||||
if (listener != null) {
|
|
||||||
listener.messageStarted(uids[i], i, uidsLength);
|
|
||||||
}
|
|
||||||
WebDavMessage message = new WebDavMessage(uids[i], this);
|
|
||||||
message.setUrl(uidToUrl.get(uids[i]));
|
|
||||||
messages.add(message);
|
|
||||||
|
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
listener.messageFinished(message, i, uidsLength);
|
listener.messageFinished(message, i, uidsLength);
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SAXException se) {
|
|
||||||
Log.e(Email.LOG_TAG, "SAXException in getMessages() " + se);
|
|
||||||
} catch (ParserConfigurationException pce) {
|
|
||||||
Log.e(Email.LOG_TAG, "ParserConfigurationException in getMessages() " + pce);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (UnsupportedEncodingException uee) {
|
|
||||||
Log.e(Email.LOG_TAG, "UnsupportedEncodingException: " + uee);
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.e(Email.LOG_TAG, "IOException: " + ioe);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return messages.toArray(new Message[] {});
|
return messages.toArray(new Message[] {});
|
||||||
|
@ -873,72 +822,16 @@ public class WebDavStore extends Store {
|
||||||
|
|
||||||
private HashMap<String, String> getMessageUrls(String[] uids) {
|
private HashMap<String, String> getMessageUrls(String[] uids) {
|
||||||
HashMap<String, String> uidToUrl = new HashMap<String, String>();
|
HashMap<String, String> uidToUrl = new HashMap<String, String>();
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
HashMap<String, String> headers = new HashMap<String, String>();
|
||||||
|
ParsedDataSet dataset = new ParsedDataSet();
|
||||||
String messageBody;
|
String messageBody;
|
||||||
|
|
||||||
/** Verify authentication */
|
|
||||||
if (needAuth()) {
|
|
||||||
authenticate();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (WebDavStore.this.mAuthenticated == false ||
|
|
||||||
WebDavStore.this.mAuthCookies == null) {
|
|
||||||
return uidToUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Retrieve and parse the XML entity for our messages */
|
/** Retrieve and parse the XML entity for our messages */
|
||||||
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
|
|
||||||
messageBody = getMessageUrlsXml(uids);
|
messageBody = getMessageUrlsXml(uids);
|
||||||
|
headers.put("Brief", "t");
|
||||||
|
|
||||||
try {
|
dataset = processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);
|
||||||
int status_code = -1;
|
uidToUrl = dataset.getUidToUrl();
|
||||||
StringEntity messageEntity = new StringEntity(messageBody);
|
|
||||||
HttpGeneric httpmethod = new HttpGeneric(this.mFolderUrl);
|
|
||||||
HttpResponse response;
|
|
||||||
HttpEntity entity;
|
|
||||||
|
|
||||||
messageEntity.setContentType("text/xml");
|
|
||||||
httpmethod.setMethod("SEARCH");
|
|
||||||
httpmethod.setEntity(messageEntity);
|
|
||||||
httpmethod.setHeader("Brief", "t");
|
|
||||||
|
|
||||||
response = httpclient.execute(httpmethod);
|
|
||||||
status_code = response.getStatusLine().getStatusCode();
|
|
||||||
|
|
||||||
if (status_code < 200 ||
|
|
||||||
status_code > 300) {
|
|
||||||
throw new IOException("Error getting messages, returned HTTP Response code " + status_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
entity = response.getEntity();
|
|
||||||
|
|
||||||
if (entity != null) {
|
|
||||||
try {
|
|
||||||
InputStream istream = entity.getContent();
|
|
||||||
SAXParserFactory spf = SAXParserFactory.newInstance();
|
|
||||||
SAXParser sp = spf.newSAXParser();
|
|
||||||
XMLReader xr = sp.getXMLReader();
|
|
||||||
WebDavHandler myHandler = new WebDavHandler();
|
|
||||||
ParsedDataSet dataset;
|
|
||||||
int uidsLength = 0;
|
|
||||||
int urlsLength = 0;
|
|
||||||
|
|
||||||
xr.setContentHandler(myHandler);
|
|
||||||
xr.parse(new InputSource(istream));
|
|
||||||
|
|
||||||
dataset = myHandler.getDataSet();
|
|
||||||
uidToUrl = dataset.getUidToUrl();
|
|
||||||
} catch (SAXException se) {
|
|
||||||
Log.e(Email.LOG_TAG, "SAXException in getMessages() " + se);
|
|
||||||
} catch (ParserConfigurationException pce) {
|
|
||||||
Log.e(Email.LOG_TAG, "ParserConfigurationException in getMessages() " + pce);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException uee) {
|
|
||||||
Log.e(Email.LOG_TAG, "UnsupportedEncodingException: " + uee);
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.e(Email.LOG_TAG, "IOException: " + ioe);
|
|
||||||
}
|
|
||||||
|
|
||||||
return uidToUrl;
|
return uidToUrl;
|
||||||
}
|
}
|
||||||
|
@ -1013,7 +906,7 @@ public class WebDavStore extends Store {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If fetch is called outside of the initial list (ie, a locally stored
|
* If fetch is called outside of the initial list (ie, a locally stored
|
||||||
* stored message), it may not have a URL associated. Verify and fix that
|
* message), it may not have a URL associated. Verify and fix that
|
||||||
*/
|
*/
|
||||||
if (wdMessage.getUrl().equals("")) {
|
if (wdMessage.getUrl().equals("")) {
|
||||||
wdMessage.setUrl(getMessageUrls(new String[] {wdMessage.getUid()}).get(wdMessage.getUid()));
|
wdMessage.setUrl(getMessageUrls(new String[] {wdMessage.getUid()}).get(wdMessage.getUid()));
|
||||||
|
@ -1086,7 +979,8 @@ public class WebDavStore extends Store {
|
||||||
*/
|
*/
|
||||||
private void fetchFlags(Message[] startMessages, MessageRetrievalListener listener) throws MessagingException {
|
private void fetchFlags(Message[] startMessages, MessageRetrievalListener listener) throws MessagingException {
|
||||||
HashMap<String, Boolean> uidToReadStatus = new HashMap<String, Boolean>();
|
HashMap<String, Boolean> uidToReadStatus = new HashMap<String, Boolean>();
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
HashMap<String, String> headers = new HashMap<String, String>();
|
||||||
|
ParsedDataSet dataset = new ParsedDataSet();
|
||||||
String messageBody = new String();
|
String messageBody = new String();
|
||||||
Message[] messages = new Message[20];
|
Message[] messages = new Message[20];
|
||||||
String[] uids;
|
String[] uids;
|
||||||
|
@ -1118,56 +1012,11 @@ public class WebDavStore extends Store {
|
||||||
uids[i] = messages[i].getUid();
|
uids[i] = messages[i].getUid();
|
||||||
}
|
}
|
||||||
|
|
||||||
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
|
|
||||||
messageBody = getMessageFlagsXml(uids);
|
messageBody = getMessageFlagsXml(uids);
|
||||||
|
headers.put("Brief", "t");
|
||||||
|
dataset = processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);
|
||||||
|
|
||||||
try {
|
uidToReadStatus = dataset.getUidToRead();
|
||||||
int status_code = -1;
|
|
||||||
StringEntity messageEntity = new StringEntity(messageBody);
|
|
||||||
HttpGeneric httpmethod = new HttpGeneric(this.mFolderUrl);
|
|
||||||
HttpResponse response;
|
|
||||||
HttpEntity entity;
|
|
||||||
|
|
||||||
messageEntity.setContentType("text/xml");
|
|
||||||
httpmethod.setMethod("SEARCH");
|
|
||||||
httpmethod.setEntity(messageEntity);
|
|
||||||
httpmethod.setHeader("Brief", "t");
|
|
||||||
|
|
||||||
response = httpclient.execute(httpmethod);
|
|
||||||
status_code = response.getStatusLine().getStatusCode();
|
|
||||||
|
|
||||||
if (status_code < 200 ||
|
|
||||||
status_code > 300) {
|
|
||||||
throw new IOException("Error getting message flags, returned HTTP Response code " + status_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
entity = response.getEntity();
|
|
||||||
|
|
||||||
if (entity != null) {
|
|
||||||
try {
|
|
||||||
InputStream istream = entity.getContent();
|
|
||||||
SAXParserFactory spf = SAXParserFactory.newInstance();
|
|
||||||
SAXParser sp = spf.newSAXParser();
|
|
||||||
XMLReader xr = sp.getXMLReader();
|
|
||||||
WebDavHandler myHandler = new WebDavHandler();
|
|
||||||
ParsedDataSet dataset;
|
|
||||||
|
|
||||||
xr.setContentHandler(myHandler);
|
|
||||||
xr.parse(new InputSource(istream));
|
|
||||||
|
|
||||||
dataset = myHandler.getDataSet();
|
|
||||||
uidToReadStatus = dataset.getUidToRead();
|
|
||||||
} catch (SAXException se) {
|
|
||||||
Log.e(Email.LOG_TAG, "SAXException in fetch() " + se);
|
|
||||||
} catch (ParserConfigurationException pce) {
|
|
||||||
Log.e(Email.LOG_TAG, "ParserConfigurationException in fetch() " + pce);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException uee) {
|
|
||||||
Log.e(Email.LOG_TAG, "UnsupportedEncodingException: " + uee);
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.e(Email.LOG_TAG, "IOException: " + ioe);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0, count = messages.length; i < count; i++) {
|
for (int i = 0, count = messages.length; i < count; i++) {
|
||||||
if (!(messages[i] instanceof WebDavMessage)) {
|
if (!(messages[i] instanceof WebDavMessage)) {
|
||||||
|
@ -1195,7 +1044,12 @@ public class WebDavStore extends Store {
|
||||||
*/
|
*/
|
||||||
private void fetchEnvelope(Message[] startMessages, MessageRetrievalListener listener) throws MessagingException {
|
private void fetchEnvelope(Message[] startMessages, MessageRetrievalListener listener) throws MessagingException {
|
||||||
HashMap<String, ParsedMessageEnvelope> envelopes = new HashMap<String, ParsedMessageEnvelope>();
|
HashMap<String, ParsedMessageEnvelope> envelopes = new HashMap<String, ParsedMessageEnvelope>();
|
||||||
|
HashMap<String, String> headers = new HashMap<String, String>();
|
||||||
|
ParsedDataSet dataset = new ParsedDataSet();
|
||||||
|
String messageBody = new String();
|
||||||
|
String[] uids;
|
||||||
Message[] messages = new Message[10];
|
Message[] messages = new Message[10];
|
||||||
|
|
||||||
if (startMessages == null ||
|
if (startMessages == null ||
|
||||||
startMessages.length == 0) {
|
startMessages.length == 0) {
|
||||||
return;
|
return;
|
||||||
|
@ -1210,71 +1064,23 @@ public class WebDavStore extends Store {
|
||||||
newMessages[i - 10] = startMessages[i];
|
newMessages[i - 10] = startMessages[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/** System.arraycopy(startMessages, 0, messages, 0, 10);
|
|
||||||
System.arraycopy(startMessages, 10, newMessages, 0, startMessages.length - 10);*/
|
|
||||||
fetchEnvelope(newMessages, listener);
|
fetchEnvelope(newMessages, listener);
|
||||||
} else {
|
} else {
|
||||||
messages = startMessages;
|
messages = startMessages;
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
uids = new String[messages.length];
|
||||||
String messageBody = new String();
|
|
||||||
String[] uids = new String[messages.length];
|
|
||||||
|
|
||||||
for (int i = 0, count = messages.length; i < count; i++) {
|
for (int i = 0, count = messages.length; i < count; i++) {
|
||||||
uids[i] = messages[i].getUid();
|
uids[i] = messages[i].getUid();
|
||||||
}
|
}
|
||||||
|
|
||||||
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
|
|
||||||
messageBody = getMessageEnvelopeXml(uids);
|
messageBody = getMessageEnvelopeXml(uids);
|
||||||
|
headers.put("Brief", "t");
|
||||||
|
dataset = processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);
|
||||||
|
|
||||||
try {
|
envelopes = dataset.getMessageEnvelopes();
|
||||||
int status_code = -1;
|
|
||||||
StringEntity messageEntity = new StringEntity(messageBody);
|
|
||||||
HttpGeneric httpmethod = new HttpGeneric(this.mFolderUrl);
|
|
||||||
HttpResponse response;
|
|
||||||
HttpEntity entity;
|
|
||||||
|
|
||||||
messageEntity.setContentType("text/xml");
|
|
||||||
httpmethod.setMethod("SEARCH");
|
|
||||||
httpmethod.setEntity(messageEntity);
|
|
||||||
httpmethod.setHeader("Brief", "t");
|
|
||||||
|
|
||||||
response = httpclient.execute(httpmethod);
|
|
||||||
status_code = response.getStatusLine().getStatusCode();
|
|
||||||
|
|
||||||
if (status_code < 200 ||
|
|
||||||
status_code > 300) {
|
|
||||||
throw new IOException("Error getting message flags, returned HTTP Response code " + status_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
entity = response.getEntity();
|
|
||||||
|
|
||||||
if (entity != null) {
|
|
||||||
try {
|
|
||||||
InputStream istream = entity.getContent();
|
|
||||||
SAXParserFactory spf = SAXParserFactory.newInstance();
|
|
||||||
SAXParser sp = spf.newSAXParser();
|
|
||||||
XMLReader xr = sp.getXMLReader();
|
|
||||||
WebDavHandler myHandler = new WebDavHandler();
|
|
||||||
ParsedDataSet dataset;
|
|
||||||
|
|
||||||
xr.setContentHandler(myHandler);
|
|
||||||
xr.parse(new InputSource(istream));
|
|
||||||
|
|
||||||
dataset = myHandler.getDataSet();
|
|
||||||
envelopes = dataset.getMessageEnvelopes();
|
|
||||||
} catch (SAXException se) {
|
|
||||||
Log.e(Email.LOG_TAG, "SAXException in fetch() " + se);
|
|
||||||
} catch (ParserConfigurationException pce) {
|
|
||||||
Log.e(Email.LOG_TAG, "ParserConfigurationException in fetch() " + pce);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException uee) {
|
|
||||||
Log.e(Email.LOG_TAG, "UnsupportedEncodingException: " + uee);
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.e(Email.LOG_TAG, "IOException: " + ioe);
|
|
||||||
}
|
|
||||||
|
|
||||||
int count = messages.length;
|
int count = messages.length;
|
||||||
for (int i = messages.length - 1; i >= 0; i--) {
|
for (int i = messages.length - 1; i >= 0; i--) {
|
||||||
|
@ -1332,88 +1138,43 @@ public class WebDavStore extends Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void markServerMessagesRead(String[] uids) throws MessagingException {
|
private void markServerMessagesRead(String[] uids) throws MessagingException {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
|
||||||
String messageBody = new String();
|
String messageBody = new String();
|
||||||
|
HashMap<String, String> headers = new HashMap<String, String>();
|
||||||
HashMap<String, String> uidToUrl = getMessageUrls(uids);
|
HashMap<String, String> uidToUrl = getMessageUrls(uids);
|
||||||
|
ParsedDataSet dataset = new ParsedDataSet();
|
||||||
String[] urls = new String[uids.length];
|
String[] urls = new String[uids.length];
|
||||||
|
|
||||||
for (int i = 0, count = uids.length; i < count; i++) {
|
for (int i = 0, count = uids.length; i < count; i++) {
|
||||||
urls[i] = uidToUrl.get(uids[i]);
|
urls[i] = uidToUrl.get(uids[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
|
|
||||||
messageBody = getMarkMessagesReadXml(urls);
|
messageBody = getMarkMessagesReadXml(urls);
|
||||||
|
headers.put("Brief", "t");
|
||||||
|
headers.put("If-Match", "*");
|
||||||
|
|
||||||
try {
|
processRequest(this.mFolderUrl, "BPROPPATCH", messageBody, headers, false);
|
||||||
int status_code = -1;
|
|
||||||
StringEntity messageEntity = new StringEntity(messageBody);
|
|
||||||
HttpGeneric httpmethod = new HttpGeneric(this.mFolderUrl);
|
|
||||||
HttpResponse response;
|
|
||||||
HttpEntity entity;
|
|
||||||
|
|
||||||
messageEntity.setContentType("text/xml");
|
|
||||||
httpmethod.setMethod("BPROPPATCH");
|
|
||||||
httpmethod.setEntity(messageEntity);
|
|
||||||
httpmethod.setHeader("Brief", "t");
|
|
||||||
httpmethod.setHeader("If-Match", "*");
|
|
||||||
|
|
||||||
response = httpclient.execute(httpmethod);
|
|
||||||
status_code = response.getStatusLine().getStatusCode();
|
|
||||||
|
|
||||||
if (status_code < 200 ||
|
|
||||||
status_code > 300) {
|
|
||||||
throw new IOException("Error marking messages as read, returned HTTP Response code " + status_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
entity = response.getEntity();
|
|
||||||
|
|
||||||
} catch (UnsupportedEncodingException uee) {
|
|
||||||
Log.e(Email.LOG_TAG, "UnsupportedEncodingException: " + uee);
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.e(Email.LOG_TAG, "IOException: " + ioe);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteServerMessages(String[] uids) throws MessagingException {
|
private void deleteServerMessages(String[] uids) throws MessagingException {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
|
||||||
HashMap<String, String> uidToUrl = getMessageUrls(uids);
|
HashMap<String, String> uidToUrl = getMessageUrls(uids);
|
||||||
String[] urls = new String[uids.length];
|
String[] urls = new String[uids.length];
|
||||||
|
|
||||||
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
|
|
||||||
|
|
||||||
for (int i = 0, count = uids.length; i < count; i++) {
|
for (int i = 0, count = uids.length; i < count; i++) {
|
||||||
try {
|
HashMap<String, String> headers = new HashMap<String, String>();
|
||||||
int status_code = -1;
|
String uid = uids[i];
|
||||||
String uid = uids[i];
|
String url = uidToUrl.get(uid);
|
||||||
String url = uidToUrl.get(uids[i]);
|
String destinationUrl = generateDeleteUrl(url);
|
||||||
HttpGeneric httpmethod = new HttpGeneric(url);
|
|
||||||
HttpResponse response;
|
|
||||||
HttpEntity entity;
|
|
||||||
String destinationUrl = generateDeleteUrl(url);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the destination is the same as the origin, assume delete forever
|
* If the destination is the same as the origin, assume delete forever
|
||||||
*/
|
*/
|
||||||
if (destinationUrl.equals(url)) {
|
if (destinationUrl.equals(url)) {
|
||||||
httpmethod.setMethod("DELETE");
|
headers.put("Brief", "t");
|
||||||
httpmethod.setHeader("Brief", "t");
|
processRequest(url, "DELETE", null, headers, false);
|
||||||
} else {
|
} else {
|
||||||
httpmethod.setMethod("MOVE");
|
headers.put("Destination", generateDeleteUrl(url));
|
||||||
httpmethod.setHeader("Destination", generateDeleteUrl(url));
|
headers.put("Brief", "t");
|
||||||
httpmethod.setHeader("Brief", "t");
|
processRequest(url, "MOVE", null, headers, false);
|
||||||
}
|
|
||||||
|
|
||||||
response = httpclient.execute(httpmethod);
|
|
||||||
status_code = response.getStatusLine().getStatusCode();
|
|
||||||
|
|
||||||
if (status_code < 200 ||
|
|
||||||
status_code > 300) {
|
|
||||||
throw new IOException("Error deleting message url, Response Code: "+status_code);
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException uee) {
|
|
||||||
Log.e(Email.LOG_TAG, "UnsupportedEncodingException: " + uee);
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.e(Email.LOG_TAG, "IOException: " + ioe);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue