Exemple #1
0
  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;
  }
 private void fetchEnvelope(
     List<Pop3Message> messages, MessageRetrievalListener<Pop3Message> listener)
     throws IOException, MessagingException {
   int unsizedMessages = 0;
   for (Message message : messages) {
     if (message.getSize() == -1) {
       unsizedMessages++;
     }
   }
   if (unsizedMessages == 0) {
     return;
   }
   if (unsizedMessages < 50 && mMessageCount > 5000) {
     /*
      * In extreme cases we'll do a command per message instead of a bulk request
      * to hopefully save some time and bandwidth.
      */
     for (int i = 0, count = messages.size(); i < count; i++) {
       Pop3Message message = messages.get(i);
       if (listener != null) {
         listener.messageStarted(message.getUid(), i, count);
       }
       String response =
           executeSimpleCommand(
               String.format(
                   Locale.US, LIST_COMMAND + " %d", mUidToMsgNumMap.get(message.getUid())));
       String[] listParts = response.split(" ");
       // int msgNum = Integer.parseInt(listParts[1]);
       int msgSize = Integer.parseInt(listParts[2]);
       message.setSize(msgSize);
       if (listener != null) {
         listener.messageFinished(message, i, count);
       }
     }
   } else {
     Set<String> msgUidIndex = new HashSet<String>();
     for (Message message : messages) {
       msgUidIndex.add(message.getUid());
     }
     int i = 0, count = messages.size();
     String response = executeSimpleCommand(LIST_COMMAND);
     while ((response = readLine()) != null) {
       if (response.equals(".")) {
         break;
       }
       String[] listParts = response.split(" ");
       int msgNum = Integer.parseInt(listParts[0]);
       int msgSize = Integer.parseInt(listParts[1]);
       Pop3Message pop3Message = mMsgNumToMsgMap.get(msgNum);
       if (pop3Message != null && msgUidIndex.contains(pop3Message.getUid())) {
         if (listener != null) {
           listener.messageStarted(pop3Message.getUid(), i, count);
         }
         pop3Message.setSize(msgSize);
         if (listener != null) {
           listener.messageFinished(pop3Message, i, count);
         }
         i++;
       }
     }
   }
 }