diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-10 18:11:40 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-10 18:11:40 +1200 |
commit | bd09a39f12c4471bdc342e5a16fbabdffdc645d1 (patch) | |
tree | db94476ee604c2026c5653975c526352093d5628 /src/org/traccar/http | |
parent | 953852e6e984f8a639cf1a5ac4116fff755d538a (diff) | |
download | traccar-server-bd09a39f12c4471bdc342e5a16fbabdffdc645d1.tar.gz traccar-server-bd09a39f12c4471bdc342e5a16fbabdffdc645d1.tar.bz2 traccar-server-bd09a39f12c4471bdc342e5a16fbabdffdc645d1.zip |
Refactor web app architecture
Diffstat (limited to 'src/org/traccar/http')
-rw-r--r-- | src/org/traccar/http/AsyncServlet.java | 2 | ||||
-rw-r--r-- | src/org/traccar/http/BaseServlet.java | 13 | ||||
-rw-r--r-- | src/org/traccar/http/DeviceServlet.java | 6 | ||||
-rw-r--r-- | src/org/traccar/http/JsonConverter.java | 5 | ||||
-rw-r--r-- | src/org/traccar/http/MainServlet.java | 24 |
5 files changed, 32 insertions, 18 deletions
diff --git a/src/org/traccar/http/AsyncServlet.java b/src/org/traccar/http/AsyncServlet.java index f00257f80..2bc6ff7a1 100644 --- a/src/org/traccar/http/AsyncServlet.java +++ b/src/org/traccar/http/AsyncServlet.java @@ -182,7 +182,7 @@ public class AsyncServlet extends HttpServlet { context.setTimeout(ASYNC_TIMEOUT); HttpServletRequest req = (HttpServletRequest) context.getRequest(); - long userId = (Long) req.getSession().getAttribute(MainServlet.USER_ID); + long userId = (Long) req.getSession().getAttribute(MainServlet.USER_KEY); synchronized (asyncSessions) { diff --git a/src/org/traccar/http/BaseServlet.java b/src/org/traccar/http/BaseServlet.java index 9249b33d8..122dbe70b 100644 --- a/src/org/traccar/http/BaseServlet.java +++ b/src/org/traccar/http/BaseServlet.java @@ -26,10 +26,11 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.traccar.model.User; public abstract class BaseServlet extends HttpServlet { - public static final String USER_ID = "userId"; + public static final String USER_KEY = "user"; @Override protected final void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { @@ -46,16 +47,16 @@ public abstract class BaseServlet extends HttpServlet { protected abstract boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception; public long getUserId(HttpSession session) { - Long userId = (Long) session.getAttribute(USER_ID); - if (userId == null) { + User user = (User) session.getAttribute(USER_KEY); + if (user == null) { throw new AccessControlException("User is not logged in"); } - return userId; + return user.getId(); } - public void sendResponse(Writer writer) throws IOException { + public void sendResponse(Writer writer, boolean success) throws IOException { JsonObjectBuilder result = Json.createObjectBuilder(); - result.add("success", true); + result.add("success", success); writer.write(result.build().toString()); } diff --git a/src/org/traccar/http/DeviceServlet.java b/src/org/traccar/http/DeviceServlet.java index a06ea5368..1387c2a13 100644 --- a/src/org/traccar/http/DeviceServlet.java +++ b/src/org/traccar/http/DeviceServlet.java @@ -47,19 +47,19 @@ public class DeviceServlet extends BaseServlet { Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); Context.getDataManager().addDevice(device); Context.getDataManager().linkDevice(getUserId(req.getSession()), device.getId()); - sendResponse(resp.getWriter()); + sendResponse(resp.getWriter(), JsonConverter.objectToJson(device)); } private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { Context.getDataManager().updateDevice(JsonConverter.objectFromJson( req.getReader(), new Device())); - sendResponse(resp.getWriter()); + sendResponse(resp.getWriter(), true); } private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { Context.getDataManager().removeDevice(JsonConverter.objectFromJson( req.getReader(), new Device())); - sendResponse(resp.getWriter()); + sendResponse(resp.getWriter(), true); } } diff --git a/src/org/traccar/http/JsonConverter.java b/src/org/traccar/http/JsonConverter.java index 0802fb9ea..f7c5a5622 100644 --- a/src/org/traccar/http/JsonConverter.java +++ b/src/org/traccar/http/JsonConverter.java @@ -29,6 +29,7 @@ import javax.json.JsonArray; import javax.json.JsonArrayBuilder; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; +import javax.json.JsonValue; import org.traccar.model.Factory; public class JsonConverter { @@ -57,7 +58,9 @@ public class JsonConverter { } 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()); + 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)) { diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java index 036c472f8..18430f0c3 100644 --- a/src/org/traccar/http/MainServlet.java +++ b/src/org/traccar/http/MainServlet.java @@ -41,24 +41,34 @@ public class MainServlet extends BaseServlet { } private void session(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - resp.getWriter().println("{ success: true, session: " + (req.getSession().getAttribute(USER_ID) != null) + " }"); + User user = (User) req.getSession().getAttribute(USER_KEY); + if (user != null) { + sendResponse(resp.getWriter(), JsonConverter.objectToJson(user)); + } else { + sendResponse(resp.getWriter(), false); + } } private void login(HttpServletRequest req, HttpServletResponse resp) throws Exception { - req.getSession().setAttribute(USER_ID, Context.getDataManager().login( - req.getParameter("email"), req.getParameter("password"))); - sendResponse(resp.getWriter()); + User user = Context.getDataManager().login( + req.getParameter("email"), req.getParameter("password")); + if (user != null) { + req.getSession().setAttribute(USER_KEY, user); + sendResponse(resp.getWriter(), JsonConverter.objectToJson(user)); + } else { + sendResponse(resp.getWriter(), false); + } } private void logout(HttpServletRequest req, HttpServletResponse resp) throws Exception { - req.getSession().removeAttribute(USER_ID); - sendResponse(resp.getWriter()); + req.getSession().removeAttribute(USER_KEY); + sendResponse(resp.getWriter(), true); } private void register(HttpServletRequest req, HttpServletResponse resp) throws Exception { User user = JsonConverter.objectFromJson(req.getReader(), new User()); Context.getDataManager().addUser(user); - sendResponse(resp.getWriter()); + sendResponse(resp.getWriter(), true); } } |