diff options
Diffstat (limited to 'src/org')
-rw-r--r-- | src/org/traccar/Context.java | 9 | ||||
-rw-r--r-- | src/org/traccar/WebDataHandler.java | 7 | ||||
-rw-r--r-- | src/org/traccar/api/AsyncSocket.java | 13 | ||||
-rw-r--r-- | src/org/traccar/database/QueryBuilder.java | 41 | ||||
-rw-r--r-- | src/org/traccar/model/MiscFormatter.java | 75 | ||||
-rw-r--r-- | src/org/traccar/notification/EventForwarder.java | 30 | ||||
-rw-r--r-- | src/org/traccar/web/CsvBuilder.java | 17 | ||||
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 178 |
8 files changed, 67 insertions, 303 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index d859e91bb..6983e32f6 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -15,6 +15,7 @@ */ package org.traccar; +import com.fasterxml.jackson.databind.ObjectMapper; import com.ning.http.client.AsyncHttpClient; import java.net.InetAddress; @@ -65,6 +66,12 @@ public final class Context { return loggerEnabled; } + private static ObjectMapper objectMapper; + + public static ObjectMapper getObjectMapper() { + return objectMapper; + } + private static IdentityManager identityManager; public static IdentityManager getIdentityManager() { @@ -181,6 +188,8 @@ public final class Context { Log.setupLogger(config); } + objectMapper = new ObjectMapper(); + if (config.hasKey("database.url")) { dataManager = new DataManager(config); } diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index f99d7c97a..eaf0978ef 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -15,10 +15,10 @@ */ package org.traccar; +import com.fasterxml.jackson.core.JsonProcessingException; import org.traccar.helper.Checksum; import org.traccar.helper.Log; import org.traccar.model.Device; -import org.traccar.model.MiscFormatter; import org.traccar.model.Position; import java.io.UnsupportedEncodingException; @@ -77,8 +77,6 @@ public class WebDataHandler extends BaseDataHandler { Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); - String attributes = MiscFormatter.toJsonString(position.getAttributes()); - String request = url .replace("{name}", device.getName()) .replace("{uniqueId}", device.getUniqueId()) @@ -105,9 +103,10 @@ public class WebDataHandler extends BaseDataHandler { 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 error) { + } catch (UnsupportedEncodingException | JsonProcessingException error) { Log.warning(error); } } diff --git a/src/org/traccar/api/AsyncSocket.java b/src/org/traccar/api/AsyncSocket.java index b12d5d252..70523d253 100644 --- a/src/org/traccar/api/AsyncSocket.java +++ b/src/org/traccar/api/AsyncSocket.java @@ -15,17 +15,16 @@ */ package org.traccar.api; +import com.fasterxml.jackson.core.JsonProcessingException; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.WebSocketAdapter; import org.traccar.Context; import org.traccar.database.ConnectionManager; +import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.web.JsonConverter; -import javax.json.Json; -import javax.json.JsonObjectBuilder; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -84,11 +83,11 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U private void sendData(Map<String, Collection<?>> data) { if (!data.isEmpty() && isConnected()) { - JsonObjectBuilder json = Json.createObjectBuilder(); - for (Map.Entry<String, Collection<?>> entry : data.entrySet()) { - json.add(entry.getKey(), JsonConverter.arrayToJson(entry.getValue())); + try { + getRemote().sendString(Context.getObjectMapper().writeValueAsString(data), null); + } catch (JsonProcessingException e) { + Log.warning(e); } - getRemote().sendString(json.build().toString(), null); } } } diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index 201240f2f..127229700 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -15,15 +15,13 @@ */ package org.traccar.database; +import com.fasterxml.jackson.core.JsonProcessingException; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.MiscFormatter; -import javax.json.Json; -import javax.json.JsonReader; -import javax.json.stream.JsonParsingException; import javax.sql.DataSource; -import java.io.StringReader; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.Connection; @@ -279,14 +277,15 @@ public final class QueryBuilder { setDate(name, (Date) method.invoke(object)); } else if (method.getReturnType().equals(byte[].class)) { setBlob(name, (byte[]) method.invoke(object)); - } else if (method.getReturnType().equals(Map.class)) { - if (Context.getConfig().getBoolean("database.xml")) { + } else { + if (method.getReturnType().equals(Map.class) + && Context.getConfig().getBoolean("database.xml")) { setString(name, MiscFormatter.toXmlString((Map) method.invoke(object))); } else { - setString(name, MiscFormatter.toJsonString((Map) method.invoke(object))); + setString(name, Context.getObjectMapper().writeValueAsString(method.invoke(object))); } } - } catch (IllegalAccessException | InvocationTargetException error) { + } catch (IllegalAccessException | InvocationTargetException | JsonProcessingException error) { Log.warning(error); } } @@ -380,28 +379,28 @@ public final class QueryBuilder { } } }); - } else if (parameterType.equals(Map.class)) { + } else if (parameterType.equals(byte[].class)) { processors.add(new ResultSetProcessor<T>() { @Override public void process(T object, ResultSet resultSet) throws SQLException { - String value = resultSet.getString(name); - if (value != null) { - try (JsonReader reader = Json.createReader(new StringReader(value))) { - method.invoke(object, MiscFormatter.fromJson(reader.readObject())); - } catch (IllegalAccessException | InvocationTargetException | JsonParsingException error) { - Log.warning(error); - } + try { + method.invoke(object, resultSet.getBytes(name)); + } catch (IllegalAccessException | InvocationTargetException error) { + Log.warning(error); } } }); - } else if (parameterType.equals(byte[].class)) { + } else { processors.add(new ResultSetProcessor<T>() { @Override public void process(T object, ResultSet resultSet) throws SQLException { - try { - method.invoke(object, resultSet.getBytes(name)); - } catch (IllegalAccessException | InvocationTargetException error) { - Log.warning(error); + String value = resultSet.getString(name); + if (value != null) { + try { + method.invoke(object, Context.getObjectMapper().readValue(value, Map.class)); + } catch (InvocationTargetException | IllegalAccessException | IOException error) { + Log.warning(error); + } } } }); diff --git a/src/org/traccar/model/MiscFormatter.java b/src/org/traccar/model/MiscFormatter.java index 6194a998f..c6511f063 100644 --- a/src/org/traccar/model/MiscFormatter.java +++ b/src/org/traccar/model/MiscFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2016 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,22 +15,10 @@ */ package org.traccar.model; -import org.traccar.helper.Log; -import org.traccar.web.JsonConverter; -import javax.json.Json; -import javax.json.JsonNumber; -import javax.json.JsonObject; -import javax.json.JsonObjectBuilder; -import javax.json.JsonString; -import javax.json.JsonValue; import java.text.DecimalFormat; -import java.util.LinkedHashMap; import java.util.Map; -/** - * Format extended tracker status - */ public final class MiscFormatter { private MiscFormatter() { @@ -65,65 +53,4 @@ public final class MiscFormatter { return result.toString(); } - public static JsonObject toJson(Map<String, Object> attributes) { - JsonObjectBuilder json = Json.createObjectBuilder(); - - for (Map.Entry<String, Object> entry : attributes.entrySet()) { - if (entry.getValue() instanceof String) { - json.add(entry.getKey(), (String) entry.getValue()); - } else if (entry.getValue() instanceof Integer) { - json.add(entry.getKey(), (Integer) entry.getValue()); - } else if (entry.getValue() instanceof Long) { - json.add(entry.getKey(), (Long) entry.getValue()); - } else if (entry.getValue() instanceof Double) { - json.add(entry.getKey(), (Double) entry.getValue()); - } else if (entry.getValue() instanceof Boolean) { - json.add(entry.getKey(), (Boolean) entry.getValue()); - } else if (entry.getValue() == null) { - json.add(entry.getKey(), JsonValue.NULL); - } else { - json.add(entry.getKey(), JsonConverter.objectToJson(entry.getValue())); - } - } - - return json.build(); - } - - public static Map<String, Object> fromJson(JsonObject json) { - - Map<String, Object> attributes = new LinkedHashMap<>(); - - for (Map.Entry<String, JsonValue> entry : json.entrySet()) { - JsonValue.ValueType type = entry.getValue().getValueType(); - switch (type) { - case STRING: - attributes.put(entry.getKey(), ((JsonString) entry.getValue()).getString()); - break; - case NUMBER: - JsonNumber number = (JsonNumber) entry.getValue(); - if (number.isIntegral()) { - attributes.put(entry.getKey(), number.longValue()); - } else { - attributes.put(entry.getKey(), number.doubleValue()); - } - break; - case TRUE: - attributes.put(entry.getKey(), true); - break; - case FALSE: - attributes.put(entry.getKey(), false); - break; - default: - Log.warning(new IllegalArgumentException(type.name())); - break; - } - } - - return attributes; - } - - public static String toJsonString(Map<String, Object> attributes) { - return toJson(attributes).toString(); - } - } diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index 9bd116339..b39a9155f 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -15,19 +15,18 @@ */ package org.traccar.notification; -import java.nio.charset.StandardCharsets; - -import javax.json.Json; -import javax.json.JsonObjectBuilder; - +import com.fasterxml.jackson.core.JsonProcessingException; +import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; import org.traccar.Context; +import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Geofence; import org.traccar.model.Position; -import org.traccar.web.JsonConverter; -import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; public final class EventForwarder { @@ -67,24 +66,29 @@ public final class EventForwarder { } private byte[] preparePayload(Event event, Position position) { - JsonObjectBuilder json = Json.createObjectBuilder(); - json.add(KEY_EVENT, JsonConverter.objectToJson(event)); + Map<String, Object> data = new HashMap<>(); + data.put(KEY_EVENT, event); if (position != null) { - json.add(KEY_POSITION, JsonConverter.objectToJson(position)); + data.put(KEY_POSITION, position); } if (event.getDeviceId() != 0) { Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId()); if (device != null) { - json.add(KEY_DEVICE, JsonConverter.objectToJson(device)); + data.put(KEY_DEVICE, device); } } if (event.getGeofenceId() != 0) { Geofence geofence = Context.getGeofenceManager().getGeofence(event.getGeofenceId()); if (geofence != null) { - json.add(KEY_GEOFENCE, JsonConverter.objectToJson(geofence)); + data.put(KEY_GEOFENCE, geofence); } } - return json.build().toString().getBytes(StandardCharsets.UTF_8); + try { + return Context.getObjectMapper().writeValueAsString(data).getBytes(StandardCharsets.UTF_8); + } catch (JsonProcessingException e) { + Log.warning(e); + return null; + } } } diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java index 532f06e3d..31b389873 100644 --- a/src/org/traccar/web/CsvBuilder.java +++ b/src/org/traccar/web/CsvBuilder.java @@ -27,11 +27,12 @@ import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; +import com.fasterxml.jackson.core.JsonProcessingException; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; +import org.traccar.Context; import org.traccar.helper.Log; -import org.traccar.model.MiscFormatter; public class CsvBuilder { @@ -95,11 +96,15 @@ public class CsvBuilder { } else if (method.getReturnType().equals(Map.class)) { Map value = (Map) method.invoke(object); if (value != null) { - String map = MiscFormatter.toJson(value).toString(); - map = map.replaceAll("[\\{\\}\"]", ""); - map = map.replaceAll(",", " "); - builder.append(map); - addSeparator(); + try { + String map = Context.getObjectMapper().writeValueAsString(value); + map = map.replaceAll("[\\{\\}\"]", ""); + map = map.replaceAll(",", " "); + builder.append(map); + addSeparator(); + } catch (JsonProcessingException e) { + Log.warning(e); + } } } } catch (IllegalAccessException | InvocationTargetException error) { diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java deleted file mode 100644 index bec0afe8e..000000000 --- a/src/org/traccar/web/JsonConverter.java +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright 2015 - 2016 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.web; - -import org.joda.time.DateTime; -import org.joda.time.format.DateTimeFormatter; -import org.joda.time.format.ISODateTimeFormat; -import org.traccar.helper.Log; -import org.traccar.model.MiscFormatter; - -import javax.json.Json; -import javax.json.JsonArray; -import javax.json.JsonArrayBuilder; -import javax.json.JsonObject; -import javax.json.JsonObjectBuilder; -import javax.json.JsonReader; -import javax.json.JsonValue; -import java.beans.Introspector; -import java.io.Reader; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.text.ParseException; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Map; - -public final class JsonConverter { - - private JsonConverter() { - } - - private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTime(); - - public static Date parseDate(String value) { - return DATE_FORMAT.parseDateTime(value).toDate(); - } - - public static <T> T objectFromJson(Reader reader, Class<T> clazz) throws ParseException { - try (JsonReader jsonReader = Json.createReader(reader)) { - return objectFromJson(jsonReader.readObject(), clazz); - } - } - - public static <T> T objectFromJson(JsonObject json, Class<T> clazz) { - try { - T object = clazz.newInstance(); - Method[] methods = object.getClass().getMethods(); - return objectFromJson(json, object, methods); - } catch (InstantiationException | IllegalAccessException e) { - throw new IllegalArgumentException(); - } - } - - private static <T> T objectFromJson(JsonObject json, T object, Method[] methods) { - for (final Method method : methods) { - if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { - - final String name = Introspector.decapitalize(method.getName().substring(3)); - Class<?> parameterType = method.getParameterTypes()[0]; - - if (json.containsKey(name) && !json.isNull(name)) { - try { - if (parameterType.equals(boolean.class)) { - method.invoke(object, json.getBoolean(name)); - } else if (parameterType.equals(int.class)) { - method.invoke(object, json.getJsonNumber(name).intValue()); - } else if (parameterType.equals(long.class)) { - if (json.get(name).getValueType() == JsonValue.ValueType.NUMBER) { - method.invoke(object, json.getJsonNumber(name).longValue()); - } - } else if (parameterType.equals(double.class)) { - method.invoke(object, json.getJsonNumber(name).doubleValue()); - } else if (parameterType.equals(String.class)) { - method.invoke(object, json.getString(name)); - } else if (parameterType.equals(Date.class)) { - method.invoke(object, DATE_FORMAT.parseDateTime(json.getString(name)).toDate()); - } else if (parameterType.equals(Map.class)) { - method.invoke(object, MiscFormatter.fromJson(json.getJsonObject(name))); - } - } catch (IllegalAccessException | InvocationTargetException error) { - Log.warning(error); - } - } - } - } - return object; - } - - public static <T> JsonObject objectToJson(T object) { - - JsonObjectBuilder json = Json.createObjectBuilder(); - - Method[] methods = object.getClass().getMethods(); - - for (Method method : methods) { - if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { - String name = Introspector.decapitalize(method.getName().substring(3)); - try { - if (method.getReturnType().equals(boolean.class)) { - json.add(name, (Boolean) method.invoke(object)); - } else if (method.getReturnType().equals(int.class)) { - json.add(name, (Integer) method.invoke(object)); - } else if (method.getReturnType().equals(long.class)) { - json.add(name, (Long) method.invoke(object)); - } else if (method.getReturnType().equals(double.class)) { - json.add(name, (Double) method.invoke(object)); - } else if (method.getReturnType().equals(String.class)) { - String value = (String) method.invoke(object); - if (value != null) { - json.add(name, value); - } - } else if (method.getReturnType().equals(Date.class)) { - Date value = (Date) method.invoke(object); - if (value != null) { - json.add(name, DATE_FORMAT.print(new DateTime(value))); - } - } else if (method.getReturnType().equals(Map.class)) { - Map value = (Map) method.invoke(object); - if (value != null) { - json.add(name, MiscFormatter.toJson(value)); - } - } else if (method.getReturnType().equals(List.class)) { - List value = (List) method.invoke(object); - if (value != null) { - json.add(name, arrayToJson(value)); - } - } - } catch (IllegalAccessException | InvocationTargetException error) { - Log.warning(error); - } - } - } - - return json.build(); - } - - public static JsonArray arrayToJson(Collection<?> array) { - - JsonArrayBuilder json = Json.createArrayBuilder(); - - for (Object object : array) { - switch (object.getClass().getSimpleName().toLowerCase()) { - case "string": - json.add(object.toString()); - break; - case "long": - json.add((long) object); - break; - case "double": - json.add((double) object); - break; - case "boolean": - json.add((boolean) object); - break; - default: - json.add(objectToJson(object)); - break; - } - } - - return json.build(); - } - -} |