Search in sources :

Example 1 with MailMessageModel

use of org.datatransferproject.types.common.models.mail.MailMessageModel in project data-transfer-project by google.

the class GoogleMailExporter method export.

@Override
public ExportResult<MailContainerResource> export(UUID id, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
    // Create a new gmail service for the authorized user
    Gmail gmail = getOrCreateGmail(authData);
    Messages.List request = null;
    try {
        request = gmail.users().messages().list(USER).setMaxResults(PAGE_SIZE);
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    if (exportInformation.isPresent() && exportInformation.get().getPaginationData() != null) {
        request.setPageToken(((StringPaginationToken) exportInformation.get().getPaginationData()).getToken());
    }
    ListMessagesResponse response = null;
    try {
        response = request.execute();
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    List<MailMessageModel> results = new ArrayList<>(response.getMessages().size());
    // as we can't store all the mail messages in memory at once.
    for (Message listMessage : response.getMessages()) {
        Message getResponse = null;
        try {
            getResponse = gmail.users().messages().get(USER, listMessage.getId()).setFormat("raw").execute();
        } catch (IOException e) {
            return new ExportResult<>(e);
        }
        // TODO: note this doesn't transfer things like labels
        results.add(new MailMessageModel(getResponse.getRaw(), getResponse.getLabelIds()));
    }
    PaginationData newPage = null;
    ResultType resultType = ResultType.END;
    if (response.getNextPageToken() != null) {
        newPage = new StringPaginationToken(response.getNextPageToken());
        resultType = ResultType.CONTINUE;
    }
    MailContainerResource mailContainerResource = new MailContainerResource(null, results);
    return new ExportResult<>(resultType, mailContainerResource, new ContinuationData(newPage));
}
Also used : Messages(com.google.api.services.gmail.Gmail.Users.Messages) PaginationData(org.datatransferproject.types.common.PaginationData) Message(com.google.api.services.gmail.model.Message) Gmail(com.google.api.services.gmail.Gmail) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) MailMessageModel(org.datatransferproject.types.common.models.mail.MailMessageModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 2 with MailMessageModel

use of org.datatransferproject.types.common.models.mail.MailMessageModel in project data-transfer-project by google.

the class MailContainerResourceTest method verifySerializeDeserialize.

@Test
public void verifySerializeDeserialize() throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerSubtypes(MailContainerResource.class);
    List<MailContainerModel> containers = ImmutableList.of(new MailContainerModel("id1", "container1"), new MailContainerModel("id2", "container2"));
    List<MailMessageModel> messages = ImmutableList.of(new MailMessageModel("foo", ImmutableList.of("1")), new MailMessageModel("bar", ImmutableList.of("1", "2'")));
    ContainerResource data = new MailContainerResource(containers, messages);
    String serialized = objectMapper.writeValueAsString(data);
    ContainerResource deserializedModel = objectMapper.readValue(serialized, ContainerResource.class);
    Truth.assertThat(deserializedModel).isNotNull();
    Truth.assertThat(deserializedModel).isInstanceOf(MailContainerResource.class);
    MailContainerResource deserialized = (MailContainerResource) deserializedModel;
    Truth.assertThat(deserialized.getMessages()).hasSize(2);
    Truth.assertThat(deserialized.getFolders()).hasSize(2);
    Truth.assertThat(deserialized).isEqualTo(data);
}
Also used : ContainerResource(org.datatransferproject.types.common.models.ContainerResource) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) MailContainerModel(org.datatransferproject.types.common.models.mail.MailContainerModel) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) MailMessageModel(org.datatransferproject.types.common.models.mail.MailMessageModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with MailMessageModel

use of org.datatransferproject.types.common.models.mail.MailMessageModel in project data-transfer-project by google.

the class GoogleMailExporterTest method exportMessagesFirstSet.

@Test
public void exportMessagesFirstSet() throws IOException {
    setUpSingleMessageResponse();
    // Looking at first page, with at least one page after it
    messageListResponse.setNextPageToken(NEXT_TOKEN);
    // Run test
    ExportResult<MailContainerResource> result = googleMailExporter.export(JOB_ID, null, Optional.empty());
    // Check results
    // Verify correct methods were called
    InOrder inOrder = Mockito.inOrder(messages, messageListRequest, get);
    // First request
    inOrder.verify(messages).list(GoogleMailExporter.USER);
    inOrder.verify(messageListRequest).setMaxResults(GoogleMailExporter.PAGE_SIZE);
    verify(messageListRequest, never()).setPageToken(anyString());
    // Second request
    inOrder.verify(messages).get(GoogleMailExporter.USER, MESSAGE_ID);
    inOrder.verify(get).setFormat("raw");
    inOrder.verify(get).execute();
    // Check pagination token
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(NEXT_TOKEN);
    // Check messages
    Collection<MailMessageModel> actualMail = result.getExportedData().getMessages();
    assertThat(actualMail.stream().map(MailMessageModel::getRawString).collect(Collectors.toList())).containsExactly(MESSAGE_RAW);
    assertThat(actualMail.stream().map(MailMessageModel::getContainerIds).collect(Collectors.toList())).containsExactly(MESSAGE_LABELS);
}
Also used : InOrder(org.mockito.InOrder) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) MailMessageModel(org.datatransferproject.types.common.models.mail.MailMessageModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 4 with MailMessageModel

use of org.datatransferproject.types.common.models.mail.MailMessageModel in project data-transfer-project by google.

the class GoogleMailImporter method importMessages.

/**
 * Import each message in {@code messages} into the import account with it's associated labels.
 */
private void importMessages(TokensAndUrlAuthData authData, IdempotentImportExecutor idempotentExecutor, Collection<MailMessageModel> messages) throws Exception {
    for (MailMessageModel mailMessageModel : messages) {
        idempotentExecutor.executeAndSwallowIOExceptions(mailMessageModel.toString(), // them.
        "Mail message: " + mailMessageModel.getRawString().substring(0, Math.min(50, mailMessageModel.getRawString().length())), () -> {
            // Gather the label ids that will be associated with this message
            ImmutableList.Builder<String> importedLabelIds = ImmutableList.builder();
            for (String exportedLabelIdOrName : mailMessageModel.getContainerIds()) {
                // By this time all the label ids have been added to tempdata
                String importedLabelId = idempotentExecutor.getCachedValue(exportedLabelIdOrName);
                if (importedLabelId != null) {
                    importedLabelIds.add(exportedLabelIdOrName);
                } else {
                    // TODO remove after testing
                    monitor.debug(() -> "labels should have been added prior to importing messages");
                }
            }
            // Create the message to import
            Message newMessage = new Message().setRaw(mailMessageModel.getRawString()).setLabelIds(importedLabelIds.build());
            return getOrCreateGmail(authData).users().messages().insert(USER, newMessage).execute().getId();
        });
    }
}
Also used : Message(com.google.api.services.gmail.model.Message) ImmutableList(com.google.common.collect.ImmutableList) MailMessageModel(org.datatransferproject.types.common.models.mail.MailMessageModel)

Aggregations

MailMessageModel (org.datatransferproject.types.common.models.mail.MailMessageModel)4 MailContainerResource (org.datatransferproject.types.common.models.mail.MailContainerResource)3 Message (com.google.api.services.gmail.model.Message)2 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)2 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Gmail (com.google.api.services.gmail.Gmail)1 Messages (com.google.api.services.gmail.Gmail.Users.Messages)1 ListMessagesResponse (com.google.api.services.gmail.model.ListMessagesResponse)1 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)1 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)1 PaginationData (org.datatransferproject.types.common.PaginationData)1 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)1 MailContainerModel (org.datatransferproject.types.common.models.mail.MailContainerModel)1 InOrder (org.mockito.InOrder)1