use of org.apache.camel.component.google.mail.GoogleMailComponent 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 org.apache.camel.component.google.mail.GoogleMailComponent in project wildfly-camel by wildfly-extras.
the class GoogleMailIntegrationTest method testGoogleMailLabels.
@Test
public void testGoogleMailLabels() 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 = "labels";
// test route for create
from("direct://CREATE").to("google-mail://" + pathPrefix + "/create");
// 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 patch
from("direct://PATCH").to("google-mail://" + pathPrefix + "/patch");
// test route for update
from("direct://UPDATE").to("google-mail://" + pathPrefix + "/update");
}
});
try {
camelctx.start();
ProducerTemplate template = camelctx.createProducerTemplate();
// using String message body for single parameter "userId"
ListLabelsResponse labels = template.requestBody("direct://LIST", CURRENT_USERID, ListLabelsResponse.class);
final String testLabel = getClass().getSimpleName() + ".labels " + UUID.randomUUID().toString();
String labelId;
if (findLabel(labels, testLabel) == null) {
Map<String, Object> headers = new HashMap<>();
// parameter type is String
headers.put("CamelGoogleMail.userId", CURRENT_USERID);
Label label = new Label().setName(testLabel).setMessageListVisibility("show").setLabelListVisibility("labelShow");
// parameter type is com.google.api.services.gmail.model.Label
headers.put("CamelGoogleMail.content", label);
Label result = template.requestBodyAndHeaders("direct://CREATE", null, headers, Label.class);
Assert.assertNotNull("create result", result);
labelId = result.getId();
} else {
labelId = findLabel(labels, testLabel).getId();
}
// using String message body for single parameter "userId"
labels = template.requestBody("direct://LIST", CURRENT_USERID, ListLabelsResponse.class);
Assert.assertTrue(findLabel(labels, testLabel) != null);
Map<String, Object> headers = new HashMap<>();
// parameter type is String
headers.put("CamelGoogleMail.userId", CURRENT_USERID);
// parameter type is String
headers.put("CamelGoogleMail.id", labelId);
template.requestBodyAndHeaders("direct://DELETE", null, headers);
// using String message body for single parameter "userId"
labels = template.requestBody("direct://LIST", CURRENT_USERID, ListLabelsResponse.class);
Assert.assertTrue(findLabel(labels, testLabel) == null);
} finally {
camelctx.stop();
}
}
use of org.apache.camel.component.google.mail.GoogleMailComponent in project camel by apache.
the class GoogleMailComponentAutoConfiguration method configureGoogleMailComponent.
@Lazy
@Bean(name = "google-mail-component")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(GoogleMailComponent.class)
public GoogleMailComponent configureGoogleMailComponent(CamelContext camelContext, GoogleMailComponentConfiguration configuration) throws Exception {
GoogleMailComponent component = new GoogleMailComponent();
component.setCamelContext(camelContext);
Map<String, Object> parameters = new HashMap<>();
IntrospectionSupport.getProperties(configuration, parameters, null, false);
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
Object value = entry.getValue();
Class<?> paramClass = value.getClass();
if (paramClass.getName().endsWith("NestedConfiguration")) {
Class nestedClass = null;
try {
nestedClass = (Class) paramClass.getDeclaredField("CAMEL_NESTED_CLASS").get(null);
HashMap<String, Object> nestedParameters = new HashMap<>();
IntrospectionSupport.getProperties(value, nestedParameters, null, false);
Object nestedProperty = nestedClass.newInstance();
IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), nestedProperty, nestedParameters);
entry.setValue(nestedProperty);
} catch (NoSuchFieldException e) {
}
}
}
IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), component, parameters);
return component;
}
use of org.apache.camel.component.google.mail.GoogleMailComponent in project wildfly-camel by wildfly-extras.
the class GoogleMailIntegrationTest method profile.
@Test
public void profile() 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 = "users";
// test route for attachments
from("direct://GETPROFILE").to("google-mail://" + pathPrefix + "/getProfile?inBody=userId");
}
});
try {
camelctx.start();
ProducerTemplate template = camelctx.createProducerTemplate();
// using String message body for single parameter "userId"
final Profile result = template.requestBody("direct://GETPROFILE", CURRENT_USERID, Profile.class);
Assert.assertNotNull("getProfile result", result);
Assert.assertNotNull("Should be email address associated with current account", result.getEmailAddress());
System.out.println("getProfile: " + result);
} finally {
camelctx.stop();
}
}
use of org.apache.camel.component.google.mail.GoogleMailComponent in project wildfly-camel by wildfly-extras.
the class GoogleMailIntegrationTest method messages.
@Test
public void messages() 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 = "messages";
// test route for attachments
from("direct://ATTACHMENTS").to("google-mail://" + pathPrefix + "/attachments");
// 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 gmailImport
from("direct://GMAILIMPORT").to("google-mail://" + pathPrefix + "/gmailImport");
// test route for gmailImport
from("direct://GMAILIMPORT_1").to("google-mail://" + pathPrefix + "/gmailImport");
// test route for insert
from("direct://INSERT").to("google-mail://" + pathPrefix + "/insert");
// test route for insert
from("direct://INSERT_1").to("google-mail://" + pathPrefix + "/insert");
// 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 send
from("direct://SEND").to("google-mail://" + pathPrefix + "/send");
// test route for send
from("direct://SEND_1").to("google-mail://" + pathPrefix + "/send");
// 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();
ProducerTemplate template = camelctx.createProducerTemplate();
// ==== Send test email ====
final String subject = getClass().getSimpleName() + ".messages " + UUID.randomUUID().toString();
Message testEmail = createMessage(template, subject);
Map<String, Object> headers = new HashMap<>();
// parameter type is String
headers.put("CamelGoogleMail.userId", CURRENT_USERID);
// parameter type is com.google.api.services.gmail.model.Message
headers.put("CamelGoogleMail.content", testEmail);
Message result = template.requestBodyAndHeaders("direct://SEND", null, headers, Message.class);
Assert.assertNotNull("send result", result);
String testEmailId = result.getId();
// ==== Search for message we just sent ====
headers = new HashMap<>();
headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
// using String message body for single parameter "userId"
ListMessagesResponse listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
Assert.assertTrue(idInList(testEmailId, listOfMessages));
// ===== trash it ====
headers = new HashMap<>();
// parameter type is String
headers.put("CamelGoogleMail.userId", CURRENT_USERID);
// parameter type is String
headers.put("CamelGoogleMail.id", testEmailId);
template.requestBodyAndHeaders("direct://TRASH", null, headers);
// ==== Search for message we just trashed ====
headers = new HashMap<>();
headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
// using String message body for single parameter "userId"
listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
Assert.assertFalse(idInList(testEmailId, listOfMessages));
// ===== untrash it ====
headers = new HashMap<>();
// parameter type is String
headers.put("CamelGoogleMail.userId", CURRENT_USERID);
// parameter type is String
headers.put("CamelGoogleMail.id", testEmailId);
template.requestBodyAndHeaders("direct://UNTRASH", null, headers);
// ==== Search for message we just untrashed ====
headers = new HashMap<>();
headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
// using String message body for single parameter "userId"
listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
Assert.assertTrue(idInList(testEmailId, listOfMessages));
// ===== permanently delete it ====
headers = new HashMap<>();
// parameter type is String
headers.put("CamelGoogleMail.userId", CURRENT_USERID);
// parameter type is String
headers.put("CamelGoogleMail.id", testEmailId);
template.requestBodyAndHeaders("direct://DELETE", null, headers);
// ==== Search for message we just deleted ====
headers = new HashMap<>();
headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
// using String message body for single parameter "userId"
listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
Assert.assertFalse(idInList(testEmailId, listOfMessages));
} finally {
camelctx.stop();
}
}
Aggregations