From a4fb24732188e4e0b7cc44079eea02c85e142028 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 1 Dec 2015 14:37:26 +1300 Subject: Refactor base and other resource classes --- src/org/traccar/api/resource/DeviceResource.java | 64 +++++++++++++----------- src/org/traccar/api/resource/UserResource.java | 63 ++++++++++++----------- 2 files changed, 70 insertions(+), 57 deletions(-) (limited to 'src/org/traccar/api/resource') 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 { +public class DeviceResource extends BaseResource { @GET - @RolesAllowed(User.ROLE_ADMIN) - @Override - public Collection 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 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 { +public class UserResource extends BaseResource { @GET - @RolesAllowed(User.ROLE_ADMIN) - @Override - public Collection 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 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); + } } } -- cgit v1.2.3