diff options
-rw-r--r-- | src/org/traccar/api/BaseResource.java | 95 | ||||
-rw-r--r-- | src/org/traccar/api/resource/DeviceResource.java | 64 | ||||
-rw-r--r-- | src/org/traccar/api/resource/UserResource.java | 63 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 72 |
4 files changed, 86 insertions, 208 deletions
diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java index 9b0c2cf79..5a05c6732 100644 --- a/src/org/traccar/api/BaseResource.java +++ b/src/org/traccar/api/BaseResource.java @@ -15,98 +15,5 @@ */ package org.traccar.api; -import java.sql.SQLException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.SecurityContext; -import org.traccar.Context; -import org.traccar.helper.Clazz; -import org.traccar.model.User; - -public class BaseResource<T> { - - private static final String ERROR_KEY = "error"; - - private final Class<T> clazz = Clazz.getGenericArgumentType(getClass()); - - @javax.ws.rs.core.Context - private SecurityContext securityContext; - - private static Map<String, String> getError(Exception e) { - Map<String, String> error = new HashMap<>(); - error.put(ERROR_KEY, e.getMessage()); - return error; - } - - public Collection<T> getEntities() { - Collection<T> collection; - try { - collection = Context.getDataManager().get(clazz); - } catch (SQLException e) { - throw new WebApplicationException( - Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build()); - } - if (collection == null || collection.isEmpty()) { - throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build()); - } else { - return collection; - } - } - - public T getEntity(long id) { - validateSecurityContext(User.ROLE_USER, id); - T entity = Clazz.newInstance(clazz); - try { - Clazz.setId(entity, id); - entity = Context.getDataManager().get(entity); - } catch (Exception e) { - throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build()); - } - if (entity == null) { - throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build()); - } else { - return entity; - } - } - - public Response postEntity(T entity) { - try { - Context.getDataManager().add(entity); - return Response.status(Response.Status.OK).entity(entity).build(); - } catch (Exception e) { - return Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build(); - } - } - - public Response putEntity(long id, T entity) { - try { - Clazz.setId(entity, id); - Context.getDataManager().update(entity); - return Response.status(Response.Status.OK).entity(entity).build(); - } catch (Exception e) { - return Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build(); - } - } - - public Response deleteEntity(long id) { - try { - T entity = Clazz.newInstance(clazz); - Clazz.setId(entity, id); - Context.getDataManager().remove(entity); - return Response.status(Response.Status.NO_CONTENT).build(); - } catch (Exception e) { - return Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build(); - } - } - - private void validateSecurityContext(String role, long id) { - UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal(); - if (!securityContext.isUserInRole(role) && !userPrincipal.getId().equals(id)) { - throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).build()); - } - } - +public class BaseResource { } diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 50589171d..00b77e16c 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -15,9 +15,11 @@ */ package org.traccar.api.resource; +import org.traccar.Context; import org.traccar.api.BaseResource; + +import java.sql.SQLException; import java.util.Collection; -import javax.annotation.security.RolesAllowed; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -26,52 +28,56 @@ import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.traccar.model.Device; -import org.traccar.model.User; @Path("devices") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class DeviceResource extends BaseResource<Device> { +public class DeviceResource extends BaseResource { @GET - @RolesAllowed(User.ROLE_ADMIN) - @Override - public Collection<Device> getEntities() { - return super.getEntities(); - } - - @GET - @Path("{id}") - @RolesAllowed(User.ROLE_USER) - @Override - public Device getEntity(@PathParam("id") long id) { - return super.getEntity(id); + public Collection<Device> get() { + try { + return Context.getDataManager().getAllDevices(); + } catch (SQLException e) { + throw new WebApplicationException(e); + } } @POST - @RolesAllowed(User.ROLE_USER) - @Override - public Response postEntity(Device entity) { - return super.postEntity(entity); + public Response add(Device entity) { + try { + Context.getDataManager().addDevice(entity); + return Response.ok(entity).build(); + } catch (SQLException e) { + throw new WebApplicationException(e); + } } - @PUT @Path("{id}") - @RolesAllowed(User.ROLE_USER) - @Override - public Response putEntity(@PathParam("id") long id, Device entity) { - return super.putEntity(id, entity); + @PUT + public Response update(@PathParam("id") long id, Device entity) { + try { + entity.setId(id); + Context.getDataManager().updateDevice(entity); + return Response.ok(entity).build(); + } catch (SQLException e) { + throw new WebApplicationException(e); + } } - @DELETE @Path("{id}") - @RolesAllowed(User.ROLE_USER) - @Override - public Response deleteEntity(@PathParam("id") long id) { - return super.deleteEntity(id); + @DELETE + public Response remove(@PathParam("id") long id) { + try { + Context.getDataManager().removeDevice(id); + return Response.noContent().build(); + } catch (SQLException e) { + throw new WebApplicationException(e); + } } } diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index 8e17787c2..43bef891f 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -15,8 +15,8 @@ */ package org.traccar.api.resource; +import java.sql.SQLException; import java.util.Collection; -import javax.annotation.security.RolesAllowed; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -25,52 +25,59 @@ import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; + +import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.User; @Path("users") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class UserResource extends BaseResource<User> { +public class UserResource extends BaseResource { @GET - @RolesAllowed(User.ROLE_ADMIN) - @Override - public Collection<User> getEntities() { - return super.getEntities(); - } - - @GET - @Path("{id}") - @RolesAllowed(User.ROLE_USER) - @Override - public User getEntity(@PathParam("id") long id) { - return super.getEntity(id); + public Collection<User> get() { + try { + return Context.getDataManager().getUsers(); + } catch (SQLException e) { + throw new WebApplicationException(e); + } } @POST - @RolesAllowed(User.ROLE_USER) - @Override - public Response postEntity(User entity) { - return super.postEntity(entity); + public Response add(User entity) { + try { + Context.getDataManager().addUser(entity); + return Response.ok(entity).build(); + } catch (SQLException e) { + throw new WebApplicationException(e); + } } - @PUT @Path("{id}") - @RolesAllowed(User.ROLE_USER) - @Override - public Response putEntity(@PathParam("id") long id, User entity) { - return super.putEntity(id, entity); + @PUT + public Response update(@PathParam("id") long id, User entity) { + try { + entity.setId(id); + Context.getDataManager().updateUser(entity); + return Response.ok(entity).build(); + } catch (SQLException e) { + throw new WebApplicationException(e); + } } - @DELETE @Path("{id}") - @RolesAllowed(User.ROLE_USER) - @Override - public Response deleteEntity(@PathParam("id") long id) { - return super.deleteEntity(id); + @DELETE + public Response remove(@PathParam("id") long id) { + try { + Context.getDataManager().removeUser(id); + return Response.noContent().build(); + } catch (SQLException e) { + throw new WebApplicationException(e); + } } } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 31d7155d3..f01280836 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -278,12 +278,19 @@ public class DataManager implements IdentityManager { } } + @Deprecated public void removeUser(User user) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.deleteUser")) .setObject(user) .executeUpdate(); } + public void removeUser(long userId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.deleteUser")) + .setLong("id", userId) + .executeUpdate(); + } + public Collection<Permission> getPermissions() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.getPermissionsAll")) .executeQuery(new Permission()); @@ -318,6 +325,7 @@ public class DataManager implements IdentityManager { .executeUpdate(); } + @Deprecated public void removeDevice(Device device) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.deleteDevice")) .setObject(device) @@ -325,6 +333,13 @@ public class DataManager implements IdentityManager { AsyncServlet.sessionRefreshDevice(device.getId()); } + public void removeDevice(long deviceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.deleteDevice")) + .setLong("id", deviceId) + .executeUpdate(); + AsyncServlet.sessionRefreshDevice(deviceId); + } + public void linkDevice(long userId, long deviceId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkDevice")) .setLong("userId", userId) @@ -387,61 +402,4 @@ public class DataManager implements IdentityManager { .executeUpdate(); } - public <T> Collection<T> get(Class<T> clazz) throws SQLException { - if (clazz.equals(User.class)) { - return (Collection<T>) getUsers(); - } else if (clazz.equals(Device.class)) { - return (Collection<T>) getAllDevices(); - } - return null; - } - - public <T> T get(T entity) throws Exception { - if (entity instanceof User) { - return (T) getUser(Clazz.getId(entity)); - } else if (entity instanceof Device) { - return (T) getDeviceById(Clazz.getId(entity)); - } - return null; - } - - 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); - } - } } |