From 16771c045a3d6fdffad322e55b6d346c93a03f71 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 8 Aug 2016 11:55:27 +0500 Subject: - Used @Consumes and @Produces instead of direct header access - Renamed General to Summary - Other changes --- src/org/traccar/api/BaseResource.java | 9 --- src/org/traccar/api/resource/ReportResource.java | 85 +++++++++++++----------- 2 files changed, 48 insertions(+), 46 deletions(-) (limited to 'src/org/traccar/api') 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 deviceIds, @QueryParam("groupId") final List 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 deviceIds, @QueryParam("groupId") final List 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 deviceIds, @QueryParam("groupId") final List groupIds, + @QueryParam("type") final List 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 deviceIds, @QueryParam("groupId") final List groupIds, @QueryParam("type") final List 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 deviceIds, @QueryParam("groupId") final List 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 deviceIds, @QueryParam("groupId") final List 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(); } } -- cgit v1.2.3