diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-30 13:55:05 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-30 13:55:05 +1300 |
commit | cefc12afe98f610aa0f266bcac4361f9ec414ee5 (patch) | |
tree | 48d47a0eec0c2d8a3ee7f448f8d535d7d826d546 /src/org/traccar/web | |
parent | c6692703c8099577cb86c3eace5d8d1bc11f212d (diff) | |
parent | 1a25796d62bdf88f2cba0387a09e2eb71599e588 (diff) | |
download | trackermap-server-cefc12afe98f610aa0f266bcac4361f9ec414ee5.tar.gz trackermap-server-cefc12afe98f610aa0f266bcac4361f9ec414ee5.tar.bz2 trackermap-server-cefc12afe98f610aa0f266bcac4361f9ec414ee5.zip |
Merge master of guterresrafael/traccar into rest
Diffstat (limited to 'src/org/traccar/web')
-rw-r--r-- | src/org/traccar/web/BaseServlet.java | 53 | ||||
-rw-r--r-- | src/org/traccar/web/CommandServlet.java | 9 | ||||
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 16 | ||||
-rw-r--r-- | src/org/traccar/web/MainServlet.java | 8 | ||||
-rw-r--r-- | src/org/traccar/web/WebServer.java | 17 |
5 files changed, 52 insertions, 51 deletions
diff --git a/src/org/traccar/web/BaseServlet.java b/src/org/traccar/web/BaseServlet.java index 283edf1e5..d215c62d0 100644 --- a/src/org/traccar/web/BaseServlet.java +++ b/src/org/traccar/web/BaseServlet.java @@ -19,9 +19,10 @@ import org.traccar.helper.Log; import java.io.IOException; import java.io.Writer; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.security.AccessControlException; import java.util.Collection; -import java.util.Map; import javax.json.Json; import javax.json.JsonObjectBuilder; import javax.json.JsonStructure; @@ -32,20 +33,14 @@ import javax.servlet.http.HttpServletResponse; import org.jboss.netty.handler.codec.http.HttpHeaders; import org.jboss.netty.util.CharsetUtil; import org.traccar.Context; -import org.traccar.helper.Authorization; -import org.traccar.model.User; public abstract class BaseServlet extends HttpServlet { - public static final String USER_KEY = "user"; + public static final String USER_ID_KEY = "user"; public static final String ALLOW_ORIGIN_VALUE = "*"; public static final String ALLOW_HEADERS_VALUE = "Origin, X-Requested-With, Content-Type, Accept"; public static final String ALLOW_METHODS_VALUE = "GET, POST, PUT, DELETE"; public static final String APPLICATION_JSON = "application/json"; - public static final String GET = "GET"; - public static final String POST = "POST"; - public static final String PUT = "PUT"; - public static final String DELETE = "DELETE"; @Override protected final void service( @@ -61,7 +56,8 @@ public abstract class BaseServlet extends HttpServlet { if (allowed == null) { resp.setHeader(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, ALLOW_ORIGIN_VALUE); } else if (allowed.contains(origin)) { - resp.setHeader(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, origin); + String originSafe = URLEncoder.encode(origin, StandardCharsets.UTF_8.displayName()); + resp.setHeader(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, originSafe); } if (!handle(getCommand(req), req, resp)) { @@ -70,7 +66,6 @@ public abstract class BaseServlet extends HttpServlet { } catch (Exception error) { if (error instanceof AccessControlException) { resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - resp.addHeader(HttpHeaders.Names.WWW_AUTHENTICATE, Authorization.WWW_AUTHENTICATE_VALUE); } else if (error instanceof SecurityException) { resp.setStatus(HttpServletResponse.SC_FORBIDDEN); } @@ -82,21 +77,11 @@ public abstract class BaseServlet extends HttpServlet { String command, HttpServletRequest req, HttpServletResponse resp) throws Exception; public long getUserId(HttpServletRequest req) throws Exception { - String authorization = req.getHeader(HttpHeaders.Names.AUTHORIZATION); - if (authorization != null && !authorization.isEmpty()) { - Map<String, String> authMap = Authorization.parse(authorization); - String username = authMap.get(Authorization.USERNAME); - String password = authMap.get(Authorization.PASSWORD); - User user = Context.getDataManager().login(username, password); - if (user != null) { - return user.getId(); - } - } - Long userId = (Long) req.getSession().getAttribute(USER_KEY); - if (userId == null) { - throw new AccessControlException("User not logged in"); + Object userId = req.getSession().getAttribute(USER_ID_KEY); + if (userId != null) { + return (Long) userId; } - return userId; + throw new AccessControlException("User not logged in"); } public void sendResponse(Writer writer, boolean success) throws IOException { @@ -129,26 +114,12 @@ public abstract class BaseServlet extends HttpServlet { writer.write(result.build().toString()); } - private String getCommand(HttpServletRequest req) { + protected String getCommand(HttpServletRequest req) { String command = req.getPathInfo(); if (command == null) { - switch (req.getMethod()) { - case GET: - command = "/get"; - break; - case POST: - command = "/add"; - break; - case PUT: - command = "/update"; - break; - case DELETE: - command = "/remove"; - break; - default: - command = ""; - } + command = ""; } return command; } + } diff --git a/src/org/traccar/web/CommandServlet.java b/src/org/traccar/web/CommandServlet.java index 67bca2d57..d307913df 100644 --- a/src/org/traccar/web/CommandServlet.java +++ b/src/org/traccar/web/CommandServlet.java @@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletResponse; import org.traccar.Context; import org.traccar.database.ActiveDevice; import org.traccar.model.Command; +import org.traccar.model.Device; public class CommandServlet extends BaseServlet { @@ -49,19 +50,17 @@ public class CommandServlet extends BaseServlet { } private void send(HttpServletRequest req, HttpServletResponse resp) throws Exception { - - Command command = JsonConverter.objectFromJson(req.getReader(), new Command()); - Context.getPermissionsManager().checkDevice(getUserId(req), command.getDeviceId()); + Command command = JsonConverter.objectFromJson(req.getReader(), Command.class); + Context.getPermissionsManager().check(Device.class, getUserId(req), command.getDeviceId()); getActiveDevice(command.getDeviceId()).sendCommand(command); sendResponse(resp.getWriter(), true); } private void raw(HttpServletRequest req, HttpServletResponse resp) throws Exception { - JsonObject json = Json.createReader(req.getReader()).readObject(); long deviceId = json.getJsonNumber("deviceId").longValue(); String command = json.getString("command"); - Context.getPermissionsManager().checkDevice(getUserId(req), deviceId); + Context.getPermissionsManager().check(Device.class, getUserId(req), deviceId); getActiveDevice(deviceId).write(command); sendResponse(resp.getWriter(), true); } diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index c01ce8bd6..38721db61 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -34,6 +34,7 @@ import javax.json.JsonValue; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; +import org.traccar.helper.Clazz; import org.traccar.helper.Log; import org.traccar.model.Factory; import org.traccar.model.MiscFormatter; @@ -57,9 +58,23 @@ public final class JsonConverter { 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); + } + } + public static <T> T objectFromJson(JsonObject json, Class<T> clazz) { + T object = Clazz.newInstance(clazz); Method[] methods = object.getClass().getMethods(); + return objectFromJson(json, object, methods); + } + private static <T> T objectFromJson(JsonObject json, T object, Method[] methods) { for (final Method method : methods) { if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { @@ -91,7 +106,6 @@ public final class JsonConverter { } } } - return object; } diff --git a/src/org/traccar/web/MainServlet.java b/src/org/traccar/web/MainServlet.java index 63ff27813..40bfcddb5 100644 --- a/src/org/traccar/web/MainServlet.java +++ b/src/org/traccar/web/MainServlet.java @@ -45,7 +45,7 @@ public class MainServlet extends BaseServlet { } private void session(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Long userId = (Long) req.getSession().getAttribute(USER_KEY); + Long userId = (Long) req.getSession().getAttribute(USER_ID_KEY); if (userId != null) { sendResponse(resp.getWriter(), JsonConverter.objectToJson( Context.getDataManager().getUser(userId))); @@ -58,7 +58,7 @@ public class MainServlet extends BaseServlet { User user = Context.getDataManager().login( req.getParameter("email"), req.getParameter("password")); if (user != null) { - req.getSession().setAttribute(USER_KEY, user.getId()); + req.getSession().setAttribute(USER_ID_KEY, user.getId()); sendResponse(resp.getWriter(), JsonConverter.objectToJson(user)); } else { sendResponse(resp.getWriter(), false); @@ -66,12 +66,12 @@ public class MainServlet extends BaseServlet { } private void logout(HttpServletRequest req, HttpServletResponse resp) throws Exception { - req.getSession().removeAttribute(USER_KEY); + req.getSession().removeAttribute(USER_ID_KEY); sendResponse(resp.getWriter(), true); } private void register(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); + User user = JsonConverter.objectFromJson(req.getReader(), User.class); Context.getDataManager().addUser(user); sendResponse(resp.getWriter(), true); } diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index f5a6acdd9..2318c4151 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -24,6 +24,8 @@ import org.eclipse.jetty.server.handler.ResourceHandler; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.webapp.WebAppContext; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.servlet.ServletContainer; import org.traccar.Config; import org.traccar.helper.Log; @@ -101,6 +103,12 @@ public class WebServer { } private void initApi() { + initOldApi(); + initRestApi(); + } + + @Deprecated + private void initOldApi() { ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); servletHandler.setContextPath("/api"); servletHandler.addServlet(new ServletHolder(new AsyncServlet()), "/async/*"); @@ -120,6 +128,15 @@ public class WebServer { handlers.addHandler(servletHandler); } + private void initRestApi() { + ResourceConfig resourceConfig = new ResourceConfig(); + resourceConfig.packages("org.traccar.api"); + ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS); + ServletHolder servletHolder = new ServletHolder(new ServletContainer(resourceConfig)); + servletHandler.addServlet(servletHolder, "/rest/*"); + handlers.addHandler(servletHandler); + } + public void start() { try { server.start(); |