From 377a2234ef8c25c54c22acf9db36d102347e0ad4 Mon Sep 17 00:00:00 2001 From: nativbarak Date: Sun, 4 Feb 2018 15:31:10 +0200 Subject: added POST option to forward url --- src/org/traccar/BasePipelineFactory.java | 2 +- src/org/traccar/WebDataHandler.java | 70 +++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 7 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index aa188d209..e8034779d 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -238,7 +238,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { } if (Context.getConfig().getBoolean("forward.enable")) { - pipeline.addLast("webHandler", new WebDataHandler(Context.getConfig().getString("forward.url"))); + pipeline.addLast("webHandler", new WebDataHandler(Context.getConfig().getString("forward.url"), Context.getConfig().getBoolean("forward.json"))); } if (commandResultEventHandler != null) { diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 401216276..10fac2e41 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -16,6 +16,7 @@ package org.traccar; import com.fasterxml.jackson.core.JsonProcessingException; +import com.ning.http.client.AsyncHttpClient; import org.traccar.helper.Checksum; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -24,17 +25,16 @@ import org.traccar.model.Position; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.Calendar; -import java.util.Formatter; -import java.util.Locale; -import java.util.TimeZone; +import java.util.*; public class WebDataHandler extends BaseDataHandler { private final String url; + private final Boolean json; - public WebDataHandler(String url) { + public WebDataHandler(String url, Boolean json) { this.url = url; + this.json = json; } private static String formatSentence(Position position) { @@ -121,10 +121,68 @@ public class WebDataHandler extends BaseDataHandler { @Override protected Position handlePosition(Position position) { + if(json){ + AsyncHttpClient.BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); + requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name()); - Context.getAsyncHttpClient().prepareGet(formatRequest(position)).execute(); + requestBuilder.addHeader("Content-Type", getContentType()); + setContent(position, requestBuilder); + requestBuilder.execute(); + }else { + Context.getAsyncHttpClient().prepareGet(formatRequest(position)).execute(); + } return position; } + protected String getContentType() { + return "application/json; charset=utf-8"; + } + + protected void setContent(Position position, AsyncHttpClient.BoundRequestBuilder requestBuilder) { + requestBuilder.setBody(prepareJsonPayload(position)); + } + + protected String prepareJsonPayload(Position position) { + + Map data = new HashMap<>(); + Device device = Context.getIdentityManager().getById(position.getDeviceId()); + + data.put("name", device.getName()); + data.put("uniqueId", device.getUniqueId()); + data.put("status", device.getStatus()); + data.put("deviceId", String.valueOf(position.getDeviceId())); + data.put("protocol", String.valueOf(position.getProtocol())); + data.put("deviceTime", String.valueOf(position.getDeviceTime().getTime())); + data.put("fixTime", String.valueOf(position.getFixTime().getTime())); + data.put("valid", String.valueOf(position.getValid())); + data.put("latitude", String.valueOf(position.getLatitude())); + data.put("longitude", String.valueOf(position.getLongitude())); + data.put("speed", String.valueOf(position.getSpeed())); + data.put("course", String.valueOf(position.getCourse())); + data.put("statusCode", calculateStatus(position)); + + if (position.getAddress() != null) { + try { + data.put("address", URLEncoder.encode(position.getAddress(), StandardCharsets.UTF_8.name())); + } catch (UnsupportedEncodingException error) { + Log.warning(error); + } + } + + try { + String attributes = Context.getObjectMapper().writeValueAsString(position.getAttributes()); + + data.put("attributes", URLEncoder.encode(attributes, StandardCharsets.UTF_8.name())); + } catch (UnsupportedEncodingException | JsonProcessingException error) { + Log.warning(error); + } + + try { + return Context.getObjectMapper().writeValueAsString(data); + } catch (JsonProcessingException e) { + Log.warning(e); + return null; + } + } } -- cgit v1.2.3 From dc092138fc69bb5f8f137be435309d3945c49b13 Mon Sep 17 00:00:00 2001 From: nativbarak Date: Sun, 4 Feb 2018 17:12:51 +0200 Subject: Fixed length on basepipelinefactory --- src/org/traccar/BasePipelineFactory.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/org') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index e8034779d..4407caae4 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -238,7 +238,9 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { } if (Context.getConfig().getBoolean("forward.enable")) { - pipeline.addLast("webHandler", new WebDataHandler(Context.getConfig().getString("forward.url"), Context.getConfig().getBoolean("forward.json"))); + final Boolean json = Context.getConfig().getBoolean("forward.json"); + final String url = Context.getConfig().getString("forward.url"); + pipeline.addLast("webHandler", new WebDataHandler(url, json)); } if (commandResultEventHandler != null) { -- cgit v1.2.3 From fc3106e742beba6aeaebb66083380c6b9e1082d0 Mon Sep 17 00:00:00 2001 From: nativbarak Date: Sun, 4 Feb 2018 17:21:51 +0200 Subject: Code refactoring --- src/org/traccar/WebDataHandler.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 10fac2e41..a4c95aa3c 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -121,7 +121,7 @@ public class WebDataHandler extends BaseDataHandler { @Override protected Position handlePosition(Position position) { - if(json){ + if (json) { AsyncHttpClient.BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name()); @@ -129,7 +129,8 @@ public class WebDataHandler extends BaseDataHandler { setContent(position, requestBuilder); requestBuilder.execute(); - }else { + + } else { Context.getAsyncHttpClient().prepareGet(formatRequest(position)).execute(); } return position; -- cgit v1.2.3 From 6ae8a397c29a3d7a16d9cf481e2141e3e70921fd Mon Sep 17 00:00:00 2001 From: nativbarak Date: Sun, 4 Feb 2018 17:26:05 +0200 Subject: code refactoring --- src/org/traccar/WebDataHandler.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index a4c95aa3c..a30a41208 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -22,10 +22,16 @@ import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Position; +import java.util.HashMap; +import java.util.Map; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.Calendar; +import java.util.Formatter; +import java.util.Locale; +import java.util.TimeZone; + public class WebDataHandler extends BaseDataHandler { @@ -129,7 +135,7 @@ public class WebDataHandler extends BaseDataHandler { setContent(position, requestBuilder); requestBuilder.execute(); - + } else { Context.getAsyncHttpClient().prepareGet(formatRequest(position)).execute(); } -- cgit v1.2.3 From 00b1715aa854f62b2cc90753c05d558cea9dd6ea Mon Sep 17 00:00:00 2001 From: nativbarak Date: Mon, 5 Feb 2018 10:59:55 +0200 Subject: changed POST to use full object --- src/org/traccar/WebDataHandler.java | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index a30a41208..e45a47835 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -37,6 +37,8 @@ public class WebDataHandler extends BaseDataHandler { private final String url; private final Boolean json; + private static final String KEY_POSITION = "position"; + private static final String KEY_DEVICE = "device"; public WebDataHandler(String url, Boolean json) { this.url = url; @@ -153,39 +155,20 @@ public class WebDataHandler extends BaseDataHandler { protected String prepareJsonPayload(Position position) { Map data = new HashMap<>(); - Device device = Context.getIdentityManager().getById(position.getDeviceId()); - - data.put("name", device.getName()); - data.put("uniqueId", device.getUniqueId()); - data.put("status", device.getStatus()); - data.put("deviceId", String.valueOf(position.getDeviceId())); - data.put("protocol", String.valueOf(position.getProtocol())); - data.put("deviceTime", String.valueOf(position.getDeviceTime().getTime())); - data.put("fixTime", String.valueOf(position.getFixTime().getTime())); - data.put("valid", String.valueOf(position.getValid())); - data.put("latitude", String.valueOf(position.getLatitude())); - data.put("longitude", String.valueOf(position.getLongitude())); - data.put("speed", String.valueOf(position.getSpeed())); - data.put("course", String.valueOf(position.getCourse())); - data.put("statusCode", calculateStatus(position)); - if (position.getAddress() != null) { - try { - data.put("address", URLEncoder.encode(position.getAddress(), StandardCharsets.UTF_8.name())); - } catch (UnsupportedEncodingException error) { - Log.warning(error); - } + if (position != null) { + data.put(KEY_POSITION, position); } - try { - String attributes = Context.getObjectMapper().writeValueAsString(position.getAttributes()); - - data.put("attributes", URLEncoder.encode(attributes, StandardCharsets.UTF_8.name())); - } catch (UnsupportedEncodingException | JsonProcessingException error) { - Log.warning(error); + if (position.getDeviceId() != 0) { + Device device = Context.getIdentityManager().getById(position.getDeviceId()); + if (device != null) { + data.put(KEY_DEVICE, device); + } } try { + System.out.println(Context.getObjectMapper().writeValueAsString(data).toString()); return Context.getObjectMapper().writeValueAsString(data); } catch (JsonProcessingException e) { Log.warning(e); -- cgit v1.2.3 From ed4a723e518d444bcfd6c310c4ed448b8ee8dedf Mon Sep 17 00:00:00 2001 From: nativbarak Date: Mon, 5 Feb 2018 13:35:19 +0200 Subject: added unit test, fixed spaces, fixed primitive types --- src/org/traccar/WebDataHandler.java | 6 ++---- test/org/traccar/WebDataHandlerTest.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index e45a47835..8bfabf183 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -32,15 +32,14 @@ import java.util.Formatter; import java.util.Locale; import java.util.TimeZone; - public class WebDataHandler extends BaseDataHandler { private final String url; - private final Boolean json; + private final boolean json; private static final String KEY_POSITION = "position"; private static final String KEY_DEVICE = "device"; - public WebDataHandler(String url, Boolean json) { + public WebDataHandler(String url, boolean json) { this.url = url; this.json = json; } @@ -168,7 +167,6 @@ public class WebDataHandler extends BaseDataHandler { } try { - System.out.println(Context.getObjectMapper().writeValueAsString(data).toString()); return Context.getObjectMapper().writeValueAsString(data); } catch (JsonProcessingException e) { Log.warning(e); diff --git a/test/org/traccar/WebDataHandlerTest.java b/test/org/traccar/WebDataHandlerTest.java index 70c744b3a..3e79232ec 100644 --- a/test/org/traccar/WebDataHandlerTest.java +++ b/test/org/traccar/WebDataHandlerTest.java @@ -3,6 +3,8 @@ package org.traccar; import org.junit.Test; import org.traccar.model.Position; +import java.text.ParseException; + import static org.junit.Assert.assertEquals; public class WebDataHandlerTest extends ProtocolTest { @@ -20,4 +22,17 @@ public class WebDataHandlerTest extends ProtocolTest { } + @Test + public void testBuilderRequest() throws ParseException { + + Position p = position("2016-01-01 01:02:03.000", true, 20, 30); + + WebDataHandler handler = new WebDataHandler("http://localhost/", true); + + assertEquals( + "{\"position\":{\"id\":0,\"attributes\":{},\"deviceId\":0,\"type\":null,\"protocol\":null,\"serverTime\":null,\"deviceTime\":1451610123000,\"fixTime\":1451610123000,\"outdated\":false,\"valid\":true,\"latitude\":20.0,\"longitude\":30.0,\"altitude\":0.0,\"speed\":0.0,\"course\":0.0,\"address\":null,\"accuracy\":0.0,\"network\":null}}", + handler.prepareJsonPayload(p)); + + } + } -- cgit v1.2.3 From 4969ecd1d16324f488f01f1f0873e101c1609302 Mon Sep 17 00:00:00 2001 From: nativbarak Date: Tue, 6 Feb 2018 10:28:21 +0200 Subject: removed unneccesary methods --- src/org/traccar/WebDataHandler.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 8bfabf183..5a0aab3e4 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -132,9 +132,9 @@ public class WebDataHandler extends BaseDataHandler { AsyncHttpClient.BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name()); - requestBuilder.addHeader("Content-Type", getContentType()); + requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8"); - setContent(position, requestBuilder); + requestBuilder.setBody(prepareJsonPayload(position)); requestBuilder.execute(); } else { @@ -143,14 +143,6 @@ public class WebDataHandler extends BaseDataHandler { return position; } - protected String getContentType() { - return "application/json; charset=utf-8"; - } - - protected void setContent(Position position, AsyncHttpClient.BoundRequestBuilder requestBuilder) { - requestBuilder.setBody(prepareJsonPayload(position)); - } - protected String prepareJsonPayload(Position position) { Map data = new HashMap<>(); -- cgit v1.2.3 From da1f01f9367e882b90b495cfae9b67d71e0cf1bb Mon Sep 17 00:00:00 2001 From: nativbarak Date: Wed, 7 Feb 2018 09:11:01 +0200 Subject: fixed inline variables + test name --- src/org/traccar/BasePipelineFactory.java | 5 ++--- test/org/traccar/WebDataHandlerTest.java | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 4407caae4..5a077da7c 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -238,9 +238,8 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { } if (Context.getConfig().getBoolean("forward.enable")) { - final Boolean json = Context.getConfig().getBoolean("forward.json"); - final String url = Context.getConfig().getString("forward.url"); - pipeline.addLast("webHandler", new WebDataHandler(url, json)); + pipeline.addLast("webHandler", new WebDataHandler(Context.getConfig().getString("forward.url"), + Context.getConfig().getBoolean("forward.json"))); } if (commandResultEventHandler != null) { diff --git a/test/org/traccar/WebDataHandlerTest.java b/test/org/traccar/WebDataHandlerTest.java index 3e79232ec..30b50d8b9 100644 --- a/test/org/traccar/WebDataHandlerTest.java +++ b/test/org/traccar/WebDataHandlerTest.java @@ -23,7 +23,7 @@ public class WebDataHandlerTest extends ProtocolTest { } @Test - public void testBuilderRequest() throws ParseException { + public void testPrepareJsonPayload() throws ParseException { Position p = position("2016-01-01 01:02:03.000", true, 20, 30); -- cgit v1.2.3 From 3a0a269dca63dacc1199cc738aacdfb2c2ecc7bf Mon Sep 17 00:00:00 2001 From: nativbarak Date: Wed, 7 Feb 2018 09:56:41 +0200 Subject: remove unnecasseray checks --- src/org/traccar/WebDataHandler.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 5a0aab3e4..f7a7222d8 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -146,17 +146,10 @@ public class WebDataHandler extends BaseDataHandler { protected String prepareJsonPayload(Position position) { Map data = new HashMap<>(); + Device device = Context.getIdentityManager().getById(position.getDeviceId()); - if (position != null) { - data.put(KEY_POSITION, position); - } - - if (position.getDeviceId() != 0) { - Device device = Context.getIdentityManager().getById(position.getDeviceId()); - if (device != null) { - data.put(KEY_DEVICE, device); - } - } + data.put(KEY_POSITION, position); + data.put(KEY_DEVICE, device); try { return Context.getObjectMapper().writeValueAsString(data); -- cgit v1.2.3 From 56669ce1a869d7a1e2ebda0ecf1d38174026bab9 Mon Sep 17 00:00:00 2001 From: nativbarak Date: Wed, 7 Feb 2018 11:09:30 +0200 Subject: Fixed correct checks --- src/org/traccar/WebDataHandler.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index f7a7222d8..adc82486c 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -148,8 +148,13 @@ public class WebDataHandler extends BaseDataHandler { Map data = new HashMap<>(); Device device = Context.getIdentityManager().getById(position.getDeviceId()); - data.put(KEY_POSITION, position); - data.put(KEY_DEVICE, device); + if (position != null) { + data.put(KEY_POSITION, position); + } + + if (device != null) { + data.put(KEY_DEVICE, device); + } try { return Context.getObjectMapper().writeValueAsString(data); -- cgit v1.2.3