aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2018-10-16 14:13:13 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2018-10-16 14:13:13 +1300
commit017fbd08ecbffecd1557ec022d5adc49af73300c (patch)
tree903c668263354162bf661ac6e050b438613e26f4
parent4a49c75d675439605a3f8c3ac1719fa2c3b596b8 (diff)
downloadtraccar-server-017fbd08ecbffecd1557ec022d5adc49af73300c.tar.gz
traccar-server-017fbd08ecbffecd1557ec022d5adc49af73300c.tar.bz2
traccar-server-017fbd08ecbffecd1557ec022d5adc49af73300c.zip
Start implementing injection
-rw-r--r--src/org/traccar/BasePipelineFactory.java2
-rw-r--r--src/org/traccar/MainModule.java22
-rw-r--r--src/org/traccar/WebDataHandler.java36
-rw-r--r--test/org/traccar/WebDataHandlerTest.java3
4 files changed, 53 insertions, 10 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java
index aa02fb817..d511af214 100644
--- a/src/org/traccar/BasePipelineFactory.java
+++ b/src/org/traccar/BasePipelineFactory.java
@@ -288,7 +288,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> {
}
if (Context.getConfig().getBoolean("forward.enable")) {
- pipeline.addLast(new WebDataHandler(
+ pipeline.addLast(Main.getInjector().getInstance(WebDataHandler.Factory.class).create(
Context.getConfig().getString("forward.url"), Context.getConfig().getBoolean("forward.json")));
}
diff --git a/src/org/traccar/MainModule.java b/src/org/traccar/MainModule.java
index 99f665134..6c66a90b8 100644
--- a/src/org/traccar/MainModule.java
+++ b/src/org/traccar/MainModule.java
@@ -15,12 +15,34 @@
*/
package org.traccar;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.AbstractModule;
+import com.google.inject.Provides;
+import com.google.inject.assistedinject.FactoryModuleBuilder;
+import org.traccar.database.IdentityManager;
+
+import javax.ws.rs.client.Client;
public class MainModule extends AbstractModule {
+ @Provides
+ public static ObjectMapper getObjectMapper() {
+ return Context.getObjectMapper();
+ }
+
+ @Provides
+ public static IdentityManager getIdentityManager() {
+ return Context.getIdentityManager();
+ }
+
+ @Provides
+ public static Client getClient() {
+ return Context.getClient();
+ }
+
@Override
protected void configure() {
+ install(new FactoryModuleBuilder().build(WebDataHandler.Factory.class));
}
}
diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java
index f15821a11..3e47ada70 100644
--- a/src/org/traccar/WebDataHandler.java
+++ b/src/org/traccar/WebDataHandler.java
@@ -16,12 +16,17 @@
package org.traccar;
import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.inject.assistedinject.Assisted;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.traccar.database.IdentityManager;
import org.traccar.helper.Checksum;
import org.traccar.model.Device;
import org.traccar.model.Position;
+import javax.inject.Inject;
+import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import java.util.HashMap;
import java.util.Map;
@@ -37,12 +42,27 @@ public class WebDataHandler extends BaseDataHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(WebDataHandler.class);
- 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) {
+ private final IdentityManager identityManager;
+ private final ObjectMapper objectMapper;
+ private final Client client;
+
+ private final String url;
+ private final boolean json;
+
+ public interface Factory {
+ WebDataHandler create(String url, boolean json);
+ }
+
+ @Inject
+ public WebDataHandler(
+ IdentityManager identityManager, ObjectMapper objectMapper, Client client,
+ @Assisted String url, @Assisted boolean json) {
+ this.identityManager = identityManager;
+ this.objectMapper = objectMapper;
+ this.client = client;
this.url = url;
this.json = json;
}
@@ -85,7 +105,7 @@ public class WebDataHandler extends BaseDataHandler {
public String formatRequest(Position position) throws UnsupportedEncodingException, JsonProcessingException {
- Device device = Context.getIdentityManager().getById(position.getDeviceId());
+ Device device = identityManager.getById(position.getDeviceId());
String request = url
.replace("{name}", URLEncoder.encode(device.getName(), StandardCharsets.UTF_8.name()))
@@ -110,7 +130,7 @@ public class WebDataHandler extends BaseDataHandler {
}
if (request.contains("{attributes}")) {
- String attributes = Context.getObjectMapper().writeValueAsString(position.getAttributes());
+ String attributes = objectMapper.writeValueAsString(position.getAttributes());
request = request.replace(
"{attributes}", URLEncoder.encode(attributes, StandardCharsets.UTF_8.name()));
}
@@ -125,10 +145,10 @@ public class WebDataHandler extends BaseDataHandler {
@Override
protected Position handlePosition(Position position) {
if (json) {
- Context.getClient().target(url).request().async().post(Entity.json(prepareJsonPayload(position)));
+ client.target(url).request().async().post(Entity.json(prepareJsonPayload(position)));
} else {
try {
- Context.getClient().target(formatRequest(position)).request().async().get();
+ client.target(formatRequest(position)).request().async().get();
} catch (UnsupportedEncodingException | JsonProcessingException e) {
LOGGER.warn("Forwarding formatting error", e);
}
@@ -139,7 +159,7 @@ public class WebDataHandler extends BaseDataHandler {
protected Map<String, Object> prepareJsonPayload(Position position) {
Map<String, Object> data = new HashMap<>();
- Device device = Context.getIdentityManager().getById(position.getDeviceId());
+ Device device = identityManager.getById(position.getDeviceId());
data.put(KEY_POSITION, position);
diff --git a/test/org/traccar/WebDataHandlerTest.java b/test/org/traccar/WebDataHandlerTest.java
index 70c744b3a..9b003a425 100644
--- a/test/org/traccar/WebDataHandlerTest.java
+++ b/test/org/traccar/WebDataHandlerTest.java
@@ -12,7 +12,8 @@ public class WebDataHandlerTest extends ProtocolTest {
Position p = position("2016-01-01 01:02:03.000", true, 20, 30);
- WebDataHandler handler = new WebDataHandler("http://localhost/?fixTime={fixTime}&gprmc={gprmc}&name={name}", false);
+ WebDataHandler handler = new WebDataHandler(
+ Context.getIdentityManager(), null, null, "http://localhost/?fixTime={fixTime}&gprmc={gprmc}&name={name}", false);
assertEquals(
"http://localhost/?fixTime=1451610123000&gprmc=$GPRMC,010203.000,A,2000.0000,N,03000.0000,E,0.00,0.00,010116,,*05&name=test",