use of org.apache.sling.commons.messaging.Result in project sling by apache.
the class SimpleMailServiceIT method sendWithoutAuthentication.
@Test
public void sendWithoutAuthentication() throws Exception {
final Dictionary<String, Object> properties = MailBuilderConfigurations.minimal();
createFactoryConfiguration(FACTORY_PID, properties);
final MessageService messageService = getService(MessageService.class);
final CompletableFuture<Result> future = messageService.send("simple test message", "recipient@example.net");
try {
future.get();
} catch (Exception e) {
logger.info(e.getMessage(), e);
assertTrue(ExceptionUtils.getRootCause(e) instanceof AuthenticationFailedException);
}
}
use of org.apache.sling.commons.messaging.Result in project sling by apache.
the class SimpleMailServiceIT method send.
@Test
public void send() throws Exception {
final Dictionary<String, Object> properties = MailBuilderConfigurations.full(wiser.getServer().getPort());
createFactoryConfiguration(FACTORY_PID, properties);
final MessageService messageService = getService(MessageService.class);
final CompletableFuture<Result> future = messageService.send("simple test message", "recipient@example.net");
final MailResult result = (MailResult) future.get();
final String message = new String(result.getMessage(), StandardCharsets.UTF_8);
// TODO assert
logger.info("message: {}", message);
}
use of org.apache.sling.commons.messaging.Result in project sling by apache.
the class FormServlet method doPost.
@Override
protected void doPost(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
final Map<String, Object> base = new LinkedHashMap<>();
final ValueMapDecorator parameters = new ValueMapDecorator(base);
final Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) {
final String name = names.nextElement();
parameters.put(name, request.getRequestParameter(name));
}
logger.debug("parameters: {}", parameters);
final String formType = request.getParameter("formType");
logger.debug("form type is '{}'", formType);
final Form form = FormFactory.build(formType, parameters);
if (form == null) {
fail(null, 400, request, response);
return;
}
final String resourcePath = request.getRequestPathInfo().getResourcePath();
final ValidationModel validationModel = validationService.getValidationModel(form.getResourceType(), resourcePath, false);
if (validationModel == null) {
logger.error("no validation model found");
fail(form, 500, request, response);
return;
}
final ValidationResult validationResult = validationService.validate(parameters, validationModel);
form.setValidationResult(validationResult);
if (!validationResult.isValid()) {
logger.debug("validation result not valid");
fail(form, 400, request, response);
return;
}
// render form with message template
// TODO
final String template = "/apps/fling/messaging/form/comment.txt";
final Map<String, Object> variables = Collections.singletonMap("form", form);
final String message;
try (final ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {
final IContext context = new DefaultSlingContext(resourceResolver, Locale.ENGLISH, variables);
logger.debug("rendering message template '{}' with variables: {}", template, variables);
message = templateEngine.process(template, context);
} catch (Exception e) {
// TODO
// TODO
logger.error("sending message failed: {}", e.getMessage(), e);
fail(form, 500, request, response);
return;
}
logger.debug("message: '{}'", message);
try {
final CompletableFuture<Result> future = messageService.send(message, recipient);
future.get(1, TimeUnit.SECONDS);
logger.debug("comment [{}] form sent to {}", message, recipient);
} catch (Exception e) {
logger.error("sending message failed: {}", e.getMessage(), e);
fail(form, 500, request, response);
return;
}
succeed(form, request, response);
}
use of org.apache.sling.commons.messaging.Result in project sling by apache.
the class SimpleMailServiceIT method sendWithData.
@Test
public void sendWithData() throws Exception {
final Dictionary<String, Object> properties = MailBuilderConfigurations.full(wiser.getServer().getPort());
createFactoryConfiguration(FACTORY_PID, properties);
final MessageService messageService = getService(MessageService.class);
final Map configuration = Collections.singletonMap("mail.subject", "Testing the Simple Mail Service with a custom subject");
final Map data = Collections.singletonMap("mail", configuration);
final CompletableFuture<Result> future = messageService.send("simple test message", "recipient@example.net", data);
final MailResult result = (MailResult) future.get();
final String message = new String(result.getMessage(), StandardCharsets.UTF_8);
// TODO assert
logger.info("message: {}", message);
}
use of org.apache.sling.commons.messaging.Result in project sling by apache.
the class MessageSender method activate.
@Activate
public void activate() throws ExecutionException, InterruptedException {
logger.info("activate()");
for (int i = 0; i < 10; i++) {
final String message = "a simple text message";
final String recipient = "form@fling";
final String subject = String.format("message number %s", i);
final Map configuration = Collections.singletonMap("mail.subject", subject);
final CompletableFuture<Result> future = messageService.send(message, recipient, Collections.singletonMap("mail", configuration));
future.thenAccept(result -> {
final byte[] bytes = (byte[]) result.getMessage();
logger.debug("message sent: {}", new String(bytes, StandardCharsets.UTF_8));
});
}
}
Aggregations