diff options
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/database/DataManager.java | 39 | ||||
-rw-r--r-- | src/org/traccar/database/PermissionsManager.java | 12 | ||||
-rw-r--r-- | src/org/traccar/helper/Clazz.java | 105 | ||||
-rw-r--r-- | src/org/traccar/helper/CommandCall.java | 51 | ||||
-rw-r--r-- | src/org/traccar/web/BaseServlet.java | 36 | ||||
-rw-r--r-- | src/org/traccar/web/BaseServletResource.java | 169 | ||||
-rw-r--r-- | src/org/traccar/web/CommandServlet.java | 9 | ||||
-rw-r--r-- | src/org/traccar/web/DeviceServlet.java | 67 | ||||
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 13 | ||||
-rw-r--r-- | src/org/traccar/web/MainServlet.java | 8 | ||||
-rw-r--r-- | src/org/traccar/web/PositionServlet.java | 10 | ||||
-rw-r--r-- | src/org/traccar/web/ServerServlet.java | 27 | ||||
-rw-r--r-- | src/org/traccar/web/UserServlet.java | 77 | ||||
-rw-r--r-- | src/org/traccar/web/WebServer.java | 5 |
14 files changed, 520 insertions, 108 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index a40b87d24..530ec1779 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -385,4 +385,43 @@ public class DataManager implements IdentityManager { .executeUpdate(); } + public void add(Object entity) throws SQLException { + if (entity instanceof User) { + addUser((User) entity); + } else if (entity instanceof Device) { + addDevice((Device) entity); + } else if (entity instanceof Position) { + addPosition((Position) entity); + } + } + + public void update(Object entity) throws SQLException { + if (entity instanceof User) { + updateUser((User) entity); + } else if (entity instanceof Device) { + updateDevice((Device) entity); + } else if (entity instanceof Server) { + updateServer((Server) entity); + } + } + + public void remove(Object entity) throws SQLException { + if (entity instanceof User) { + removeUser((User) entity); + } else if (entity instanceof Device) { + removeDevice((Device) entity); + } + } + + public void link(Class clazz, long userId, long entityId) throws SQLException { + if (clazz.equals(Device.class)) { + linkDevice(userId, entityId); + } + } + + public void unlink(Class clazz, long userId, long entityId) throws SQLException { + if (clazz.equals(Device.class)) { + unlinkDevice(userId, entityId); + } + } } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index a38a29c32..138641973 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -22,6 +22,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; import org.traccar.helper.Log; +import org.traccar.model.Device; import org.traccar.model.Permission; import org.traccar.model.User; @@ -66,7 +67,7 @@ public class PermissionsManager { } } - public void checkUser(long userId, long otherUserId) throws SecurityException { + private void checkUser(long userId, long otherUserId) throws SecurityException { if (userId != otherUserId) { checkAdmin(userId); } @@ -76,10 +77,17 @@ public class PermissionsManager { return getNotNull(userId); } - public void checkDevice(long userId, long deviceId) throws SecurityException { + private void checkDevice(long userId, long deviceId) throws SecurityException { if (!getNotNull(userId).contains(deviceId)) { throw new SecurityException("Device access denied"); } } + public <T> void check(Class<T> clazz, long userId, long entityId) throws SecurityException { + if (clazz.equals(User.class)) { + checkUser(userId, entityId); + } else if (clazz.equals(Device.class)) { + checkDevice(userId, entityId); + } + } } diff --git a/src/org/traccar/helper/Clazz.java b/src/org/traccar/helper/Clazz.java new file mode 100644 index 000000000..bdde940a0 --- /dev/null +++ b/src/org/traccar/helper/Clazz.java @@ -0,0 +1,105 @@ +/* + * 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.helper; + +import java.beans.Introspector; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +public final class Clazz { + + private Clazz() { + } + + public static Class getGenericArgumentType(Class currentClass, Class genericSuperClass) { + return getGenericArgumentType(currentClass, genericSuperClass, 0); + } + + public static Class getGenericArgumentType(Class currentClass, int argumentIndex) { + return getGenericArgumentType(currentClass, null, argumentIndex); + } + + public static Class getGenericArgumentType(Class currentClass) { + return getGenericArgumentType(currentClass, null, 0); + } + + public static Class getGenericArgumentType(Class currentClass, Class genericSuperClass, int argumentIndex) { + Type superType = currentClass.getGenericSuperclass(); + if (superType == null) { + throw new IllegalArgumentException(); + } + if (!(superType instanceof ParameterizedType) + || genericSuperClass != null + && ((ParameterizedType) superType).getRawType() != genericSuperClass) { + return getGenericArgumentType(currentClass.getSuperclass(), genericSuperClass, argumentIndex); + } + Object[] args = ((ParameterizedType) superType).getActualTypeArguments(); + if (argumentIndex >= args.length) { + throw new IllegalArgumentException(); + } + return cast(Class.class, args[argumentIndex]); + } + + public static <T> T newInstance(Class<T> clazz) { + try { + return clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException(); + } + } + + public static <T> T cast(Class<T> classe, Object objeto) { + if (classe.isAssignableFrom(objeto.getClass())) { + return classe.cast(objeto); + } + throw new ClassCastException(); + } + + public static Class forName(String className) { + try { + return Class.forName(className, false, Thread.currentThread().getContextClassLoader()); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(e); + } + } + + public static long getId(Object entity) throws Exception { + Method[] methods = entity.getClass().getMethods(); + for (final Method method : methods) { + if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { + final String name = Introspector.decapitalize(method.getName().substring(3)); + if (name.equals("id")) { + return Long.parseLong(method.invoke(entity).toString()); + } + } + } + throw new IllegalArgumentException(); + } + + public static void setId(Object entity, long id) throws Exception { + Method[] methods = entity.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)); + if (name.equals("id")) { + method.invoke(entity, id); + break; + } + } + } + } +} diff --git a/src/org/traccar/helper/CommandCall.java b/src/org/traccar/helper/CommandCall.java new file mode 100644 index 000000000..d5da79348 --- /dev/null +++ b/src/org/traccar/helper/CommandCall.java @@ -0,0 +1,51 @@ +/* + * 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.helper; + +public abstract class CommandCall<T> { + + private long userId; + private T entity; + + public void before() throws Exception { + //Do nothing. + } + + public void check() throws Exception { + //Do nothing. + } + + public void after() throws Exception { + //Do nothing. + } + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + public T getEntity() { + return entity; + } + + public void setEntity(T entity) { + this.entity = entity; + } + +} diff --git a/src/org/traccar/web/BaseServlet.java b/src/org/traccar/web/BaseServlet.java index 283edf1e5..916eb6a18 100644 --- a/src/org/traccar/web/BaseServlet.java +++ b/src/org/traccar/web/BaseServlet.java @@ -37,15 +37,11 @@ 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( @@ -82,6 +78,10 @@ public abstract class BaseServlet extends HttpServlet { String command, HttpServletRequest req, HttpServletResponse resp) throws Exception; public long getUserId(HttpServletRequest req) throws Exception { + Object userId = req.getSession().getAttribute(USER_ID_KEY); + if (userId != null) { + return (Long) userId; + } String authorization = req.getHeader(HttpHeaders.Names.AUTHORIZATION); if (authorization != null && !authorization.isEmpty()) { Map<String, String> authMap = Authorization.parse(authorization); @@ -92,11 +92,7 @@ public abstract class BaseServlet extends HttpServlet { return user.getId(); } } - Long userId = (Long) req.getSession().getAttribute(USER_KEY); - if (userId == null) { - throw new AccessControlException("User not logged in"); - } - return userId; + throw new AccessControlException("User not logged in"); } public void sendResponse(Writer writer, boolean success) throws IOException { @@ -129,26 +125,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/BaseServletResource.java b/src/org/traccar/web/BaseServletResource.java new file mode 100644 index 000000000..318f0d7b6 --- /dev/null +++ b/src/org/traccar/web/BaseServletResource.java @@ -0,0 +1,169 @@ +/* + * 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.web; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.traccar.Context; +import org.traccar.helper.CommandCall; +import org.traccar.helper.Clazz; + +/** + * + * @author Rafael + */ +public abstract class BaseServletResource<T> extends BaseServlet { + + private final Class<T> clazz = Clazz.getGenericArgumentType(getClass()); + + public static final String GET = "GET"; + public static final String POST = "POST"; + public static final String PUT = "PUT"; + public static final String DELETE = "DELETE"; + + public static final String PATH_PARAM_ID = "/\\d"; + public static final String SLASH = "/"; + public static final String VOID = ""; + + @Override + protected String getCommand(HttpServletRequest req) { + String command = req.getPathInfo(); + if (command == null || command.matches(PATH_PARAM_ID)) { + 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 = ""; + } + } + return command; + } + + protected String getPathParamId(String pathInfo) { + if (pathInfo != null && pathInfo.matches(PATH_PARAM_ID)) { + return pathInfo.replaceAll(SLASH, VOID); + } + return null; + } + + protected abstract void get(HttpServletRequest req, HttpServletResponse resp) throws Exception; + + protected void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { + add(req, resp, null); + } + + protected void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { + update(req, resp, null); + } + + protected void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { + remove(req, resp, null); + } + + protected void add(HttpServletRequest req, HttpServletResponse resp, CommandCall<T> commandCall) throws Exception { + if (commandCall != null) { + commandCall.before(); + } + + T entity = JsonConverter.objectFromJson(req.getReader(), this.clazz); + long userId = getUserId(req); + if (commandCall != null) { + commandCall.setUserId(userId); + commandCall.setEntity(entity); + commandCall.check(); + } + + Context.getDataManager().add(entity); + + long entityId = Clazz.getId(entity); + Context.getDataManager().link(this.clazz, userId, entityId); + + if (commandCall != null) { + commandCall.after(); + } + + sendResponse(resp.getWriter(), JsonConverter.objectToJson(entity)); + } + + protected void update(HttpServletRequest req, HttpServletResponse resp, + CommandCall<T> commandCall) throws Exception { + if (commandCall != null) { + commandCall.before(); + } + + T entity = JsonConverter.objectFromJson(req.getReader(), this.clazz); + String entityId = getPathParamId(req.getPathInfo()); + if (entityId != null) { + Clazz.setId(entity, Long.parseLong(entityId)); + } + long userId = getUserId(req); + + if (commandCall != null) { + commandCall.setUserId(userId); + commandCall.setEntity(entity); + commandCall.check(); + } + + Context.getDataManager().update(entity); + + if (commandCall != null) { + commandCall.after(); + } + + sendResponse(resp.getWriter(), true); + } + + protected void remove(HttpServletRequest req, HttpServletResponse resp, + CommandCall<T> commandCall) throws Exception { + if (commandCall != null) { + commandCall.before(); + } + + T entity = Clazz.newInstance(this.clazz); + String entityId = getPathParamId(req.getPathInfo()); + if (entityId != null) { + Clazz.setId(entity, Long.parseLong(entityId)); + } else { + entity = JsonConverter.objectFromJson(req.getReader(), this.clazz); + } + long userId = getUserId(req); + + if (commandCall != null) { + commandCall.setUserId(userId); + commandCall.setEntity(entity); + commandCall.check(); + } + + Context.getDataManager().remove(entity); + + if (commandCall != null) { + commandCall.after(); + } + + sendResponse(resp.getWriter(), true); + } + +} diff --git a/src/org/traccar/web/CommandServlet.java b/src/org/traccar/web/CommandServlet.java index 958f1a888..be2d50ccc 100644 --- a/src/org/traccar/web/CommandServlet.java +++ b/src/org/traccar/web/CommandServlet.java @@ -7,6 +7,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 { @@ -34,19 +35,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/DeviceServlet.java b/src/org/traccar/web/DeviceServlet.java index 8f983ad78..7900538bb 100644 --- a/src/org/traccar/web/DeviceServlet.java +++ b/src/org/traccar/web/DeviceServlet.java @@ -18,9 +18,11 @@ package org.traccar.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.traccar.Context; +import org.traccar.helper.CommandCall; import org.traccar.model.Device; +import org.traccar.model.User; -public class DeviceServlet extends BaseServlet { +public class DeviceServlet extends BaseServletResource<Device> { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { @@ -50,7 +52,8 @@ public class DeviceServlet extends BaseServlet { return true; } - private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { + @Override + protected void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { if (Boolean.parseBoolean(req.getParameter("all"))) { Context.getPermissionsManager().checkAdmin(getUserId(req)); sendResponse(resp.getWriter(), JsonConverter.arrayToJson( @@ -63,39 +66,57 @@ public class DeviceServlet extends BaseServlet { } else { userId = getUserId(req); } - Context.getPermissionsManager().checkUser(getUserId(req), userId); + Context.getPermissionsManager().check(User.class, getUserId(req), userId); sendResponse(resp.getWriter(), JsonConverter.arrayToJson( Context.getDataManager().getDevices(userId))); } } - private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); - long userId = getUserId(req); - Context.getDataManager().addDevice(device); - Context.getDataManager().linkDevice(userId, device.getId()); - Context.getPermissionsManager().refresh(); - sendResponse(resp.getWriter(), JsonConverter.objectToJson(device)); + @Override + protected void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { + super.add(req, resp, new CommandCall<Device>() { + + @Override + public void after() throws Exception { + Context.getDataManager().link(Device.class, getUserId(), getEntity().getId()); + Context.getPermissionsManager().refresh(); + } + + }); } - private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); - Context.getPermissionsManager().checkDevice(getUserId(req), device.getId()); - Context.getDataManager().updateDevice(device); - sendResponse(resp.getWriter(), true); + @Override + protected void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { + super.update(req, resp, new CommandCall<Device>() { + + @Override + public void check() throws Exception { + Context.getPermissionsManager().check(Device.class, getUserId(), getEntity().getId()); + } + + }); } - private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); - Context.getPermissionsManager().checkDevice(getUserId(req), device.getId()); - Context.getDataManager().removeDevice(device); - Context.getPermissionsManager().refresh(); - sendResponse(resp.getWriter(), true); + @Override + protected void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { + super.remove(req, resp, new CommandCall<Device>() { + + @Override + public void check() throws Exception { + Context.getPermissionsManager().check(Device.class, getUserId(), getEntity().getId()); + } + + @Override + public void after() throws Exception { + Context.getPermissionsManager().refresh(); + } + + }); } private void link(HttpServletRequest req, HttpServletResponse resp) throws Exception { Context.getPermissionsManager().checkAdmin(getUserId(req)); - Context.getDataManager().linkDevice( + Context.getDataManager().link(Device.class, Long.parseLong(req.getParameter("userId")), Long.parseLong(req.getParameter("deviceId"))); Context.getPermissionsManager().refresh(); @@ -104,7 +125,7 @@ public class DeviceServlet extends BaseServlet { private void unlink(HttpServletRequest req, HttpServletResponse resp) throws Exception { Context.getPermissionsManager().checkAdmin(getUserId(req)); - Context.getDataManager().unlinkDevice( + Context.getDataManager().unlink(Device.class, Long.parseLong(req.getParameter("userId")), Long.parseLong(req.getParameter("deviceId"))); Context.getPermissionsManager().refresh(); diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index c01ce8bd6..a8b68613b 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -34,8 +34,8 @@ 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; public final class JsonConverter { @@ -49,17 +49,15 @@ public final class JsonConverter { return DATE_FORMAT.parseDateTime(value).toDate(); } - public static <T extends Factory> T objectFromJson(Reader reader, T prototype) throws ParseException { + public static <T> T objectFromJson(Reader reader, Class<T> clazz) throws ParseException { try (JsonReader jsonReader = Json.createReader(reader)) { - return objectFromJson(jsonReader.readObject(), prototype); + return objectFromJson(jsonReader.readObject(), clazz); } } - public static <T extends Factory> T objectFromJson(JsonObject json, T prototype) { - T object = (T) prototype.create(); - + public static <T> T objectFromJson(JsonObject json, Class<T> clazz) { + T object = Clazz.newInstance(clazz); Method[] methods = object.getClass().getMethods(); - for (final Method method : methods) { if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { @@ -91,7 +89,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/PositionServlet.java b/src/org/traccar/web/PositionServlet.java index 796d6a81f..c63968251 100644 --- a/src/org/traccar/web/PositionServlet.java +++ b/src/org/traccar/web/PositionServlet.java @@ -23,8 +23,9 @@ import org.traccar.model.Position; import java.util.HashMap; import java.util.Map; +import org.traccar.model.Device; -public class PositionServlet extends BaseServlet { +public class PositionServlet extends BaseServletResource<Position> { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { @@ -42,9 +43,10 @@ public class PositionServlet extends BaseServlet { return true; } - private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { + @Override + protected void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { long deviceId = Long.parseLong(req.getParameter("deviceId")); - Context.getPermissionsManager().checkDevice(getUserId(req), deviceId); + Context.getPermissionsManager().check(Device.class, getUserId(req), deviceId); sendResponse(resp.getWriter(), JsonConverter.arrayToJson( Context.getDataManager().getPositions( getUserId(req), deviceId, @@ -59,7 +61,7 @@ public class PositionServlet extends BaseServlet { for (String deviceIdString : req.getParameterValues("devicesId")) { Long deviceId = Long.parseLong(deviceIdString); - Context.getPermissionsManager().checkDevice(userId, deviceId); + Context.getPermissionsManager().check(Device.class, userId, deviceId); Position position = Context.getConnectionManager().getLastPosition(deviceId); positions.put(deviceId.toString(), position); diff --git a/src/org/traccar/web/ServerServlet.java b/src/org/traccar/web/ServerServlet.java index 7ed096bc6..312876f36 100644 --- a/src/org/traccar/web/ServerServlet.java +++ b/src/org/traccar/web/ServerServlet.java @@ -18,16 +18,17 @@ package org.traccar.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.traccar.Context; +import org.traccar.helper.CommandCall; import org.traccar.model.Server; -public class ServerServlet extends BaseServlet { +public class ServerServlet extends BaseServletResource<Server> { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { switch (command) { case "/get": - get(resp); + get(req, resp); break; case "/update": update(req, resp); @@ -38,16 +39,22 @@ public class ServerServlet extends BaseServlet { return true; } - private void get(HttpServletResponse resp) throws Exception { - sendResponse(resp.getWriter(), JsonConverter.objectToJson( - Context.getDataManager().getServer())); + @Override + protected void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { + super.update(req, resp, new CommandCall() { + + @Override + public void check() { + Context.getPermissionsManager().checkAdmin(getUserId()); + } + + }); } - private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Server server = JsonConverter.objectFromJson(req.getReader(), new Server()); - Context.getPermissionsManager().checkAdmin(getUserId(req)); - Context.getDataManager().updateServer(server); - sendResponse(resp.getWriter(), true); + @Override + protected void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { + sendResponse(resp.getWriter(), JsonConverter.objectToJson( + Context.getDataManager().getServer())); } } diff --git a/src/org/traccar/web/UserServlet.java b/src/org/traccar/web/UserServlet.java index 6bd870d4d..f7ae19fa9 100644 --- a/src/org/traccar/web/UserServlet.java +++ b/src/org/traccar/web/UserServlet.java @@ -18,9 +18,10 @@ package org.traccar.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.traccar.Context; +import org.traccar.helper.CommandCall; import org.traccar.model.User; -public class UserServlet extends BaseServlet { +public class UserServlet extends BaseServletResource<User> { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { @@ -44,38 +45,64 @@ public class UserServlet extends BaseServlet { return true; } - private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { + @Override + protected void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { Context.getPermissionsManager().checkAdmin(getUserId(req)); sendResponse(resp.getWriter(), JsonConverter.arrayToJson( Context.getDataManager().getUsers())); } - private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); - Context.getPermissionsManager().checkUser(getUserId(req), user.getId()); - Context.getDataManager().addUser(user); - Context.getPermissionsManager().refresh(); - sendResponse(resp.getWriter(), JsonConverter.objectToJson(user)); - } + @Override + protected void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { + super.add(req, resp, new CommandCall<User>() { - private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); - if (user.getAdmin()) { - Context.getPermissionsManager().checkAdmin(getUserId(req)); - } else { - Context.getPermissionsManager().checkUser(getUserId(req), user.getId()); - } - Context.getDataManager().updateUser(user); - Context.getPermissionsManager().refresh(); - sendResponse(resp.getWriter(), true); + @Override + public void check() throws Exception { + Context.getPermissionsManager().check(User.class, getUserId(), getEntity().getId()); + } + + @Override + public void after() throws Exception { + Context.getPermissionsManager().refresh(); + } + }); } - private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); - Context.getPermissionsManager().checkUser(getUserId(req), user.getId()); - Context.getDataManager().removeUser(user); - Context.getPermissionsManager().refresh(); - sendResponse(resp.getWriter(), true); + @Override + protected void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { + super.update(req, resp, new CommandCall<User>() { + + @Override + public void check() { + if (getEntity().getAdmin()) { + Context.getPermissionsManager().checkAdmin(getUserId()); + } else { + Context.getPermissionsManager().check(User.class, getUserId(), getEntity().getId()); + } + } + + @Override + public void after() { + Context.getPermissionsManager().refresh(); + } + + }); } + @Override + protected void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { + super.remove(req, resp, new CommandCall<User>() { + + @Override + public void check() throws Exception { + Context.getPermissionsManager().check(User.class, getUserId(), getEntity().getId()); + } + + @Override + public void after() throws Exception { + Context.getPermissionsManager().refresh(); + } + + }); + } } diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 36c43736d..2e7e1a31b 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -102,10 +102,15 @@ public class WebServer { servletHandler.setContextPath("/api"); servletHandler.addServlet(new ServletHolder(new AsyncServlet()), "/async/*"); servletHandler.addServlet(new ServletHolder(new ServerServlet()), "/server/*"); + servletHandler.addServlet(new ServletHolder(new ServerServlet()), "/servers/*"); servletHandler.addServlet(new ServletHolder(new UserServlet()), "/user/*"); + servletHandler.addServlet(new ServletHolder(new UserServlet()), "/users/*"); servletHandler.addServlet(new ServletHolder(new DeviceServlet()), "/device/*"); + servletHandler.addServlet(new ServletHolder(new DeviceServlet()), "/devices/*"); servletHandler.addServlet(new ServletHolder(new PositionServlet()), "/position/*"); + servletHandler.addServlet(new ServletHolder(new PositionServlet()), "/positions/*"); servletHandler.addServlet(new ServletHolder(new CommandServlet()), "/command/*"); + servletHandler.addServlet(new ServletHolder(new CommandServlet()), "/commands/*"); servletHandler.addServlet(new ServletHolder(new MainServlet()), "/*"); handlers.addHandler(servletHandler); } |