PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks-mail/src/main/java/com/google/sitebricks/mail/imap/MessageExtractor.java

http://github.com/dhanji/sitebricks
Java | 68 lines | 46 code | 8 blank | 14 comment | 6 complexity | f2ca6af994011c30821c74670be66ace MD5 | raw file
 1package com.google.sitebricks.mail.imap;
 2
 3import com.google.common.collect.Lists;
 4
 5import java.util.List;
 6
 7/**
 8 * Extracts a Message from a complete IMAP fetch. Specifically
 9 * a "fetch full" command which comes back with subject, sender, uid
10 * internaldate and rfc822.size (length) and all body parts.
11 * <p>
12 * This is the more robust form of {@link MessageStatus}, which should
13 * be preferred for fetching just subjects/status info as this extraction
14 * mechanism can be slower for messages with large bodies.
15 *
16 * @author dhanji@gmail.com (Dhanji R. Prasanna)
17 */
18class MessageExtractor implements Extractor<List<MessageStatus>> {
19  private static final String ENVELOPE_PREFIX = "(ENVELOPE ";
20  private static final String INTERNALDATE = "INTERNALDATE";
21
22  @Override
23  public List<MessageStatus> extract(List<String> messages) {
24    List<MessageStatus> statuses = Lists.newArrayList();
25    for (String message : messages) {
26      String[] split = message.split("[ ]+", 3);
27
28      // Only parse Fetch responses.
29      if (split.length > 1 && "FETCH".equalsIgnoreCase(split[1])) {
30        // Strip the "XX FETCH" sequence prefix first.
31//        statuses.add(parseEnvelope(split[2]));
32      }
33    }
34
35    return statuses;
36  }
37
38
39  private static List<String> tokenize(String message) {
40    List<String> pieces = Lists.newArrayList();
41    char[] chars = message.toCharArray();
42    boolean inString = false;
43    StringBuilder token = new StringBuilder();
44    for (int i = 0; i < chars.length; i++) {
45      char c = chars[i];
46      if (c == '"') {
47
48        // Close of string, bake this token.
49        if (inString) {
50          pieces.add(token.toString().trim());
51          token = new StringBuilder();
52          inString = false;
53        } else
54          inString = true;
55
56        continue;
57      }
58
59      // Skip parentheticals
60      if (!inString && (c == '(' || c == ')')) {
61        continue;
62      }
63
64      token.append(c);
65    }
66    return pieces;
67  }
68}