diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-10-16 14:13:13 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-10-16 14:13:13 +1300 |
commit | 017fbd08ecbffecd1557ec022d5adc49af73300c (patch) | |
tree | 903c668263354162bf661ac6e050b438613e26f4 /src | |
parent | 4a49c75d675439605a3f8c3ac1719fa2c3b596b8 (diff) | |
download | traccar-server-017fbd08ecbffecd1557ec022d5adc49af73300c.tar.gz traccar-server-017fbd08ecbffecd1557ec022d5adc49af73300c.tar.bz2 traccar-server-017fbd08ecbffecd1557ec022d5adc49af73300c.zip |
Start implementing injection
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/BasePipelineFactory.java | 2 | ||||
-rw-r--r-- | src/org/traccar/MainModule.java | 22 | ||||
-rw-r--r-- | src/org/traccar/WebDataHandler.java | 36 |
3 files changed, 51 insertions, 9 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); |