aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/web
diff options
context:
space:
mode:
authorRafael Guterres <guterresrafael@gmail.com>2015-11-21 01:57:46 -0200
committerRafael Guterres <guterresrafael@gmail.com>2015-11-21 01:57:46 -0200
commitf5a266953e53a7f1785bcb584759582621ec9de3 (patch)
tree06aed01befc06db1384d731d5d17ee897335c302 /src/org/traccar/web
parentcfe237cf26c6309b8431b3b81d589628ae363804 (diff)
downloadtraccar-server-f5a266953e53a7f1785bcb584759582621ec9de3.tar.gz
traccar-server-f5a266953e53a7f1785bcb584759582621ec9de3.tar.bz2
traccar-server-f5a266953e53a7f1785bcb584759582621ec9de3.zip
Add support to generic resources and refactor in classes to support types and commandpattern.
Diffstat (limited to 'src/org/traccar/web')
-rw-r--r--src/org/traccar/web/BaseServlet.java36
-rw-r--r--src/org/traccar/web/BaseServletResource.java169
-rw-r--r--src/org/traccar/web/CommandServlet.java9
-rw-r--r--src/org/traccar/web/DeviceServlet.java67
-rw-r--r--src/org/traccar/web/JsonConverter.java13
-rw-r--r--src/org/traccar/web/MainServlet.java8
-rw-r--r--src/org/traccar/web/PositionServlet.java10
-rw-r--r--src/org/traccar/web/ServerServlet.java27
-rw-r--r--src/org/traccar/web/UserServlet.java77
-rw-r--r--src/org/traccar/web/WebServer.java5
10 files changed, 315 insertions, 106 deletions
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 b19894dd6..d39e6b488 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);
}