aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/notification
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-11 08:28:41 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-11 08:28:41 -0700
commita401b40ee3b69d5679031a1e1d7287a0a56f4160 (patch)
tree30b0a1dfdc6b396dcb12947f19307b749e531446 /src/main/java/org/traccar/notification
parent5b269c0e309b70866ad167fb148eafcbad5a8b26 (diff)
downloadtrackermap-server-a401b40ee3b69d5679031a1e1d7287a0a56f4160.tar.gz
trackermap-server-a401b40ee3b69d5679031a1e1d7287a0a56f4160.tar.bz2
trackermap-server-a401b40ee3b69d5679031a1e1d7287a0a56f4160.zip
Inject velocity engine
Diffstat (limited to 'src/main/java/org/traccar/notification')
-rw-r--r--src/main/java/org/traccar/notification/NotificationFormatter.java20
-rw-r--r--src/main/java/org/traccar/notification/TextTemplateFormatter.java23
2 files changed, 28 insertions, 15 deletions
diff --git a/src/main/java/org/traccar/notification/NotificationFormatter.java b/src/main/java/org/traccar/notification/NotificationFormatter.java
index 2d3b90412..fa244d9b4 100644
--- a/src/main/java/org/traccar/notification/NotificationFormatter.java
+++ b/src/main/java/org/traccar/notification/NotificationFormatter.java
@@ -27,18 +27,26 @@ import org.traccar.model.Server;
import org.traccar.model.User;
import org.traccar.session.cache.CacheManager;
-public final class NotificationFormatter {
+import javax.inject.Inject;
- private NotificationFormatter() {
+public class NotificationFormatter {
+
+ private final CacheManager cacheManager;
+ private final TextTemplateFormatter textTemplateFormatter;
+
+ @Inject
+ public NotificationFormatter(
+ CacheManager cacheManager, TextTemplateFormatter textTemplateFormatter) {
+ this.cacheManager = cacheManager;
+ this.textTemplateFormatter = textTemplateFormatter;
}
- public static NotificationMessage formatMessage(
- CacheManager cacheManager, User user, Event event, Position position, String templatePath) {
+ public NotificationMessage formatMessage(User user, Event event, Position position, String templatePath) {
Server server = cacheManager.getServer();
Device device = cacheManager.getObject(Device.class, event.getDeviceId());
- VelocityContext velocityContext = TextTemplateFormatter.prepareContext(server, user);
+ VelocityContext velocityContext = textTemplateFormatter.prepareContext(server, user);
velocityContext.put("device", device);
velocityContext.put("event", event);
@@ -59,7 +67,7 @@ public final class NotificationFormatter {
velocityContext.put("driver", cacheManager.findDriverByUniqueId(device.getId(), driverUniqueId));
}
- return TextTemplateFormatter.formatMessage(velocityContext, event.getType(), templatePath);
+ return textTemplateFormatter.formatMessage(velocityContext, event.getType(), templatePath);
}
}
diff --git a/src/main/java/org/traccar/notification/TextTemplateFormatter.java b/src/main/java/org/traccar/notification/TextTemplateFormatter.java
index 9072ec89e..bca18f53c 100644
--- a/src/main/java/org/traccar/notification/TextTemplateFormatter.java
+++ b/src/main/java/org/traccar/notification/TextTemplateFormatter.java
@@ -17,29 +17,34 @@ package org.traccar.notification;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.NumberTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.traccar.Context;
import org.traccar.helper.model.UserUtil;
import org.traccar.model.Server;
import org.traccar.model.User;
+import javax.inject.Inject;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.Locale;
-public final class TextTemplateFormatter {
+public class TextTemplateFormatter {
private static final Logger LOGGER = LoggerFactory.getLogger(TextTemplateFormatter.class);
- private TextTemplateFormatter() {
+ private final VelocityEngine velocityEngine;
+
+ @Inject
+ public TextTemplateFormatter(VelocityEngine velocityEngine) {
+ this.velocityEngine = velocityEngine;
}
- public static VelocityContext prepareContext(Server server, User user) {
+ public VelocityContext prepareContext(Server server, User user) {
VelocityContext velocityContext = new VelocityContext();
@@ -48,7 +53,7 @@ public final class TextTemplateFormatter {
velocityContext.put("timezone", UserUtil.getTimezone(server, user));
}
- velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url"));
+ velocityContext.put("webUrl", velocityEngine.getProperty("web.url"));
velocityContext.put("dateTool", new DateTool());
velocityContext.put("numberTool", new NumberTool());
velocityContext.put("locale", Locale.getDefault());
@@ -56,23 +61,23 @@ public final class TextTemplateFormatter {
return velocityContext;
}
- public static Template getTemplate(String name, String path) {
+ public Template getTemplate(String name, String path) {
String templateFilePath;
Template template;
try {
templateFilePath = Paths.get(path, name + ".vm").toString();
- template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
+ template = velocityEngine.getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
} catch (ResourceNotFoundException error) {
LOGGER.warn("Notification template error", error);
templateFilePath = Paths.get(path, "unknown.vm").toString();
- template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
+ template = velocityEngine.getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
}
return template;
}
- public static NotificationMessage formatMessage(VelocityContext velocityContext, String name, String templatePath) {
+ public NotificationMessage formatMessage(VelocityContext velocityContext, String name, String templatePath) {
StringWriter writer = new StringWriter();
getTemplate(name, templatePath).merge(velocityContext, writer);
return new NotificationMessage((String) velocityContext.get("subject"), writer.toString());