aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/web/UserServlet.java
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/UserServlet.java
parentcfe237cf26c6309b8431b3b81d589628ae363804 (diff)
downloadtrackermap-server-f5a266953e53a7f1785bcb584759582621ec9de3.tar.gz
trackermap-server-f5a266953e53a7f1785bcb584759582621ec9de3.tar.bz2
trackermap-server-f5a266953e53a7f1785bcb584759582621ec9de3.zip
Add support to generic resources and refactor in classes to support types and commandpattern.
Diffstat (limited to 'src/org/traccar/web/UserServlet.java')
-rw-r--r--src/org/traccar/web/UserServlet.java77
1 files changed, 52 insertions, 25 deletions
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();
+ }
+
+ });
+ }
}