From 04a221799a75373452a17ab8462801f2b650c055 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sun, 2 Sep 2012 18:24:15 -0300 Subject: [PATCH] Add filter for avoiding multiple emails on error reporting. --- distribution/copy-dist.sh | 1 + .../wisemapping/mail/NotificationService.java | 38 +++++++++++-------- .../com/wisemapping/mail/NotifierFilter.java | 35 +++++++++++++++++ 3 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 wise-webapp/src/main/java/com/wisemapping/mail/NotifierFilter.java diff --git a/distribution/copy-dist.sh b/distribution/copy-dist.sh index ff1260f9..6500a9f8 100755 --- a/distribution/copy-dist.sh +++ b/distribution/copy-dist.sh @@ -16,5 +16,6 @@ WISE_BIN_FILE_PATH=./target/${WISE_BIN_FILE_NAME} scp ${WISE_BIN_FILE_PATH} thecrow@wisemapping.com:${SERVER_DOWNLOAD_DIR} # It's there ? +cd target wget -S http://downloads.wisemapping.org/stable/${WISE_BIN_FILE_NAME} #wget -S http://downloads.wisemapping.org/stable/${WISE_SRC_FILE_NAME} diff --git a/wise-webapp/src/main/java/com/wisemapping/mail/NotificationService.java b/wise-webapp/src/main/java/com/wisemapping/mail/NotificationService.java index 7cc3e428..e323fea8 100644 --- a/wise-webapp/src/main/java/com/wisemapping/mail/NotificationService.java +++ b/wise-webapp/src/main/java/com/wisemapping/mail/NotificationService.java @@ -41,9 +41,10 @@ final public class NotificationService { @Autowired private Mailer mailer; private String baseUrl; + private NotifierFilter notificationFilter; public NotificationService() { - + this.notificationFilter = new NotifierFilter(); } public void newCollaboration(@NotNull Collaboration collaboration, @NotNull Mindmap mindmap, @NotNull User user, @Nullable String message) { @@ -141,14 +142,14 @@ final public class NotificationService { } public void sendRegistrationEmail(@NotNull User user) { - throw new UnsupportedOperationException("Not implemented yet"); +// throw new UnsupportedOperationException("Not implemented yet"); // try { -// final Map model = new HashMap(); -// model.put("user", user); -// 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"); +// final Map model = new HashMap(); +// 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); // } @@ -156,22 +157,24 @@ final public class NotificationService { public void reportJavascriptException(@NotNull Mindmap mindmap, @Nullable User user, @Nullable String jsErrorMsg, @NotNull HttpServletRequest request) { - final Map model = new HashMap(); + final Map model = new HashMap(); model.put("errorMsg", jsErrorMsg); try { model.put("mapXML", StringEscapeUtils.escapeXml(mindmap.getXmlStr())); } catch (UnsupportedEncodingException e) { // Ignore ... } - model.put("mapId", mindmap.getId()); + model.put("mapId", Integer.toString(mindmap.getId())); model.put("mapTitle", mindmap.getTitle()); sendNotification(model, user, request); } - private void sendNotification(@NotNull Map model, @Nullable User user, @NotNull HttpServletRequest request) { + private void sendNotification(@NotNull Map model, @Nullable User user, @NotNull HttpServletRequest request) { model.put("fullName", (user != null ? user.getFullName() : "'anonymous'")); - model.put("email", (user != null ? user.getEmail() : "'anonymous'")); + final String userEmail = user != null ? user.getEmail() : "'anonymous'"; + + model.put("email", userEmail); model.put("userAgent", request.getHeader(UserAgent.USER_AGENT_HEADER)); model.put("server", request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()); model.put("requestURI", request.getRequestURI()); @@ -181,8 +184,11 @@ final public class NotificationService { try { final String errorReporterEmail = mailer.getErrorReporterEmail(); if (errorReporterEmail != null && !errorReporterEmail.isEmpty()) { - mailer.sendEmail(mailer.getServerSenderEmail(), errorReporterEmail, "[WiseMapping] Bug from '" + (user != null ? user.getEmail() + "'" : "'anonymous'"), model, - "errorNotification.vm"); + + if (!notificationFilter.hasBeenSend(userEmail, model)) { + mailer.sendEmail(mailer.getServerSenderEmail(), errorReporterEmail, "[WiseMapping] Bug from '" + (user != null ? user.getEmail() + "'" : "'anonymous'"), model, + "errorNotification.vm"); + } } } catch (Exception e) { handleException(e); @@ -190,7 +196,7 @@ final public class NotificationService { } public void reportJavaException(@NotNull Throwable exception, @Nullable User user, @NotNull String content, @NotNull HttpServletRequest request) { - final Map model = new HashMap(); + final Map model = new HashMap(); model.put("errorMsg", stackTraceToString(exception)); model.put("mapXML", StringEscapeUtils.escapeXml(content)); @@ -198,7 +204,7 @@ final public class NotificationService { } public void reportJavaException(@NotNull Throwable exception, @Nullable User user, @NotNull HttpServletRequest request) { - final Map model = new HashMap(); + final Map model = new HashMap(); model.put("errorMsg", stackTraceToString(exception)); sendNotification(model, user, request); diff --git a/wise-webapp/src/main/java/com/wisemapping/mail/NotifierFilter.java b/wise-webapp/src/main/java/com/wisemapping/mail/NotifierFilter.java new file mode 100644 index 00000000..0492337b --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/mail/NotifierFilter.java @@ -0,0 +1,35 @@ +package com.wisemapping.mail; + +import org.jetbrains.annotations.NotNull; +import org.springframework.util.DigestUtils; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +public class NotifierFilter { + public static final int MAX_CACHE_ENTRY = 500; + private final Map emailByMd5 = Collections.synchronizedMap(new LinkedHashMap() { + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > MAX_CACHE_ENTRY; + } + }); + + public boolean hasBeenSend(@NotNull final String email, @NotNull final Map model) { + + final StringBuilder buff = new StringBuilder(); + for (String key : model.keySet()) { + buff.append(key); + buff.append("="); + buff.append(model.get(key)); + } + + final String digest = DigestUtils.md5DigestAsHex(buff.toString().getBytes()); + boolean result = emailByMd5.containsKey(digest); + if (!result) { + emailByMd5.put(digest, email); + } + return result; + } + +}