Search in sources :

Example 1 with MailContainerModel

use of org.dataportabilityproject.dataModels.mail.MailContainerModel in project data-transfer-project by google.

the class ImapMailHelper method getMessages.

/**
 * Get all messages in an account.
 */
private MailModelWrapper getMessages(String host, String account, String password, Folder parentFolder, boolean fetchMessages, PaginationInformation paginationInformation) throws MessagingException, IOException {
    int foldersSize = 0;
    // Find containers to and be imported
    ImmutableCollection.Builder<MailContainerModel> folders = ImmutableList.builder();
    ImmutableCollection.Builder<Resource> folderIds = ImmutableList.builder();
    log("Calling list for folder: %s", parentFolder.getName());
    Folder[] subFolders = parentFolder.list();
    log("Folder: %s, subFolders: %d", parentFolder.getName(), subFolders.length);
    for (Folder folder : subFolders) {
        // This will tell the framework to create this folder on import
        folders.add(new MailContainerModel(folder.getName(), folder.getFullName()));
        // Content for these resources will be 'fetched' by the framework
        folderIds.add(new IdOnlyResource(folder.getName()));
        foldersSize++;
    }
    log("foldersSize: %d", foldersSize);
    // Get messages in the folder
    ImmutableCollection.Builder<MailMessageModel> resources = ImmutableList.builder();
    log("fetchMessages: %b", fetchMessages);
    PaginationInformation nextPaginationInformation = null;
    if (fetchMessages) {
        parentFolder.open(Folder.READ_ONLY);
        int start = getStart(paginationInformation);
        int end = getEnd(start, parentFolder.getMessageCount());
        if (end < parentFolder.getMessageCount()) {
            // Indicates page to be fetched on next request
            nextPaginationInformation = new IntPaginationToken(end + 1);
        }
        log("Fetching messages for foder: %s, start: %d, end: %d", parentFolder.getFullName(), start, end);
        Message[] messages = parentFolder.getMessages(start, end);
        log("Fetched message for folder: %s, messages: %s", parentFolder.getFullName(), messages.length);
        for (Message message : messages) {
            log("Message, contentType: %s ,size: %s", message.getContentType(), message.getSize());
            ImmutableList<String> folderId = ImmutableList.of(parentFolder.getName());
            resources.add(new MailMessageModel(createRawMessage(message), folderId));
        }
        parentFolder.close(false);
    }
    // TODO: add pagination below
    return new MailModelWrapper(folders.build(), resources.build(), new ContinuationInformation(folderIds.build(), nextPaginationInformation));
}
Also used : IntPaginationToken(org.dataportabilityproject.shared.IntPaginationToken) ImmutableCollection(com.google.common.collect.ImmutableCollection) Message(javax.mail.Message) Resource(org.dataportabilityproject.dataModels.Resource) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) Folder(javax.mail.Folder) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) MailContainerModel(org.dataportabilityproject.dataModels.mail.MailContainerModel) MailModelWrapper(org.dataportabilityproject.dataModels.mail.MailModelWrapper) MailMessageModel(org.dataportabilityproject.dataModels.mail.MailMessageModel) PaginationInformation(org.dataportabilityproject.dataModels.PaginationInformation)

Aggregations

ImmutableCollection (com.google.common.collect.ImmutableCollection)1 Folder (javax.mail.Folder)1 Message (javax.mail.Message)1 ContinuationInformation (org.dataportabilityproject.dataModels.ContinuationInformation)1 PaginationInformation (org.dataportabilityproject.dataModels.PaginationInformation)1 Resource (org.dataportabilityproject.dataModels.Resource)1 MailContainerModel (org.dataportabilityproject.dataModels.mail.MailContainerModel)1 MailMessageModel (org.dataportabilityproject.dataModels.mail.MailMessageModel)1 MailModelWrapper (org.dataportabilityproject.dataModels.mail.MailModelWrapper)1 IdOnlyResource (org.dataportabilityproject.shared.IdOnlyResource)1 IntPaginationToken (org.dataportabilityproject.shared.IntPaginationToken)1