diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-03 14:35:59 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-03 14:35:59 +1200 |
commit | 7b58913edc7d1572ef66e16bd1c989a510455b6e (patch) | |
tree | 4cdc76ee53fe86ea357c711e967f4906c7deb275 /src/org/traccar/http/DeviceServlet.java | |
parent | 18127a2dfc362ffd7de4110e013a10a9f894469b (diff) | |
download | traccar-server-7b58913edc7d1572ef66e16bd1c989a510455b6e.tar.gz traccar-server-7b58913edc7d1572ef66e16bd1c989a510455b6e.tar.bz2 traccar-server-7b58913edc7d1572ef66e16bd1c989a510455b6e.zip |
Create base servlet class
Diffstat (limited to 'src/org/traccar/http/DeviceServlet.java')
-rw-r--r-- | src/org/traccar/http/DeviceServlet.java | 115 |
1 files changed, 22 insertions, 93 deletions
diff --git a/src/org/traccar/http/DeviceServlet.java b/src/org/traccar/http/DeviceServlet.java index 81ec168d5..a06ea5368 100644 --- a/src/org/traccar/http/DeviceServlet.java +++ b/src/org/traccar/http/DeviceServlet.java @@ -15,28 +15,16 @@ */ package org.traccar.http; -import java.io.IOException; -import java.sql.SQLException; -import java.text.ParseException; -import javax.json.Json; -import javax.json.JsonObjectBuilder; -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.model.Device; -public class DeviceServlet extends HttpServlet { - - @Override - protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - String command = req.getPathInfo(); +public class DeviceServlet extends BaseServlet { - if (command == null) { - resp.sendError(HttpServletResponse.SC_BAD_REQUEST); - } else if (command.equals("/get")) { + @Override + protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { + if (command.equals("/get")) { get(req, resp); } else if (command.equals("/add")) { add(req, resp); @@ -45,92 +33,33 @@ public class DeviceServlet extends HttpServlet { } else if (command.equals("/remove")) { remove(req, resp); } else { - resp.sendError(HttpServletResponse.SC_BAD_REQUEST); + return false; } + return true; } - private void get(HttpServletRequest req, HttpServletResponse resp) throws IOException { - - long userId = (Long) req.getSession().getAttribute(MainServlet.USER_ID); - - JsonObjectBuilder result = Json.createObjectBuilder(); - - try { - result.add("success", true); - result.add("data", JsonConverter.arrayToJson( - Context.getDataManager().getDevices(userId))); - } catch(SQLException error) { - result.add("success", false); - result.add("error", error.getMessage()); - } - - resp.getWriter().println(result.build().toString()); + private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { + sendResponse(resp.getWriter(), JsonConverter.arrayToJson( + Context.getDataManager().getDevices(getUserId(req.getSession())))); } - private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException { - - long userId = (Long) req.getSession().getAttribute(MainServlet.USER_ID); - - Device device = new Device(); - try { - JsonConverter.objectFromJson(req.getReader(), device); - } catch (ParseException error) { - } - - JsonObjectBuilder result = Json.createObjectBuilder(); - - try { - result.add("success", true); - Context.getDataManager().addDevice(device); - Context.getDataManager().linkDevice(userId, device.getId()); - } catch(SQLException error) { - result.add("success", false); - result.add("error", error.getMessage()); - } - - resp.getWriter().println(result.build().toString()); + private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { + Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); + Context.getDataManager().addDevice(device); + Context.getDataManager().linkDevice(getUserId(req.getSession()), device.getId()); + sendResponse(resp.getWriter()); } - private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException { - - Device device = new Device(); - try { - JsonConverter.objectFromJson(req.getReader(), device); - } catch (ParseException error) { - } - - JsonObjectBuilder result = Json.createObjectBuilder(); - - try { - result.add("success", true); - Context.getDataManager().updateDevice(device); - } catch(SQLException error) { - result.add("success", false); - result.add("error", error.getMessage()); - } - - resp.getWriter().println(result.build().toString()); + private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { + Context.getDataManager().updateDevice(JsonConverter.objectFromJson( + req.getReader(), new Device())); + sendResponse(resp.getWriter()); } - private void remove(HttpServletRequest req, HttpServletResponse resp) throws IOException { - - Device device = new Device(); - try { - JsonConverter.objectFromJson(req.getReader(), device); - } catch (ParseException error) { - } - - JsonObjectBuilder result = Json.createObjectBuilder(); - - try { - result.add("success", true); - Context.getDataManager().removeDevice(device); - } catch(SQLException error) { - result.add("success", false); - result.add("error", error.getMessage()); - } - - resp.getWriter().println(result.build().toString()); + private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { + Context.getDataManager().removeDevice(JsonConverter.objectFromJson( + req.getReader(), new Device())); + sendResponse(resp.getWriter()); } } |