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));
}
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);
}
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);
}
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();
});
}
}
Aggregations