From 89f5ca6340b71f01e57684ed4900c801e043a9c4 Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Fri, 30 Jun 2017 12:24:55 +0200 Subject: [NEW] add GET device to API for external monitoring --- src/org/traccar/api/resource/DeviceResource.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) (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 bfd05aad5..64d40f4af 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -31,6 +31,7 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import javax.ws.rs.WebApplicationException; import java.sql.SQLException; import java.util.Collection; @@ -73,6 +74,18 @@ public class DeviceResource extends BaseResource { return Response.ok(entity).build(); } + @Path("{id}") + @GET + public Response get(@PathParam("id") long id) throws SQLException { + Context.getPermissionsManager().checkDevice(getUserId(), id); + Device device = Context.getDeviceManager().getDeviceById(id); + if (device != null) { + return Response.ok(Context.getDeviceManager().getDeviceById(id)).build(); + } else { + throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build()); + } + } + @Path("{id}") @PUT public Response update(Device entity) throws SQLException { -- cgit v1.2.3 From 4fce9334c72acf8e7ef78c7b2c7b60380e77e6cb Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Sat, 1 Jul 2017 21:20:41 +0200 Subject: [FIX] get devices with IDs works like PositionResource --- src/org/traccar/api/resource/DeviceResource.java | 46 ++++++++++++------------ swagger.json | 27 +++++--------- 2 files changed, 31 insertions(+), 42 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 64d40f4af..0f75b5e33 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -31,10 +31,11 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.WebApplicationException; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; @Path("devices") @Produces(MediaType.APPLICATION_JSON) @@ -43,20 +44,31 @@ public class DeviceResource extends BaseResource { @GET public Collection get( - @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException { - if (all) { - if (Context.getPermissionsManager().isAdmin(getUserId())) { - return Context.getDeviceManager().getAllDevices(); + @QueryParam("all") boolean all, @QueryParam("userId") long userId, + @QueryParam("id") List deviceIds) throws SQLException { + if (deviceIds.isEmpty()) { + if (all) { + if (Context.getPermissionsManager().isAdmin(getUserId())) { + return Context.getDeviceManager().getAllDevices(); + } else { + Context.getPermissionsManager().checkManager(getUserId()); + return Context.getDeviceManager().getManagedDevices(getUserId()); + } } else { - Context.getPermissionsManager().checkManager(getUserId()); - return Context.getDeviceManager().getManagedDevices(getUserId()); + if (userId == 0) { + userId = getUserId(); + } + Context.getPermissionsManager().checkUser(getUserId(), userId); + return Context.getDeviceManager().getDevices(userId); } } else { - if (userId == 0) { - userId = getUserId(); + ArrayList devices = new ArrayList<>(); + for (Long deviceId : deviceIds) { + Device device = Context.getDeviceManager().getDeviceById(deviceId); + Context.getPermissionsManager().checkDevice(getUserId(), device.getId()); + devices.add(device); } - Context.getPermissionsManager().checkUser(getUserId(), userId); - return Context.getDeviceManager().getDevices(userId); + return devices; } } @@ -74,18 +86,6 @@ public class DeviceResource extends BaseResource { return Response.ok(entity).build(); } - @Path("{id}") - @GET - public Response get(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkDevice(getUserId(), id); - Device device = Context.getDeviceManager().getDeviceById(id); - if (device != null) { - return Response.ok(Context.getDeviceManager().getDeviceById(id)).build(); - } else { - throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build()); - } - } - @Path("{id}") @PUT public Response update(Device entity) throws SQLException { diff --git a/swagger.json b/swagger.json index 4f62bdf96..734e42eab 100644 --- a/swagger.json +++ b/swagger.json @@ -57,6 +57,14 @@ }, { "$ref": "#/parameters/userId" + }, + { + "name" : "id", + "in" : "query", + "description" : "To fetch one or more devices. Multiple params can be passed like `id=31&id=42`", + "required" : false, + "type" : "integer", + "collectionFormat" : "multi" } ], "responses": { @@ -92,25 +100,6 @@ } }, "/devices/{id}": { - "get": { - "summary": "Get a Device", - "parameters": [ - { - "$ref": "#/parameters/entityId" - }, - { - "$ref": "#/parameters/Device" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/Device" - } - } - } - }, "put": { "summary": "Update a Device", "parameters": [ -- cgit v1.2.3 From c67bf883bf0aa84293b94b000536c2061c3e00b2 Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Sun, 2 Jul 2017 10:20:50 +0200 Subject: [FIX] permission check before getting device, eleminate unnecessary variable. --- src/org/traccar/api/resource/DeviceResource.java | 5 ++--- 1 file changed, 2 insertions(+), 3 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 0f75b5e33..ce46b4e29 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -64,9 +64,8 @@ public class DeviceResource extends BaseResource { } else { ArrayList devices = new ArrayList<>(); for (Long deviceId : deviceIds) { - Device device = Context.getDeviceManager().getDeviceById(deviceId); - Context.getPermissionsManager().checkDevice(getUserId(), device.getId()); - devices.add(device); + Context.getPermissionsManager().checkDevice(getUserId(), deviceId); + devices.add(Context.getDeviceManager().getDeviceById(deviceId)); } return devices; } -- cgit v1.2.3