Skip to content

Commit

Permalink
Sonar: Call async methods via an injected dependency instead of direc…
Browse files Browse the repository at this point in the history
…tly via 'this' (jhipster#24034)

* Sonar: Call async methods via an injected dependency instead of directly via 'this'

* Sonar: Call async methods via an injected dependency instead of directly via 'this' (use Autowired & Lazy)
  • Loading branch information
qmonmert authored Oct 28, 2023
1 parent ca18af8 commit 0def0e9
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Lazy;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
Expand Down Expand Up @@ -60,6 +62,10 @@ public class MailService {

private final SpringTemplateEngine templateEngine;

@Autowired
@Lazy
private MailService self;

public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
MessageSource messageSource, SpringTemplateEngine templateEngine) {

Expand Down Expand Up @@ -101,26 +107,26 @@ public class MailService {
context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
String content = templateEngine.process(templateName, context);
String subject = messageSource.getMessage(titleKey, null, locale);
sendEmail(user.getEmail(), subject, content, false, true);
self.sendEmail(user.getEmail(), subject, content, false, true);
}
<%_ if (!authenticationTypeOauth2) { _%>

@Async
public void sendActivationEmail(<%= user.persistClass %> user) {
log.debug("Sending activation email to '{}'", user.getEmail());
sendEmailFromTemplate(user, "mail/activationEmail", "email.activation.title");
self.sendEmailFromTemplate(user, "mail/activationEmail", "email.activation.title");
}

@Async
public void sendCreationEmail(<%= user.persistClass %> user) {
log.debug("Sending creation email to '{}'", user.getEmail());
sendEmailFromTemplate(user, "mail/creationEmail", "email.activation.title");
self.sendEmailFromTemplate(user, "mail/creationEmail", "email.activation.title");
}

@Async
public void sendPasswordResetMail(<%= user.persistClass %> user) {
log.debug("Sending password reset email to '{}'", user.getEmail());
sendEmailFromTemplate(user, "mail/passwordResetEmail", "email.reset.title");
self.sendEmailFromTemplate(user, "mail/passwordResetEmail", "email.reset.title");
}
<%_ } _%>
}

0 comments on commit 0def0e9

Please sign in to comment.