diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-02-20 22:54:58 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-02-20 22:54:58 -0800 |
commit | cb4ba475c71cd83a2bde40938216b55e8f686207 (patch) | |
tree | 30b4c8050a65d5e962249dd2d1ad049fc4bd229d | |
parent | 81d4920ba7e45d762f7e276195b9b085e88249a4 (diff) | |
download | trackermap-server-cb4ba475c71cd83a2bde40938216b55e8f686207.tar.gz trackermap-server-cb4ba475c71cd83a2bde40938216b55e8f686207.tar.bz2 trackermap-server-cb4ba475c71cd83a2bde40938216b55e8f686207.zip |
Support headers for forwarding
-rw-r--r-- | src/org/traccar/BasePipelineFactory.java | 4 | ||||
-rw-r--r-- | src/org/traccar/WebDataHandler.java | 33 | ||||
-rw-r--r-- | src/org/traccar/notification/EventForwarder.java | 20 | ||||
-rw-r--r-- | test/org/traccar/WebDataHandlerTest.java | 2 |
4 files changed, 35 insertions, 24 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index b45e3a280..517b92bbf 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -317,7 +317,9 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { if (Context.getConfig().getBoolean("forward.enable")) { pipeline.addLast(Main.getInjector().getInstance(WebDataHandler.Factory.class).create( - Context.getConfig().getString("forward.url"), Context.getConfig().getBoolean("forward.json"))); + Context.getConfig().getString("forward.url"), + Context.getConfig().getString("forward.header"), + Context.getConfig().getBoolean("forward.json"))); } addHandlers( diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index a8e20c698..68c6f5e7c 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2019 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import org.traccar.model.Group; import javax.inject.Inject; import javax.ws.rs.client.Client; import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; import java.util.HashMap; import java.util.Map; import java.io.UnsupportedEncodingException; @@ -51,20 +52,22 @@ public class WebDataHandler extends BaseDataHandler { private final Client client; private final String url; + private final String header; private final boolean json; public interface Factory { - WebDataHandler create(String url, boolean json); + WebDataHandler create(String url, String header, boolean json); } @Inject public WebDataHandler( IdentityManager identityManager, ObjectMapper objectMapper, Client client, - @Assisted String url, @Assisted boolean json) { + @Assisted String url, @Assisted String header, @Assisted boolean json) { this.identityManager = identityManager; this.objectMapper = objectMapper; this.client = client; this.url = url; + this.header = header; this.json = json; } @@ -157,15 +160,33 @@ public class WebDataHandler extends BaseDataHandler { @Override protected Position handlePosition(Position position) { + + String url; if (json) { - client.target(url).request().async().post(Entity.json(prepareJsonPayload(position))); + url = this.url; } else { try { - client.target(formatRequest(position)).request().async().get(); + url = formatRequest(position); } catch (UnsupportedEncodingException | JsonProcessingException e) { - LOGGER.warn("Forwarding formatting error", e); + throw new RuntimeException("Forwarding formatting error", e); + } + } + + Invocation.Builder requestBuilder = client.target(url).request(); + + 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()); } } + + if (json) { + requestBuilder.async().post(Entity.json(prepareJsonPayload(position))); + } else { + requestBuilder.async().get(); + } + return position; } diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index b8990d84f..c0010ebbd 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2019 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,8 +15,6 @@ */ package org.traccar.notification; -import org.apache.commons.collections4.MultiValuedMap; -import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Event; @@ -52,25 +50,15 @@ public abstract class EventForwarder { Invocation.Builder requestBuilder = Context.getClient().target(url).request(); if (header != null && !header.isEmpty()) { - for (Map.Entry<String, String> entry : splitKeyValues(header, ":").entries()) { - requestBuilder = requestBuilder.header(entry.getKey(), entry.getValue()); + for (String line: header.split("\\r?\\n")) { + String[] values = line.split(":", 2); + requestBuilder.header(values[0].trim(), values[1].trim()); } } executeRequest(event, position, users, requestBuilder.async()); } - 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 data; - } - protected Map<String, Object> preparePayload(Event event, Position position, Set<Long> users) { Map<String, Object> data = new HashMap<>(); data.put(KEY_EVENT, event); diff --git a/test/org/traccar/WebDataHandlerTest.java b/test/org/traccar/WebDataHandlerTest.java index 9b003a425..80e0d9a7f 100644 --- a/test/org/traccar/WebDataHandlerTest.java +++ b/test/org/traccar/WebDataHandlerTest.java @@ -13,7 +13,7 @@ public class WebDataHandlerTest extends ProtocolTest { Position p = position("2016-01-01 01:02:03.000", true, 20, 30); WebDataHandler handler = new WebDataHandler( - Context.getIdentityManager(), null, null, "http://localhost/?fixTime={fixTime}&gprmc={gprmc}&name={name}", false); + Context.getIdentityManager(), null, null, "http://localhost/?fixTime={fixTime}&gprmc={gprmc}&name={name}", null, 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", |