From e8a758072e9368f6977880e1aa607351d7cf619a Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 27 Jul 2017 13:16:59 +0500 Subject: Implement BaseObjectResource generic and combine PUT/POST/DELETE functions --- .../traccar/api/resource/AttributeResource.java | 53 ++++------------------ src/org/traccar/api/resource/CalendarResource.java | 40 +++------------- src/org/traccar/api/resource/DeviceResource.java | 49 +++----------------- src/org/traccar/api/resource/DriverResource.java | 41 +++-------------- src/org/traccar/api/resource/GeofenceResource.java | 43 +++--------------- src/org/traccar/api/resource/GroupResource.java | 47 +++---------------- src/org/traccar/api/resource/UserResource.java | 37 +++------------ 7 files changed, 47 insertions(+), 263 deletions(-) (limited to 'src/org/traccar/api/resource') diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java index c12fcd9e6..55fd39fc6 100644 --- a/src/org/traccar/api/resource/AttributeResource.java +++ b/src/org/traccar/api/resource/AttributeResource.java @@ -22,29 +22,29 @@ import java.util.HashSet; import java.util.Set; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.AttributesManager; import org.traccar.model.Attribute; import org.traccar.model.Position; -import org.traccar.model.User; import org.traccar.processing.ComputedAttributesHandler; @Path("attributes/computed") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class AttributeResource extends BaseResource { +public class AttributeResource extends BaseObjectResource { + + public AttributeResource() { + super(Attribute.class); + } @GET public Collection get( @@ -85,15 +85,11 @@ public class AttributeResource extends BaseResource { } - private Response add(Attribute entity) throws SQLException { + @POST + @Path("test") + public Response test(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getAttributesManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getAttributesManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - private Response test(long deviceId, Attribute entity) { + Context.getPermissionsManager().checkDevice(getUserId(), deviceId); Position last = Context.getIdentityManager().getLastPosition(deviceId); if (last != null) { Object result = new ComputedAttributesHandler().computeAttribute(entity, last); @@ -114,33 +110,4 @@ public class AttributeResource extends BaseResource { } } - @POST - public Response post(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - if (deviceId != 0) { - Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - return test(deviceId, entity); - } else { - return add(entity); - } - } - - @Path("{id}") - @PUT - public Response update(Attribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Attribute.class, getUserId(), entity.getId()); - Context.getAttributesManager().updateItem(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Attribute.class, getUserId(), id); - Context.getAttributesManager().removeItem(id); - return Response.noContent().build(); - } - } diff --git a/src/org/traccar/api/resource/CalendarResource.java b/src/org/traccar/api/resource/CalendarResource.java index f8d78a847..d43a3b02e 100644 --- a/src/org/traccar/api/resource/CalendarResource.java +++ b/src/org/traccar/api/resource/CalendarResource.java @@ -21,27 +21,25 @@ import java.util.Collection; import java.util.Set; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.CalendarManager; import org.traccar.model.Calendar; -import org.traccar.model.User; @Path("calendars") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class CalendarResource extends BaseResource { +public class CalendarResource extends BaseObjectResource { + + public CalendarResource() { + super(Calendar.class); + } @GET public Collection get( @@ -66,30 +64,4 @@ public class CalendarResource extends BaseResource { return calendarManager.getItems(result); } - @POST - public Response add(Calendar entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getCalendarManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getCalendarManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @PUT - public Response update(Calendar entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Calendar.class, getUserId(), entity.getId()); - Context.getCalendarManager().updateItem(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Calendar.class, getUserId(), id); - Context.getCalendarManager().removeItem(id); - return Response.noContent().build(); - } } diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index a473b7bde..1c2c653a4 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -16,19 +16,15 @@ package org.traccar.api.resource; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.DeviceManager; import org.traccar.model.Device; import org.traccar.model.DeviceTotalDistance; -import org.traccar.model.User; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @@ -43,7 +39,11 @@ import java.util.Set; @Path("devices") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class DeviceResource extends BaseResource { +public class DeviceResource extends BaseObjectResource { + + public DeviceResource() { + super(Device.class); + } @GET public Collection get( @@ -80,43 +80,6 @@ public class DeviceResource extends BaseResource { return deviceManager.getItems(result); } - @POST - public Response add(Device entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceLimit(getUserId()); - Context.getDeviceManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @PUT - public Response update(Device entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getId()); - Context.getDeviceManager().updateItem(entity); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), id); - Context.getDeviceManager().removeItem(id); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - Context.getAliasesManager().removeDevice(id); - return Response.noContent().build(); - } - @Path("{id}/distance") @PUT public Response updateTotalDistance(DeviceTotalDistance entity) throws SQLException { diff --git a/src/org/traccar/api/resource/DriverResource.java b/src/org/traccar/api/resource/DriverResource.java index d44979998..185f4a0dc 100644 --- a/src/org/traccar/api/resource/DriverResource.java +++ b/src/org/traccar/api/resource/DriverResource.java @@ -22,27 +22,25 @@ import java.util.HashSet; import java.util.Set; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.DriversManager; import org.traccar.model.Driver; -import org.traccar.model.User; @Path("drivers") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class DriverResource extends BaseResource { +public class DriverResource extends BaseObjectResource { + + public DriverResource() { + super(Driver.class); + } @GET public Collection get( @@ -83,31 +81,4 @@ public class DriverResource extends BaseResource { } - @POST - public Response add(Driver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getDriversManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getDriversManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @PUT - public Response update(Driver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Driver.class, getUserId(), entity.getId()); - Context.getDriversManager().updateItem(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Driver.class, getUserId(), id); - Context.getDriversManager().removeItem(id); - return Response.noContent().build(); - } - } diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java index 9a110fbcf..d07810732 100644 --- a/src/org/traccar/api/resource/GeofenceResource.java +++ b/src/org/traccar/api/resource/GeofenceResource.java @@ -16,22 +16,16 @@ package org.traccar.api.resource; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.GeofenceManager; import org.traccar.model.Geofence; -import org.traccar.model.User; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import java.sql.SQLException; import java.util.Collection; @@ -41,7 +35,11 @@ import java.util.Set; @Path("geofences") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class GeofenceResource extends BaseResource { +public class GeofenceResource extends BaseObjectResource { + + public GeofenceResource() { + super(Geofence.class); + } @GET public Collection get( @@ -79,34 +77,5 @@ public class GeofenceResource extends BaseResource { result.retainAll(geofenceManager.getDeviceItems(deviceId)); } return geofenceManager.getItems(result); - - } - - @POST - public Response add(Geofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getGeofenceManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getGeofenceManager().refreshUserItems(); - return Response.ok(entity).build(); } - - @Path("{id}") - @PUT - public Response update(Geofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), entity.getId()); - Context.getGeofenceManager().updateItem(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), id); - Context.getGeofenceManager().removeItem(id); - return Response.noContent().build(); - } - } diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java index 4482f06c0..2be4e6492 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -16,22 +16,16 @@ package org.traccar.api.resource; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.GroupsManager; import org.traccar.model.Group; -import org.traccar.model.User; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import java.sql.SQLException; import java.util.Collection; import java.util.Set; @@ -39,7 +33,11 @@ import java.util.Set; @Path("groups") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class GroupResource extends BaseResource { +public class GroupResource extends BaseObjectResource { + + public GroupResource() { + super(Group.class); + } @GET public Collection get( @@ -62,37 +60,4 @@ public class GroupResource extends BaseResource { } return groupsManager.getItems(result); } - - @POST - public Response add(Group entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getGroupsManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @PUT - public Response update(Group entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getId()); - Context.getGroupsManager().updateItem(entity); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), id); - Context.getGroupsManager().removeItem(id); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.noContent().build(); - } - } diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index deb2dd2b4..b22e01216 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -16,19 +16,16 @@ package org.traccar.api.resource; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.UsersManager; import org.traccar.model.ManagedUser; import org.traccar.model.User; import javax.annotation.security.PermitAll; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @@ -41,7 +38,11 @@ import java.util.Set; @Path("users") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class UserResource extends BaseResource { +public class UserResource extends BaseObjectResource { + + public UserResource() { + super(User.class); + } @GET public Collection get(@QueryParam("userId") long userId) throws SQLException { @@ -61,6 +62,7 @@ public class UserResource extends BaseResource { return usersManager.getItems(result); } + @Override @PermitAll @POST public Response add(User entity) throws SQLException { @@ -89,29 +91,4 @@ public class UserResource extends BaseResource { return Response.ok(entity).build(); } - @Path("{id}") - @PUT - public Response update(User entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - User before = Context.getPermissionsManager().getUser(entity.getId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getId()); - Context.getPermissionsManager().checkUserUpdate(getUserId(), before, entity); - Context.getUsersManager().updateItem(entity); - if (Context.getNotificationManager() != null) { - Context.getNotificationManager().refresh(); - } - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), id); - Context.getUsersManager().removeItem(id); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllUsersPermissions(); - return Response.noContent().build(); - } - } -- cgit v1.2.3