diff options
author | Abyss777 <abyss@fox5.ru> | 2016-08-08 11:55:27 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2016-08-08 11:55:27 +0500 |
commit | 16771c045a3d6fdffad322e55b6d346c93a03f71 (patch) | |
tree | 0990e6387c8143e2da90cf3cc0e4b0184c9bc8b7 | |
parent | 24346c8afd219c607e18306aa509f6029d3e850c (diff) | |
download | trackermap-server-16771c045a3d6fdffad322e55b6d346c93a03f71.tar.gz trackermap-server-16771c045a3d6fdffad322e55b6d346c93a03f71.tar.bz2 trackermap-server-16771c045a3d6fdffad322e55b6d346c93a03f71.zip |
- Used @Consumes and @Produces instead of direct header access
- Renamed General to Summary
- Other changes
-rw-r--r-- | src/org/traccar/api/BaseResource.java | 9 | ||||
-rw-r--r-- | src/org/traccar/api/resource/ReportResource.java | 85 | ||||
-rw-r--r-- | src/org/traccar/reports/Events.java | 6 | ||||
-rw-r--r-- | src/org/traccar/reports/ReportUtils.java | 2 | ||||
-rw-r--r-- | src/org/traccar/reports/Route.java | 6 | ||||
-rw-r--r-- | src/org/traccar/reports/Summary.java (renamed from src/org/traccar/reports/General.java) | 18 | ||||
-rw-r--r-- | src/org/traccar/reports/model/SummaryReport.java (renamed from src/org/traccar/reports/model/GeneralReport.java) | 8 | ||||
-rw-r--r-- | src/org/traccar/web/CsvBuilder.java | 2 |
8 files changed, 69 insertions, 67 deletions
diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java index 8259f8868..567b9735a 100644 --- a/src/org/traccar/api/BaseResource.java +++ b/src/org/traccar/api/BaseResource.java @@ -15,7 +15,6 @@ */ package org.traccar.api; -import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.SecurityContext; public class BaseResource { @@ -23,9 +22,6 @@ public class BaseResource { @javax.ws.rs.core.Context private SecurityContext securityContext; - @javax.ws.rs.core.Context - private HttpHeaders headers; - protected long getUserId() { UserPrincipal principal = (UserPrincipal) securityContext.getUserPrincipal(); if (principal != null) { @@ -33,9 +29,4 @@ public class BaseResource { } return 0; } - - protected String getAcceptHeader() { - return headers.getRequestHeaders().getFirst("Accept"); - } - } diff --git a/src/org/traccar/api/resource/ReportResource.java b/src/org/traccar/api/resource/ReportResource.java index 6c5772584..8682070b1 100644 --- a/src/org/traccar/api/resource/ReportResource.java +++ b/src/org/traccar/api/resource/ReportResource.java @@ -3,72 +3,83 @@ package org.traccar.api.resource; import java.sql.SQLException; import java.util.List; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; +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.core.Response.ResponseBuilder; import org.traccar.api.BaseResource; import org.traccar.reports.Events; -import org.traccar.reports.General; +import org.traccar.reports.Summary; import org.traccar.reports.Route; import org.traccar.web.JsonConverter; @Path("reports") +@Consumes("application/json") public class ReportResource extends BaseResource { @Path("route") @GET - public Response getRoute( + @Produces("application/json") + public Response getRouteJson( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - if (getAcceptHeader().equals("application/ms-excel")) { - ResponseBuilder response = Response.ok(Route.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type("application/ms-excel"); - return response.build(); - } - ResponseBuilder response = Response.ok(Route.getJson(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type(MediaType.APPLICATION_JSON); - return response.build(); + return Response.ok(Route.getJson(getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); + } + + @Path("route") + @GET + @Produces("application/ms-excel") + public Response getRouteCsv( + @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, + @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { + return Response.ok(Route.getCsv(getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); + } + + @Path("events") + @GET + @Produces("application/json") + public Response getEventsJson( + @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, + @QueryParam("type") final List<String> types, + @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { + return Response.ok(Events.getJson(getUserId(), deviceIds, groupIds, types, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); } @Path("events") @GET - public Response getEvents( + @Produces("application/ms-excel") + public Response getEventsCsv( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, @QueryParam("type") final List<String> types, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - if (getAcceptHeader().equals("application/ms-excel")) { - ResponseBuilder response = Response.ok(Events.getCsv(getUserId(), deviceIds, groupIds, - types, JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type("application/ms-excel"); - return response.build(); - } - ResponseBuilder response = Response.ok(Events.getJson(getUserId(), deviceIds, groupIds, types, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type(MediaType.APPLICATION_JSON); - return response.build(); + return Response.ok(Events.getCsv(getUserId(), deviceIds, groupIds, + types, JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); + } + + @Path("summary") + @GET + @Produces("application/json") + public Response getSummaryJson( + @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, + @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { + return Response.ok(Summary.getJson(getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); } - @Path("general") + @Path("summary") @GET - public Response getGeneral( + @Produces("application/ms-excel") + public Response getSummaryCsv( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - if (getAcceptHeader().equals("application/ms-excel")) { - ResponseBuilder response = Response.ok(General.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type("application/ms-excel"); - return response.build(); - } - ResponseBuilder response = Response.ok(General.getJson(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type(MediaType.APPLICATION_JSON); - return response.build(); + return Response.ok(Summary.getCsv(getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); } } diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index de4354371..9e14bd3db 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -20,7 +20,7 @@ public final class Events { public static String getJson(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Collection<String> types, Date from, Date to) throws SQLException { JsonObjectBuilder json = Json.createObjectBuilder(); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); for (String type : types) { json.add(String.valueOf(deviceId), JsonConverter.arrayToJson(Context.getDataManager() @@ -34,12 +34,12 @@ public final class Events { Collection<String> types, Date from, Date to) throws SQLException { CsvBuilder csv = new CsvBuilder(); csv.addHeaderLine(new Event()); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); for (String type : types) { csv.addArray(Context.getDataManager().getEvents(deviceId, type, from, to)); } } - return csv.get(); + return csv.build(); } } diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index c4c63e525..5041871f7 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -10,7 +10,7 @@ public final class ReportUtils { private ReportUtils() { } - public static Collection<Long> getReportedDevices(Collection<Long> deviceIds, Collection<Long> groupIds) { + public static Collection<Long> getDeviceList(Collection<Long> deviceIds, Collection<Long> groupIds) { Collection<Long> result = new ArrayList<>(); result.addAll(deviceIds); for (long groupId : groupIds) { diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index a77e3c5ff..7abe5c194 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -20,7 +20,7 @@ public final class Route { public static String getJson(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws SQLException { JsonObjectBuilder json = Json.createObjectBuilder(); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); json.add(String.valueOf(deviceId), JsonConverter.arrayToJson(Context.getDataManager() .getPositions(deviceId, from, to))); @@ -32,10 +32,10 @@ public final class Route { Date from, Date to) throws SQLException { CsvBuilder csv = new CsvBuilder(); csv.addHeaderLine(new Position()); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); csv.addArray(Context.getDataManager().getPositions(deviceId, from, to)); } - return csv.get(); + return csv.build(); } } diff --git a/src/org/traccar/reports/General.java b/src/org/traccar/reports/Summary.java index 8a2b3940b..474562002 100644 --- a/src/org/traccar/reports/General.java +++ b/src/org/traccar/reports/Summary.java @@ -12,17 +12,17 @@ import javax.json.JsonObjectBuilder; import org.traccar.Context; import org.traccar.helper.DistanceCalculator; import org.traccar.model.Position; -import org.traccar.reports.model.GeneralReport; +import org.traccar.reports.model.SummaryReport; import org.traccar.web.CsvBuilder; import org.traccar.web.JsonConverter; -public final class General { +public final class Summary { - private General() { + private Summary() { } - private static GeneralReport calculateGeneralResult(long deviceId, Date from, Date to) throws SQLException { - GeneralReport result = new GeneralReport(); + private static SummaryReport calculateGeneralResult(long deviceId, Date from, Date to) throws SQLException { + SummaryReport result = new SummaryReport(); Collection<Position> positions = Context.getDataManager().getPositions(deviceId, from, to); if (positions != null && !positions.isEmpty()) { result.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); @@ -46,7 +46,7 @@ public final class General { public static String getJson(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws SQLException { JsonObjectBuilder json = Json.createObjectBuilder(); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); json.add(String.valueOf(deviceId), JsonConverter.objectToJson(calculateGeneralResult(deviceId, from, to))); } @@ -56,11 +56,11 @@ public final class General { public static String getCsv(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws SQLException { CsvBuilder csv = new CsvBuilder(); - csv.addHeaderLine(new GeneralReport()); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + csv.addHeaderLine(new SummaryReport()); + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); csv.addLine(calculateGeneralResult(deviceId, from, to)); } - return csv.get(); + return csv.build(); } } diff --git a/src/org/traccar/reports/model/GeneralReport.java b/src/org/traccar/reports/model/SummaryReport.java index f60bb7238..aed3915f1 100644 --- a/src/org/traccar/reports/model/GeneralReport.java +++ b/src/org/traccar/reports/model/SummaryReport.java @@ -1,6 +1,6 @@ package org.traccar.reports.model; -public class GeneralReport { +public class SummaryReport { private String deviceName; public String getDeviceName() { @@ -10,7 +10,7 @@ public class GeneralReport { this.deviceName = deviceName; } - private double distance = 0; + private double distance; public double getDistance() { return distance; } @@ -22,7 +22,7 @@ public class GeneralReport { this.distance += distance; } - private double averageSpeed = 0; + private double averageSpeed; public double getAverageSpeed() { return averageSpeed; } @@ -30,7 +30,7 @@ public class GeneralReport { this.averageSpeed = averageSpeed; } - private double maxSpeed = 0; + private double maxSpeed; public double getMaxSpeed() { return maxSpeed; } diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java index d45d12873..fc0336513 100644 --- a/src/org/traccar/web/CsvBuilder.java +++ b/src/org/traccar/web/CsvBuilder.java @@ -161,7 +161,7 @@ public class CsvBuilder { } } - public String get() { + public String build() { return builder.toString(); } } |