use of com.google.api.services.gmail.model.Message in project data-transfer-project by google.
the class GoogleMailService method export.
// This currently exports each message with associated labels
@Override
public MailModelWrapper export(ExportInformation exportInformation) throws IOException {
Messages.List request = gmail.users().messages().list(USER).setMaxResults(MAX_RESULTS_PER_REQUEST);
if (exportInformation.getPaginationInformation().isPresent()) {
request.setPageToken(((StringPaginationToken) exportInformation.getPaginationInformation().get()).getId());
}
ListMessagesResponse response = request.execute();
List<MailMessageModel> results = new ArrayList<>(response.getMessages().size());
// as we can't store all the mail messagess in memory at once.
for (Message listMessage : response.getMessages()) {
Message getResponse = gmail.users().messages().get(USER, listMessage.getId()).setFormat("raw").execute();
// TODO: note this doesn't transfer things like labels
results.add(new MailMessageModel(getResponse.getRaw(), getResponse.getLabelIds()));
}
PaginationInformation pageInfo = null;
if (response.getNextPageToken() != null) {
pageInfo = new StringPaginationToken(response.getNextPageToken());
}
// TODO: export by label or by message?
return new MailModelWrapper(null, results, new ContinuationInformation(null, pageInfo));
}
use of com.google.api.services.gmail.model.Message in project wildfly-camel by wildfly-extras.
the class GoogleMailIntegrationTest method threads.
@SuppressWarnings("serial")
@Test
public void threads() throws Exception {
CamelContext camelctx = new DefaultCamelContext();
GoogleMailComponent gMailComponent = camelctx.getComponent("google-mail", GoogleMailComponent.class);
GoogleApiEnv.configure(gMailComponent.getConfiguration(), getClass(), LOG);
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
final String pathPrefix = "threads";
// test route for delete
from("direct://DELETE").to("google-mail://" + pathPrefix + "/delete");
// test route for get
from("direct://GET").to("google-mail://" + pathPrefix + "/get");
// test route for list
from("direct://LIST").to("google-mail://" + pathPrefix + "/list?inBody=userId");
// test route for modify
from("direct://MODIFY").to("google-mail://" + pathPrefix + "/modify");
// test route for trash
from("direct://TRASH").to("google-mail://" + pathPrefix + "/trash");
// test route for untrash
from("direct://UNTRASH").to("google-mail://" + pathPrefix + "/untrash");
}
});
try {
camelctx.start();
final String subject = getClass().getSimpleName() + ".threads " + UUID.randomUUID().toString();
ProducerTemplate template = camelctx.createProducerTemplate();
Message m1 = createThreadedMessage(null, subject, template);
final String threadId = m1.getThreadId();
createThreadedMessage(threadId, subject, template);
// using String message body for single parameter "userId"
ListThreadsResponse result = template.requestBodyAndHeaders("direct://LIST", CURRENT_USERID, Collections.singletonMap("CamelGoogleMail.q", "subject:\"" + subject + "\""), ListThreadsResponse.class);
Assert.assertNotNull("list result", result);
Assert.assertTrue(result.getThreads().size() > 0);
// ===== trash it ====
template.requestBodyAndHeaders("direct://TRASH", null, new HashMap<String, Object>() {
{
put("CamelGoogleMail.userId", CURRENT_USERID);
put("CamelGoogleMail.id", threadId);
}
});
// ==== Search for message we just trashed ====
result = template.requestBodyAndHeaders("direct://LIST", CURRENT_USERID, Collections.singletonMap("CamelGoogleMail.q", "subject:\"" + subject + "\""), ListThreadsResponse.class);
Assert.assertNotNull("list result", result);
Assert.assertTrue(result.getThreads() == null || result.getThreads().stream().noneMatch(t -> threadId.equals(t.getId())));
/* For some reason the thread deletion often needs some delay to succeed */
int attemptCount = 0;
for (; ; ) {
try {
template.requestBodyAndHeaders("direct://DELETE", null, new HashMap<String, Object>() {
{
put("CamelGoogleMail.userId", CURRENT_USERID);
put("CamelGoogleMail.id", threadId);
}
});
break;
/* success */
} catch (Exception e) {
if (attemptCount >= 5) {
throw e;
/* too many attempts */
} else {
/* retry */
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
}
}
attemptCount++;
}
}
} finally {
camelctx.stop();
}
}
use of com.google.api.services.gmail.model.Message in project wildfly-camel by wildfly-extras.
the class GoogleMailIntegrationTest method idInList.
private static boolean idInList(String testEmailId, ListMessagesResponse listOfMessages) {
Assert.assertNotNull("list result", listOfMessages);
List<Message> messages = listOfMessages.getMessages();
if (messages != null) {
for (Message m : listOfMessages.getMessages()) {
if (testEmailId.equals(m.getId())) {
return true;
}
}
}
return false;
}
use of com.google.api.services.gmail.model.Message in project jbpm-work-items by kiegroup.
the class SendMailWorkitemHandler method sendMessage.
public Message sendMessage(Gmail service, String to, String from, String subject, String bodyText, Document attachment) throws MessagingException, IOException {
MimeMessage mimeMessage = createEmailWithAttachment(to, from, subject, bodyText, attachment);
Message message = service.users().messages().send(from, createMessageWithEmail(mimeMessage)).execute();
return message;
}
use of com.google.api.services.gmail.model.Message in project jbpm-work-items by kiegroup.
the class GoogleMailWorkitemHandlerTest method setUp.
@Before
public void setUp() {
try {
when(auth.getGmailService(anyString(), anyString())).thenReturn(gmailService);
when(gmailService.users()).thenReturn(gmailUsers);
when(gmailUsers.messages()).thenReturn(gmailUserMessages);
when(gmailUserMessages.send(anyString(), anyObject())).thenReturn(gmailUserMessagesSend);
when(gmailUserMessagesSend.execute()).thenReturn(new Message());
} catch (Exception e) {
fail(e.getMessage());
}
}
Aggregations