236 lines
9.9 KiB
Java
Raw Normal View History

2012-06-13 23:04:29 -03:00
/*
2022-03-17 18:47:34 -03:00
* Copyright [2022] [wisemapping]
2022-02-03 21:02:28 -08:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2012-06-13 23:04:29 -03:00
package com.wisemapping.mail;
import com.wisemapping.filter.SupportedUserAgent;
2012-06-13 23:04:29 -03:00
import com.wisemapping.model.Collaboration;
import com.wisemapping.model.Mindmap;
2012-06-13 23:04:29 -03:00
import com.wisemapping.model.User;
2022-02-12 09:15:18 -08:00
import com.wisemapping.rest.model.RestLogItem;
2012-08-30 21:23:10 -03:00
import org.apache.commons.lang.StringEscapeUtils;
2022-02-03 21:02:28 -08:00
import org.apache.log4j.Logger;
2012-06-13 23:04:29 -03:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
2022-03-27 07:32:53 -03:00
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;
2012-06-13 23:04:29 -03:00
2012-08-30 21:23:10 -03:00
import javax.servlet.http.HttpServletRequest;
2022-03-27 14:29:29 -03:00
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
2012-08-30 21:23:10 -03:00
import java.io.UnsupportedEncodingException;
2012-06-13 23:04:29 -03:00
import java.util.HashMap;
2022-03-27 07:32:53 -03:00
import java.util.Locale;
2012-06-13 23:04:29 -03:00
import java.util.Map;
2022-02-11 23:56:53 -08:00
import java.util.stream.Collectors;
2012-06-13 23:04:29 -03:00
final public class NotificationService {
2022-02-03 21:02:28 -08:00
final private static Logger logger = Logger.getLogger(Mailer.class);
2022-03-27 07:32:53 -03:00
private ResourceBundleMessageSource messageSource;
2012-06-13 23:04:29 -03:00
@Autowired
private Mailer mailer;
private String baseUrl;
public void newCollaboration(@NotNull Collaboration collaboration, @NotNull Mindmap mindmap, @NotNull User user, @Nullable String message) {
2022-03-27 14:50:35 -03:00
final Locale locale = LocaleContextHolder.getLocale();
2012-06-13 23:04:29 -03:00
try {
// Sent collaboration email ...
final String formMail = mailer.getServerSenderEmail();
2012-06-13 23:04:29 -03:00
// Is the user already registered user ?.
final String collabEmail = collaboration.getCollaborator().getEmail();
// Build the subject ...
2022-03-27 14:50:35 -03:00
final String subject = messageSource.getMessage("SHARE_MAP.EMAIL_SUBJECT", new Object[]{user.getFullName()}, locale);
2012-06-13 23:04:29 -03:00
// Fill template properties ...
2022-02-11 23:56:53 -08:00
final Map<String, Object> model = new HashMap<>();
2012-06-13 23:04:29 -03:00
model.put("mindmap", mindmap);
model.put("ownerName", user.getFirstname());
model.put("mapEditUrl", getBaseUrl() + "/c/maps/" + mindmap.getId() + "/edit");
model.put("baseUrl", getBaseUrl());
2012-06-14 08:47:23 -03:00
model.put("senderMail", user.getEmail());
2012-06-16 11:37:40 -03:00
model.put("message", message);
model.put("supportEmail", mailer.getSupportEmail());
2012-06-14 08:47:23 -03:00
2012-06-13 23:04:29 -03:00
mailer.sendEmail(formMail, collabEmail, subject, model, "newCollaboration.vm");
} catch (Exception e) {
handleException(e);
}
}
public void resetPassword(@NotNull User user, @NotNull String temporalPassword) {
2022-03-27 14:29:29 -03:00
final Locale locale = LocaleContextHolder.getLocale();
final String mailSubject = messageSource.getMessage("CHANGE_PASSWORD.EMAIL_SUBJECT", null, locale);
final String messageTitle = messageSource.getMessage("CHANGE_PASSWORD.EMAIL_TITLE", null, locale);
final String messageBody = messageSource.getMessage("CHANGE_PASSWORD.EMAIL_BODY", new Object[]{temporalPassword, getBaseUrl()}, locale);
2012-06-20 13:28:45 -03:00
sendTemplateMail(user, mailSubject, messageTitle, messageBody);
}
2022-02-03 21:02:28 -08:00
2012-06-20 13:28:45 -03:00
public void passwordChanged(@NotNull User user) {
2022-03-27 14:29:29 -03:00
final Locale locale = LocaleContextHolder.getLocale();
final String mailSubject = messageSource.getMessage("PASSWORD_CHANGED.EMAIL_SUBJECT", null, locale);
final String messageTitle = messageSource.getMessage("PASSWORD_CHANGED.EMAIL_TITLE", null, locale);
final String messageBody = messageSource.getMessage("PASSWORD_CHANGED.EMAIL_BODY", null, locale);
2012-06-20 13:28:45 -03:00
sendTemplateMail(user, mailSubject, messageTitle, messageBody);
}
public void newAccountCreated(@NotNull User user) {
2022-03-27 07:32:53 -03:00
final Locale locale = LocaleContextHolder.getLocale();
final String mailSubject = messageSource.getMessage("REGISTRATION.EMAIL_SUBJECT", null, locale);
final String messageTitle = messageSource.getMessage("REGISTRATION.EMAIL_TITLE", null, locale);
final String messageBody = messageSource.getMessage("REGISTRATION.EMAIL_BODY", null, locale);
2012-06-20 13:28:45 -03:00
sendTemplateMail(user, mailSubject, messageTitle, messageBody);
}
private void sendTemplateMail(@NotNull User user, @NotNull String mailSubject, @NotNull String messageTitle, @NotNull String messageBody) {
2022-04-11 10:03:27 -03:00
final Locale locale = LocaleContextHolder.getLocale();
2012-06-20 13:28:45 -03:00
try {
2022-02-11 23:56:53 -08:00
final Map<String, Object> model = new HashMap<>();
2012-06-20 13:28:45 -03:00
model.put("firstName", user.getFirstname());
model.put("messageTitle", messageTitle);
model.put("messageBody", messageBody);
model.put("baseUrl", getBaseUrl());
2012-06-20 13:28:45 -03:00
model.put("supportEmail", mailer.getSupportEmail());
2022-04-11 10:03:27 -03:00
model.put("doNotReplay", messageSource.getMessage("EMAIL.DO_NOT_REPLAY", new Object[]{mailer.getSupportEmail()}, locale));
2022-03-27 14:29:29 -03:00
logger.debug("Email properties->" + model);
2012-06-20 13:28:45 -03:00
mailer.sendEmail(mailer.getServerSenderEmail(), user.getEmail(), mailSubject, model, "baseLayout.vm");
} catch (Exception e) {
handleException(e);
}
}
2012-06-13 23:04:29 -03:00
private void handleException(Exception e) {
System.err.println("An expected error has occurred trying to send an email notification. Usually, the main reason for this is that the SMTP server properties has not been configured properly. Edit the WEB-INF/app.properties file and verify the SMTP server configuration properties.");
2012-11-04 01:06:02 -03:00
System.err.println("Cause:" + e.getMessage());
2012-06-13 23:04:29 -03:00
}
public void setMailer(Mailer mailer) {
this.mailer = mailer;
}
public void activateAccount(@NotNull User user) {
2022-02-13 08:25:09 -08:00
final Map<String, Object> model = new HashMap<>();
model.put("user", user);
2012-06-20 13:28:45 -03:00
mailer.sendEmail(mailer.getServerSenderEmail(), user.getEmail(), "[WiseMapping] Active account", model, "activationAccountMail.vm");
}
public void sendRegistrationEmail(@NotNull User user) {
// throw new UnsupportedOperationException("Not implemented yet");
// try {
// final Map<String, String> model = new HashMap<String, String>();
// model.put("email", user.getEmail());
//// final String activationUrl = "http://wisemapping.com/c/activation?code=" + user.getActivationCode();
//// model.put("emailcheck", activationUrl);
//// mailer.sendEmail(mailer.getServerSenderEmail(), user.getEmail(), "Welcome to Wisemapping!", model,
//// "confirmationMail.vm");
// } catch (Exception e) {
// handleException(e);
// }
}
2022-02-12 09:15:18 -08:00
public void reportJavascriptException(@Nullable Mindmap mindmap, @Nullable User user, @NotNull RestLogItem errorItem, @NotNull HttpServletRequest request) {
2022-02-12 09:17:52 -08:00
final Map<String, String> summary = new HashMap<>();
summary.put("JS-MSG", errorItem.getJsErrorMsg());
summary.put("JS-STACK", errorItem.getJsStack());
2022-02-12 08:35:29 -08:00
String mindmapXML = "";
try {
2022-02-12 08:35:29 -08:00
mindmapXML = StringEscapeUtils.escapeXml(mindmap == null ? "map not found" : mindmap.getXmlStr());
2012-08-30 21:23:10 -03:00
} catch (UnsupportedEncodingException e) {
// Ignore ...
}
2022-02-12 09:17:52 -08:00
summary.put("mapId", Integer.toString(mindmap.getId()));
summary.put("mapTitle", mindmap.getTitle());
2012-08-30 21:23:10 -03:00
2022-02-12 09:17:52 -08:00
logError(summary, user, request);
2022-02-12 08:35:29 -08:00
logger.error("Unexpected editor mindmap => " + mindmapXML);
2022-02-12 09:17:52 -08:00
logger.error("Unexpected editor JS Stack => " + errorItem.getJsErrorMsg() + "-" + errorItem.getJsStack());
}
2022-02-03 21:02:28 -08:00
private void logError(@NotNull Map<String, String> model, @Nullable User user, @NotNull HttpServletRequest request) {
2012-08-30 21:23:10 -03:00
model.put("fullName", (user != null ? user.getFullName() : "'anonymous'"));
final String userEmail = user != null ? user.getEmail() : "'anonymous'";
model.put("email", userEmail);
model.put("userAgent", request.getHeader(SupportedUserAgent.USER_AGENT_HEADER));
2012-08-31 20:56:51 -03:00
model.put("server", request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort());
2012-08-30 21:23:10 -03:00
model.put("requestURI", request.getRequestURI());
2012-08-31 20:56:51 -03:00
model.put("method", request.getMethod());
2012-08-30 21:23:10 -03:00
model.put("remoteAddress", request.getRemoteAddr());
2022-02-11 23:56:53 -08:00
String errorAsString = model.keySet().stream()
.map(key -> key + "=" + model.get(key))
.collect(Collectors.joining(", ", "{", "}"));
2022-02-12 08:35:29 -08:00
logger.error("Unexpected editor info => " + errorAsString);
2012-08-30 21:23:10 -03:00
}
public void reportJavaException(@NotNull Throwable exception, @Nullable User user, @NotNull HttpServletRequest request) {
2022-02-11 23:56:53 -08:00
final Map<String, String> model = new HashMap<>();
2012-08-30 21:23:10 -03:00
model.put("errorMsg", stackTraceToString(exception));
2022-02-03 21:02:28 -08:00
logError(model, user, request);
}
public String stackTraceToString(@NotNull Throwable e) {
2022-03-27 14:29:29 -03:00
String retValue = "";
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try (sw; pw) {
e.printStackTrace(pw);
retValue = sw.toString();
2022-03-27 14:29:29 -03:00
} catch (IOException ioException) {
ioException.printStackTrace();
}
return retValue;
}
public String getBaseUrl() {
return baseUrl;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
2022-03-27 07:32:53 -03:00
public void setMessageSource(ResourceBundleMessageSource messageSource) {
this.messageSource = messageSource;
}
2012-06-13 23:04:29 -03:00
}