diff options
Diffstat (limited to 'src/main/java/org/traccar/api')
12 files changed, 119 insertions, 68 deletions
diff --git a/src/main/java/org/traccar/api/AsyncSocket.java b/src/main/java/org/traccar/api/AsyncSocket.java index 906d16b5b..b2ff5031a 100644 --- a/src/main/java/org/traccar/api/AsyncSocket.java +++ b/src/main/java/org/traccar/api/AsyncSocket.java @@ -39,7 +39,7 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U private static final String KEY_POSITIONS = "positions"; private static final String KEY_EVENTS = "events"; - private long userId; + private final long userId; public AsyncSocket(long userId) { this.userId = userId; diff --git a/src/main/java/org/traccar/api/AsyncSocketServlet.java b/src/main/java/org/traccar/api/AsyncSocketServlet.java index 9318b6fc6..51fbfb478 100644 --- a/src/main/java/org/traccar/api/AsyncSocketServlet.java +++ b/src/main/java/org/traccar/api/AsyncSocketServlet.java @@ -15,9 +15,6 @@ */ package org.traccar.api; -import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest; -import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse; -import org.eclipse.jetty.websocket.servlet.WebSocketCreator; import org.eclipse.jetty.websocket.servlet.WebSocketServlet; import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; import org.traccar.Context; @@ -30,15 +27,12 @@ public class AsyncSocketServlet extends WebSocketServlet { @Override public void configure(WebSocketServletFactory factory) { factory.getPolicy().setIdleTimeout(Context.getConfig().getLong("web.timeout", ASYNC_TIMEOUT)); - factory.setCreator(new WebSocketCreator() { - @Override - public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) { - if (req.getSession() != null) { - long userId = (Long) req.getSession().getAttribute(SessionResource.USER_ID_KEY); - return new AsyncSocket(userId); - } else { - return null; - } + factory.setCreator((req, resp) -> { + if (req.getSession() != null) { + long userId = (Long) req.getSession().getAttribute(SessionResource.USER_ID_KEY); + return new AsyncSocket(userId); + } else { + return null; } }); } diff --git a/src/main/java/org/traccar/api/BaseObjectResource.java b/src/main/java/org/traccar/api/BaseObjectResource.java index c4206f385..bc7d3c466 100644 --- a/src/main/java/org/traccar/api/BaseObjectResource.java +++ b/src/main/java/org/traccar/api/BaseObjectResource.java @@ -44,7 +44,7 @@ import org.traccar.model.User; public abstract class BaseObjectResource<T extends BaseModel> extends BaseResource { - private Class<T> baseClass; + private final Class<T> baseClass; public BaseObjectResource(Class<T> baseClass) { this.baseClass = baseClass; diff --git a/src/main/java/org/traccar/api/DateParameterConverterProvider.java b/src/main/java/org/traccar/api/DateParameterConverterProvider.java new file mode 100644 index 000000000..f07ece984 --- /dev/null +++ b/src/main/java/org/traccar/api/DateParameterConverterProvider.java @@ -0,0 +1,52 @@ +/* + * Copyright 2020 Anton Tananaev (anton@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 org.traccar.helper.DateUtil; + +import javax.ws.rs.ext.ParamConverter; +import javax.ws.rs.ext.ParamConverterProvider; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Date; + +public class DateParameterConverterProvider implements ParamConverterProvider { + + public static class DateParameterConverter implements ParamConverter<Date> { + + @Override + public Date fromString(String value) { + return value != null ? DateUtil.parseDate(value) : null; + } + + @Override + public String toString(Date value) { + return value != null ? DateUtil.formatDate(value) : null; + } + + } + + @Override + public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { + if (Date.class.equals(rawType)) { + @SuppressWarnings("unchecked") + ParamConverter<T> paramConverter = (ParamConverter<T>) new DateParameterConverter(); + return paramConverter; + } + return null; + } + +} diff --git a/src/main/java/org/traccar/api/HealthCheckService.java b/src/main/java/org/traccar/api/HealthCheckService.java index 1e8f0d731..c9ce5a6c9 100644 --- a/src/main/java/org/traccar/api/HealthCheckService.java +++ b/src/main/java/org/traccar/api/HealthCheckService.java @@ -20,6 +20,7 @@ import com.sun.jna.Native; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; +import org.traccar.config.Keys; import java.util.TimerTask; @@ -33,7 +34,7 @@ public class HealthCheckService { private long period; public HealthCheckService() { - if (Context.getConfig().getBoolean("web.healthCheck") + if (!Context.getConfig().getBoolean(Keys.WEB_DISABLE_HEALTH_CHECK) && System.getProperty("os.name").toLowerCase().startsWith("linux")) { try { systemD = Native.load("systemd", SystemD.class); diff --git a/src/main/java/org/traccar/api/UserPrincipal.java b/src/main/java/org/traccar/api/UserPrincipal.java index 80e92c2dd..175bca391 100644 --- a/src/main/java/org/traccar/api/UserPrincipal.java +++ b/src/main/java/org/traccar/api/UserPrincipal.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2020 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,7 @@ import java.security.Principal; public class UserPrincipal implements Principal { - private long userId; + private final long userId; public UserPrincipal(long userId) { this.userId = userId; diff --git a/src/main/java/org/traccar/api/resource/DeviceResource.java b/src/main/java/org/traccar/api/resource/DeviceResource.java index f9c9a139d..7006cdb84 100644 --- a/src/main/java/org/traccar/api/resource/DeviceResource.java +++ b/src/main/java/org/traccar/api/resource/DeviceResource.java @@ -52,7 +52,7 @@ public class DeviceResource extends BaseObjectResource<Device> { @QueryParam("uniqueId") List<String> uniqueIds, @QueryParam("id") List<Long> deviceIds) throws SQLException { DeviceManager deviceManager = Context.getDeviceManager(); - Set<Long> result = null; + Set<Long> result; if (all) { if (Context.getPermissionsManager().getUserAdmin(getUserId())) { result = deviceManager.getAllItems(); diff --git a/src/main/java/org/traccar/api/resource/PositionResource.java b/src/main/java/org/traccar/api/resource/PositionResource.java index 67aa6dd32..e93feaccf 100644 --- a/src/main/java/org/traccar/api/resource/PositionResource.java +++ b/src/main/java/org/traccar/api/resource/PositionResource.java @@ -17,7 +17,6 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; -import org.traccar.helper.DateUtil; import org.traccar.model.Position; import org.traccar.web.CsvBuilder; import org.traccar.web.GpxBuilder; @@ -35,6 +34,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.List; @Path("positions") @@ -50,7 +50,7 @@ public class PositionResource extends BaseResource { @GET public Collection<Position> getJson( @QueryParam("deviceId") long deviceId, @QueryParam("id") List<Long> positionIds, - @QueryParam("from") String from, @QueryParam("to") String to) + @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { if (!positionIds.isEmpty()) { ArrayList<Position> positions = new ArrayList<>(); @@ -65,8 +65,7 @@ public class PositionResource extends BaseResource { } else { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); if (from != null && to != null) { - return Context.getDataManager().getPositions( - deviceId, DateUtil.parseDate(from), DateUtil.parseDate(to)); + return Context.getDataManager().getPositions(deviceId, from, to); } else { return Collections.singleton(Context.getDeviceManager().getLastPosition(deviceId)); } @@ -76,25 +75,23 @@ public class PositionResource extends BaseResource { @GET @Produces(TEXT_CSV) public Response getCsv( - @QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to) + @QueryParam("deviceId") long deviceId, @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); CsvBuilder csv = new CsvBuilder(); csv.addHeaderLine(new Position()); - csv.addArray(Context.getDataManager().getPositions( - deviceId, DateUtil.parseDate(from), DateUtil.parseDate(to))); + csv.addArray(Context.getDataManager().getPositions(deviceId, from, to)); return Response.ok(csv.build()).header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_CSV).build(); } @GET @Produces(GPX) public Response getGpx( - @QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to) + @QueryParam("deviceId") long deviceId, @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); GpxBuilder gpx = new GpxBuilder(Context.getIdentityManager().getById(deviceId).getName()); - gpx.addPositions(Context.getDataManager().getPositions( - deviceId, DateUtil.parseDate(from), DateUtil.parseDate(to))); + gpx.addPositions(Context.getDataManager().getPositions(deviceId, from, to)); return Response.ok(gpx.build()).header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_GPX).build(); } diff --git a/src/main/java/org/traccar/api/resource/ReportResource.java b/src/main/java/org/traccar/api/resource/ReportResource.java index d371cf987..7347bfd64 100644 --- a/src/main/java/org/traccar/api/resource/ReportResource.java +++ b/src/main/java/org/traccar/api/resource/ReportResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org) * Copyright 2016 - 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.sql.SQLException; import java.util.Collection; +import java.util.Date; import java.util.List; import javax.activation.DataHandler; @@ -39,7 +40,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; import org.traccar.api.BaseResource; -import org.traccar.helper.DateUtil; +import org.traccar.helper.LogAction; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.reports.Events; @@ -97,9 +98,9 @@ public class ReportResource extends BaseResource { @GET public Collection<Position> getRoute( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, - @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - return Route.getObjects(getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { + LogAction.logReport(getUserId(), "route", from, to, deviceIds, groupIds); + return Route.getObjects(getUserId(), deviceIds, groupIds, from, to); } @Path("route") @@ -107,11 +108,11 @@ public class ReportResource extends BaseResource { @Produces(XLSX) public Response getRouteExcel( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, - @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail) + @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail) throws SQLException, IOException { return executeReport(getUserId(), mail, stream -> { - Route.getExcel(stream, getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + LogAction.logReport(getUserId(), "route", from, to, deviceIds, groupIds); + Route.getExcel(stream, getUserId(), deviceIds, groupIds, from, to); }); } @@ -120,9 +121,9 @@ public class ReportResource extends BaseResource { public Collection<Event> getEvents( @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 Events.getObjects(getUserId(), deviceIds, groupIds, types, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { + LogAction.logReport(getUserId(), "events", from, to, deviceIds, groupIds); + return Events.getObjects(getUserId(), deviceIds, groupIds, types, from, to); } @Path("events") @@ -131,11 +132,11 @@ public class ReportResource extends BaseResource { public Response getEventsExcel( @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, @QueryParam("mail") boolean mail) + @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail) throws SQLException, IOException { return executeReport(getUserId(), mail, stream -> { - Events.getExcel(stream, getUserId(), deviceIds, groupIds, types, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + LogAction.logReport(getUserId(), "events", from, to, deviceIds, groupIds); + Events.getExcel(stream, getUserId(), deviceIds, groupIds, types, from, to); }); } @@ -143,9 +144,10 @@ public class ReportResource extends BaseResource { @GET public Collection<SummaryReport> getSummary( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, - @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - return Summary.getObjects(getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("daily") boolean daily) + throws SQLException { + LogAction.logReport(getUserId(), "summary", from, to, deviceIds, groupIds); + return Summary.getObjects(getUserId(), deviceIds, groupIds, from, to, daily); } @Path("summary") @@ -153,11 +155,12 @@ public class ReportResource extends BaseResource { @Produces(XLSX) public Response getSummaryExcel( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, - @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail) + @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("daily") boolean daily, + @QueryParam("mail") boolean mail) throws SQLException, IOException { return executeReport(getUserId(), mail, stream -> { - Summary.getExcel(stream, getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + LogAction.logReport(getUserId(), "summary", from, to, deviceIds, groupIds); + Summary.getExcel(stream, getUserId(), deviceIds, groupIds, from, to, daily); }); } @@ -166,9 +169,9 @@ public class ReportResource extends BaseResource { @Produces(MediaType.APPLICATION_JSON) public Collection<TripReport> getTrips( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, - @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - return Trips.getObjects(getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { + LogAction.logReport(getUserId(), "trips", from, to, deviceIds, groupIds); + return Trips.getObjects(getUserId(), deviceIds, groupIds, from, to); } @Path("trips") @@ -176,11 +179,11 @@ public class ReportResource extends BaseResource { @Produces(XLSX) public Response getTripsExcel( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, - @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail) + @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail) throws SQLException, IOException { return executeReport(getUserId(), mail, stream -> { - Trips.getExcel(stream, getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + LogAction.logReport(getUserId(), "trips", from, to, deviceIds, groupIds); + Trips.getExcel(stream, getUserId(), deviceIds, groupIds, from, to); }); } @@ -189,9 +192,9 @@ public class ReportResource extends BaseResource { @Produces(MediaType.APPLICATION_JSON) public Collection<StopReport> getStops( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, - @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - return Stops.getObjects(getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { + LogAction.logReport(getUserId(), "stops", from, to, deviceIds, groupIds); + return Stops.getObjects(getUserId(), deviceIds, groupIds, from, to); } @Path("stops") @@ -199,11 +202,11 @@ public class ReportResource extends BaseResource { @Produces(XLSX) public Response getStopsExcel( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, - @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail) + @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail) throws SQLException, IOException { return executeReport(getUserId(), mail, stream -> { - Stops.getExcel(stream, getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + LogAction.logReport(getUserId(), "stops", from, to, deviceIds, groupIds); + Stops.getExcel(stream, getUserId(), deviceIds, groupIds, from, to); }); } diff --git a/src/main/java/org/traccar/api/resource/ServerResource.java b/src/main/java/org/traccar/api/resource/ServerResource.java index e7cad2a0c..91488afff 100644 --- a/src/main/java/org/traccar/api/resource/ServerResource.java +++ b/src/main/java/org/traccar/api/resource/ServerResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2020 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,8 +38,12 @@ public class ServerResource extends BaseResource { @PermitAll @GET - public Server get() throws SQLException { - return Context.getPermissionsManager().getServer(); + public Server get(@QueryParam("force") boolean force) throws SQLException { + if (force) { + return Context.getDataManager().getServer(); + } else { + return Context.getPermissionsManager().getServer(); + } } @PUT diff --git a/src/main/java/org/traccar/api/resource/StatisticsResource.java b/src/main/java/org/traccar/api/resource/StatisticsResource.java index e801d4ff3..58073e7d1 100644 --- a/src/main/java/org/traccar/api/resource/StatisticsResource.java +++ b/src/main/java/org/traccar/api/resource/StatisticsResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; -import org.traccar.helper.DateUtil; import org.traccar.model.Statistics; import javax.ws.rs.Consumes; @@ -28,6 +27,7 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import java.sql.SQLException; import java.util.Collection; +import java.util.Date; @Path("statistics") @Produces(MediaType.APPLICATION_JSON) @@ -36,9 +36,9 @@ public class StatisticsResource extends BaseResource { @GET public Collection<Statistics> get( - @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { + @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); - return Context.getDataManager().getStatistics(DateUtil.parseDate(from), DateUtil.parseDate(to)); + return Context.getDataManager().getStatistics(from, to); } } diff --git a/src/main/java/org/traccar/api/resource/UserResource.java b/src/main/java/org/traccar/api/resource/UserResource.java index 0b42d8d92..813ace6d6 100644 --- a/src/main/java/org/traccar/api/resource/UserResource.java +++ b/src/main/java/org/traccar/api/resource/UserResource.java @@ -48,7 +48,7 @@ public class UserResource extends BaseObjectResource<User> { @GET public Collection<User> get(@QueryParam("userId") long userId) throws SQLException { UsersManager usersManager = Context.getUsersManager(); - Set<Long> result = null; + Set<Long> result; if (Context.getPermissionsManager().getUserAdmin(getUserId())) { if (userId != 0) { result = usersManager.getUserItems(userId); |