diff options
Diffstat (limited to 'src/org/traccar/http/MainServlet.java')
-rw-r--r-- | src/org/traccar/http/MainServlet.java | 48 |
1 files changed, 11 insertions, 37 deletions
diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java index a881d3f45..8fa0a13af 100644 --- a/src/org/traccar/http/MainServlet.java +++ b/src/org/traccar/http/MainServlet.java @@ -16,66 +16,40 @@ package org.traccar.http; import java.io.IOException; -import java.sql.SQLException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import javax.json.Json; -import javax.json.JsonObjectBuilder; -import javax.servlet.AsyncContext; import javax.servlet.ServletException; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.jboss.netty.util.Timeout; -import org.jboss.netty.util.TimerTask; import org.traccar.Context; -import org.traccar.GlobalTimer; -import org.traccar.database.DataCache; -import org.traccar.helper.Log; -import org.traccar.model.Position; -public class MainServlet extends HttpServlet { - - public static final String USER_ID = "userId"; +public class MainServlet extends BaseServlet { @Override - protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - String command = req.getPathInfo(); - - if (command == null) { - resp.sendError(HttpServletResponse.SC_BAD_REQUEST); - } else if (command.equals("/session")) { + protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { + if (command.equals("/session")) { session(req, resp); } else if (command.equals("/login")) { login(req, resp); } else if (command.equals("/logout")) { logout(req, resp); } else { - resp.sendError(HttpServletResponse.SC_BAD_REQUEST); + return false; } + return true; } private void session(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().println("{ success: true, session: " + (req.getSession().getAttribute(USER_ID) != null) + " }"); } - private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - try { - req.getSession().setAttribute(USER_ID, - Context.getDataManager().login(req.getParameter("email"), req.getParameter("password"))); - resp.getWriter().println("{ success: true }"); - } catch (SQLException e) { - throw new ServletException(e); - } + 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()); } - private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException { + private void logout(HttpServletRequest req, HttpServletResponse resp) throws Exception { req.getSession().removeAttribute(USER_ID); - resp.getWriter().println("{ success: true }"); + sendResponse(resp.getWriter()); } } |