aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/api')
-rw-r--r--src/org/traccar/api/BaseResource.java95
-rw-r--r--src/org/traccar/api/resource/DeviceResource.java64
-rw-r--r--src/org/traccar/api/resource/UserResource.java63
3 files changed, 71 insertions, 151 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);
+ }
}
}