aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShinryuken <watertext@hotmail.it>2017-11-07 19:15:02 +0100
committerGitHub <noreply@github.com>2017-11-07 19:15:02 +0100
commit0801b279a4fe91c8cb1d653676f43dfe6c26a6f2 (patch)
tree5c73a1665d9ac4d24d17529a0000462bff4593d1 /src
parent5117b2e64781161c9e06f57cbee217f174d5731e (diff)
downloadtrackermap-server-0801b279a4fe91c8cb1d653676f43dfe6c26a6f2.tar.gz
trackermap-server-0801b279a4fe91c8cb1d653676f43dfe6c26a6f2.tar.bz2
trackermap-server-0801b279a4fe91c8cb1d653676f43dfe6c26a6f2.zip
User defaults when no User is available - EventForwarder refactoring
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/notification/EventForwarder.java63
-rw-r--r--src/org/traccar/notification/FormTypeEventForwarder.java24
-rw-r--r--src/org/traccar/notification/JsonTypeEventForwarder.java21
-rw-r--r--src/org/traccar/notification/NotificationFormatter.java10
4 files changed, 76 insertions, 42 deletions
diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java
index 7d3dc99ed..5b9af5f1f 100644
--- a/src/org/traccar/notification/EventForwarder.java
+++ b/src/org/traccar/notification/EventForwarder.java
@@ -33,19 +33,17 @@ import com.fasterxml.jackson.databind.ObjectWriter;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
-public final class EventForwarder {
+public abstract class EventForwarder {
private String url;
private String header;
- private boolean payloadAsParamMode;
- private String payloadParamName;
- private String additionalParams;
- private boolean prettyPrinted;
+ private final String payloadParamName;
+ private final String additionalParams;
+ private final boolean prettyPrinted;
public EventForwarder() {
url = Context.getConfig().getString("event.forward.url", "http://localhost/");
header = Context.getConfig().getString("event.forward.header", "");
- payloadAsParamMode = Context.getConfig().getBoolean("event.forward.payloadAsParamMode");
payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload");
additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", "");
prettyPrinted = Context.getConfig().getBoolean("event.forward.prettyPrintedPayload");
@@ -56,12 +54,12 @@ public final class EventForwarder {
private static final String KEY_GEOFENCE = "geofence";
private static final String KEY_DEVICE = "device";
- public void forwardEvent(Event event, Position position) {
+ public final void forwardEvent(Event event, Position position) {
BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url);
requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name());
- setContentTypeFromMode(requestBuilder);
+ requestBuilder.addHeader("Content-Type", getContentType());
if (!header.equals("")) {
FluentCaseInsensitiveStringsMap params = new FluentCaseInsensitiveStringsMap();
@@ -69,28 +67,11 @@ public final class EventForwarder {
requestBuilder.setHeaders(params);
}
- if (payloadAsParamMode) {
- setParamsPayload(event, position, requestBuilder);
- } else {
- setJsonPayload(event, position, requestBuilder);
- }
-
+ setContent(event, position, requestBuilder);
requestBuilder.execute();
}
- private void setJsonPayload(Event event, Position position, BoundRequestBuilder requestBuilder) {
- requestBuilder.setBody(preparePayload(event, position));
- }
-
- 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) {
+ protected Map<String, List<String>> splitParams(String params, String separator) {
String[] splitedLine;
Map<String, List<String>> paramsMap = new HashMap<>();
@@ -105,15 +86,7 @@ public final class EventForwarder {
return paramsMap;
}
- private void setContentTypeFromMode(BoundRequestBuilder requestBuilder) {
- if (payloadAsParamMode) {
- requestBuilder.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
- } else {
- requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8");
- }
- }
-
- private String preparePayload(Event event, Position position) {
+ protected String prepareJsonPayload(Event event, Position position) {
Map<String, Object> data = new HashMap<>();
data.put(KEY_EVENT, event);
if (position != null) {
@@ -139,10 +112,26 @@ public final class EventForwarder {
}
}
- private ObjectWriter getObjectWriter() {
+ protected ObjectWriter getObjectWriter() {
return prettyPrinted
? Context.getObjectWriterPrettyPrinter()
: Context.getObjectMapper().writer();
}
+
+ protected String getPayloadParamName() {
+ return payloadParamName;
+ }
+ protected String getAdditionalParams() {
+ return additionalParams;
+ }
+ protected boolean isPrettyPrinted() {
+ return prettyPrinted;
+ }
+
+
+ protected abstract String getContentType();
+
+ protected abstract void setContent(Event event, Position position, BoundRequestBuilder requestBuilder);
+
}
diff --git a/src/org/traccar/notification/FormTypeEventForwarder.java b/src/org/traccar/notification/FormTypeEventForwarder.java
new file mode 100644
index 000000000..d2c4aa691
--- /dev/null
+++ b/src/org/traccar/notification/FormTypeEventForwarder.java
@@ -0,0 +1,24 @@
+package org.traccar.notification;
+
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+
+import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
+
+public class FormTypeEventForwarder extends EventForwarder {
+
+ @Override
+ protected String getContentType() {
+ return "application/x-www-form-urlencoded; charset=UTF-8";
+ }
+
+ @Override
+ protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) {
+ if (!getAdditionalParams().equals("")) {
+ requestBuilder.setFormParams(splitParams(getAdditionalParams(), "="));
+ }
+ requestBuilder.addFormParam(getPayloadParamName(),
+ NotificationFormatter.formatForwarderMessage(event, position));
+ }
+
+}
diff --git a/src/org/traccar/notification/JsonTypeEventForwarder.java b/src/org/traccar/notification/JsonTypeEventForwarder.java
new file mode 100644
index 000000000..196a4eca5
--- /dev/null
+++ b/src/org/traccar/notification/JsonTypeEventForwarder.java
@@ -0,0 +1,21 @@
+package org.traccar.notification;
+
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+
+import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
+
+public class JsonTypeEventForwarder extends EventForwarder {
+
+ @Override
+ protected String getContentType() {
+
+ return "application/json; charset=utf-8";
+ }
+
+ @Override
+ protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) {
+ requestBuilder.setBody(prepareJsonPayload(event, position));
+ }
+
+}
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java
index 591f848d4..bd919cce0 100644
--- a/src/org/traccar/notification/NotificationFormatter.java
+++ b/src/org/traccar/notification/NotificationFormatter.java
@@ -39,16 +39,16 @@ public final class NotificationFormatter {
private NotificationFormatter() {
}
- public static VelocityContext prepareContext(Long userIdreq, Event event, Position position) {
-
- long userId = userIdreq != null ? userIdreq : (Long) Context.getPermissionsManager()
- .getDeviceUsers(event.getDeviceId()).toArray()[0];
+ public static VelocityContext prepareContext(Long userId, Event event, Position position) {
User user = Context.getPermissionsManager().getUser(userId);
Device device = Context.getIdentityManager().getById(event.getDeviceId());
VelocityContext velocityContext = new VelocityContext();
- velocityContext.put("user", user);
+
+ if (user != null) {
+ velocityContext.put("user", user);
+ }
velocityContext.put("device", device);
velocityContext.put("event", event);
if (position != null) {