diff options
Diffstat (limited to 'src/org/traccar/notification')
-rw-r--r-- | src/org/traccar/notification/EventForwarder.java | 59 | ||||
-rw-r--r-- | src/org/traccar/notification/JsonTypeEventForwarder.java | 12 | ||||
-rw-r--r-- | src/org/traccar/notification/MultiPartEventForwarder.java | 47 |
3 files changed, 24 insertions, 94 deletions
diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index 9d2181e21..b8990d84f 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -15,26 +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; @@ -54,37 +49,29 @@ public abstract class EventForwarder { 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) { @@ -107,16 +94,10 @@ public abstract class EventForwarder { } } data.put(KEY_USERS, Context.getUsersManager().getItems(users)); - try { - return Context.getObjectMapper().writeValueAsString(data); - } catch (JsonProcessingException e) { - Log.warning(e); - return null; - } + 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)); - } -} |