diff options
author | Anton Tananaev <anton@traccar.org> | 2022-07-04 14:30:11 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-07-04 14:30:11 -0700 |
commit | 8a5dbe0a1bd68c21d4c1e06a55e1190246774a1f (patch) | |
tree | 1af5bd998c1cf964d9b36c68466ea4772df7b3c6 /src/main/java/org/traccar/api | |
parent | 0112af2cf93743b2bcec0e16cd0d95bcc9713f3f (diff) | |
download | trackermap-server-8a5dbe0a1bd68c21d4c1e06a55e1190246774a1f.tar.gz trackermap-server-8a5dbe0a1bd68c21d4c1e06a55e1190246774a1f.tar.bz2 trackermap-server-8a5dbe0a1bd68c21d4c1e06a55e1190246774a1f.zip |
Implement CSV export (fix #2961)
Diffstat (limited to 'src/main/java/org/traccar/api')
-rw-r--r-- | src/main/java/org/traccar/api/resource/PositionResource.java | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/main/java/org/traccar/api/resource/PositionResource.java b/src/main/java/org/traccar/api/resource/PositionResource.java index 28f8eb600..b4c8d18b9 100644 --- a/src/main/java/org/traccar/api/resource/PositionResource.java +++ b/src/main/java/org/traccar/api/resource/PositionResource.java @@ -20,6 +20,7 @@ import org.traccar.helper.model.PositionUtil; import org.traccar.model.Device; import org.traccar.model.Position; import org.traccar.model.UserRestrictions; +import org.traccar.reports.CsvExportProvider; import org.traccar.reports.KmlExportProvider; import org.traccar.storage.StorageException; import org.traccar.storage.query.Columns; @@ -32,10 +33,11 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.io.ByteArrayOutputStream; +import javax.ws.rs.core.StreamingOutput; import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -49,6 +51,9 @@ public class PositionResource extends BaseResource { @Inject private KmlExportProvider kmlExportProvider; + @Inject + private CsvExportProvider csvExportProvider; + @GET public Collection<Position> getJson( @QueryParam("deviceId") long deviceId, @QueryParam("id") List<Long> positionIds, @@ -84,10 +89,33 @@ public class PositionResource extends BaseResource { @QueryParam("deviceId") long deviceId, @QueryParam("from") Date from, @QueryParam("to") Date to) throws StorageException { permissionsService.checkPermission(Device.class, getUserId(), deviceId); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - kmlExportProvider.generateKml(stream, deviceId, from, to); - return Response.ok(stream.toByteArray()) + StreamingOutput stream = output -> { + try { + kmlExportProvider.generate(output, deviceId, from, to); + } catch (StorageException e) { + throw new WebApplicationException(e); + } + }; + return Response.ok(stream) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=positions.kml").build(); } + @Path("csv") + @GET + @Produces("text/csv") + public Response getCsv( + @QueryParam("deviceId") long deviceId, + @QueryParam("from") Date from, @QueryParam("to") Date to) throws StorageException { + permissionsService.checkPermission(Device.class, getUserId(), deviceId); + StreamingOutput stream = output -> { + try { + csvExportProvider.generate(output, deviceId, from, to); + } catch (StorageException e) { + throw new WebApplicationException(e); + } + }; + return Response.ok(stream) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=positions.csv").build(); + } + } |