diff options
Diffstat (limited to 'src/org/traccar/notification')
4 files changed, 37 insertions, 96 deletions
diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index b13f8fe43..b8990d84f 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -15,25 +15,21 @@ */ package org.traccar.notification; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; -import java.util.Collections; -import java.util.List; - +import org.apache.commons.collections4.MultiValuedMap; +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.traccar.Context; -import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Geofence; +import org.traccar.model.Maintenance; import org.traccar.model.Position; -import java.nio.charset.StandardCharsets; +import javax.ws.rs.client.AsyncInvoker; +import javax.ws.rs.client.Invocation; import java.util.HashMap; import java.util.Map; import java.util.Set; -import com.ning.http.client.FluentCaseInsensitiveStringsMap; - public abstract class EventForwarder { private final String url; @@ -48,41 +44,34 @@ public abstract class EventForwarder { private static final String KEY_EVENT = "event"; private static final String KEY_GEOFENCE = "geofence"; private static final String KEY_DEVICE = "device"; + private static final String KEY_MAINTENANCE = "maintenance"; private static final String KEY_USERS = "users"; public final void forwardEvent(Event event, Position position, Set<Long> users) { - BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); - requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name()); - - requestBuilder.addHeader("Content-Type", getContentType()); + Invocation.Builder requestBuilder = Context.getClient().target(url).request(); if (header != null && !header.isEmpty()) { - FluentCaseInsensitiveStringsMap params = new FluentCaseInsensitiveStringsMap(); - params.putAll(splitIntoKeyValues(header, ":")); - requestBuilder.setHeaders(params); + for (Map.Entry<String, String> entry : splitKeyValues(header, ":").entries()) { + requestBuilder = requestBuilder.header(entry.getKey(), entry.getValue()); + } } - setContent(event, position, users, requestBuilder); - requestBuilder.execute(); + executeRequest(event, position, users, requestBuilder.async()); } - protected Map<String, List<String>> splitIntoKeyValues(String params, String separator) { - - String[] splitedLine; - Map<String, List<String>> 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(), Collections.singletonList(splitedLine[1].trim())); + protected MultiValuedMap<String, String> splitKeyValues(String params, String separator) { + MultiValuedMap<String, String> data = new ArrayListValuedHashMap<>(); + for (String line: params.split("\\r?\\n")) { + String[] values = line.split(separator, 2); + if (values.length == 2) { + data.put(values[0].trim(), values[1].trim()); } } - return paramsMap; + return data; } - protected String prepareJsonPayload(Event event, Position position, Set<Long> users) { + protected Map<String, Object> preparePayload(Event event, Position position, Set<Long> users) { Map<String, Object> data = new HashMap<>(); data.put(KEY_EVENT, event); if (position != null) { @@ -98,17 +87,17 @@ public abstract class EventForwarder { data.put(KEY_GEOFENCE, geofence); } } - data.put(KEY_USERS, Context.getUsersManager().getItems(users)); - try { - return Context.getObjectMapper().writeValueAsString(data); - } catch (JsonProcessingException e) { - Log.warning(e); - return null; + if (event.getMaintenanceId() != 0) { + Maintenance maintenance = Context.getMaintenancesManager().getById(event.getMaintenanceId()); + if (maintenance != null) { + data.put(KEY_MAINTENANCE, maintenance); + } } + data.put(KEY_USERS, Context.getUsersManager().getItems(users)); + return data; } - protected abstract String getContentType(); - protected abstract void setContent( - Event event, Position position, Set<Long> users, BoundRequestBuilder requestBuilder); + protected abstract void executeRequest( + Event event, Position position, Set<Long> users, AsyncInvoker invoker); } diff --git a/src/org/traccar/notification/JsonTypeEventForwarder.java b/src/org/traccar/notification/JsonTypeEventForwarder.java index 27ef61af1..fcafb964a 100644 --- a/src/org/traccar/notification/JsonTypeEventForwarder.java +++ b/src/org/traccar/notification/JsonTypeEventForwarder.java @@ -5,18 +5,14 @@ import java.util.Set; import org.traccar.model.Event;
import org.traccar.model.Position;
-import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
+import javax.ws.rs.client.AsyncInvoker;
+import javax.ws.rs.client.Entity;
public class JsonTypeEventForwarder extends EventForwarder {
@Override
- protected String getContentType() {
- return "application/json; charset=utf-8";
- }
-
- @Override
- protected void setContent(Event event, Position position, Set<Long> users, BoundRequestBuilder requestBuilder) {
- requestBuilder.setBody(prepareJsonPayload(event, position, users));
+ protected void executeRequest(Event event, Position position, Set<Long> users, AsyncInvoker invoker) {
+ invoker.post(Entity.json(preparePayload(event, position, users)));
}
}
diff --git a/src/org/traccar/notification/MultiPartEventForwarder.java b/src/org/traccar/notification/MultiPartEventForwarder.java deleted file mode 100644 index 6227c66cc..000000000 --- a/src/org/traccar/notification/MultiPartEventForwarder.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.traccar.notification; - -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Map; -import java.util.Set; -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, Set<Long> users, BoundRequestBuilder requestBuilder) { - - if (additionalParams != null && !additionalParams.isEmpty()) { - Map<String, List<String>> paramsToAdd = splitIntoKeyValues(additionalParams, "="); - - for (Entry<String, List<String>> param : paramsToAdd.entrySet()) { - for (String singleParamValue : param.getValue()) { - requestBuilder.addBodyPart(new StringPart(param.getKey(), singleParamValue, null, - StandardCharsets.UTF_8)); - } - } - } - requestBuilder.addBodyPart(new StringPart(payloadParamName, - prepareJsonPayload(event, position, users), "application/json", StandardCharsets.UTF_8)); - } -} diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index d1f6c3903..ddc35227e 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -1,6 +1,6 @@ /* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,6 +57,9 @@ public final class NotificationFormatter { if (event.getGeofenceId() != 0) { velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId())); } + if (event.getMaintenanceId() != 0) { + velocityContext.put("maintenance", Context.getMaintenancesManager().getById(event.getMaintenanceId())); + } String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID); if (driverUniqueId != null) { velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId)); |