aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/notification/TextTemplateFormatter.java
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/TextTemplateFormatter.java
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/TextTemplateFormatter.java')
-rw-r--r--src/main/java/org/traccar/notification/TextTemplateFormatter.java23
1 files changed, 14 insertions, 9 deletions
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());