diff options
author | Shinryuken <watertext@hotmail.it> | 2017-11-06 21:48:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-06 21:48:54 +0100 |
commit | c54a12152ccdbe04e4aa9b63b990a00f8702bce4 (patch) | |
tree | 182af00e5b32645891ecd8509f62f56a2a8ebe64 /src/org/traccar/notification/EventForwarder.java | |
parent | 8e8d58251ad623c0d7c4d7af35d92f71d36012ae (diff) | |
download | trackermap-server-c54a12152ccdbe04e4aa9b63b990a00f8702bce4.tar.gz trackermap-server-c54a12152ccdbe04e4aa9b63b990a00f8702bce4.tar.bz2 trackermap-server-c54a12152ccdbe04e4aa9b63b990a00f8702bce4.zip |
Using templates for event forwarded messages
Diffstat (limited to 'src/org/traccar/notification/EventForwarder.java')
-rw-r--r-- | src/org/traccar/notification/EventForwarder.java | 37 |
1 files changed, 19 insertions, 18 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(); } |