From b800c16dff17f18b5c079e3403904d61bda86e88 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 7 Aug 2018 22:00:53 +1200 Subject: Encode device name (fix #3995) --- src/org/traccar/WebDataHandler.java | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 763493437..65b272df4 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -80,12 +80,12 @@ public class WebDataHandler extends BaseDataHandler { } } - public String formatRequest(Position position) { + public String formatRequest(Position position) throws UnsupportedEncodingException, JsonProcessingException { Device device = Context.getIdentityManager().getById(position.getDeviceId()); String request = url - .replace("{name}", device.getName()) + .replace("{name}", URLEncoder.encode(device.getName(), StandardCharsets.UTF_8.name())) .replace("{uniqueId}", device.getUniqueId()) .replace("{status}", device.getStatus()) .replace("{deviceId}", String.valueOf(position.getDeviceId())) @@ -102,22 +102,14 @@ public class WebDataHandler extends BaseDataHandler { .replace("{statusCode}", calculateStatus(position)); if (position.getAddress() != null) { - try { - request = request.replace( - "{address}", URLEncoder.encode(position.getAddress(), StandardCharsets.UTF_8.name())); - } catch (UnsupportedEncodingException error) { - Log.warning(error); - } + request = request.replace( + "{address}", URLEncoder.encode(position.getAddress(), StandardCharsets.UTF_8.name())); } if (request.contains("{attributes}")) { - try { - String attributes = Context.getObjectMapper().writeValueAsString(position.getAttributes()); - request = request.replace( - "{attributes}", URLEncoder.encode(attributes, StandardCharsets.UTF_8.name())); - } catch (UnsupportedEncodingException | JsonProcessingException error) { - Log.warning(error); - } + String attributes = Context.getObjectMapper().writeValueAsString(position.getAttributes()); + request = request.replace( + "{attributes}", URLEncoder.encode(attributes, StandardCharsets.UTF_8.name())); } if (request.contains("{gprmc}")) { @@ -132,7 +124,11 @@ public class WebDataHandler extends BaseDataHandler { if (json) { Context.getClient().target(url).request().async().post(Entity.json(prepareJsonPayload(position))); } else { - Context.getClient().target(formatRequest(position)).request().async().get(); + try { + Context.getClient().target(formatRequest(position)).request().async().get(); + } catch (UnsupportedEncodingException | JsonProcessingException e) { + Log.warning(e); + } } return position; } -- cgit v1.2.3