From 82da12c1218ca7f1e4e1562cc183cc8a059045e6 Mon Sep 17 00:00:00 2001 From: Srk Date: Wed, 22 Nov 2017 22:59:37 +0100 Subject: Minor refactoring --- src/org/traccar/notification/EventForwarder.java | 9 +++++---- .../traccar/notification/MultiPartEventForwarder.java | 19 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index cc83aefbc..1354901a7 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -20,6 +20,7 @@ import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; import java.util.Arrays; import java.util.List; +import org.apache.commons.lang3.StringUtils; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -39,7 +40,7 @@ public abstract class EventForwarder { public EventForwarder() { url = Context.getConfig().getString("event.forward.url", "http://localhost/"); - header = Context.getConfig().getString("event.forward.header", ""); + header = Context.getConfig().getString("event.forward.header"); } private static final String KEY_POSITION = "position"; @@ -54,9 +55,9 @@ public abstract class EventForwarder { requestBuilder.addHeader("Content-Type", getContentType()); - if (!header.equals("")) { + if (StringUtils.isNotEmpty(header)) { FluentCaseInsensitiveStringsMap params = new FluentCaseInsensitiveStringsMap(); - params.putAll(splitParams(header, ":")); + params.putAll(splitIntoKeyValues(header, ":")); requestBuilder.setHeaders(params); } @@ -64,7 +65,7 @@ public abstract class EventForwarder { requestBuilder.execute(); } - protected Map> splitParams(String params, String separator) { + protected Map> splitIntoKeyValues(String params, String separator) { String[] splitedLine; Map> paramsMap = new HashMap<>(); diff --git a/src/org/traccar/notification/MultiPartEventForwarder.java b/src/org/traccar/notification/MultiPartEventForwarder.java index a77ad5975..6a3baecb3 100644 --- a/src/org/traccar/notification/MultiPartEventForwarder.java +++ b/src/org/traccar/notification/MultiPartEventForwarder.java @@ -1,10 +1,11 @@ package org.traccar.notification; -import java.nio.charset.StandardCharsets; +import static java.nio.charset.StandardCharsets.UTF_8; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.apache.commons.lang3.StringUtils; import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; @@ -19,7 +20,7 @@ public class MultiPartEventForwarder extends EventForwarder { public MultiPartEventForwarder() { payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); - additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); + additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams"); } @Override @@ -30,18 +31,16 @@ public class MultiPartEventForwarder extends EventForwarder { @Override protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) { - if (!additionalParams.equals("")) { - Map> paramsToAdd = splitParams(additionalParams, "="); + if (StringUtils.isNotEmpty(additionalParams)) { + Map> paramsToAdd = splitIntoKeyValues(additionalParams, "="); - for (Entry> en : paramsToAdd.entrySet()) { - for (String value : en.getValue()) { - requestBuilder.addBodyPart(new StringPart(en.getKey(), value, null, StandardCharsets.UTF_8)); + for (Entry> param : paramsToAdd.entrySet()) { + for (String singleParamValue : param.getValue()) { + requestBuilder.addBodyPart(new StringPart(param.getKey(), singleParamValue, null, UTF_8)); } } } requestBuilder.addBodyPart(new StringPart(payloadParamName, - prepareJsonPayload(event, position), - "application/json", - StandardCharsets.UTF_8)); + prepareJsonPayload(event, position), "application/json", UTF_8)); } } -- cgit v1.2.3