Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ActivationEventConsumer {
- private Logger log = LoggerFactory.getLogger(ActivationEventConsumer.class);
- @Inject
- public ActivationEventConsumer(Config cfg, MailService mailService, UserService service) {
- Util.setup(cfg);
- final DecodedJWT tok = Util.decode(cfg.getString("jwt.clienttoken"));
- service.activationEvents()
- .subscribe()
- .withGroupId("auth-ActivationEventConsumer")
- .atLeastOnce(Flow.<UserEvent>create()
- .map(this::toMail)
- .mapAsync(1, optMail -> {
- if (optMail.isPresent()) {
- return mailService
- .send()
- .handleRequestHeader(authenticate(tok))
- .invoke(optMail.get())
- .thenApply(r -> {
- if (!r.messageId.isPresent()) {
- log.error("Mail could not be sent to "+optMail.get().to+" -- Reason: "+r.errorMessage.get());
- }
- return Done.getInstance();
- });
- } else {
- return CompletableFuture.completedFuture(Done.getInstance());
- }
- })
- );
- }
- private Optional<Mail> toMail(UserEvent event) {
- if (event instanceof UserEvent.NewActivationCode) {
- UserEvent.NewActivationCode ev = (UserEvent.NewActivationCode) event;
- log.info("Sending Activation-Mail for user "+ev.uuid.toString());
- return Optional.of(toActCodeMailing(ev));
- } else {
- return Optional.empty();
- }
- }
- //TODO: Replace with a template.
- private Mail toActCodeMailing(UserEvent.NewActivationCode e) {
- StringBuilder body = new StringBuilder();
- body.append("Hello ");
- body.append(e.firstname);
- body.append(",\n\nwelcome to our System! To activate your account please click on the following link:");
- body.append("\nhttp://blahblah");
- body.append("\n\nAlternatively, use the following Activation-Code: ");
- body.append(e.activationCode);
- body.append(" to enable your Account.\n\n");
- body.append("Your M2M Team");
- return new Mail("test@test.com", e.mail, "Account Activation", body.toString());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement