aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
authorShinryuken <watertext@hotmail.it>2017-11-06 21:48:54 +0100
committerGitHub <noreply@github.com>2017-11-06 21:48:54 +0100
commitc54a12152ccdbe04e4aa9b63b990a00f8702bce4 (patch)
tree182af00e5b32645891ecd8509f62f56a2a8ebe64 /src/org/traccar
parent8e8d58251ad623c0d7c4d7af35d92f71d36012ae (diff)
downloadtraccar-server-c54a12152ccdbe04e4aa9b63b990a00f8702bce4.tar.gz
traccar-server-c54a12152ccdbe04e4aa9b63b990a00f8702bce4.tar.bz2
traccar-server-c54a12152ccdbe04e4aa9b63b990a00f8702bce4.zip
Using templates for event forwarded messages
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/notification/EventForwarder.java37
-rw-r--r--src/org/traccar/notification/NotificationFormatter.java47
2 files changed, 54 insertions, 30 deletions
diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java
index 7daa4d16c..7d3dc99ed 100644
--- a/src/org/traccar/notification/EventForwarder.java
+++ b/src/org/traccar/notification/EventForwarder.java
@@ -40,7 +40,7 @@ public final class EventForwarder {
private boolean payloadAsParamMode;
private String payloadParamName;
private String additionalParams;
- private ObjectWriter internalObjectWriter;
+ private boolean prettyPrinted;
public EventForwarder() {
url = Context.getConfig().getString("event.forward.url", "http://localhost/");
@@ -48,8 +48,7 @@ public final class EventForwarder {
payloadAsParamMode = Context.getConfig().getBoolean("event.forward.payloadAsParamMode");
payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload");
additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", "");
-
- internalObjectWriter = initInternalObjectWriter();
+ prettyPrinted = Context.getConfig().getBoolean("event.forward.prettyPrintedPayload");
}
private static final String KEY_POSITION = "position";
@@ -70,23 +69,25 @@ public final class EventForwarder {
requestBuilder.setHeaders(params);
}
- setContent(requestBuilder, preparePayload(event, position));
+ if (payloadAsParamMode) {
+ setParamsPayload(event, position, requestBuilder);
+ } else {
+ setJsonPayload(event, position, requestBuilder);
+ }
requestBuilder.execute();
}
- private void setContent(BoundRequestBuilder requestBuilder, String payload) {
-
- if (payloadAsParamMode) {
-
- if (!additionalParams.equals("")) {
- requestBuilder.setFormParams(splitParams(additionalParams, "="));
- }
- requestBuilder.addFormParam(payloadParamName, payload);
+ private void setJsonPayload(Event event, Position position, BoundRequestBuilder requestBuilder) {
+ requestBuilder.setBody(preparePayload(event, position));
+ }
- } else {
- requestBuilder.setBody(payload);
+ private void setParamsPayload(Event event, Position position, BoundRequestBuilder requestBuilder) {
+ if (!additionalParams.equals("")) {
+ requestBuilder.setFormParams(splitParams(additionalParams, "="));
}
+ requestBuilder.addFormParam(payloadParamName,
+ NotificationFormatter.formatForwarderMessage(event, position));
}
private Map<String, List<String>> splitParams(String params, String separator) {
@@ -131,16 +132,16 @@ public final class EventForwarder {
}
}
try {
- return internalObjectWriter.writeValueAsString(data);
+ return getObjectWriter().writeValueAsString(data);
} catch (JsonProcessingException e) {
Log.warning(e);
return null;
}
}
- private ObjectWriter initInternalObjectWriter() {
- return Context.getConfig().getBoolean("event.forward.prettyPrintedPayload")
- ? Context.getObjectMapper().writerWithDefaultPrettyPrinter()
+ private ObjectWriter getObjectWriter() {
+ return prettyPrinted
+ ? Context.getObjectWriterPrettyPrinter()
: Context.getObjectMapper().writer();
}
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java
index 8da819430..56dbf7270 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;
@@ -38,7 +39,11 @@ public final class NotificationFormatter {
private NotificationFormatter() {
}
- public static VelocityContext prepareContext(long userId, Event event, Position position) {
+ public static VelocityContext prepareContext(Long userIdreq, Event event, Position position) {
+
+ long userId = userIdreq != null ? userIdreq : (Long) Context.getPermissionsManager()
+ .getDeviceUsers(event.getDeviceId()).toArray()[0];
+
User user = Context.getPermissionsManager().getUser(userId);
Device device = Context.getIdentityManager().getById(event.getDeviceId());
@@ -68,31 +73,49 @@ 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 = formatterLogic(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 formatterLogic(null, userId, event, position, templatePath);
+ }
+
+ public static String formatForwarderMessage(Event event, Position position) {
+ String templatePath = Context.getConfig().getString("forwarder.templatesPath", "forwarder");
+
+ return formatterLogic(null, null, event, position, templatePath);
+ }
+
+ private static String formatterLogic(VelocityContext vc, Long userId, Event event, Position position,
+ String templatePath) {
+
+ VelocityContext velocityContext = vc != null ? vc : 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();
}
+
}