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 eb16fb4d..593f2386 100644 --- a/wise-webapp/src/main/java/com/wisemapping/mail/NotificationService.java +++ b/wise-webapp/src/main/java/com/wisemapping/mail/NotificationService.java @@ -18,16 +18,20 @@ package com.wisemapping.mail; +import com.wisemapping.filter.UserAgent; import com.wisemapping.model.Collaboration; import com.wisemapping.model.Mindmap; import com.wisemapping.model.User; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringEscapeUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.springframework.beans.factory.annotation.Autowired; +import javax.servlet.http.HttpServletRequest; import java.io.PrintWriter; import java.io.StringWriter; +import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; @@ -150,19 +154,30 @@ final public class NotificationService { // } } - public void reportMindmapEditorError(@NotNull Mindmap mindmap, @Nullable User user, @NotNull String userAgent, @Nullable String jsErrorMsg) { + public void reportJavascriptException(@NotNull Mindmap mindmap, @Nullable User user, @Nullable String jsErrorMsg, @NotNull HttpServletRequest request) { + + 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("mapTitle", mindmap.getTitle()); + + sendNotification(model, user, 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'")); + model.put("userAgent", request.getHeader(UserAgent.USER_AGENT_HEADER)); + model.put("server", request.getScheme() + "://" + request.getServerName() + "/" + request.getServerPort()); + model.put("requestURI", request.getRequestURI()); + model.put("remoteAddress", request.getRemoteAddr()); try { - final Map model = new HashMap(); - model.put("user", user); - model.put("email", (user != null ? user.getEmail() : "'anonymous'")); - model.put("errorMsg", jsErrorMsg); - model.put("mapXML", mindmap.getXmlStr().replaceAll("<", "<")); - model.put("mapId", mindmap.getId()); - model.put("mapTitle", mindmap.getTitle()); - model.put("userAgent", userAgent); - model.put("details", "Editor"); - final String errorReporterEmail = mailer.getErrorReporterEmail(); if (errorReporterEmail != null && !errorReporterEmail.isEmpty()) { mailer.sendEmail(mailer.getServerSenderEmail(), errorReporterEmail, "[WiseMapping] Bug from '" + (user != null ? user.getEmail() + "'" : "'anonymous'"), model, @@ -173,43 +188,19 @@ final public class NotificationService { } } - public void reportMindmapExportError(@NotNull String exportContent, @Nullable User user, @Nullable String userAgent, @NotNull Throwable exception) { - try { - final Map model = new HashMap(); - model.put("user", user); - model.put("email", (user != null ? user.getEmail() : "'anonymous'")); - model.put("errorMsg", stackTraceToString(exception)); - model.put("mapXML", exportContent.replaceAll("<", "<")); - model.put("userAgent", userAgent); - model.put("details", "Export"); + public void reportJavaException(@NotNull Throwable exception, @Nullable User user, @NotNull String content, @NotNull HttpServletRequest request) { + final Map model = new HashMap(); + model.put("errorMsg", stackTraceToString(exception)); + model.put("mapXML", StringEscapeUtils.escapeXml(content)); - 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"); - } - } catch (Exception e) { - handleException(e); - } + sendNotification(model, user, request); } - public void reportUnexpectedError(@NotNull Throwable exception, @Nullable User user, @NotNull String userAgent) { - try { - final Map model = new HashMap(); - model.put("user", user); - model.put("email", (user != null ? user.getEmail() : "'anonymous'")); - model.put("errorMsg", stackTraceToString(exception)); - model.put("userAgent", userAgent); - model.put("details", "Unexpected"); + public void reportJavaException(@NotNull Throwable exception, @Nullable User user, @NotNull HttpServletRequest request) { + final Map model = new HashMap(); + model.put("errorMsg", stackTraceToString(exception)); - 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"); - } - } catch (Exception e) { - handleException(e); - } + sendNotification(model, user, request); } public String stackTraceToString(@NotNull Throwable e) { diff --git a/wise-webapp/src/main/java/com/wisemapping/mail/NotifyingExceptionResolver.java b/wise-webapp/src/main/java/com/wisemapping/mail/NotifyingExceptionResolver.java index 2abeb15b..0841f427 100644 --- a/wise-webapp/src/main/java/com/wisemapping/mail/NotifyingExceptionResolver.java +++ b/wise-webapp/src/main/java/com/wisemapping/mail/NotifyingExceptionResolver.java @@ -30,7 +30,7 @@ public class NotifyingExceptionResolver extends SimpleMappingExceptionResolver { private void sendNotification(@NotNull Exception ex, @NotNull HttpServletRequest request) { final User user = Utils.getUser(); - notificationService.reportUnexpectedError(ex, user, request.getHeader("User-Agent")); + notificationService.reportJavaException(ex, user, request); } public void setExclude(final Set exclude) { diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/AccountController.java b/wise-webapp/src/main/java/com/wisemapping/rest/AccountController.java index bb14961e..05bb6c82 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/AccountController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/AccountController.java @@ -26,12 +26,15 @@ import com.wisemapping.security.Utils; import com.wisemapping.service.MindmapService; import com.wisemapping.service.UserService; import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletRequest; + @Controller public class AccountController extends BaseController { @Qualifier("userService") @@ -99,11 +102,11 @@ public class AccountController extends BaseController { @RequestMapping(method = RequestMethod.POST, value = "logger/editor", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"}) @ResponseStatus(value = HttpStatus.NO_CONTENT) - public void changePassword(@RequestBody RestLogItem item) { + public void changePassword(@RequestBody RestLogItem item, @NotNull HttpServletRequest request) { final Mindmap mindmap = mindmapService.findMindmapById(item.getMapId()); final User user = Utils.getUser(); logger.error("Unexpected editor error - " + item.getJsErrorMsg()); - notificationService.reportMindmapEditorError(mindmap, user, item.getUserAgent(), item.getJsErrorMsg() + "\n" + item.getJsStack()); + notificationService.reportJavascriptException(mindmap, user, item.getJsErrorMsg() + "\n" + item.getJsStack(), request); } } 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 3f0944b3..f18c9c26 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java @@ -31,8 +31,6 @@ import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.servlet.support.RequestContextUtils; -import org.springframework.web.util.WebUtils; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; @@ -62,7 +60,7 @@ public class BaseController { @ResponseBody public String handleServerErrors(@NotNull Exception ex, @NotNull HttpServletRequest request) { final User user = Utils.getUser(); - notificationService.reportUnexpectedError(ex, user, request.getHeader(UserAgent.USER_AGENT_HEADER)); + notificationService.reportJavaException(ex, user, request); return ex.getMessage(); } diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/view/TransformView.java b/wise-webapp/src/main/java/com/wisemapping/rest/view/TransformView.java index 685474eb..bfeeaf49 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/view/TransformView.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/view/TransformView.java @@ -44,7 +44,7 @@ public class TransformView extends AbstractView { @Autowired private Jaxb2Marshaller jaxbMarshaller; - public TransformView(@NotNull final String contentType,@NotNull NotificationService notificationService) { + public TransformView(@NotNull final String contentType, @NotNull NotificationService notificationService) { this.contentType = contentType; this.notificationService = notificationService; this.exportFormat = ExportFormat.fromContentType(contentType); @@ -93,7 +93,7 @@ public class TransformView extends AbstractView { factory.export(properties, null, outputStream, content); } } catch (Throwable e) { - notificationService.reportMindmapExportError(content, Utils.getUser(), request.getHeader("User-Agent"),e); + notificationService.reportJavaException(e, Utils.getUser(), content, request); } } @@ -101,5 +101,6 @@ public class TransformView extends AbstractView { public String getContentType() { return contentType; } + private static final String IMG_SIZE_PARAMETER = "imgSize"; } diff --git a/wise-webapp/src/main/resources/mail/errorNotification.vm b/wise-webapp/src/main/resources/mail/errorNotification.vm index 7f81abae..a0765eaf 100644 --- a/wise-webapp/src/main/resources/mail/errorNotification.vm +++ b/wise-webapp/src/main/resources/mail/errorNotification.vm @@ -1,10 +1,13 @@
    -
  • Details: ${details}
  • -
  • User Name: ${user.fullName}
  • +
  • User Name: ${fullName}
  • Email: ${email}
  • +
  • Server: ${server}
  • +
  • RequestURI: ${requestURI}
  • User Agent: ${userAgent}
  • +
  • remoteAddress: ${remoteAddress}
  • + #if($mapId)
  • Mindmap Id: ${mapId}
  • #end