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 --- src/org/traccar/Context.java | 28 +++++ src/org/traccar/api/BaseObjectResource.java | 126 +++++++++++++++++++++ .../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 ++---- 9 files changed, 201 insertions(+), 263 deletions(-) create mode 100644 src/org/traccar/api/BaseObjectResource.java (limited to 'src/org') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index dfb8c5f47..75a7d179b 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -28,6 +28,7 @@ import org.eclipse.jetty.util.URIUtil; import org.traccar.database.AliasesManager; import org.traccar.database.CalendarManager; import org.traccar.database.AttributesManager; +import org.traccar.database.BaseObjectManager; import org.traccar.database.ConnectionManager; import org.traccar.database.DataManager; import org.traccar.database.DeviceManager; @@ -51,6 +52,14 @@ import org.traccar.geocoder.OpenCageGeocoder; import org.traccar.geocoder.Geocoder; import org.traccar.geolocation.UnwiredGeolocationProvider; import org.traccar.helper.Log; +import org.traccar.model.Attribute; +import org.traccar.model.BaseModel; +import org.traccar.model.Calendar; +import org.traccar.model.Device; +import org.traccar.model.Driver; +import org.traccar.model.Geofence; +import org.traccar.model.Group; +import org.traccar.model.User; import org.traccar.geolocation.GoogleGeolocationProvider; import org.traccar.geolocation.GeolocationProvider; import org.traccar.geolocation.MozillaGeolocationProvider; @@ -369,4 +378,23 @@ public final class Context { identityManager = testIdentityManager; } + public static BaseObjectManager getManager(Class clazz) { + if (clazz.equals(Device.class)) { + return (BaseObjectManager) deviceManager; + } else if (clazz.equals(Group.class)) { + return (BaseObjectManager) groupsManager; + } else if (clazz.equals(User.class)) { + return (BaseObjectManager) usersManager; + } else if (clazz.equals(Calendar.class)) { + return (BaseObjectManager) calendarManager; + } else if (clazz.equals(Attribute.class)) { + return (BaseObjectManager) attributesManager; + } else if (clazz.equals(Geofence.class)) { + return (BaseObjectManager) geofenceManager; + } else if (clazz.equals(Driver.class)) { + return (BaseObjectManager) driversManager; + } + return null; + } + } diff --git a/src/org/traccar/api/BaseObjectResource.java b/src/org/traccar/api/BaseObjectResource.java new file mode 100644 index 000000000..11e9a5d01 --- /dev/null +++ b/src/org/traccar/api/BaseObjectResource.java @@ -0,0 +1,126 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.api; + +import java.sql.SQLException; + +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; + +import org.traccar.Context; +import org.traccar.database.BaseObjectManager; +import org.traccar.database.ExtendedObjectManager; +import org.traccar.database.SimpleObjectManager; +import org.traccar.model.BaseModel; +import org.traccar.model.Device; +import org.traccar.model.Group; +import org.traccar.model.User; + +public class BaseObjectResource extends BaseResource { + + private Class baseClass; + + public BaseObjectResource(Class baseClass) { + this.baseClass = baseClass; + } + + @POST + public Response add(T entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + if (baseClass.equals(Device.class)) { + Context.getPermissionsManager().checkDeviceReadonly(getUserId()); + Context.getPermissionsManager().checkDeviceLimit(getUserId()); + } + + BaseObjectManager manager = Context.getManager(baseClass); + manager.addItem(entity); + + Context.getDataManager().linkObject(User.class, getUserId(), baseClass, entity.getId(), true); + + if (manager instanceof SimpleObjectManager) { + ((SimpleObjectManager) manager).refreshUserItems(); + } + if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) { + Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); + Context.getPermissionsManager().refreshAllExtendedPermissions(); + } + return Response.ok(entity).build(); + } + + @Path("{id}") + @PUT + public Response update(T entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + if (baseClass.equals(Device.class)) { + Context.getPermissionsManager().checkDeviceReadonly(getUserId()); + } + if (baseClass.equals(User.class)) { + User before = Context.getPermissionsManager().getUser(entity.getId()); + Context.getPermissionsManager().checkUserUpdate(getUserId(), before, (User) entity); + } + Context.getPermissionsManager().checkPermission(baseClass, getUserId(), entity.getId()); + + Context.getManager(baseClass).updateItem(entity); + + if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) { + Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); + Context.getPermissionsManager().refreshAllExtendedPermissions(); + } + if (baseClass.equals(User.class) && 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()); + if (baseClass.equals(Device.class)) { + Context.getPermissionsManager().checkDeviceReadonly(getUserId()); + } + Context.getPermissionsManager().checkPermission(baseClass, getUserId(), id); + + BaseObjectManager manager = Context.getManager(baseClass); + manager.removeItem(id); + + if (manager instanceof SimpleObjectManager) { + ((SimpleObjectManager) manager).refreshUserItems(); + } + if (manager instanceof ExtendedObjectManager) { + ((ExtendedObjectManager) manager).refreshExtendedPermissions(); + } + if (baseClass.equals(Group.class) || baseClass.equals(Device.class) || baseClass.equals(User.class)) { + Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); + if (baseClass.equals(User.class)) { + Context.getPermissionsManager().refreshAllUsersPermissions(); + } else { + Context.getPermissionsManager().refreshAllExtendedPermissions(); + } + } + // deprecated + if (baseClass.equals(Device.class)) { + Context.getAliasesManager().removeDevice(id); + } + return Response.noContent().build(); + } + +} 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 From 5446776933ff412361a3c7812c2bb6de9a78ccaf Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 27 Jul 2017 16:46:52 +0500 Subject: Optimize a few IFs --- src/org/traccar/Context.java | 2 +- src/org/traccar/api/BaseObjectResource.java | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 75a7d179b..306a37e83 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -378,7 +378,7 @@ public final class Context { identityManager = testIdentityManager; } - public static BaseObjectManager getManager(Class clazz) { + public static BaseObjectManager getManager(Class clazz) { if (clazz.equals(Device.class)) { return (BaseObjectManager) deviceManager; } else if (clazz.equals(Group.class)) { diff --git a/src/org/traccar/api/BaseObjectResource.java b/src/org/traccar/api/BaseObjectResource.java index 11e9a5d01..ea5d4966f 100644 --- a/src/org/traccar/api/BaseObjectResource.java +++ b/src/org/traccar/api/BaseObjectResource.java @@ -34,7 +34,7 @@ import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.model.User; -public class BaseObjectResource extends BaseResource { +public abstract class BaseObjectResource extends BaseResource { private Class baseClass; @@ -57,8 +57,7 @@ public class BaseObjectResource extends BaseResource { if (manager instanceof SimpleObjectManager) { ((SimpleObjectManager) manager).refreshUserItems(); - } - if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) { + } else if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) { Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); Context.getPermissionsManager().refreshAllExtendedPermissions(); } @@ -71,8 +70,7 @@ public class BaseObjectResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); if (baseClass.equals(Device.class)) { Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - } - if (baseClass.equals(User.class)) { + } else if (baseClass.equals(User.class)) { User before = Context.getPermissionsManager().getUser(entity.getId()); Context.getPermissionsManager().checkUserUpdate(getUserId(), before, (User) entity); } @@ -83,8 +81,7 @@ public class BaseObjectResource extends BaseResource { if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) { Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); Context.getPermissionsManager().refreshAllExtendedPermissions(); - } - if (baseClass.equals(User.class) && Context.getNotificationManager() != null) { + } else if (baseClass.equals(User.class) && Context.getNotificationManager() != null) { Context.getNotificationManager().refresh(); } return Response.ok(entity).build(); @@ -116,7 +113,7 @@ public class BaseObjectResource extends BaseResource { Context.getPermissionsManager().refreshAllExtendedPermissions(); } } - // deprecated + // Next should be removed with Attribute Aliases if (baseClass.equals(Device.class)) { Context.getAliasesManager().removeDevice(id); } -- cgit v1.2.3 From 34d02fbb63eb3d0cfbbcf63de50561831b0ab38d Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 28 Jul 2017 08:19:56 +0500 Subject: One more condition optimizing --- src/org/traccar/api/BaseObjectResource.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/api/BaseObjectResource.java b/src/org/traccar/api/BaseObjectResource.java index ea5d4966f..4475e3b4d 100644 --- a/src/org/traccar/api/BaseObjectResource.java +++ b/src/org/traccar/api/BaseObjectResource.java @@ -101,9 +101,9 @@ public abstract class BaseObjectResource extends BaseResour if (manager instanceof SimpleObjectManager) { ((SimpleObjectManager) manager).refreshUserItems(); - } - if (manager instanceof ExtendedObjectManager) { - ((ExtendedObjectManager) manager).refreshExtendedPermissions(); + if (manager instanceof ExtendedObjectManager) { + ((ExtendedObjectManager) manager).refreshExtendedPermissions(); + } } if (baseClass.equals(Group.class) || baseClass.equals(Device.class) || baseClass.equals(User.class)) { Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); -- cgit v1.2.3