From a739bb3e0bea194f77f6c89b243cefa27ebcb86a Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sun, 19 Nov 2023 08:28:40 -0800 Subject: [PATCH] Move more services out of XML definition. --- .../com/wisemapping/rest/BaseController.java | 2 +- .../MailerService.java} | 30 ++++++++++++------- .../service/MindmapServiceImpl.java | 1 - .../NotificationService.java | 28 ++++++++++------- .../{mail => service}/NotifierFilter.java | 2 +- .../wisemapping/service/UserServiceImpl.java | 1 - .../service/google/GoogleService.java | 8 +++++ .../webapp/WEB-INF/wisemapping-service.xml | 24 --------------- 8 files changed, 46 insertions(+), 50 deletions(-) rename wise-webapp/src/main/java/com/wisemapping/{mail/Mailer.java => service/MailerService.java} (84%) rename wise-webapp/src/main/java/com/wisemapping/{mail => service}/NotificationService.java (90%) rename wise-webapp/src/main/java/com/wisemapping/{mail => service}/NotifierFilter.java (98%) diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java b/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java index 8acc8ece..fe6b9d63 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java @@ -21,7 +21,7 @@ import com.wisemapping.exceptions.ClientException; import com.wisemapping.exceptions.OAuthAuthenticationException; import com.wisemapping.exceptions.Severity; import com.wisemapping.exceptions.ValidationException; -import com.wisemapping.mail.NotificationService; +import com.wisemapping.service.NotificationService; import com.wisemapping.model.User; import com.wisemapping.rest.model.RestErrors; import com.wisemapping.security.Utils; diff --git a/wise-webapp/src/main/java/com/wisemapping/mail/Mailer.java b/wise-webapp/src/main/java/com/wisemapping/service/MailerService.java similarity index 84% rename from wise-webapp/src/main/java/com/wisemapping/mail/Mailer.java rename to wise-webapp/src/main/java/com/wisemapping/service/MailerService.java index 0cca8d1f..5239d02c 100644 --- a/wise-webapp/src/main/java/com/wisemapping/mail/Mailer.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/MailerService.java @@ -17,36 +17,44 @@ */ -package com.wisemapping.mail; +package com.wisemapping.service; import com.wisemapping.util.VelocityEngineUtils; import com.wisemapping.util.VelocityEngineWrapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessagePreparator; import jakarta.validation.constraints.NotNull; +import org.springframework.stereotype.Service; + import java.nio.charset.StandardCharsets; import java.util.Map; -public final class Mailer { +@Service +public final class MailerService { //~ Instance fields ...................................................................................... + @Autowired private JavaMailSender mailSender; + + @Autowired private VelocityEngineWrapper velocityEngineWrapper; - private final String serverFromEmail; - private final String supportEmail; - private final String errorReporterEmail; + + @Value("${mail.serverSendEmail") + private String serverFromEmail; + + @Value("${mail.supportEmail}") + private String supportEmail; + + @Value("${mail.errorReporterEmail}") + private String errorReporterEmail; //~ Methods .............................................................................................. - public Mailer(@NotNull String siteEmail, @NotNull String supportEmail, @NotNull String errorReporterEmail) { - this.serverFromEmail = siteEmail; - this.supportEmail = supportEmail; - this.errorReporterEmail = errorReporterEmail; - } - public String getServerSenderEmail() { return serverFromEmail; } diff --git a/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java b/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java index c9ed4207..41e20091 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java @@ -20,7 +20,6 @@ package com.wisemapping.service; import com.wisemapping.dao.MindmapManager; import com.wisemapping.exceptions.WiseMappingException; -import com.wisemapping.mail.NotificationService; import com.wisemapping.model.*; import com.wisemapping.security.Utils; import org.jetbrains.annotations.NotNull; diff --git a/wise-webapp/src/main/java/com/wisemapping/mail/NotificationService.java b/wise-webapp/src/main/java/com/wisemapping/service/NotificationService.java similarity index 90% rename from wise-webapp/src/main/java/com/wisemapping/mail/NotificationService.java rename to wise-webapp/src/main/java/com/wisemapping/service/NotificationService.java index fc7ce39b..81e4f24e 100644 --- a/wise-webapp/src/main/java/com/wisemapping/mail/NotificationService.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/NotificationService.java @@ -16,7 +16,7 @@ * limitations under the License. */ -package com.wisemapping.mail; +package com.wisemapping.service; import com.wisemapping.filter.SupportedUserAgent; import com.wisemapping.model.Collaboration; @@ -29,10 +29,13 @@ import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.support.ResourceBundleMessageSource; import jakarta.servlet.http.HttpServletRequest; +import org.springframework.stereotype.Service; + import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; @@ -42,13 +45,16 @@ import java.util.Locale; import java.util.Map; import java.util.stream.Collectors; +@Service final public class NotificationService { final private static Logger logger = LogManager.getLogger(); + @Autowired private ResourceBundleMessageSource messageSource; @Autowired - private Mailer mailer; + private MailerService mailerService; + @Value("${site.baseurl:http://localhost:8080/}") private String baseUrl; public void newCollaboration(@NotNull Collaboration collaboration, @NotNull Mindmap mindmap, @NotNull User user, @Nullable String message) { @@ -56,7 +62,7 @@ final public class NotificationService { try { // Sent collaboration email ... - final String formMail = mailer.getServerSenderEmail(); + final String formMail = mailerService.getServerSenderEmail(); // Is the user already registered user ?. final String collabEmail = collaboration.getCollaborator().getEmail(); @@ -72,14 +78,14 @@ final public class NotificationService { model.put("baseUrl", getBaseUrl()); model.put("senderMail", user.getEmail()); model.put("message", message); - model.put("doNotReplay", messageSource.getMessage("EMAIL.DO_NOT_REPLAY", new Object[]{mailer.getSupportEmail()}, locale)); + model.put("doNotReplay", messageSource.getMessage("EMAIL.DO_NOT_REPLAY", new Object[]{mailerService.getSupportEmail()}, locale)); // To resolve resources on templates ... model.put("noArg", new Object[]{}); model.put("messages", messageSource); model.put("locale", locale); - mailer.sendEmail(formMail, collabEmail, subject, model, "newCollaboration.vm"); + mailerService.sendEmail(formMail, collabEmail, subject, model, "newCollaboration.vm"); } catch (Exception e) { handleException(e); } @@ -125,8 +131,8 @@ final public class NotificationService { model.put("messageTitle", messageTitle); model.put("messageBody", messageBody); model.put("baseUrl", getBaseUrl()); - model.put("supportEmail", mailer.getSupportEmail()); - model.put("doNotReplay", messageSource.getMessage("EMAIL.DO_NOT_REPLAY", new Object[]{mailer.getSupportEmail()}, locale)); + model.put("supportEmail", mailerService.getSupportEmail()); + model.put("doNotReplay", messageSource.getMessage("EMAIL.DO_NOT_REPLAY", new Object[]{mailerService.getSupportEmail()}, locale)); // To resolve resources on templates ... model.put("noArg", new Object[]{}); @@ -134,7 +140,7 @@ final public class NotificationService { model.put("locale", locale); logger.debug("Email properties->" + model); - mailer.sendEmail(mailer.getServerSenderEmail(), user.getEmail(), mailSubject, model, "baseLayout.vm"); + mailerService.sendEmail(mailerService.getServerSenderEmail(), user.getEmail(), mailSubject, model, "baseLayout.vm"); } catch (Exception e) { handleException(e); } @@ -146,15 +152,15 @@ final public class NotificationService { } - public void setMailer(Mailer mailer) { - this.mailer = mailer; + public void setMailer(MailerService mailerService) { + this.mailerService = mailerService; } public void activateAccount(@NotNull User user) { final Map model = new HashMap<>(); model.put("user", user); - mailer.sendEmail(mailer.getServerSenderEmail(), user.getEmail(), "[WiseMapping] Active account", model, "activationAccountMail.vm"); + mailerService.sendEmail(mailerService.getServerSenderEmail(), user.getEmail(), "[WiseMapping] Active account", model, "activationAccountMail.vm"); } public void sendRegistrationEmail(@NotNull User user) { diff --git a/wise-webapp/src/main/java/com/wisemapping/mail/NotifierFilter.java b/wise-webapp/src/main/java/com/wisemapping/service/NotifierFilter.java similarity index 98% rename from wise-webapp/src/main/java/com/wisemapping/mail/NotifierFilter.java rename to wise-webapp/src/main/java/com/wisemapping/service/NotifierFilter.java index 52803f93..d6f171a9 100644 --- a/wise-webapp/src/main/java/com/wisemapping/mail/NotifierFilter.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/NotifierFilter.java @@ -15,7 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.wisemapping.mail; +package com.wisemapping.service; import org.jetbrains.annotations.NotNull; import org.springframework.util.DigestUtils; diff --git a/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java b/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java index f62aea75..60cd0b44 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java @@ -22,7 +22,6 @@ import com.wisemapping.dao.UserManager; import com.wisemapping.exceptions.InvalidMindmapException; import com.wisemapping.exceptions.OAuthAuthenticationException; import com.wisemapping.exceptions.WiseMappingException; -import com.wisemapping.mail.NotificationService; import com.wisemapping.model.*; import com.wisemapping.rest.model.RestResetPasswordAction; import com.wisemapping.rest.model.RestResetPasswordResponse; diff --git a/wise-webapp/src/main/java/com/wisemapping/service/google/GoogleService.java b/wise-webapp/src/main/java/com/wisemapping/service/google/GoogleService.java index 24cc4256..0978a311 100644 --- a/wise-webapp/src/main/java/com/wisemapping/service/google/GoogleService.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/google/GoogleService.java @@ -20,6 +20,8 @@ package com.wisemapping.service.google; import java.util.HashMap; import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Service; @@ -30,11 +32,17 @@ import com.wisemapping.service.google.http.HttpInvokerException; @Service public class GoogleService { + @Autowired private HttpInvoker httpInvoker; + @Value("${security.oauth2.google.confirmUrl}") private String optinConfirmUrl; + @Value("${security.oauth2.google.userinfoUrl}") private String accountBasicDataUrl; + @Value("${security.oauth2.google.clientId}") private String clientId; + @Value("${security.oauth2.google.clientSecret}") private String clientSecret; + @Value("${security.oauth2.google.callbackUrl}") private String callbackUrl; public void setHttpInvoker(HttpInvoker httpInvoker) { diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-service.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-service.xml index ac79a5b9..581baba5 100755 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-service.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-service.xml @@ -9,24 +9,6 @@ - - - - - - - - - - - - - - - - - - @@ -42,12 +24,6 @@ - - - - - -