diff options
Diffstat (limited to 'src/org/traccar/web')
-rw-r--r-- | src/org/traccar/web/DeviceServlet.java | 6 | ||||
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 31 | ||||
-rw-r--r-- | src/org/traccar/web/ServerServlet.java | 2 | ||||
-rw-r--r-- | src/org/traccar/web/UserServlet.java | 6 |
4 files changed, 14 insertions, 31 deletions
diff --git a/src/org/traccar/web/DeviceServlet.java b/src/org/traccar/web/DeviceServlet.java index 8f983ad78..a501bd8f6 100644 --- a/src/org/traccar/web/DeviceServlet.java +++ b/src/org/traccar/web/DeviceServlet.java @@ -70,7 +70,7 @@ public class DeviceServlet extends BaseServlet { } private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); + Device device = JsonConverter.objectFromJson(req.getReader(), Device.class); long userId = getUserId(req); Context.getDataManager().addDevice(device); Context.getDataManager().linkDevice(userId, device.getId()); @@ -79,14 +79,14 @@ public class DeviceServlet extends BaseServlet { } private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); + Device device = JsonConverter.objectFromJson(req.getReader(), Device.class); Context.getPermissionsManager().checkDevice(getUserId(req), device.getId()); Context.getDataManager().updateDevice(device); sendResponse(resp.getWriter(), true); } private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); + Device device = JsonConverter.objectFromJson(req.getReader(), Device.class); Context.getPermissionsManager().checkDevice(getUserId(req), device.getId()); Context.getDataManager().removeDevice(device); Context.getPermissionsManager().refresh(); diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index 2ef61fb13..8bf5607fb 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -35,7 +35,6 @@ 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.Factory; import org.traccar.model.MiscFormatter; public final class JsonConverter { @@ -43,32 +42,12 @@ public final class JsonConverter { private JsonConverter() { } - private static <T> T newClassInstance(Class<T> clazz) { - try { - return clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - throw new IllegalArgumentException(); - } - } - private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTime(); public static Date parseDate(String value) { return DATE_FORMAT.parseDateTime(value).toDate(); } - public static <T extends Factory> T objectFromJson(Reader reader, T prototype) throws ParseException { - try (JsonReader jsonReader = Json.createReader(reader)) { - return objectFromJson(jsonReader.readObject(), prototype); - } - } - - public static <T extends Factory> T objectFromJson(JsonObject json, T prototype) { - T object = (T) prototype.create(); - Method[] methods = object.getClass().getMethods(); - return objectFromJson(json, object, methods); - } - public static <T> T objectFromJson(Reader reader, Class<T> clazz) throws ParseException { try (JsonReader jsonReader = Json.createReader(reader)) { return objectFromJson(jsonReader.readObject(), clazz); @@ -76,9 +55,13 @@ public final class JsonConverter { } public static <T> T objectFromJson(JsonObject json, Class<T> clazz) { - T object = newClassInstance(clazz); - Method[] methods = object.getClass().getMethods(); - return objectFromJson(json, object, methods); + 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) { diff --git a/src/org/traccar/web/ServerServlet.java b/src/org/traccar/web/ServerServlet.java index 7ed096bc6..b0ee181da 100644 --- a/src/org/traccar/web/ServerServlet.java +++ b/src/org/traccar/web/ServerServlet.java @@ -44,7 +44,7 @@ public class ServerServlet extends BaseServlet { } private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Server server = JsonConverter.objectFromJson(req.getReader(), new Server()); + Server server = JsonConverter.objectFromJson(req.getReader(), Server.class); Context.getPermissionsManager().checkAdmin(getUserId(req)); Context.getDataManager().updateServer(server); sendResponse(resp.getWriter(), true); diff --git a/src/org/traccar/web/UserServlet.java b/src/org/traccar/web/UserServlet.java index 6bd870d4d..ef1888667 100644 --- a/src/org/traccar/web/UserServlet.java +++ b/src/org/traccar/web/UserServlet.java @@ -51,7 +51,7 @@ public class UserServlet extends BaseServlet { } private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); + User user = JsonConverter.objectFromJson(req.getReader(), User.class); Context.getPermissionsManager().checkUser(getUserId(req), user.getId()); Context.getDataManager().addUser(user); Context.getPermissionsManager().refresh(); @@ -59,7 +59,7 @@ public class UserServlet extends BaseServlet { } private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); + User user = JsonConverter.objectFromJson(req.getReader(), User.class); if (user.getAdmin()) { Context.getPermissionsManager().checkAdmin(getUserId(req)); } else { @@ -71,7 +71,7 @@ public class UserServlet extends BaseServlet { } private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); + User user = JsonConverter.objectFromJson(req.getReader(), User.class); Context.getPermissionsManager().checkUser(getUserId(req), user.getId()); Context.getDataManager().removeUser(user); Context.getPermissionsManager().refresh(); |