From c7928ef4a722cb40e9911c8e76e308fc48c85567 Mon Sep 17 00:00:00 2001 From: Srk Date: Sun, 19 Nov 2017 11:13:48 +0100 Subject: - Global jsonPrettyPrinting option - Minor refactoring - Config option name changed from "event.forward.prettyPrintedPayload" to "config.mapper.prettyPrintedJson" --- src/org/traccar/Context.java | 188 ++++++++++++----------- src/org/traccar/notification/EventForwarder.java | 15 +- 2 files changed, 98 insertions(+), 105 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 2317b7039..5d2d5a86a 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -16,7 +16,6 @@ package org.traccar; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.SerializationFeature; import com.ning.http.client.AsyncHttpClient; @@ -99,12 +98,6 @@ public final class Context { return objectMapper; } - private static ObjectWriter objectWriterPrettyPrinter; - - public static ObjectWriter getObjectWriterPrettyPrinter() { - return objectWriterPrettyPrinter; - } - private static IdentityManager identityManager; public static IdentityManager getIdentityManager() { @@ -289,7 +282,9 @@ public final class Context { objectMapper = new ObjectMapper(); objectMapper.setConfig( objectMapper.getSerializationConfig().without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)); - objectWriterPrettyPrinter = objectMapper.writerWithDefaultPrettyPrinter(); + if (Context.getConfig().getBoolean("config.mapper.prettyPrintedJson")) { + objectMapper.enable(SerializationFeature.INDENT_OUTPUT); + } if (config.hasKey("database.url")) { dataManager = new DataManager(config); @@ -308,59 +303,11 @@ public final class Context { identityManager = deviceManager; if (config.getBoolean("geocoder.enable")) { - String type = config.getString("geocoder.type", "google"); - String url = config.getString("geocoder.url"); - String key = config.getString("geocoder.key"); - String language = config.getString("geocoder.language"); - - int cacheSize = config.getInteger("geocoder.cacheSize"); - switch (type) { - case "nominatim": - geocoder = new NominatimGeocoder(url, key, language, cacheSize); - break; - case "gisgraphy": - geocoder = new GisgraphyGeocoder(url, cacheSize); - break; - case "mapquest": - geocoder = new MapQuestGeocoder(url, key, cacheSize); - break; - case "opencage": - geocoder = new OpenCageGeocoder(url, key, cacheSize); - break; - case "bingmaps": - geocoder = new BingMapsGeocoder(url, key, cacheSize); - break; - case "factual": - geocoder = new FactualGeocoder(url, key, cacheSize); - break; - case "geocodefarm": - geocoder = new GeocodeFarmGeocoder(key, language, cacheSize); - break; - default: - geocoder = new GoogleGeocoder(key, language, cacheSize); - break; - } + initReverseGeocoder(); } if (config.getBoolean("geolocation.enable")) { - String type = config.getString("geolocation.type", "mozilla"); - String url = config.getString("geolocation.url"); - String key = config.getString("geolocation.key"); - - switch (type) { - case "google": - geolocationProvider = new GoogleGeolocationProvider(key); - break; - case "opencellid": - geolocationProvider = new OpenCellIdGeolocationProvider(key); - break; - case "unwired": - geolocationProvider = new UnwiredGeolocationProvider(url, key); - break; - default: - geolocationProvider = new MozillaGeolocationProvider(key); - break; - } + initLocationResolutionSystem(); } if (config.getBoolean("web.enable")) { @@ -374,39 +321,17 @@ public final class Context { tripsConfig = initTripsConfig(); if (config.getBoolean("event.enable")) { - geofenceManager = new GeofenceManager(dataManager); - calendarManager = new CalendarManager(dataManager); - notificationManager = new NotificationManager(dataManager); - Properties velocityProperties = new Properties(); - velocityProperties.setProperty("file.resource.loader.path", - Context.getConfig().getString("templates.rootPath", "templates") + "/"); - velocityProperties.setProperty("runtime.log.logsystem.class", - "org.apache.velocity.runtime.log.NullLogChute"); - - String address; - try { - address = config.getString("web.address", InetAddress.getLocalHost().getHostAddress()); - } catch (UnknownHostException e) { - address = "localhost"; - } - - String webUrl = URIUtil.newURI("http", address, config.getInteger("web.port", 8082), "", ""); - webUrl = Context.getConfig().getString("web.url", webUrl); - velocityProperties.setProperty("web.url", webUrl); - - velocityEngine = new VelocityEngine(); - velocityEngine.init(velocityProperties); - - motionEventHandler = new MotionEventHandler(tripsConfig); - overspeedEventHandler = new OverspeedEventHandler( - Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, - Context.getConfig().getBoolean("event.overspeed.notRepeat")); + initEventsSubsystem(); } serverManager = new ServerManager(); if (config.getBoolean("event.forward.enable")) { - eventForwarder = getConfiguredEventForwarder(); + if (Context.getConfig().getBoolean("event.forward.payloadAsParamMode")) { + eventForwarder = new FormTypeEventForwarder(); + } else { + eventForwarder = new JsonTypeEventForwarder(); + } } attributesManager = new AttributesManager(dataManager); @@ -423,17 +348,98 @@ public final class Context { } - private static EventForwarder getConfiguredEventForwarder() { - if (Context.getConfig().getBoolean("event.forward.payloadAsParamMode")) { - return new FormTypeEventForwarder(); + private static void initLocationResolutionSystem() { + + String type = config.getString("geolocation.type", "mozilla"); + String url = config.getString("geolocation.url"); + String key = config.getString("geolocation.key"); + + switch (type) { + case "google": + geolocationProvider = new GoogleGeolocationProvider(key); + break; + case "opencellid": + geolocationProvider = new OpenCellIdGeolocationProvider(key); + break; + case "unwired": + geolocationProvider = new UnwiredGeolocationProvider(url, key); + break; + default: + geolocationProvider = new MozillaGeolocationProvider(key); + break; + } + } + + private static void initEventsSubsystem() { + + geofenceManager = new GeofenceManager(dataManager); + calendarManager = new CalendarManager(dataManager); + notificationManager = new NotificationManager(dataManager); + Properties velocityProperties = new Properties(); + velocityProperties.setProperty("file.resource.loader.path", + Context.getConfig().getString("templates.rootPath", "templates") + "/"); + velocityProperties.setProperty("runtime.log.logsystem.class", + "org.apache.velocity.runtime.log.NullLogChute"); + + String address; + try { + address = config.getString("web.address", InetAddress.getLocalHost().getHostAddress()); + } catch (UnknownHostException e) { + address = "localhost"; + } + + String webUrl = URIUtil.newURI("http", address, config.getInteger("web.port", 8082), "", ""); + webUrl = Context.getConfig().getString("web.url", webUrl); + velocityProperties.setProperty("web.url", webUrl); + + velocityEngine = new VelocityEngine(); + velocityEngine.init(velocityProperties); + + motionEventHandler = new MotionEventHandler(tripsConfig); + overspeedEventHandler = new OverspeedEventHandler( + Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, + Context.getConfig().getBoolean("event.overspeed.notRepeat")); + } + + private static void initReverseGeocoder() { + + String type = config.getString("geocoder.type", "google"); + String url = config.getString("geocoder.url"); + String key = config.getString("geocoder.key"); + String language = config.getString("geocoder.language"); + + int cacheSize = config.getInteger("geocoder.cacheSize"); + switch (type) { + case "nominatim": + geocoder = new NominatimGeocoder(url, key, language, cacheSize); + break; + case "gisgraphy": + geocoder = new GisgraphyGeocoder(url, cacheSize); + break; + case "mapquest": + geocoder = new MapQuestGeocoder(url, key, cacheSize); + break; + case "opencage": + geocoder = new OpenCageGeocoder(url, key, cacheSize); + break; + case "bingmaps": + geocoder = new BingMapsGeocoder(url, key, cacheSize); + break; + case "factual": + geocoder = new FactualGeocoder(url, key, cacheSize); + break; + case "geocodefarm": + geocoder = new GeocodeFarmGeocoder(key, language, cacheSize); + break; + default: + geocoder = new GoogleGeocoder(key, language, cacheSize); + break; } - return new JsonTypeEventForwarder(); } public static void init(IdentityManager testIdentityManager) { config = new Config(); objectMapper = new ObjectMapper(); - objectWriterPrettyPrinter = objectMapper.writerWithDefaultPrettyPrinter(); identityManager = testIdentityManager; } diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index c21799d74..d7e0eca19 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -29,7 +29,6 @@ import org.traccar.model.Geofence; import org.traccar.model.Position; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectWriter; import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; import com.ning.http.client.FluentCaseInsensitiveStringsMap; @@ -39,14 +38,12 @@ public abstract class EventForwarder { private String header; private final String payloadParamName; private final String additionalParams; - private final boolean prettyPrinted; public EventForwarder() { url = Context.getConfig().getString("event.forward.url", "http://localhost/"); header = Context.getConfig().getString("event.forward.header", ""); payloadParamName = Context.getConfig().getString("event.forward.paramMode.payloadParamName", "payload"); additionalParams = Context.getConfig().getString("event.forward.paramMode.additionalParams", ""); - prettyPrinted = Context.getConfig().getBoolean("event.forward.prettyPrintedPayload"); } private static final String KEY_POSITION = "position"; @@ -105,29 +102,19 @@ public abstract class EventForwarder { } } try { - return getObjectWriter().writeValueAsString(data); + return Context.getObjectMapper().writeValueAsString(data); } catch (JsonProcessingException e) { Log.warning(e); return null; } } - protected ObjectWriter getObjectWriter() { - return prettyPrinted - ? Context.getObjectWriterPrettyPrinter() - : Context.getObjectMapper().writer(); - } - - protected String getPayloadParamName() { return payloadParamName; } protected String getAdditionalParams() { return additionalParams; } - protected boolean isPrettyPrinted() { - return prettyPrinted; - } protected abstract String getContentType(); protected abstract void setContent(Event event, Position position, BoundRequestBuilder requestBuilder); -- cgit v1.2.3