Esempio n. 1
0
  /**
   * Fetches and parses the message envelopes for the supplied messages. The idea is to have this be
   * recursive so that we do a series of medium calls instead of one large massive call or a large
   * number of smaller calls. Call it a happy balance
   */
  private void fetchEnvelope(
      List<WebDavMessage> startMessages, MessageRetrievalListener<WebDavMessage> listener)
      throws MessagingException {
    Map<String, String> headers = new HashMap<String, String>();
    String messageBody;
    String[] uids;
    List<WebDavMessage> messages = new ArrayList<WebDavMessage>(10);

    if (startMessages == null || startMessages.isEmpty()) {
      return;
    }

    if (startMessages.size() > 10) {
      List<WebDavMessage> newMessages = new ArrayList<WebDavMessage>(startMessages.size() - 10);
      for (int i = 0, count = startMessages.size(); i < count; i++) {
        if (i < 10) {
          messages.add(i, startMessages.get(i));
        } else {
          newMessages.add(i - 10, startMessages.get(i));
        }
      }

      fetchEnvelope(newMessages, listener);
    } else {
      messages.addAll(startMessages);
    }

    uids = new String[messages.size()];

    for (int i = 0, count = messages.size(); i < count; i++) {
      uids[i] = messages.get(i).getUid();
    }

    messageBody = store.getMessageEnvelopeXml(uids);
    headers.put("Brief", "t");
    DataSet dataset = store.processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);

    Map<String, ParsedMessageEnvelope> envelopes = dataset.getMessageEnvelopes();

    int count = messages.size();
    for (int i = messages.size() - 1; i >= 0; i--) {
      WebDavMessage message = messages.get(i);
      if (listener != null) {
        listener.messageStarted(messages.get(i).getUid(), i, count);
      }

      ParsedMessageEnvelope envelope = envelopes.get(message.getUid());
      if (envelope != null) {
        message.setNewHeaders(envelope);
        message.setFlagInternal(Flag.SEEN, envelope.getReadStatus());
      } else {
        Log.e(LOG_TAG, "Asked to get metadata for a non-existent message: " + message.getUid());
      }

      if (listener != null) {
        listener.messageFinished(messages.get(i), i, count);
      }
    }
  }
Esempio n. 2
0
  /**
   * Fetches and sets the message flags for the supplied messages. The idea is to have this be
   * recursive so that we do a series of medium calls instead of one large massive call or a large
   * number of smaller calls.
   */
  private void fetchFlags(
      List<WebDavMessage> startMessages, MessageRetrievalListener<WebDavMessage> listener)
      throws MessagingException {
    HashMap<String, String> headers = new HashMap<String, String>();
    String messageBody;
    List<Message> messages = new ArrayList<Message>(20);
    String[] uids;

    if (startMessages == null || startMessages.isEmpty()) {
      return;
    }

    if (startMessages.size() > 20) {
      List<WebDavMessage> newMessages = new ArrayList<WebDavMessage>(startMessages.size() - 20);
      for (int i = 0, count = startMessages.size(); i < count; i++) {
        if (i < 20) {
          messages.add(startMessages.get(i));
        } else {
          newMessages.add(startMessages.get(i));
        }
      }

      fetchFlags(newMessages, listener);
    } else {
      messages.addAll(startMessages);
    }

    uids = new String[messages.size()];

    for (int i = 0, count = messages.size(); i < count; i++) {
      uids[i] = messages.get(i).getUid();
    }

    messageBody = store.getMessageFlagsXml(uids);
    headers.put("Brief", "t");
    DataSet dataset = store.processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);

    if (dataset == null) {
      throw new MessagingException("Data Set from request was null");
    }

    Map<String, Boolean> uidToReadStatus = dataset.getUidToRead();

    for (int i = 0, count = messages.size(); i < count; i++) {
      if (!(messages.get(i) instanceof WebDavMessage)) {
        throw new MessagingException("WebDavStore fetch called with non-WebDavMessage");
      }
      WebDavMessage wdMessage = (WebDavMessage) messages.get(i);

      try {
        wdMessage.setFlagInternal(Flag.SEEN, uidToReadStatus.get(wdMessage.getUid()));
      } catch (NullPointerException e) {
        Log.v(
            LOG_TAG,
            "Under some weird circumstances, setting the read status when syncing from webdav threw an NPE. Skipping.");
      }
    }
  }
Esempio n. 3
0
  /**
   * 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);
      }
    }
  }