public List<? extends Message> appendWebDavMessages(List<? extends Message> messages) throws MessagingException { List<Message> retMessages = new ArrayList<Message>(messages.size()); WebDavHttpClient httpclient = store.getHttpClient(); for (Message message : messages) { HttpGeneric httpmethod; HttpResponse response; StringEntity bodyEntity; int statusCode; try { ByteArrayOutputStream out; out = new ByteArrayOutputStream(message.getSize()); open(Folder.OPEN_MODE_RW); EOLConvertingOutputStream msgOut = new EOLConvertingOutputStream(new BufferedOutputStream(out, 1024)); message.writeTo(msgOut); msgOut.flush(); bodyEntity = new StringEntity(out.toString(), "UTF-8"); bodyEntity.setContentType("message/rfc822"); String messageURL = mFolderUrl; if (!messageURL.endsWith("/")) { messageURL += "/"; } messageURL += encodeUtf8(message.getUid() + ":" + System.currentTimeMillis() + ".eml"); Log.i(LOG_TAG, "Uploading message as " + messageURL); httpmethod = new HttpGeneric(messageURL); httpmethod.setMethod("PUT"); httpmethod.setEntity(bodyEntity); String mAuthString = store.getAuthString(); if (mAuthString != null) { httpmethod.setHeader("Authorization", mAuthString); } response = httpclient.executeOverride(httpmethod, store.getContext()); statusCode = response.getStatusLine().getStatusCode(); if (statusCode < 200 || statusCode > 300) { // TODO: Could we handle a login timeout here? throw new IOException( "Error with status code " + statusCode + " while sending/appending message. Response = " + response.getStatusLine().toString() + " for message " + messageURL); } WebDavMessage retMessage = new WebDavMessage(message.getUid(), this); retMessage.setUrl(messageURL); retMessages.add(retMessage); } catch (Exception e) { throw new MessagingException("Unable to append", e); } } return retMessages; }
@Override public void open(int mode) throws MessagingException { store.getHttpClient(); this.mIsOpen = true; }
/** * Fetches the full messages or up to {@param lines} lines and passes them to the message parser. */ private void fetchMessages( List<WebDavMessage> messages, MessageRetrievalListener<WebDavMessage> listener, int lines) throws MessagingException { WebDavHttpClient httpclient; httpclient = store.getHttpClient(); /** We can't hand off to processRequest() since we need the stream to parse. */ for (int i = 0, count = messages.size(); i < count; i++) { WebDavMessage wdMessage = messages.get(i); int statusCode = 0; if (listener != null) { listener.messageStarted(wdMessage.getUid(), i, count); } /** * If fetch is called outside of the initial list (ie, a locally stored message), it may not * have a URL associated. Verify and fix that */ if (wdMessage.getUrl().equals("")) { wdMessage.setUrl(getMessageUrls(new String[] {wdMessage.getUid()}).get(wdMessage.getUid())); Log.i( LOG_TAG, "Fetching messages with UID = '" + wdMessage.getUid() + "', URL = '" + wdMessage.getUrl() + "'"); if (wdMessage.getUrl().equals("")) { throw new MessagingException("Unable to get URL for message"); } } try { Log.i( LOG_TAG, "Fetching message with UID = '" + wdMessage.getUid() + "', URL = '" + wdMessage.getUrl() + "'"); HttpGet httpget = new HttpGet(new URI(wdMessage.getUrl())); HttpResponse response; HttpEntity entity; httpget.setHeader("translate", "f"); if (store.getAuthentication() == WebDavConstants.AUTH_TYPE_BASIC) { httpget.setHeader("Authorization", store.getAuthString()); } response = httpclient.executeOverride(httpget, store.getContext()); statusCode = response.getStatusLine().getStatusCode(); entity = response.getEntity(); if (statusCode < 200 || statusCode > 300) { throw new IOException( "Error during with code " + statusCode + " during fetch: " + response.getStatusLine().toString()); } if (entity != null) { InputStream istream = null; StringBuilder buffer = new StringBuilder(); String tempText; String resultText; BufferedReader reader = null; int currentLines = 0; try { istream = WebDavHttpClient.getUngzippedContent(entity); if (lines != -1) { // Convert the ungzipped input stream into a StringBuilder // containing the given line count reader = new BufferedReader(new InputStreamReader(istream), 8192); while ((tempText = reader.readLine()) != null && (currentLines < lines)) { buffer.append(tempText).append("\r\n"); currentLines++; } IOUtils.closeQuietly(istream); resultText = buffer.toString(); istream = new ByteArrayInputStream(resultText.getBytes("UTF-8")); } // Parse either the entire message stream, or a stream of the given lines wdMessage.parse(istream); } catch (IOException ioe) { Log.e( LOG_TAG, "IOException: " + ioe.getMessage() + "\nTrace: " + WebDavUtils.processException(ioe)); throw new MessagingException("I/O Error", ioe); } finally { IOUtils.closeQuietly(reader); IOUtils.closeQuietly(istream); } } else { Log.v(LOG_TAG, "Empty response"); } } catch (IllegalArgumentException iae) { Log.e( LOG_TAG, "IllegalArgumentException caught " + iae + "\nTrace: " + WebDavUtils.processException(iae)); throw new MessagingException("IllegalArgumentException caught", iae); } catch (URISyntaxException use) { Log.e( LOG_TAG, "URISyntaxException caught " + use + "\nTrace: " + WebDavUtils.processException(use)); throw new MessagingException("URISyntaxException caught", use); } catch (IOException ioe) { Log.e( LOG_TAG, "Non-success response code loading message, response code was " + statusCode + "\nURL: " + wdMessage.getUrl() + "\nError: " + ioe.getMessage() + "\nTrace: " + WebDavUtils.processException(ioe)); throw new MessagingException("Failure code " + statusCode, ioe); } if (listener != null) { listener.messageFinished(wdMessage, i, count); } } }