diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-03 12:09:00 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-03 12:09:00 +1200 |
commit | 18127a2dfc362ffd7de4110e013a10a9f894469b (patch) | |
tree | 24ced00567acbc751985b71ffd99ce067213a34d /src/org/traccar/http | |
parent | 427113e0aabf1b8ab54288263c3e809f292a5a05 (diff) | |
download | traccar-server-18127a2dfc362ffd7de4110e013a10a9f894469b.tar.gz traccar-server-18127a2dfc362ffd7de4110e013a10a9f894469b.tar.bz2 traccar-server-18127a2dfc362ffd7de4110e013a10a9f894469b.zip |
Implmenet reflection json conversion
Diffstat (limited to 'src/org/traccar/http')
-rw-r--r-- | src/org/traccar/http/AsyncServlet.java | 3 | ||||
-rw-r--r-- | src/org/traccar/http/DeviceServlet.java | 21 | ||||
-rw-r--r-- | src/org/traccar/http/JsonConverter.java | 120 | ||||
-rw-r--r-- | src/org/traccar/http/MainServlet.java | 1 |
4 files changed, 125 insertions, 20 deletions
diff --git a/src/org/traccar/http/AsyncServlet.java b/src/org/traccar/http/AsyncServlet.java index c66126667..927e93c78 100644 --- a/src/org/traccar/http/AsyncServlet.java +++ b/src/org/traccar/http/AsyncServlet.java @@ -20,7 +20,6 @@ import org.jboss.netty.util.TimerTask; import org.traccar.Context; import org.traccar.GlobalTimer; import org.traccar.database.DataCache; -import org.traccar.database.ObjectConverter; import org.traccar.helper.Log; import org.traccar.model.Position; @@ -160,7 +159,7 @@ public class AsyncServlet extends HttpServlet { JsonObjectBuilder result = Json.createObjectBuilder(); result.add("success", true); - result.add("data", ObjectConverter.arrayToJson(positions.values())); + result.add("data", JsonConverter.arrayToJson(positions.values())); positions.clear(); try { diff --git a/src/org/traccar/http/DeviceServlet.java b/src/org/traccar/http/DeviceServlet.java index 98f00b320..81ec168d5 100644 --- a/src/org/traccar/http/DeviceServlet.java +++ b/src/org/traccar/http/DeviceServlet.java @@ -19,16 +19,12 @@ import java.io.IOException; import java.sql.SQLException; import java.text.ParseException; import javax.json.Json; -import javax.json.JsonObject; import javax.json.JsonObjectBuilder; -import javax.json.JsonReader; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.traccar.Context; -import org.traccar.database.ObjectConverter; -import org.traccar.helper.Log; import org.traccar.model.Device; public class DeviceServlet extends HttpServlet { @@ -61,7 +57,7 @@ public class DeviceServlet extends HttpServlet { try { result.add("success", true); - result.add("data", ObjectConverter.arrayToJson( + result.add("data", JsonConverter.arrayToJson( Context.getDataManager().getDevices(userId))); } catch(SQLException error) { result.add("success", false); @@ -75,13 +71,10 @@ public class DeviceServlet extends HttpServlet { long userId = (Long) req.getSession().getAttribute(MainServlet.USER_ID); - JsonReader jsonReader = Json.createReader(req.getReader()); - JsonObject jsonObject = jsonReader.readObject(); Device device = new Device(); try { - device.fromJson(jsonObject); + JsonConverter.objectFromJson(req.getReader(), device); } catch (ParseException error) { - Log.warning(error); } JsonObjectBuilder result = Json.createObjectBuilder(); @@ -100,13 +93,10 @@ public class DeviceServlet extends HttpServlet { private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException { - JsonReader jsonReader = Json.createReader(req.getReader()); - JsonObject jsonObject = jsonReader.readObject(); Device device = new Device(); try { - device.fromJson(jsonObject); + JsonConverter.objectFromJson(req.getReader(), device); } catch (ParseException error) { - Log.warning(error); } JsonObjectBuilder result = Json.createObjectBuilder(); @@ -124,13 +114,10 @@ public class DeviceServlet extends HttpServlet { private void remove(HttpServletRequest req, HttpServletResponse resp) throws IOException { - JsonReader jsonReader = Json.createReader(req.getReader()); - JsonObject jsonObject = jsonReader.readObject(); Device device = new Device(); try { - device.fromJson(jsonObject); + JsonConverter.objectFromJson(req.getReader(), device); } catch (ParseException error) { - Log.warning(error); } JsonObjectBuilder result = Json.createObjectBuilder(); diff --git a/src/org/traccar/http/JsonConverter.java b/src/org/traccar/http/JsonConverter.java new file mode 100644 index 000000000..1b02e33d2 --- /dev/null +++ b/src/org/traccar/http/JsonConverter.java @@ -0,0 +1,120 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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.http; + +import java.beans.Introspector; +import java.io.Reader; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.Date; +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import org.traccar.model.Factory; + +public class JsonConverter { + + public static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"); + + public static <T extends Factory> T objectFromJson(Reader reader, T prototype) throws ParseException { + return objectFromJson(Json.createReader(reader).readObject(), prototype); + } + + public static <T extends Factory> T objectFromJson(JsonObject json, T prototype) throws ParseException { + + T object = (T) prototype.create(); + + Method[] methods = prototype.getClass().getMethods(); + + 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)) 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)) { + 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, dateFormat.parse(json.getString(name))); + } + } catch (IllegalAccessException error) { + } catch (InvocationTargetException 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 = 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)) { + json.add(name, (String) method.invoke(object)); + } else if (method.getReturnType().equals(Date.class)) { + json.add(name, dateFormat.format((Date) method.invoke(object))); + } + } catch (IllegalAccessException error) { + } catch (InvocationTargetException error) { + } + } + } + + return json.build(); + } + + public static JsonArray arrayToJson(Collection<?> array) { + + JsonArrayBuilder json = Json.createArrayBuilder(); + + for (Object object : array) { + json.add(objectToJson(object)); + } + + return json.build(); + } + +} diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java index 2ba97ed57..a881d3f45 100644 --- a/src/org/traccar/http/MainServlet.java +++ b/src/org/traccar/http/MainServlet.java @@ -34,7 +34,6 @@ import org.jboss.netty.util.TimerTask; import org.traccar.Context; import org.traccar.GlobalTimer; import org.traccar.database.DataCache; -import org.traccar.database.ObjectConverter; import org.traccar.helper.Log; import org.traccar.model.Position; |