aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-03-05 15:06:59 -0800
committerGitHub <noreply@github.com>2020-03-05 15:06:59 -0800
commit74b5d0c139dd9a76064395aa733330083fd46fb8 (patch)
tree6d73596ad6696c6f9f99a590aa136491e518efc8 /src/main/java/org/traccar
parent9bb835a9d45737d5090d1b78ee69310d59cb0a43 (diff)
parent3c82764799784d3d30d969dbfef9a075f4874278 (diff)
downloadtraccar-server-74b5d0c139dd9a76064395aa733330083fd46fb8.tar.gz
traccar-server-74b5d0c139dd9a76064395aa733330083fd46fb8.tar.bz2
traccar-server-74b5d0c139dd9a76064395aa733330083fd46fb8.zip
Merge pull request #4495 from presidentio/allowing-custom-content-type-header-in-location-forward
Allowing custom content type header in location forward
Diffstat (limited to 'src/main/java/org/traccar')
-rw-r--r--src/main/java/org/traccar/WebDataHandler.java22
-rw-r--r--src/main/java/org/traccar/config/Keys.java7
2 files changed, 26 insertions, 3 deletions
diff --git a/src/main/java/org/traccar/WebDataHandler.java b/src/main/java/org/traccar/WebDataHandler.java
index 39e54616b..d6bfb126b 100644
--- a/src/main/java/org/traccar/WebDataHandler.java
+++ b/src/main/java/org/traccar/WebDataHandler.java
@@ -33,6 +33,8 @@ import org.traccar.model.Position;
import org.traccar.model.Group;
import javax.inject.Inject;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
@@ -65,6 +67,7 @@ public class WebDataHandler extends BaseDataHandler {
private final String url;
private final String header;
private final boolean json;
+ private final boolean urlVariables;
private final boolean retryEnabled;
private final int retryDelay;
@@ -83,6 +86,7 @@ public class WebDataHandler extends BaseDataHandler {
this.url = config.getString(Keys.FORWARD_URL);
this.header = config.getString(Keys.FORWARD_HEADER);
this.json = config.getBoolean(Keys.FORWARD_JSON);
+ this.urlVariables = config.getBoolean(Keys.FORWARD_URL_VARIABLES);
this.retryEnabled = config.getBoolean(Keys.FORWARD_RETRY_ENABLE);
this.retryDelay = config.getInteger(Keys.FORWARD_RETRY_DELAY, 100);
@@ -184,12 +188,13 @@ public class WebDataHandler extends BaseDataHandler {
private int retries = 0;
private Map<String, Object> payload;
private Invocation.Builder requestBuilder;
+ private MediaType mediaType = MediaType.APPLICATION_JSON_TYPE;
AsyncRequestAndCallback(Position position) {
String formattedUrl;
try {
- formattedUrl = json ? url : formatRequest(position);
+ formattedUrl = (json && !urlVariables) ? url : formatRequest(position);
} catch (UnsupportedEncodingException | JsonProcessingException e) {
throw new RuntimeException("Forwarding formatting error", e);
}
@@ -198,7 +203,13 @@ public class WebDataHandler extends BaseDataHandler {
if (header != null && !header.isEmpty()) {
for (String line: header.split("\\r?\\n")) {
String[] values = line.split(":", 2);
- requestBuilder.header(values[0].trim(), values[1].trim());
+ String headerName = values[0].trim();
+ String headerValue = values[1].trim();
+ if (headerName.equals(HttpHeaders.CONTENT_TYPE)) {
+ mediaType = MediaType.valueOf(headerValue);
+ } else {
+ requestBuilder.header(headerName, headerValue);
+ }
}
}
@@ -211,7 +222,12 @@ public class WebDataHandler extends BaseDataHandler {
private void send() {
if (json) {
- requestBuilder.async().post(Entity.json(payload), this);
+ try {
+ Entity<String> entity = Entity.entity(objectMapper.writeValueAsString(payload), mediaType);
+ requestBuilder.async().post(entity, this);
+ } catch (JsonProcessingException e) {
+ throw new RuntimeException("Failed to serialize location to json", e);
+ }
} else {
requestBuilder.async().get(this);
}
diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java
index d88b36d28..200ef8aa3 100644
--- a/src/main/java/org/traccar/config/Keys.java
+++ b/src/main/java/org/traccar/config/Keys.java
@@ -101,6 +101,13 @@ public final class Keys {
"forward.json", Boolean.class);
/**
+ * Boolean value to enable URL parameters in json mode. For example, {uniqueId} for device identifier,
+ * {latitude} and {longitude} for coordinates.
+ */
+ public static final ConfigKey FORWARD_URL_VARIABLES = new ConfigKey(
+ "forward.urlVariables", Boolean.class);
+
+ /**
* Position forwarding retrying enable. When enabled, additional attempts are made to deliver positions. If initial
* delivery fails, because of an unreachable server or an HTTP response different from '2xx', the software waits
* for 'forward.retry.delay' milliseconds to retry delivery. On subsequent failures, this delay is duplicated.