Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't use self reference #24431

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,29 @@ 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;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;
<%_ if (reactive) { _%>
import reactor.core.publisher.Mono;
<%_ } else { _%>
import org.springframework.scheduling.annotation.Async;
<%_ } _%>
<%_
const localSendEmailFromTemplateApi = reactive ? 'sendEmailFromTemplate' : 'sendEmailFromTemplateSync';
_%>


/**
* Service for sending emails.
* Service for sending emails asynchronously.
<%_ if (!reactive) { _%>
* <p>
* We use the {@link Async} annotation to send emails asynchronously.
<%_ } _%>
*/
@Service
public class MailService {
Expand All @@ -62,10 +70,6 @@ public class MailService {

private final SpringTemplateEngine templateEngine;

@Autowired
@Lazy
private MailService self;

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

Expand All @@ -75,8 +79,21 @@ public class MailService {
this.templateEngine = templateEngine;
}

<%_ if (!reactive) { _%>
@Async
<%_ } _%>
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
<%_ if (reactive) { _%>
Mono.defer(() -> {
this.sendEmailSync(to, subject, content, isMultipart, isHtml);
return Mono.empty();
}).subscribe();
<%_ } else { _%>
this.sendEmailSync(to, subject, content, isMultipart, isHtml);
<%_ } _%>
}

private void sendEmailSync(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
isMultipart, isHtml, to, subject, content);

Expand All @@ -95,8 +112,21 @@ public class MailService {
}
}

<%_ if (!reactive) { _%>
@Async
<%_ } _%>
public void sendEmailFromTemplate(<%= user.persistClass %> user, String templateName, String titleKey) {
<%_ if (reactive) { _%>
Mono.defer(() -> {
this.sendEmailFromTemplateSync(user, templateName, titleKey);
return Mono.empty();
}).subscribe();
<%_ } else { _%>
this.sendEmailFromTemplateSync(user, templateName, titleKey);
<%_ } _%>
}

private void sendEmailFromTemplateSync(<%= user.persistClass %> user, String templateName, String titleKey) {
if (user.getEmail() == null) {
log.debug("Email doesn't exist for user '{}'", user.getLogin());
return;
Expand All @@ -107,26 +137,32 @@ public class MailService {
context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
String content = templateEngine.process(templateName, context);
String subject = messageSource.getMessage(titleKey, null, locale);
self.sendEmail(user.getEmail(), subject, content, false, true);
this.sendEmailSync(user.getEmail(), subject, content, false, true);
}
<%_ if (!authenticationTypeOauth2) { _%>

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

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

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