From 1593a9dee20ee7355f9f5feb6d121a5ad69a283f Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 13 Jul 2016 19:04:38 +0500 Subject: Renamed to EventForwarder and moved to Context --- src/org/traccar/notification/EventForwarder.java | 71 ++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/org/traccar/notification/EventForwarder.java (limited to 'src/org/traccar/notification/EventForwarder.java') diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java new file mode 100644 index 000000000..32f8a1ffc --- /dev/null +++ b/src/org/traccar/notification/EventForwarder.java @@ -0,0 +1,71 @@ +package org.traccar.notification; + +import java.nio.charset.StandardCharsets; + +import javax.json.Json; +import javax.json.JsonObjectBuilder; + +import org.traccar.Context; +import org.traccar.model.Device; +import org.traccar.model.Event; +import org.traccar.model.Geofence; +import org.traccar.model.Position; +import org.traccar.web.JsonConverter; + +import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; + +public final class NotificationForward { + + private NotificationForward() { + } + + private static final String USER_AGENT = "Traccar Server"; + + private static final String KEY_POSITION = "position"; + private static final String KEY_EVENT = "event"; + private static final String KEY_GEOFENCE = "geofence"; + private static final String KEY_DEVICE = "device"; + + public static void forwardEvent(Event event, Position position) { + String url = Context.getConfig().getString("event.forward.url", "http://localhost/"); + String header = Context.getConfig().getString("event.forward.header", ""); + + BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); + + requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8"); + requestBuilder.addHeader("User-Agent", USER_AGENT); + if (!header.equals("")) { + String[] headerLines = header.split("\\r?\\n"); + for (String headerLine: headerLines) { + String[] splitedLine = headerLine.split(":", 2); + if (splitedLine.length == 2) { + requestBuilder.addHeader(splitedLine[0].trim(), splitedLine[1].trim()); + } + } + } + + requestBuilder.setBody(preparePayload(event, position)); + requestBuilder.execute(); + } + + private static byte[] preparePayload(Event event, Position position) { + JsonObjectBuilder json = Json.createObjectBuilder(); + json.add(KEY_EVENT, JsonConverter.objectToJson(event)); + if (position != null) { + json.add(KEY_POSITION, JsonConverter.objectToJson(position)); + } + if (event.getDeviceId() != 0) { + Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId()); + if (device != null) { + json.add(KEY_DEVICE, JsonConverter.objectToJson(device)); + } + } + if (event.getGeofenceId() != 0) { + Geofence geofence = Context.getGeofenceManager().getGeofence(event.getGeofenceId()); + if (geofence != null) { + json.add(KEY_GEOFENCE, JsonConverter.objectToJson(geofence)); + } + } + return json.build().toString().getBytes(StandardCharsets.UTF_8); + } +} -- cgit v1.2.3 From 3502e07f0aee825f8599526428dd1962d81f9933 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 13 Jul 2016 19:11:36 +0500 Subject: Renamed to EventForwarder and moved to Context --- src/org/traccar/Context.java | 11 +++++++++++ src/org/traccar/database/NotificationManager.java | 8 ++------ src/org/traccar/notification/EventForwarder.java | 16 ++++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) (limited to 'src/org/traccar/notification/EventForwarder.java') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index ebe700221..0bb18b675 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -35,6 +35,7 @@ import org.traccar.helper.Log; import org.traccar.location.LocationProvider; import org.traccar.location.MozillaLocationProvider; import org.traccar.location.OpenCellIdLocationProvider; +import org.traccar.notification.EventForwarder; import org.traccar.web.WebServer; public final class Context { @@ -120,6 +121,12 @@ public final class Context { return ASYNC_HTTP_CLIENT; } + private static EventForwarder eventForwarder; + + public static EventForwarder getEventForvarder() { + return eventForwarder; + } + public static void init(String[] arguments) throws Exception { config = new Config(); @@ -210,6 +217,10 @@ public final class Context { serverManager = new ServerManager(); + if (config.getBoolean("event.forward.enable")) { + eventForwarder = new EventForwarder(); + } + } public static void init(IdentityManager testIdentityManager) { diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 90edce43e..911c5d2dc 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -31,7 +31,6 @@ import org.traccar.helper.Log; import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Position; -import org.traccar.notification.NotificationForward; import org.traccar.notification.NotificationMail; public class NotificationManager { @@ -42,11 +41,8 @@ public class NotificationManager { private final ReadWriteLock notificationsLock = new ReentrantReadWriteLock(); - private final boolean forward; - public NotificationManager(DataManager dataManager) { this.dataManager = dataManager; - forward = Context.getConfig().getBoolean("event.forward.enable"); refresh(); } @@ -72,8 +68,8 @@ public class NotificationManager { } } } - if (forward) { - NotificationForward.forwardEvent(event, position); + if (Context.getEventForvarder() != null) { + Context.getEventForvarder().forwardEvent(event, position); } } diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index 32f8a1ffc..096026541 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -14,9 +14,14 @@ import org.traccar.web.JsonConverter; import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; -public final class NotificationForward { +public final class EventForwarder { - private NotificationForward() { + private String url; + private String header; + + public EventForwarder() { + url = Context.getConfig().getString("event.forward.url", "http://localhost/"); + header = Context.getConfig().getString("event.forward.header", ""); } private static final String USER_AGENT = "Traccar Server"; @@ -26,9 +31,8 @@ public final class NotificationForward { private static final String KEY_GEOFENCE = "geofence"; private static final String KEY_DEVICE = "device"; - public static void forwardEvent(Event event, Position position) { - String url = Context.getConfig().getString("event.forward.url", "http://localhost/"); - String header = Context.getConfig().getString("event.forward.header", ""); + public void forwardEvent(Event event, Position position) { + BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); @@ -48,7 +52,7 @@ public final class NotificationForward { requestBuilder.execute(); } - private static byte[] preparePayload(Event event, Position position) { + private byte[] preparePayload(Event event, Position position) { JsonObjectBuilder json = Json.createObjectBuilder(); json.add(KEY_EVENT, JsonConverter.objectToJson(event)); if (position != null) { -- cgit v1.2.3