Guest User

Untitled

a guest
Nov 26th, 2018
39
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ActivationEventConsumer {
  2.  
  3. private Logger log = LoggerFactory.getLogger(ActivationEventConsumer.class);
  4.  
  5. @Inject
  6. public ActivationEventConsumer(Config cfg, MailService mailService, UserService service) {
  7. Util.setup(cfg);
  8. final DecodedJWT tok = Util.decode(cfg.getString("jwt.clienttoken"));
  9. service.activationEvents()
  10. .subscribe()
  11. .withGroupId("auth-ActivationEventConsumer")
  12. .atLeastOnce(Flow.<UserEvent>create()
  13. .map(this::toMail)
  14. .mapAsync(1, optMail -> {
  15. if (optMail.isPresent()) {
  16. return mailService
  17. .send()
  18. .handleRequestHeader(authenticate(tok))
  19. .invoke(optMail.get())
  20. .thenApply(r -> {
  21. if (!r.messageId.isPresent()) {
  22. log.error("Mail could not be sent to "+optMail.get().to+" -- Reason: "+r.errorMessage.get());
  23. }
  24. return Done.getInstance();
  25. });
  26. } else {
  27. return CompletableFuture.completedFuture(Done.getInstance());
  28. }
  29. })
  30. );
  31. }
  32.  
  33. private Optional<Mail> toMail(UserEvent event) {
  34. if (event instanceof UserEvent.NewActivationCode) {
  35. UserEvent.NewActivationCode ev = (UserEvent.NewActivationCode) event;
  36. log.info("Sending Activation-Mail for user "+ev.uuid.toString());
  37. return Optional.of(toActCodeMailing(ev));
  38. } else {
  39. return Optional.empty();
  40. }
  41. }
  42.  
  43. //TODO: Replace with a template.
  44. private Mail toActCodeMailing(UserEvent.NewActivationCode e) {
  45. StringBuilder body = new StringBuilder();
  46. body.append("Hello ");
  47. body.append(e.firstname);
  48. body.append(",\n\nwelcome to our System! To activate your account please click on the following link:");
  49. body.append("\nhttp://blahblah");
  50. body.append("\n\nAlternatively, use the following Activation-Code: ");
  51. body.append(e.activationCode);
  52. body.append(" to enable your Account.\n\n");
  53. body.append("Your M2M Team");
  54. return new Mail("test@test.com", e.mail, "Account Activation", body.toString());
  55. }
RAW Paste Data