From d1327cef4df28065b3d9879b07c826d6623f98e0 Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Sat, 4 Nov 2017 17:34:11 +0100 Subject: Adding support for additional parameters on post body request (pushover support) Hi, i implemented a new modality when sending event messages inside EventForwarder, now you can send requests as a "form-urlencoded", this means that the actual payload will be sent as a form parameter (the parameter name is configurable), this strategy will let you to add another parameters inside the form body message too. You can add new custom form params almost on the same way as the custom headers are inserted (event.forward.header), the only difference is that you will use the equals "=" char instead of ":" as key/value separator. You can enable this mode using the option: "event.forward.payloadAsParamMode", when using this mode you can use the other 2 configuration elements, "event.forward.paramMode.payloadParamName" if you want to change the form name param of the payload (default is "payload") and "event.forward.paramMode.additionalParams" to specify additional key/value form params. I added an option to prettyPrint the payload messages too, you can enable it using "event.forward.prettyPrintedPayload", i decided to add this option only for the EventForwarder behaviour. This is an example (using all the new params) that lets you integrate traccar server with the pushover platform. 60 true true https://api.pushover.net/1/messages.json true true message token=YOUR_TOKEN_ID user=YOUR_USER_ID I tested the new options and they work pretty well. Tried to make the change as clean as possible and avoiding code duplications. This mode does not change or affects the actual behabiour of the module. Let me know what do you think. --- src/org/traccar/notification/EventForwarder.java | 88 +++++++++++++++++++----- 1 file changed, 72 insertions(+), 16 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index ac37f980c..7daa4d16c 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -15,8 +15,12 @@ */ package org.traccar.notification; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -24,18 +28,28 @@ import org.traccar.model.Event; import org.traccar.model.Geofence; import org.traccar.model.Position; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; +import com.ning.http.client.FluentCaseInsensitiveStringsMap; public final class EventForwarder { private String url; private String header; + private boolean payloadAsParamMode; + private String payloadParamName; + private String additionalParams; + private ObjectWriter internalObjectWriter; 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", ""); + + internalObjectWriter = initInternalObjectWriter(); } private static final String KEY_POSITION = "position"; @@ -46,23 +60,59 @@ public final class EventForwarder { public void forwardEvent(Event event, Position position) { BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); + requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name()); + + setContentTypeFromMode(requestBuilder); - requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8"); if (!header.equals("")) { - String[] headerLines = header.split("\\r?\\n"); - for (String headerLine: headerLines) { - String[] splitedLine = headerLine.split(":", 2); - if (splitedLine.length == 2) { - requestBuilder.setHeader(splitedLine[0].trim(), splitedLine[1].trim()); - } - } + FluentCaseInsensitiveStringsMap params = new FluentCaseInsensitiveStringsMap(); + params.putAll(splitParams(header, ":")); + requestBuilder.setHeaders(params); } - requestBuilder.setBody(preparePayload(event, position)); + setContent(requestBuilder, preparePayload(event, position)); + requestBuilder.execute(); } - private byte[] preparePayload(Event event, Position position) { + private void setContent(BoundRequestBuilder requestBuilder, String payload) { + + if (payloadAsParamMode) { + + if (!additionalParams.equals("")) { + requestBuilder.setFormParams(splitParams(additionalParams, "=")); + } + requestBuilder.addFormParam(payloadParamName, payload); + + } else { + requestBuilder.setBody(payload); + } + } + + private Map> splitParams(String params, String separator) { + + String[] splitedLine; + Map> paramsMap = new HashMap<>(); + String[] paramsLines = params.split("\\r?\\n"); + + for (String paramLine: paramsLines) { + splitedLine = paramLine.split(separator, 2); + if (splitedLine.length == 2) { + paramsMap.put(splitedLine[0].trim(), Arrays.asList(splitedLine[1].trim())); + } + } + 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) { Map data = new HashMap<>(); data.put(KEY_EVENT, event); if (position != null) { @@ -81,11 +131,17 @@ public final class EventForwarder { } } try { - return Context.getObjectMapper().writeValueAsString(data).getBytes(StandardCharsets.UTF_8); + return internalObjectWriter.writeValueAsString(data); } catch (JsonProcessingException e) { Log.warning(e); return null; } } + private ObjectWriter initInternalObjectWriter() { + return Context.getConfig().getBoolean("event.forward.prettyPrintedPayload") + ? Context.getObjectMapper().writerWithDefaultPrettyPrinter() + : Context.getObjectMapper().writer(); + } + } -- cgit v1.2.3 From 8e8d58251ad623c0d7c4d7af35d92f71d36012ae Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Mon, 6 Nov 2017 21:46:54 +0100 Subject: ObjectWriter for pretty-printing messaging --- src/org/traccar/Context.java | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/org') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 3b24c6460..4e7246095 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -16,6 +16,7 @@ package org.traccar; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.SerializationFeature; import com.ning.http.client.AsyncHttpClient; @@ -96,6 +97,12 @@ public final class Context { return objectMapper; } + private static ObjectWriter objectWriterPrettyPrinter; + + public static ObjectWriter getObjectWriterPrettyPrinter() { + return objectWriterPrettyPrinter; + } + private static IdentityManager identityManager; public static IdentityManager getIdentityManager() { @@ -280,6 +287,7 @@ public final class Context { objectMapper = new ObjectMapper(); objectMapper.setConfig( objectMapper.getSerializationConfig().without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)); + objectWriterPrettyPrinter = objectMapper.writerWithDefaultPrettyPrinter(); if (config.hasKey("database.url")) { dataManager = new DataManager(config); @@ -416,6 +424,7 @@ public final class Context { public static void init(IdentityManager testIdentityManager) { config = new Config(); objectMapper = new ObjectMapper(); + objectWriterPrettyPrinter = objectMapper.writerWithDefaultPrettyPrinter(); identityManager = testIdentityManager; } -- cgit v1.2.3 From c54a12152ccdbe04e4aa9b63b990a00f8702bce4 Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Mon, 6 Nov 2017 21:48:54 +0100 Subject: Using templates for event forwarded messages --- src/org/traccar/notification/EventForwarder.java | 37 ++++++++--------- .../notification/NotificationFormatter.java | 47 ++++++++++++++++------ 2 files changed, 54 insertions(+), 30 deletions(-) (limited to 'src/org') 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> 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(); } + } -- cgit v1.2.3 From 80904fd5794b1eb3ef7644c52ce3d09facf9fd04 Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Mon, 6 Nov 2017 23:04:40 +0100 Subject: Minor refactoring changes --- src/org/traccar/notification/NotificationFormatter.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 56dbf7270..591f848d4 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -111,7 +111,10 @@ public final class NotificationFormatter { private static String formatterLogic(VelocityContext vc, Long userId, Event event, Position position, String templatePath) { - VelocityContext velocityContext = vc != null ? vc : prepareContext(userId, event, position); + VelocityContext velocityContext = vc; + if (velocityContext == null) { + velocityContext = prepareContext(userId, event, position); + } StringWriter writer = new StringWriter(); getTemplate(event, templatePath).merge(velocityContext, writer); -- cgit v1.2.3 From 5117b2e64781161c9e06f57cbee217f174d5731e Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Tue, 7 Nov 2017 19:12:37 +0100 Subject: User defaults when no User is available --- src/org/traccar/database/PermissionsManager.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 07b60ba58..203f25a3d 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -56,8 +56,8 @@ public class PermissionsManager { refreshDeviceAndGroupPermissions(); } - public User getUser(long userId) { - return (User) usersManager.getById(userId); + public User getUser(Long userId) { + return userId != null ? (User) usersManager.getById(userId) : null; } public Set getGroupPermissions(long userId) { @@ -416,10 +416,15 @@ public class PermissionsManager { return null; } - public Object lookupAttribute(long userId, String key, Object defaultValue) { + public Object lookupAttribute(Long userId, String key, Object defaultValue) { Object preference; + Object userPreference = null; Object serverPreference = server.getAttributes().get(key); - Object userPreference = getUser(userId).getAttributes().get(key); + + if (userId != null) { + userPreference = getUser(userId).getAttributes().get(key); + } + if (server.getForceSettings()) { preference = serverPreference != null ? serverPreference : userPreference; } else { -- cgit v1.2.3 From 0801b279a4fe91c8cb1d653676f43dfe6c26a6f2 Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Tue, 7 Nov 2017 19:15:02 +0100 Subject: User defaults when no User is available - EventForwarder refactoring --- src/org/traccar/notification/EventForwarder.java | 63 +++++++++------------- .../notification/FormTypeEventForwarder.java | 24 +++++++++ .../notification/JsonTypeEventForwarder.java | 21 ++++++++ .../notification/NotificationFormatter.java | 10 ++-- 4 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 src/org/traccar/notification/FormTypeEventForwarder.java create mode 100644 src/org/traccar/notification/JsonTypeEventForwarder.java (limited to 'src/org') 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> splitParams(String params, String separator) { + protected Map> splitParams(String params, String separator) { String[] splitedLine; Map> 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 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) { -- cgit v1.2.3 From ccb2e9e5e0581406963ac63a9d08acb7c2549834 Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Tue, 7 Nov 2017 19:17:08 +0100 Subject: User defaults when no User is available --- src/org/traccar/reports/ReportUtils.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index f6f386e99..22c7981d8 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -61,19 +61,19 @@ public final class ReportUtils { } } - public static String getDistanceUnit(long userId) { + public static String getDistanceUnit(Long userId) { return (String) Context.getPermissionsManager().lookupAttribute(userId, "distanceUnit", "km"); } - public static String getSpeedUnit(long userId) { + public static String getSpeedUnit(Long userId) { return (String) Context.getPermissionsManager().lookupAttribute(userId, "speedUnit", "kn"); } - public static String getVolumeUnit(long userId) { + public static String getVolumeUnit(Long userId) { return (String) Context.getPermissionsManager().lookupAttribute(userId, "volumeUnit", "ltr"); } - public static TimeZone getTimezone(long userId) { + public static TimeZone getTimezone(Long userId) { String timezone = (String) Context.getPermissionsManager().lookupAttribute(userId, "timezone", null); return timezone != null ? TimeZone.getTimeZone(timezone) : TimeZone.getDefault(); } @@ -137,7 +137,7 @@ public final class ReportUtils { return null; } - public static org.jxls.common.Context initializeContext(long userId) { + public static org.jxls.common.Context initializeContext(Long userId) { org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); jxlsContext.putVar("distanceUnit", getDistanceUnit(userId)); jxlsContext.putVar("speedUnit", getSpeedUnit(userId)); -- cgit v1.2.3 From e61584b14a2cfe0075964bdc7207adb55062d61b Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Tue, 7 Nov 2017 19:17:43 +0100 Subject: User defaults when no User is available --- src/org/traccar/Context.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/org') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 4e7246095..2317b7039 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -70,6 +70,8 @@ import org.traccar.geolocation.GeolocationProvider; import org.traccar.geolocation.MozillaGeolocationProvider; import org.traccar.geolocation.OpenCellIdGeolocationProvider; import org.traccar.notification.EventForwarder; +import org.traccar.notification.FormTypeEventForwarder; +import org.traccar.notification.JsonTypeEventForwarder; import org.traccar.reports.model.TripsConfig; import org.traccar.smpp.SmppClient; import org.traccar.web.WebServer; @@ -404,7 +406,7 @@ public final class Context { serverManager = new ServerManager(); if (config.getBoolean("event.forward.enable")) { - eventForwarder = new EventForwarder(); + eventForwarder = getConfiguredEventForwarder(); } attributesManager = new AttributesManager(dataManager); @@ -421,6 +423,13 @@ public final class Context { } + private static EventForwarder getConfiguredEventForwarder() { + if (Context.getConfig().getBoolean("event.forward.payloadAsParamMode")) { + return new FormTypeEventForwarder(); + } + return new JsonTypeEventForwarder(); + } + public static void init(IdentityManager testIdentityManager) { config = new Config(); objectMapper = new ObjectMapper(); -- cgit v1.2.3 From 3363f82d05a02be3a035f984b1c7a365f327414f Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Wed, 8 Nov 2017 08:29:45 +0100 Subject: Default payload format as json, plaintext available as option --- src/org/traccar/notification/EventForwarder.java | 7 +++++-- src/org/traccar/notification/FormTypeEventForwarder.java | 8 ++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index 5b9af5f1f..4e5d75d68 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -39,6 +39,7 @@ public abstract class EventForwarder { private String header; private final String payloadParamName; private final String additionalParams; + private final boolean useTemplatesForPayload; private final boolean prettyPrinted; public EventForwarder() { @@ -46,6 +47,7 @@ public abstract class EventForwarder { header = Context.getConfig().getString("event.forward.header", ""); payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); + useTemplatesForPayload = Context.getConfig().getBoolean("event.forward.paramMode.useTemplatesForPayload"); prettyPrinted = Context.getConfig().getBoolean("event.forward.prettyPrintedPayload"); } @@ -125,13 +127,14 @@ public abstract class EventForwarder { protected String getAdditionalParams() { return additionalParams; } + protected boolean isUseTemplatesForPayload() { + return useTemplatesForPayload; + } 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 index d2c4aa691..e7564885d 100644 --- a/src/org/traccar/notification/FormTypeEventForwarder.java +++ b/src/org/traccar/notification/FormTypeEventForwarder.java @@ -17,8 +17,12 @@ public class FormTypeEventForwarder extends EventForwarder { if (!getAdditionalParams().equals("")) { requestBuilder.setFormParams(splitParams(getAdditionalParams(), "=")); } - requestBuilder.addFormParam(getPayloadParamName(), - NotificationFormatter.formatForwarderMessage(event, position)); + + String payload = isUseTemplatesForPayload() + ? NotificationFormatter.formatForwarderMessage(event, position) + : prepareJsonPayload(event, position); + + requestBuilder.addFormParam(getPayloadParamName(), payload); } } -- cgit v1.2.3 From b86161aacf53a0f66dba43ee4933bf4c8e17f072 Mon Sep 17 00:00:00 2001 From: Shinryuken Date: Mon, 13 Nov 2017 21:38:17 +0100 Subject: removing NotificationFormatter from EventForwarder --- src/org/traccar/notification/EventForwarder.java | 5 ----- src/org/traccar/notification/FormTypeEventForwarder.java | 6 +----- 2 files changed, 1 insertion(+), 10 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index 4e5d75d68..c21799d74 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -39,7 +39,6 @@ public abstract class EventForwarder { private String header; private final String payloadParamName; private final String additionalParams; - private final boolean useTemplatesForPayload; private final boolean prettyPrinted; public EventForwarder() { @@ -47,7 +46,6 @@ public abstract class EventForwarder { header = Context.getConfig().getString("event.forward.header", ""); payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); - useTemplatesForPayload = Context.getConfig().getBoolean("event.forward.paramMode.useTemplatesForPayload"); prettyPrinted = Context.getConfig().getBoolean("event.forward.prettyPrintedPayload"); } @@ -127,9 +125,6 @@ public abstract class EventForwarder { protected String getAdditionalParams() { return additionalParams; } - protected boolean isUseTemplatesForPayload() { - return useTemplatesForPayload; - } protected boolean isPrettyPrinted() { return prettyPrinted; } diff --git a/src/org/traccar/notification/FormTypeEventForwarder.java b/src/org/traccar/notification/FormTypeEventForwarder.java index e7564885d..dede95707 100644 --- a/src/org/traccar/notification/FormTypeEventForwarder.java +++ b/src/org/traccar/notification/FormTypeEventForwarder.java @@ -18,11 +18,7 @@ public class FormTypeEventForwarder extends EventForwarder { requestBuilder.setFormParams(splitParams(getAdditionalParams(), "=")); } - String payload = isUseTemplatesForPayload() - ? NotificationFormatter.formatForwarderMessage(event, position) - : prepareJsonPayload(event, position); - - requestBuilder.addFormParam(getPayloadParamName(), payload); + requestBuilder.addFormParam(getPayloadParamName(), prepareJsonPayload(event, position)); } } -- cgit v1.2.3 From abf9687234ed94dc130cab896355604f2c4115f3 Mon Sep 17 00:00:00 2001 From: Srk Date: Sat, 18 Nov 2017 19:20:09 +0100 Subject: Revert "User defaults when no User is available" This reverts commit ccb2e9e5e0581406963ac63a9d08acb7c2549834. --- src/org/traccar/reports/ReportUtils.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 22c7981d8..f6f386e99 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -61,19 +61,19 @@ public final class ReportUtils { } } - public static String getDistanceUnit(Long userId) { + public static String getDistanceUnit(long userId) { return (String) Context.getPermissionsManager().lookupAttribute(userId, "distanceUnit", "km"); } - public static String getSpeedUnit(Long userId) { + public static String getSpeedUnit(long userId) { return (String) Context.getPermissionsManager().lookupAttribute(userId, "speedUnit", "kn"); } - public static String getVolumeUnit(Long userId) { + public static String getVolumeUnit(long userId) { return (String) Context.getPermissionsManager().lookupAttribute(userId, "volumeUnit", "ltr"); } - public static TimeZone getTimezone(Long userId) { + public static TimeZone getTimezone(long userId) { String timezone = (String) Context.getPermissionsManager().lookupAttribute(userId, "timezone", null); return timezone != null ? TimeZone.getTimeZone(timezone) : TimeZone.getDefault(); } @@ -137,7 +137,7 @@ public final class ReportUtils { return null; } - public static org.jxls.common.Context initializeContext(Long userId) { + public static org.jxls.common.Context initializeContext(long userId) { org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); jxlsContext.putVar("distanceUnit", getDistanceUnit(userId)); jxlsContext.putVar("speedUnit", getSpeedUnit(userId)); -- cgit v1.2.3 From f8fe96bf6f14d841b30284ae9a0973e9c5c98054 Mon Sep 17 00:00:00 2001 From: Srk Date: Sat, 18 Nov 2017 19:22:10 +0100 Subject: Revert "User defaults when no User is available" This reverts commit 5117b2e64781161c9e06f57cbee217f174d5731e. --- src/org/traccar/database/PermissionsManager.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 203f25a3d..07b60ba58 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -56,8 +56,8 @@ public class PermissionsManager { refreshDeviceAndGroupPermissions(); } - public User getUser(Long userId) { - return userId != null ? (User) usersManager.getById(userId) : null; + public User getUser(long userId) { + return (User) usersManager.getById(userId); } public Set getGroupPermissions(long userId) { @@ -416,15 +416,10 @@ public class PermissionsManager { return null; } - public Object lookupAttribute(Long userId, String key, Object defaultValue) { + public Object lookupAttribute(long userId, String key, Object defaultValue) { Object preference; - Object userPreference = null; Object serverPreference = server.getAttributes().get(key); - - if (userId != null) { - userPreference = getUser(userId).getAttributes().get(key); - } - + Object userPreference = getUser(userId).getAttributes().get(key); if (server.getForceSettings()) { preference = serverPreference != null ? serverPreference : userPreference; } else { -- cgit v1.2.3 From fbc49050c626ccd3d25370031204cc64e5a8f5c2 Mon Sep 17 00:00:00 2001 From: Srk Date: Sat, 18 Nov 2017 19:40:10 +0100 Subject: Removing notification templates references --- src/org/traccar/notification/NotificationFormatter.java | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index bd919cce0..385f117ea 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -102,12 +102,6 @@ public final class NotificationFormatter { 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) { -- cgit v1.2.3 From e6ae0f1de880ab887d84aa31fe88b2e1eb67bad8 Mon Sep 17 00:00:00 2001 From: Srk Date: Sat, 18 Nov 2017 19:52:34 +0100 Subject: Rollback number as primitive type --- src/org/traccar/notification/NotificationFormatter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 385f117ea..c537a64e0 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -39,7 +39,7 @@ public final class NotificationFormatter { private NotificationFormatter() { } - public static VelocityContext prepareContext(Long userId, Event event, Position position) { + public static VelocityContext prepareContext(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); Device device = Context.getIdentityManager().getById(event.getDeviceId()); -- cgit v1.2.3 From 7d2f57e2f45b8e3f92324eb4f9fd1cdd975d311c Mon Sep 17 00:00:00 2001 From: Srk Date: Sat, 18 Nov 2017 20:10:33 +0100 Subject: Rollback getting defaults without having an user instance --- src/org/traccar/notification/NotificationFormatter.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index c537a64e0..ba3fdbecb 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -45,10 +45,7 @@ public final class NotificationFormatter { Device device = Context.getIdentityManager().getById(event.getDeviceId()); VelocityContext velocityContext = new VelocityContext(); - - if (user != null) { - velocityContext.put("user", user); - } + velocityContext.put("user", user); velocityContext.put("device", device); velocityContext.put("event", event); if (position != null) { -- cgit v1.2.3 From c7928ef4a722cb40e9911c8e76e308fc48c85567 Mon Sep 17 00:00:00 2001 From: Srk Date: Sun, 19 Nov 2017 11:13:48 +0100 Subject: - Global jsonPrettyPrinting option - Minor refactoring - Config option name changed from "event.forward.prettyPrintedPayload" to "config.mapper.prettyPrintedJson" --- src/org/traccar/Context.java | 188 ++++++++++++----------- src/org/traccar/notification/EventForwarder.java | 15 +- 2 files changed, 98 insertions(+), 105 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 2317b7039..5d2d5a86a 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -16,7 +16,6 @@ package org.traccar; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.SerializationFeature; import com.ning.http.client.AsyncHttpClient; @@ -99,12 +98,6 @@ public final class Context { return objectMapper; } - private static ObjectWriter objectWriterPrettyPrinter; - - public static ObjectWriter getObjectWriterPrettyPrinter() { - return objectWriterPrettyPrinter; - } - private static IdentityManager identityManager; public static IdentityManager getIdentityManager() { @@ -289,7 +282,9 @@ public final class Context { objectMapper = new ObjectMapper(); objectMapper.setConfig( objectMapper.getSerializationConfig().without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)); - objectWriterPrettyPrinter = objectMapper.writerWithDefaultPrettyPrinter(); + if (Context.getConfig().getBoolean("config.mapper.prettyPrintedJson")) { + objectMapper.enable(SerializationFeature.INDENT_OUTPUT); + } if (config.hasKey("database.url")) { dataManager = new DataManager(config); @@ -308,59 +303,11 @@ public final class Context { identityManager = deviceManager; if (config.getBoolean("geocoder.enable")) { - String type = config.getString("geocoder.type", "google"); - String url = config.getString("geocoder.url"); - String key = config.getString("geocoder.key"); - String language = config.getString("geocoder.language"); - - int cacheSize = config.getInteger("geocoder.cacheSize"); - switch (type) { - case "nominatim": - geocoder = new NominatimGeocoder(url, key, language, cacheSize); - break; - case "gisgraphy": - geocoder = new GisgraphyGeocoder(url, cacheSize); - break; - case "mapquest": - geocoder = new MapQuestGeocoder(url, key, cacheSize); - break; - case "opencage": - geocoder = new OpenCageGeocoder(url, key, cacheSize); - break; - case "bingmaps": - geocoder = new BingMapsGeocoder(url, key, cacheSize); - break; - case "factual": - geocoder = new FactualGeocoder(url, key, cacheSize); - break; - case "geocodefarm": - geocoder = new GeocodeFarmGeocoder(key, language, cacheSize); - break; - default: - geocoder = new GoogleGeocoder(key, language, cacheSize); - break; - } + initReverseGeocoder(); } if (config.getBoolean("geolocation.enable")) { - String type = config.getString("geolocation.type", "mozilla"); - String url = config.getString("geolocation.url"); - String key = config.getString("geolocation.key"); - - switch (type) { - case "google": - geolocationProvider = new GoogleGeolocationProvider(key); - break; - case "opencellid": - geolocationProvider = new OpenCellIdGeolocationProvider(key); - break; - case "unwired": - geolocationProvider = new UnwiredGeolocationProvider(url, key); - break; - default: - geolocationProvider = new MozillaGeolocationProvider(key); - break; - } + initLocationResolutionSystem(); } if (config.getBoolean("web.enable")) { @@ -374,39 +321,17 @@ public final class Context { tripsConfig = initTripsConfig(); if (config.getBoolean("event.enable")) { - geofenceManager = new GeofenceManager(dataManager); - calendarManager = new CalendarManager(dataManager); - notificationManager = new NotificationManager(dataManager); - Properties velocityProperties = new Properties(); - velocityProperties.setProperty("file.resource.loader.path", - Context.getConfig().getString("templates.rootPath", "templates") + "/"); - velocityProperties.setProperty("runtime.log.logsystem.class", - "org.apache.velocity.runtime.log.NullLogChute"); - - String address; - try { - address = config.getString("web.address", InetAddress.getLocalHost().getHostAddress()); - } catch (UnknownHostException e) { - address = "localhost"; - } - - String webUrl = URIUtil.newURI("http", address, config.getInteger("web.port", 8082), "", ""); - webUrl = Context.getConfig().getString("web.url", webUrl); - velocityProperties.setProperty("web.url", webUrl); - - velocityEngine = new VelocityEngine(); - velocityEngine.init(velocityProperties); - - motionEventHandler = new MotionEventHandler(tripsConfig); - overspeedEventHandler = new OverspeedEventHandler( - Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, - Context.getConfig().getBoolean("event.overspeed.notRepeat")); + initEventsSubsystem(); } serverManager = new ServerManager(); if (config.getBoolean("event.forward.enable")) { - eventForwarder = getConfiguredEventForwarder(); + if (Context.getConfig().getBoolean("event.forward.payloadAsParamMode")) { + eventForwarder = new FormTypeEventForwarder(); + } else { + eventForwarder = new JsonTypeEventForwarder(); + } } attributesManager = new AttributesManager(dataManager); @@ -423,17 +348,98 @@ public final class Context { } - private static EventForwarder getConfiguredEventForwarder() { - if (Context.getConfig().getBoolean("event.forward.payloadAsParamMode")) { - return new FormTypeEventForwarder(); + private static void initLocationResolutionSystem() { + + String type = config.getString("geolocation.type", "mozilla"); + String url = config.getString("geolocation.url"); + String key = config.getString("geolocation.key"); + + switch (type) { + case "google": + geolocationProvider = new GoogleGeolocationProvider(key); + break; + case "opencellid": + geolocationProvider = new OpenCellIdGeolocationProvider(key); + break; + case "unwired": + geolocationProvider = new UnwiredGeolocationProvider(url, key); + break; + default: + geolocationProvider = new MozillaGeolocationProvider(key); + break; + } + } + + private static void initEventsSubsystem() { + + geofenceManager = new GeofenceManager(dataManager); + calendarManager = new CalendarManager(dataManager); + notificationManager = new NotificationManager(dataManager); + Properties velocityProperties = new Properties(); + velocityProperties.setProperty("file.resource.loader.path", + Context.getConfig().getString("templates.rootPath", "templates") + "/"); + velocityProperties.setProperty("runtime.log.logsystem.class", + "org.apache.velocity.runtime.log.NullLogChute"); + + String address; + try { + address = config.getString("web.address", InetAddress.getLocalHost().getHostAddress()); + } catch (UnknownHostException e) { + address = "localhost"; + } + + String webUrl = URIUtil.newURI("http", address, config.getInteger("web.port", 8082), "", ""); + webUrl = Context.getConfig().getString("web.url", webUrl); + velocityProperties.setProperty("web.url", webUrl); + + velocityEngine = new VelocityEngine(); + velocityEngine.init(velocityProperties); + + motionEventHandler = new MotionEventHandler(tripsConfig); + overspeedEventHandler = new OverspeedEventHandler( + Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, + Context.getConfig().getBoolean("event.overspeed.notRepeat")); + } + + private static void initReverseGeocoder() { + + String type = config.getString("geocoder.type", "google"); + String url = config.getString("geocoder.url"); + String key = config.getString("geocoder.key"); + String language = config.getString("geocoder.language"); + + int cacheSize = config.getInteger("geocoder.cacheSize"); + switch (type) { + case "nominatim": + geocoder = new NominatimGeocoder(url, key, language, cacheSize); + break; + case "gisgraphy": + geocoder = new GisgraphyGeocoder(url, cacheSize); + break; + case "mapquest": + geocoder = new MapQuestGeocoder(url, key, cacheSize); + break; + case "opencage": + geocoder = new OpenCageGeocoder(url, key, cacheSize); + break; + case "bingmaps": + geocoder = new BingMapsGeocoder(url, key, cacheSize); + break; + case "factual": + geocoder = new FactualGeocoder(url, key, cacheSize); + break; + case "geocodefarm": + geocoder = new GeocodeFarmGeocoder(key, language, cacheSize); + break; + default: + geocoder = new GoogleGeocoder(key, language, cacheSize); + break; } - return new JsonTypeEventForwarder(); } public static void init(IdentityManager testIdentityManager) { config = new Config(); objectMapper = new ObjectMapper(); - objectWriterPrettyPrinter = objectMapper.writerWithDefaultPrettyPrinter(); identityManager = testIdentityManager; } diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index c21799d74..d7e0eca19 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -29,7 +29,6 @@ import org.traccar.model.Geofence; import org.traccar.model.Position; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectWriter; import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; import com.ning.http.client.FluentCaseInsensitiveStringsMap; @@ -39,14 +38,12 @@ public abstract class EventForwarder { private String header; 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", ""); payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); - prettyPrinted = Context.getConfig().getBoolean("event.forward.prettyPrintedPayload"); } private static final String KEY_POSITION = "position"; @@ -105,29 +102,19 @@ public abstract class EventForwarder { } } try { - return getObjectWriter().writeValueAsString(data); + return Context.getObjectMapper().writeValueAsString(data); } catch (JsonProcessingException e) { Log.warning(e); return null; } } - 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); -- cgit v1.2.3 From 0daa91ca51ecbb23a7f962fd64bfe5817f8eaa29 Mon Sep 17 00:00:00 2001 From: Srk Date: Mon, 20 Nov 2017 19:55:11 +0100 Subject: Minor refactoring --- src/org/traccar/Context.java | 10 +++++----- src/org/traccar/notification/EventForwarder.java | 10 +++++----- src/org/traccar/notification/JsonTypeEventForwarder.java | 1 - src/org/traccar/notification/NotificationFormatter.java | 6 +++--- 4 files changed, 13 insertions(+), 14 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index a03b14b1b..d77b6878f 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -325,7 +325,7 @@ public final class Context { objectMapper = new ObjectMapper(); objectMapper.setConfig( objectMapper.getSerializationConfig().without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)); - if (Context.getConfig().getBoolean("config.mapper.prettyPrintedJson")) { + if (Context.getConfig().getBoolean("mapper.prettyPrintedJson")) { objectMapper.enable(SerializationFeature.INDENT_OUTPUT); } @@ -354,7 +354,7 @@ public final class Context { } if (config.getBoolean("geolocation.enable")) { - initLocationResolutionSystem(); + initGeolocationModule(); } if (config.getBoolean("web.enable")) { @@ -368,7 +368,7 @@ public final class Context { tripsConfig = initTripsConfig(); if (config.getBoolean("event.enable")) { - initEventsSubsystem(); + initEventsModule(); } serverManager = new ServerManager(); @@ -395,7 +395,7 @@ public final class Context { } - private static void initLocationResolutionSystem() { + private static void initGeolocationModule() { String type = config.getString("geolocation.type", "mozilla"); String url = config.getString("geolocation.url"); @@ -417,7 +417,7 @@ public final class Context { } } - private static void initEventsSubsystem() { + private static void initEventsModule() { geofenceManager = new GeofenceManager(dataManager); calendarManager = new CalendarManager(dataManager); diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index d7e0eca19..1a10d7ca2 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -15,11 +15,10 @@ */ package org.traccar.notification; -import java.nio.charset.StandardCharsets; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.traccar.Context; import org.traccar.helper.Log; @@ -28,8 +27,9 @@ import org.traccar.model.Event; import org.traccar.model.Geofence; import org.traccar.model.Position; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; import com.ning.http.client.FluentCaseInsensitiveStringsMap; public abstract class EventForwarder { diff --git a/src/org/traccar/notification/JsonTypeEventForwarder.java b/src/org/traccar/notification/JsonTypeEventForwarder.java index 196a4eca5..c1e4220d0 100644 --- a/src/org/traccar/notification/JsonTypeEventForwarder.java +++ b/src/org/traccar/notification/JsonTypeEventForwarder.java @@ -9,7 +9,6 @@ public class JsonTypeEventForwarder extends EventForwarder { @Override protected String getContentType() { - return "application/json; charset=utf-8"; } diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index ba3fdbecb..114825a83 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -88,7 +88,7 @@ public final class NotificationFormatter { public static MailMessage formatMailMessage(long userId, Event event, Position position) { String templatePath = Context.getConfig().getString("mail.templatesPath", "mail"); VelocityContext velocityContext = prepareContext(userId, event, position); - String formattedMessage = formatterLogic(velocityContext, userId, event, position, templatePath); + String formattedMessage = formatMessage(velocityContext, userId, event, position, templatePath); return new MailMessage((String) velocityContext.get("subject"), formattedMessage); } @@ -96,10 +96,10 @@ public final class NotificationFormatter { public static String formatSmsMessage(long userId, Event event, Position position) { String templatePath = Context.getConfig().getString("sms.templatesPath", "sms"); - return formatterLogic(null, userId, event, position, templatePath); + return formatMessage(null, userId, event, position, templatePath); } - private static String formatterLogic(VelocityContext vc, Long userId, Event event, Position position, + private static String formatMessage(VelocityContext vc, Long userId, Event event, Position position, String templatePath) { VelocityContext velocityContext = vc; -- cgit v1.2.3 From 2092b8ffbe6341d4256ee269cfff59df9612d7a7 Mon Sep 17 00:00:00 2001 From: Srk Date: Mon, 20 Nov 2017 20:10:52 +0100 Subject: Minor refactoring --- src/org/traccar/notification/EventForwarder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index 1a10d7ca2..aa3ce177a 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -34,8 +34,8 @@ import com.ning.http.client.FluentCaseInsensitiveStringsMap; public abstract class EventForwarder { - private String url; - private String header; + private final String url; + private final String header; private final String payloadParamName; private final String additionalParams; -- cgit v1.2.3 From 07eae46acf26e2d978655f8ec0e5fd09c47c1546 Mon Sep 17 00:00:00 2001 From: Srk Date: Mon, 20 Nov 2017 22:18:00 +0100 Subject: Refactoring: parameters only for one mode of eventForwarder --- src/org/traccar/notification/EventForwarder.java | 11 ----------- src/org/traccar/notification/FormTypeEventForwarder.java | 15 ++++++++++++--- 2 files changed, 12 insertions(+), 14 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index aa3ce177a..cc83aefbc 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -36,14 +36,10 @@ public abstract class EventForwarder { private final String url; private final String header; - private final String payloadParamName; - private final String additionalParams; public EventForwarder() { url = Context.getConfig().getString("event.forward.url", "http://localhost/"); header = Context.getConfig().getString("event.forward.header", ""); - payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); - additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); } private static final String KEY_POSITION = "position"; @@ -109,13 +105,6 @@ public abstract class EventForwarder { } } - protected String getPayloadParamName() { - return payloadParamName; - } - protected String getAdditionalParams() { - return additionalParams; - } - 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 index dede95707..1478faa03 100644 --- a/src/org/traccar/notification/FormTypeEventForwarder.java +++ b/src/org/traccar/notification/FormTypeEventForwarder.java @@ -1,5 +1,6 @@ package org.traccar.notification; +import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; @@ -7,6 +8,14 @@ import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; public class FormTypeEventForwarder extends EventForwarder { + private final String payloadParamName; + private final String additionalParams; + + public FormTypeEventForwarder() { + payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); + additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); + } + @Override protected String getContentType() { return "application/x-www-form-urlencoded; charset=UTF-8"; @@ -14,11 +23,11 @@ public class FormTypeEventForwarder extends EventForwarder { @Override protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) { - if (!getAdditionalParams().equals("")) { - requestBuilder.setFormParams(splitParams(getAdditionalParams(), "=")); + if (!additionalParams.equals("")) { + requestBuilder.setFormParams(splitParams(additionalParams, "=")); } - requestBuilder.addFormParam(getPayloadParamName(), prepareJsonPayload(event, position)); + requestBuilder.addFormParam(payloadParamName, prepareJsonPayload(event, position)); } } -- cgit v1.2.3 From 906d376b4f8c199b36064a0cd58cdf9c3ccb712f Mon Sep 17 00:00:00 2001 From: Srk Date: Wed, 22 Nov 2017 19:50:19 +0100 Subject: Changing form method --- src/org/traccar/Context.java | 4 +- .../notification/FormTypeEventForwarder.java | 33 --------------- .../notification/MultiPartEventForwarder.java | 47 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 35 deletions(-) delete mode 100644 src/org/traccar/notification/FormTypeEventForwarder.java create mode 100644 src/org/traccar/notification/MultiPartEventForwarder.java (limited to 'src/org') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index d77b6878f..3681cc2cc 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -71,8 +71,8 @@ import org.traccar.geolocation.GeolocationProvider; import org.traccar.geolocation.MozillaGeolocationProvider; import org.traccar.geolocation.OpenCellIdGeolocationProvider; import org.traccar.notification.EventForwarder; -import org.traccar.notification.FormTypeEventForwarder; import org.traccar.notification.JsonTypeEventForwarder; +import org.traccar.notification.MultiPartEventForwarder; import org.traccar.reports.model.TripsConfig; import org.traccar.smpp.SmppClient; import org.traccar.web.WebServer; @@ -375,7 +375,7 @@ public final class Context { if (config.getBoolean("event.forward.enable")) { if (Context.getConfig().getBoolean("event.forward.payloadAsParamMode")) { - eventForwarder = new FormTypeEventForwarder(); + eventForwarder = new MultiPartEventForwarder(); } else { eventForwarder = new JsonTypeEventForwarder(); } diff --git a/src/org/traccar/notification/FormTypeEventForwarder.java b/src/org/traccar/notification/FormTypeEventForwarder.java deleted file mode 100644 index 1478faa03..000000000 --- a/src/org/traccar/notification/FormTypeEventForwarder.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.traccar.notification; - -import org.traccar.Context; -import org.traccar.model.Event; -import org.traccar.model.Position; - -import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; - -public class FormTypeEventForwarder extends EventForwarder { - - private final String payloadParamName; - private final String additionalParams; - - public FormTypeEventForwarder() { - payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); - additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); - } - - @Override - protected String getContentType() { - return "application/x-www-form-urlencoded; charset=UTF-8"; - } - - @Override - protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) { - if (!additionalParams.equals("")) { - requestBuilder.setFormParams(splitParams(additionalParams, "=")); - } - - requestBuilder.addFormParam(payloadParamName, prepareJsonPayload(event, position)); - } - -} diff --git a/src/org/traccar/notification/MultiPartEventForwarder.java b/src/org/traccar/notification/MultiPartEventForwarder.java new file mode 100644 index 000000000..a77ad5975 --- /dev/null +++ b/src/org/traccar/notification/MultiPartEventForwarder.java @@ -0,0 +1,47 @@ +package org.traccar.notification; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.traccar.Context; +import org.traccar.model.Event; +import org.traccar.model.Position; + +import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; +import com.ning.http.client.multipart.StringPart; + +public class MultiPartEventForwarder extends EventForwarder { + + private final String payloadParamName; + private final String additionalParams; + + public MultiPartEventForwarder() { + payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); + additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); + } + + @Override + protected String getContentType() { + return "multipart/form-data"; + } + + @Override + protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) { + + if (!additionalParams.equals("")) { + Map> paramsToAdd = splitParams(additionalParams, "="); + + for (Entry> en : paramsToAdd.entrySet()) { + for (String value : en.getValue()) { + requestBuilder.addBodyPart(new StringPart(en.getKey(), value, null, StandardCharsets.UTF_8)); + } + } + } + requestBuilder.addBodyPart(new StringPart(payloadParamName, + prepareJsonPayload(event, position), + "application/json", + StandardCharsets.UTF_8)); + } +} -- cgit v1.2.3 From 82da12c1218ca7f1e4e1562cc183cc8a059045e6 Mon Sep 17 00:00:00 2001 From: Srk Date: Wed, 22 Nov 2017 22:59:37 +0100 Subject: Minor refactoring --- src/org/traccar/notification/EventForwarder.java | 9 +++++---- .../traccar/notification/MultiPartEventForwarder.java | 19 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index cc83aefbc..1354901a7 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -20,6 +20,7 @@ import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; import java.util.Arrays; import java.util.List; +import org.apache.commons.lang3.StringUtils; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -39,7 +40,7 @@ public abstract class EventForwarder { public EventForwarder() { url = Context.getConfig().getString("event.forward.url", "http://localhost/"); - header = Context.getConfig().getString("event.forward.header", ""); + header = Context.getConfig().getString("event.forward.header"); } private static final String KEY_POSITION = "position"; @@ -54,9 +55,9 @@ public abstract class EventForwarder { requestBuilder.addHeader("Content-Type", getContentType()); - if (!header.equals("")) { + if (StringUtils.isNotEmpty(header)) { FluentCaseInsensitiveStringsMap params = new FluentCaseInsensitiveStringsMap(); - params.putAll(splitParams(header, ":")); + params.putAll(splitIntoKeyValues(header, ":")); requestBuilder.setHeaders(params); } @@ -64,7 +65,7 @@ public abstract class EventForwarder { requestBuilder.execute(); } - protected Map> splitParams(String params, String separator) { + protected Map> splitIntoKeyValues(String params, String separator) { String[] splitedLine; Map> paramsMap = new HashMap<>(); diff --git a/src/org/traccar/notification/MultiPartEventForwarder.java b/src/org/traccar/notification/MultiPartEventForwarder.java index a77ad5975..6a3baecb3 100644 --- a/src/org/traccar/notification/MultiPartEventForwarder.java +++ b/src/org/traccar/notification/MultiPartEventForwarder.java @@ -1,10 +1,11 @@ package org.traccar.notification; -import java.nio.charset.StandardCharsets; +import static java.nio.charset.StandardCharsets.UTF_8; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.apache.commons.lang3.StringUtils; import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; @@ -19,7 +20,7 @@ public class MultiPartEventForwarder extends EventForwarder { public MultiPartEventForwarder() { payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); - additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); + additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams"); } @Override @@ -30,18 +31,16 @@ public class MultiPartEventForwarder extends EventForwarder { @Override protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) { - if (!additionalParams.equals("")) { - Map> paramsToAdd = splitParams(additionalParams, "="); + if (StringUtils.isNotEmpty(additionalParams)) { + Map> paramsToAdd = splitIntoKeyValues(additionalParams, "="); - for (Entry> en : paramsToAdd.entrySet()) { - for (String value : en.getValue()) { - requestBuilder.addBodyPart(new StringPart(en.getKey(), value, null, StandardCharsets.UTF_8)); + for (Entry> param : paramsToAdd.entrySet()) { + for (String singleParamValue : param.getValue()) { + requestBuilder.addBodyPart(new StringPart(param.getKey(), singleParamValue, null, UTF_8)); } } } requestBuilder.addBodyPart(new StringPart(payloadParamName, - prepareJsonPayload(event, position), - "application/json", - StandardCharsets.UTF_8)); + prepareJsonPayload(event, position), "application/json", UTF_8)); } } -- cgit v1.2.3 From 00c5cef324ed644ca5cae282864f3bca9629467a Mon Sep 17 00:00:00 2001 From: Srk Date: Thu, 23 Nov 2017 18:38:33 +0100 Subject: Minor refactoring --- src/org/traccar/notification/EventForwarder.java | 3 +-- src/org/traccar/notification/MultiPartEventForwarder.java | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index 1354901a7..68c3ea221 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -20,7 +20,6 @@ import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; import java.util.Arrays; import java.util.List; -import org.apache.commons.lang3.StringUtils; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -55,7 +54,7 @@ public abstract class EventForwarder { requestBuilder.addHeader("Content-Type", getContentType()); - if (StringUtils.isNotEmpty(header)) { + if (header != null && !header.isEmpty()) { FluentCaseInsensitiveStringsMap params = new FluentCaseInsensitiveStringsMap(); params.putAll(splitIntoKeyValues(header, ":")); requestBuilder.setHeaders(params); diff --git a/src/org/traccar/notification/MultiPartEventForwarder.java b/src/org/traccar/notification/MultiPartEventForwarder.java index 6a3baecb3..f4c36d3e4 100644 --- a/src/org/traccar/notification/MultiPartEventForwarder.java +++ b/src/org/traccar/notification/MultiPartEventForwarder.java @@ -1,11 +1,10 @@ package org.traccar.notification; -import static java.nio.charset.StandardCharsets.UTF_8; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import org.apache.commons.lang3.StringUtils; import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; @@ -31,16 +30,17 @@ public class MultiPartEventForwarder extends EventForwarder { @Override protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) { - if (StringUtils.isNotEmpty(additionalParams)) { + if (additionalParams != null && !additionalParams.isEmpty()) { Map> paramsToAdd = splitIntoKeyValues(additionalParams, "="); for (Entry> param : paramsToAdd.entrySet()) { for (String singleParamValue : param.getValue()) { - requestBuilder.addBodyPart(new StringPart(param.getKey(), singleParamValue, null, UTF_8)); + requestBuilder.addBodyPart(new StringPart(param.getKey(), singleParamValue, null, + StandardCharsets.UTF_8)); } } } requestBuilder.addBodyPart(new StringPart(payloadParamName, - prepareJsonPayload(event, position), "application/json", UTF_8)); + prepareJsonPayload(event, position), "application/json", StandardCharsets.UTF_8)); } } -- cgit v1.2.3