aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/notification/NotificationFormatter.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2017-11-24 16:55:15 +1300
committerGitHub <noreply@github.com>2017-11-24 16:55:15 +1300
commit924c28dc7435c7b1f95a3a19d191affb408ce704 (patch)
tree13264afdabb54f08aa786d61fdf3e2c55afde23e /src/org/traccar/notification/NotificationFormatter.java
parenta358302a32872fe9d6def421166703f3a586e337 (diff)
parent00c5cef324ed644ca5cae282864f3bca9629467a (diff)
downloadtrackermap-server-924c28dc7435c7b1f95a3a19d191affb408ce704.tar.gz
trackermap-server-924c28dc7435c7b1f95a3a19d191affb408ce704.tar.bz2
trackermap-server-924c28dc7435c7b1f95a3a19d191affb408ce704.zip
Merge pull request #3624 from Shinryuken/payload-as-form-param
Support for custom params on EventForwarder
Diffstat (limited to 'src/org/traccar/notification/NotificationFormatter.java')
-rw-r--r--src/org/traccar/notification/NotificationFormatter.java39
1 files changed, 28 insertions, 11 deletions
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java
index 8da819430..114825a83 100644
--- a/src/org/traccar/notification/NotificationFormatter.java
+++ b/src/org/traccar/notification/NotificationFormatter.java
@@ -18,6 +18,7 @@ package org.traccar.notification;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Paths;
import java.util.Locale;
import org.apache.velocity.Template;
@@ -39,6 +40,7 @@ public final class NotificationFormatter {
}
public static VelocityContext prepareContext(long userId, Event event, Position position) {
+
User user = Context.getPermissionsManager().getUser(userId);
Device device = Context.getIdentityManager().getById(event.getDeviceId());
@@ -68,31 +70,46 @@ public final class NotificationFormatter {
}
public static Template getTemplate(Event event, String path) {
+
+ String templateFilePath;
Template template;
+
try {
- template = Context.getVelocityEngine().getTemplate(path + event.getType() + ".vm",
- StandardCharsets.UTF_8.name());
+ templateFilePath = Paths.get(path, event.getType() + ".vm").toString();
+ template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
} catch (ResourceNotFoundException error) {
Log.warning(error);
- template = Context.getVelocityEngine().getTemplate(path + "unknown.vm",
- StandardCharsets.UTF_8.name());
+ templateFilePath = Paths.get(path, "unknown.vm").toString();
+ template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
}
return template;
}
public static MailMessage formatMailMessage(long userId, Event event, Position position) {
+ String templatePath = Context.getConfig().getString("mail.templatesPath", "mail");
VelocityContext velocityContext = prepareContext(userId, event, position);
- StringWriter writer = new StringWriter();
- getTemplate(event, Context.getConfig().getString("mail.templatesPath", "mail") + "/")
- .merge(velocityContext, writer);
- return new MailMessage((String) velocityContext.get("subject"), writer.toString());
+ String formattedMessage = formatMessage(velocityContext, userId, event, position, templatePath);
+
+ return new MailMessage((String) velocityContext.get("subject"), formattedMessage);
}
public static String formatSmsMessage(long userId, Event event, Position position) {
- VelocityContext velocityContext = prepareContext(userId, event, position);
+ String templatePath = Context.getConfig().getString("sms.templatesPath", "sms");
+
+ return formatMessage(null, userId, event, position, templatePath);
+ }
+
+ private static String formatMessage(VelocityContext vc, Long userId, Event event, Position position,
+ String templatePath) {
+
+ VelocityContext velocityContext = vc;
+ if (velocityContext == null) {
+ velocityContext = prepareContext(userId, event, position);
+ }
StringWriter writer = new StringWriter();
- getTemplate(event, Context.getConfig().getString("sms.templatesPath", "sms") + "/")
- .merge(velocityContext, writer);
+ getTemplate(event, templatePath).merge(velocityContext, writer);
+
return writer.toString();
}
+
}