From 8535983fea8c09046c53f2ebf388bbf3fae594ce Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 25 Jan 2018 20:02:22 +1300 Subject: Clean up unnecessary code --- src/org/traccar/web/CsvBuilder.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java index 31b389873..2bbe89788 100644 --- a/src/org/traccar/web/CsvBuilder.java +++ b/src/org/traccar/web/CsvBuilder.java @@ -75,16 +75,16 @@ public class CsvBuilder { if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { try { if (method.getReturnType().equals(boolean.class)) { - builder.append((Boolean) method.invoke(object)); + builder.append(method.invoke(object)); addSeparator(); } else if (method.getReturnType().equals(int.class)) { - builder.append((Integer) method.invoke(object)); + builder.append(method.invoke(object)); addSeparator(); } else if (method.getReturnType().equals(long.class)) { - builder.append((Long) method.invoke(object)); + builder.append(method.invoke(object)); addSeparator(); } else if (method.getReturnType().equals(double.class)) { - builder.append((Double) method.invoke(object)); + builder.append(method.invoke(object)); addSeparator(); } else if (method.getReturnType().equals(String.class)) { builder.append((String) method.invoke(object)); -- cgit v1.2.3 From 0f87445428a831e0f0d3b69f6f07f2a60272f2fb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 25 Jan 2018 20:24:40 +1300 Subject: Clean up unused imports --- src/org/traccar/api/resource/DeviceResource.java | 2 +- src/org/traccar/api/resource/SessionResource.java | 11 +++++------ src/org/traccar/notification/EventForwarder.java | 4 ++-- src/org/traccar/web/CsvBuilder.java | 2 +- test/org/traccar/FilterHandlerTest.java | 2 -- test/org/traccar/ProtocolTest.java | 1 - test/org/traccar/WebDataHandlerTest.java | 1 - test/org/traccar/calendar/CalendarTest.java | 1 - test/org/traccar/database/DataManagerTest.java | 1 - test/org/traccar/database/GroupTreeTest.java | 1 - test/org/traccar/geocoder/AddressFormatTest.java | 1 - test/org/traccar/geocoder/GeocoderTest.java | 1 - test/org/traccar/geofence/GeofenceCircleTest.java | 1 - test/org/traccar/geofence/GeofencePolygonTest.java | 1 - test/org/traccar/geofence/GeofencePolylineTest.java | 1 - test/org/traccar/geolocation/GeolocationProviderTest.java | 1 - test/org/traccar/helper/BcdUtilTest.java | 1 - test/org/traccar/helper/BitUtilTest.java | 1 - test/org/traccar/helper/ChecksumTest.java | 1 - test/org/traccar/helper/DateBuilderTest.java | 1 - test/org/traccar/helper/DateUtilTest.java | 1 - test/org/traccar/helper/DistanceCalculatorTest.java | 1 - test/org/traccar/helper/LocationTreeTest.java | 1 - test/org/traccar/helper/ObdDecoderTest.java | 1 - test/org/traccar/helper/PatternBuilderTest.java | 1 - test/org/traccar/helper/StringFinderTest.java | 5 ----- test/org/traccar/notification/NotificiationMailTest.java | 4 +++- test/org/traccar/processing/ComputedAttributesTest.java | 1 - test/org/traccar/protocol/AdmProtocolEncoderTest.java | 1 - test/org/traccar/protocol/AplicomFrameDecoderTest.java | 1 - test/org/traccar/protocol/At2000FrameDecoderTest.java | 1 - test/org/traccar/protocol/AtrackFrameDecoderTest.java | 1 - test/org/traccar/protocol/EskyFrameDecoderTest.java | 1 - test/org/traccar/protocol/GalileoFrameDecoderTest.java | 1 - test/org/traccar/protocol/GatorProtocolDecoderTest.java | 1 - test/org/traccar/protocol/Gl200FrameDecoderTest.java | 1 - test/org/traccar/protocol/Gps056FrameDecoderTest.java | 1 - test/org/traccar/protocol/Gps103ProtocolEncoderTest.java | 1 - test/org/traccar/protocol/GranitFrameDecoderTest.java | 1 - test/org/traccar/protocol/Gt06FrameDecoderTest.java | 1 - test/org/traccar/protocol/H02FrameDecoderTest.java | 1 - test/org/traccar/protocol/H02ProtocolEncoderTest.java | 1 - test/org/traccar/protocol/HuaShengFrameDecoderTest.java | 1 - test/org/traccar/protocol/HuabaoFrameDecoderTest.java | 1 - test/org/traccar/protocol/Jt600FrameDecoderTest.java | 1 - test/org/traccar/protocol/L100FrameDecoderTest.java | 1 - test/org/traccar/protocol/MegastekFrameDecoderTest.java | 1 - test/org/traccar/protocol/MeiligaoFrameDecoderTest.java | 1 - test/org/traccar/protocol/MeitrackFrameDecoderTest.java | 1 - test/org/traccar/protocol/MeitrackProtocolEncoderTest.java | 1 - test/org/traccar/protocol/MiniFinderProtocolEncoderTest.java | 1 - test/org/traccar/protocol/NvsFrameDecoderTest.java | 1 - test/org/traccar/protocol/Pt502FrameDecoderTest.java | 1 - test/org/traccar/protocol/Pt502ProtocolEncoderTest.java | 1 - test/org/traccar/protocol/TelicFrameDecoderTest.java | 2 -- test/org/traccar/protocol/Tk103ProtocolEncoderTest.java | 3 --- test/org/traccar/protocol/TmgFrameDecoderTest.java | 1 - test/org/traccar/protocol/TotemFrameDecoderTest.java | 1 - test/org/traccar/protocol/TotemProtocolEncoderTest.java | 1 - test/org/traccar/protocol/TzoneProtocolDecoderTest.java | 1 - test/org/traccar/protocol/UlbotechFrameDecoderTest.java | 1 - test/org/traccar/protocol/Vt200FrameDecoderTest.java | 1 - test/org/traccar/protocol/VtfmsFrameDecoderTest.java | 1 - test/org/traccar/protocol/WatchProtocolEncoderTest.java | 1 - test/org/traccar/protocol/WondexFrameDecoderTest.java | 1 - test/org/traccar/protocol/WondexProtocolEncoderTest.java | 1 - test/org/traccar/protocol/XexunFrameDecoderTest.java | 1 - test/org/traccar/reports/ReportUtilsTest.java | 1 - 68 files changed, 12 insertions(+), 82 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index ddf8832ed..87927e45b 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -71,7 +71,7 @@ public class DeviceResource extends BaseObjectResource { result = deviceManager.getUserItems(userId); } } else { - result = new HashSet(); + result = new HashSet<>(); for (String uniqueId : uniqueIds) { Device device = deviceManager.getByUniqueId(uniqueId); Context.getPermissionsManager().checkDevice(getUserId(), device.getId()); diff --git a/src/org/traccar/api/resource/SessionResource.java b/src/org/traccar/api/resource/SessionResource.java index 3f7842626..2a0bd4364 100644 --- a/src/org/traccar/api/resource/SessionResource.java +++ b/src/org/traccar/api/resource/SessionResource.java @@ -61,15 +61,14 @@ public class SessionResource extends BaseResource { Cookie[] cookies = request.getCookies(); String email = null, password = null; if (cookies != null) { - for (int i = 0; i < cookies.length; i++) { - if (cookies[i].getName().equals(USER_COOKIE_KEY)) { + for (Cookie cookie : cookies) { + if (cookie.getName().equals(USER_COOKIE_KEY)) { byte[] emailBytes = DatatypeConverter.parseBase64Binary( - URLDecoder.decode(cookies[i].getValue(), StandardCharsets.US_ASCII.name())); + URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name())); email = new String(emailBytes, StandardCharsets.UTF_8); - } - if (cookies[i].getName().equals(PASS_COOKIE_KEY)) { + } else if (cookie.getName().equals(PASS_COOKIE_KEY)) { byte[] passwordBytes = DatatypeConverter.parseBase64Binary( - URLDecoder.decode(cookies[i].getValue(), StandardCharsets.US_ASCII.name())); + URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name())); password = new String(passwordBytes, StandardCharsets.UTF_8); } } diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index ad8134f18..d1bdd4aba 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -17,7 +17,7 @@ package org.traccar.notification; import com.fasterxml.jackson.core.JsonProcessingException; import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.traccar.Context; @@ -73,7 +73,7 @@ public abstract class EventForwarder { for (String paramLine: paramsLines) { splitedLine = paramLine.split(separator, 2); if (splitedLine.length == 2) { - paramsMap.put(splitedLine[0].trim(), Arrays.asList(splitedLine[1].trim())); + paramsMap.put(splitedLine[0].trim(), Collections.singletonList(splitedLine[1].trim())); } } return paramsMap; diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java index 2bbe89788..fa99f1b18 100644 --- a/src/org/traccar/web/CsvBuilder.java +++ b/src/org/traccar/web/CsvBuilder.java @@ -51,7 +51,7 @@ public class CsvBuilder { private SortedSet getSortedMethods(Object object) { Method[] methodArray = object.getClass().getMethods(); - SortedSet methods = new TreeSet(new Comparator() { + SortedSet methods = new TreeSet<>(new Comparator() { @Override public int compare(Method m1, Method m2) { if (m1.getName().equals("getAttributes") && !m1.getName().equals(m2.getName())) { diff --git a/test/org/traccar/FilterHandlerTest.java b/test/org/traccar/FilterHandlerTest.java index 0d488a7fb..8b1ba3b1f 100644 --- a/test/org/traccar/FilterHandlerTest.java +++ b/test/org/traccar/FilterHandlerTest.java @@ -3,8 +3,6 @@ package org.traccar; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.traccar.database.IdentityManager; -import org.traccar.model.Device; import org.traccar.model.Position; import java.util.Date; diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java index 246e3d0e7..e67b53aa8 100644 --- a/test/org/traccar/ProtocolTest.java +++ b/test/org/traccar/ProtocolTest.java @@ -5,7 +5,6 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.handler.codec.http.DefaultHttpRequest; import org.jboss.netty.handler.codec.http.HttpMethod; import org.jboss.netty.handler.codec.http.HttpVersion; -import org.junit.Assert; import org.traccar.model.CellTower; import org.traccar.model.Command; import org.traccar.model.Position; diff --git a/test/org/traccar/WebDataHandlerTest.java b/test/org/traccar/WebDataHandlerTest.java index be05e0855..70b098b0f 100644 --- a/test/org/traccar/WebDataHandlerTest.java +++ b/test/org/traccar/WebDataHandlerTest.java @@ -1,6 +1,5 @@ package org.traccar; -import org.junit.Assert; import org.junit.Test; import org.traccar.model.Position; diff --git a/test/org/traccar/calendar/CalendarTest.java b/test/org/traccar/calendar/CalendarTest.java index ae68af9c0..0de89f97f 100644 --- a/test/org/traccar/calendar/CalendarTest.java +++ b/test/org/traccar/calendar/CalendarTest.java @@ -7,7 +7,6 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; -import org.junit.Assert; import org.junit.Test; import org.traccar.model.Calendar; diff --git a/test/org/traccar/database/DataManagerTest.java b/test/org/traccar/database/DataManagerTest.java index 3e3dcab6c..f9945c994 100644 --- a/test/org/traccar/database/DataManagerTest.java +++ b/test/org/traccar/database/DataManagerTest.java @@ -1,6 +1,5 @@ package org.traccar.database; -import org.junit.Assert; import org.junit.Test; import org.traccar.model.Attribute; import org.traccar.model.Device; diff --git a/test/org/traccar/database/GroupTreeTest.java b/test/org/traccar/database/GroupTreeTest.java index 3e75f9073..b547aab60 100644 --- a/test/org/traccar/database/GroupTreeTest.java +++ b/test/org/traccar/database/GroupTreeTest.java @@ -1,6 +1,5 @@ package org.traccar.database; -import org.junit.Assert; import org.junit.Test; import org.traccar.model.Device; import org.traccar.model.Group; diff --git a/test/org/traccar/geocoder/AddressFormatTest.java b/test/org/traccar/geocoder/AddressFormatTest.java index 12f38fca4..0cc5168ef 100644 --- a/test/org/traccar/geocoder/AddressFormatTest.java +++ b/test/org/traccar/geocoder/AddressFormatTest.java @@ -1,6 +1,5 @@ package org.traccar.geocoder; -import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/geocoder/GeocoderTest.java b/test/org/traccar/geocoder/GeocoderTest.java index 3b53495c4..1ddc43ad2 100644 --- a/test/org/traccar/geocoder/GeocoderTest.java +++ b/test/org/traccar/geocoder/GeocoderTest.java @@ -2,7 +2,6 @@ package org.traccar.geocoder; import java.util.Locale; -import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; diff --git a/test/org/traccar/geofence/GeofenceCircleTest.java b/test/org/traccar/geofence/GeofenceCircleTest.java index 020b10a0b..259a8fb77 100644 --- a/test/org/traccar/geofence/GeofenceCircleTest.java +++ b/test/org/traccar/geofence/GeofenceCircleTest.java @@ -2,7 +2,6 @@ package org.traccar.geofence; import java.text.ParseException; -import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/geofence/GeofencePolygonTest.java b/test/org/traccar/geofence/GeofencePolygonTest.java index 31f35120f..94b73af3a 100644 --- a/test/org/traccar/geofence/GeofencePolygonTest.java +++ b/test/org/traccar/geofence/GeofencePolygonTest.java @@ -2,7 +2,6 @@ package org.traccar.geofence; import java.text.ParseException; -import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/geofence/GeofencePolylineTest.java b/test/org/traccar/geofence/GeofencePolylineTest.java index 1b51c17f6..1e9dcb7c3 100644 --- a/test/org/traccar/geofence/GeofencePolylineTest.java +++ b/test/org/traccar/geofence/GeofencePolylineTest.java @@ -2,7 +2,6 @@ package org.traccar.geofence; import java.text.ParseException; -import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/geolocation/GeolocationProviderTest.java b/test/org/traccar/geolocation/GeolocationProviderTest.java index 97b5b32da..2729052d6 100644 --- a/test/org/traccar/geolocation/GeolocationProviderTest.java +++ b/test/org/traccar/geolocation/GeolocationProviderTest.java @@ -1,6 +1,5 @@ package org.traccar.geolocation; -import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.traccar.BaseTest; diff --git a/test/org/traccar/helper/BcdUtilTest.java b/test/org/traccar/helper/BcdUtilTest.java index 9ffceacd9..e97aad09f 100644 --- a/test/org/traccar/helper/BcdUtilTest.java +++ b/test/org/traccar/helper/BcdUtilTest.java @@ -1,7 +1,6 @@ package org.traccar.helper; import org.jboss.netty.buffer.ChannelBuffers; -import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/helper/BitUtilTest.java b/test/org/traccar/helper/BitUtilTest.java index 683c4e6b8..90431bf55 100644 --- a/test/org/traccar/helper/BitUtilTest.java +++ b/test/org/traccar/helper/BitUtilTest.java @@ -1,6 +1,5 @@ package org.traccar.helper; -import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/helper/ChecksumTest.java b/test/org/traccar/helper/ChecksumTest.java index 1c20ba899..a7c66a2a2 100644 --- a/test/org/traccar/helper/ChecksumTest.java +++ b/test/org/traccar/helper/ChecksumTest.java @@ -2,7 +2,6 @@ package org.traccar.helper; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; -import org.junit.Assert; import org.junit.Test; import java.nio.charset.StandardCharsets; diff --git a/test/org/traccar/helper/DateBuilderTest.java b/test/org/traccar/helper/DateBuilderTest.java index 8b2898fed..b6323cc1d 100644 --- a/test/org/traccar/helper/DateBuilderTest.java +++ b/test/org/traccar/helper/DateBuilderTest.java @@ -1,6 +1,5 @@ package org.traccar.helper; -import org.junit.Assert; import org.junit.Test; import java.text.DateFormat; diff --git a/test/org/traccar/helper/DateUtilTest.java b/test/org/traccar/helper/DateUtilTest.java index b0bae4d11..ec42e71ae 100644 --- a/test/org/traccar/helper/DateUtilTest.java +++ b/test/org/traccar/helper/DateUtilTest.java @@ -1,6 +1,5 @@ package org.traccar.helper; -import org.junit.Assert; import org.junit.Test; import java.text.DateFormat; diff --git a/test/org/traccar/helper/DistanceCalculatorTest.java b/test/org/traccar/helper/DistanceCalculatorTest.java index f12a1ec83..a7457b6c4 100644 --- a/test/org/traccar/helper/DistanceCalculatorTest.java +++ b/test/org/traccar/helper/DistanceCalculatorTest.java @@ -1,6 +1,5 @@ package org.traccar.helper; -import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/helper/LocationTreeTest.java b/test/org/traccar/helper/LocationTreeTest.java index 3984b1142..21604144a 100644 --- a/test/org/traccar/helper/LocationTreeTest.java +++ b/test/org/traccar/helper/LocationTreeTest.java @@ -1,6 +1,5 @@ package org.traccar.helper; -import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; diff --git a/test/org/traccar/helper/ObdDecoderTest.java b/test/org/traccar/helper/ObdDecoderTest.java index d9cc2ed6b..1ffe68c8b 100644 --- a/test/org/traccar/helper/ObdDecoderTest.java +++ b/test/org/traccar/helper/ObdDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.helper; -import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/helper/PatternBuilderTest.java b/test/org/traccar/helper/PatternBuilderTest.java index a8fb1aa93..4c76bc463 100644 --- a/test/org/traccar/helper/PatternBuilderTest.java +++ b/test/org/traccar/helper/PatternBuilderTest.java @@ -1,6 +1,5 @@ package org.traccar.helper; -import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/helper/StringFinderTest.java b/test/org/traccar/helper/StringFinderTest.java index 6bc78ea82..f35f9a759 100644 --- a/test/org/traccar/helper/StringFinderTest.java +++ b/test/org/traccar/helper/StringFinderTest.java @@ -1,16 +1,11 @@ package org.traccar.helper; import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBufferFactory; import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.buffer.HeapChannelBufferFactory; -import org.junit.Assert; import org.junit.Test; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; public class StringFinderTest { diff --git a/test/org/traccar/notification/NotificiationMailTest.java b/test/org/traccar/notification/NotificiationMailTest.java index 975505a3a..b82bec02e 100644 --- a/test/org/traccar/notification/NotificiationMailTest.java +++ b/test/org/traccar/notification/NotificiationMailTest.java @@ -1,5 +1,6 @@ package org.traccar.notification; +import org.junit.Ignore; import org.junit.Test; import javax.mail.Message; @@ -24,7 +25,8 @@ public class NotificiationMailTest { private static final int PORT = 25; - //@Test + @Ignore + @Test public void test() throws Exception { Properties props = System.getProperties(); diff --git a/test/org/traccar/processing/ComputedAttributesTest.java b/test/org/traccar/processing/ComputedAttributesTest.java index 88ebfd9a9..160067915 100644 --- a/test/org/traccar/processing/ComputedAttributesTest.java +++ b/test/org/traccar/processing/ComputedAttributesTest.java @@ -2,7 +2,6 @@ package org.traccar.processing; import java.util.Date; -import org.junit.Assert; import org.junit.Test; import org.traccar.model.Attribute; import org.traccar.model.Position; diff --git a/test/org/traccar/protocol/AdmProtocolEncoderTest.java b/test/org/traccar/protocol/AdmProtocolEncoderTest.java index fd4ea4af0..cb0a31ceb 100644 --- a/test/org/traccar/protocol/AdmProtocolEncoderTest.java +++ b/test/org/traccar/protocol/AdmProtocolEncoderTest.java @@ -16,7 +16,6 @@ */ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; diff --git a/test/org/traccar/protocol/AplicomFrameDecoderTest.java b/test/org/traccar/protocol/AplicomFrameDecoderTest.java index 6ba8f9334..581f7696f 100644 --- a/test/org/traccar/protocol/AplicomFrameDecoderTest.java +++ b/test/org/traccar/protocol/AplicomFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/At2000FrameDecoderTest.java b/test/org/traccar/protocol/At2000FrameDecoderTest.java index 52490c107..5613f832e 100644 --- a/test/org/traccar/protocol/At2000FrameDecoderTest.java +++ b/test/org/traccar/protocol/At2000FrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/AtrackFrameDecoderTest.java b/test/org/traccar/protocol/AtrackFrameDecoderTest.java index 12ce783ad..9db4fd052 100644 --- a/test/org/traccar/protocol/AtrackFrameDecoderTest.java +++ b/test/org/traccar/protocol/AtrackFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/EskyFrameDecoderTest.java b/test/org/traccar/protocol/EskyFrameDecoderTest.java index e8902e8be..3b16a9051 100644 --- a/test/org/traccar/protocol/EskyFrameDecoderTest.java +++ b/test/org/traccar/protocol/EskyFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/GalileoFrameDecoderTest.java b/test/org/traccar/protocol/GalileoFrameDecoderTest.java index 049c16efa..7a7c97ba6 100644 --- a/test/org/traccar/protocol/GalileoFrameDecoderTest.java +++ b/test/org/traccar/protocol/GalileoFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/GatorProtocolDecoderTest.java b/test/org/traccar/protocol/GatorProtocolDecoderTest.java index ae6240b3e..360a3abed 100644 --- a/test/org/traccar/protocol/GatorProtocolDecoderTest.java +++ b/test/org/traccar/protocol/GatorProtocolDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/Gl200FrameDecoderTest.java b/test/org/traccar/protocol/Gl200FrameDecoderTest.java index 2112ce466..e90c6495a 100644 --- a/test/org/traccar/protocol/Gl200FrameDecoderTest.java +++ b/test/org/traccar/protocol/Gl200FrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/Gps056FrameDecoderTest.java b/test/org/traccar/protocol/Gps056FrameDecoderTest.java index 58c64f59e..ce21f733f 100644 --- a/test/org/traccar/protocol/Gps056FrameDecoderTest.java +++ b/test/org/traccar/protocol/Gps056FrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/Gps103ProtocolEncoderTest.java b/test/org/traccar/protocol/Gps103ProtocolEncoderTest.java index cfb1d5b90..f888ee252 100644 --- a/test/org/traccar/protocol/Gps103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Gps103ProtocolEncoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; diff --git a/test/org/traccar/protocol/GranitFrameDecoderTest.java b/test/org/traccar/protocol/GranitFrameDecoderTest.java index aa5a5b580..130b9dbd7 100644 --- a/test/org/traccar/protocol/GranitFrameDecoderTest.java +++ b/test/org/traccar/protocol/GranitFrameDecoderTest.java @@ -2,7 +2,6 @@ package org.traccar.protocol; import java.nio.ByteOrder; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/Gt06FrameDecoderTest.java b/test/org/traccar/protocol/Gt06FrameDecoderTest.java index 97230695a..cf6d1cfd7 100644 --- a/test/org/traccar/protocol/Gt06FrameDecoderTest.java +++ b/test/org/traccar/protocol/Gt06FrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/H02FrameDecoderTest.java b/test/org/traccar/protocol/H02FrameDecoderTest.java index 214f1c9fa..bdb2ff37b 100644 --- a/test/org/traccar/protocol/H02FrameDecoderTest.java +++ b/test/org/traccar/protocol/H02FrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/H02ProtocolEncoderTest.java b/test/org/traccar/protocol/H02ProtocolEncoderTest.java index bf3978385..e7b409a88 100644 --- a/test/org/traccar/protocol/H02ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/H02ProtocolEncoderTest.java @@ -1,7 +1,6 @@ package org.traccar.protocol; import org.joda.time.DateTime; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; diff --git a/test/org/traccar/protocol/HuaShengFrameDecoderTest.java b/test/org/traccar/protocol/HuaShengFrameDecoderTest.java index dc2536319..0f24d4b5c 100644 --- a/test/org/traccar/protocol/HuaShengFrameDecoderTest.java +++ b/test/org/traccar/protocol/HuaShengFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/HuabaoFrameDecoderTest.java b/test/org/traccar/protocol/HuabaoFrameDecoderTest.java index 98caf1dca..2d3937903 100644 --- a/test/org/traccar/protocol/HuabaoFrameDecoderTest.java +++ b/test/org/traccar/protocol/HuabaoFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/Jt600FrameDecoderTest.java b/test/org/traccar/protocol/Jt600FrameDecoderTest.java index 15e6ec18c..ae0948987 100644 --- a/test/org/traccar/protocol/Jt600FrameDecoderTest.java +++ b/test/org/traccar/protocol/Jt600FrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/L100FrameDecoderTest.java b/test/org/traccar/protocol/L100FrameDecoderTest.java index ab70678d5..fc6a892dc 100644 --- a/test/org/traccar/protocol/L100FrameDecoderTest.java +++ b/test/org/traccar/protocol/L100FrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/MegastekFrameDecoderTest.java b/test/org/traccar/protocol/MegastekFrameDecoderTest.java index 9a327bb1f..68606a98a 100644 --- a/test/org/traccar/protocol/MegastekFrameDecoderTest.java +++ b/test/org/traccar/protocol/MegastekFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/MeiligaoFrameDecoderTest.java b/test/org/traccar/protocol/MeiligaoFrameDecoderTest.java index 214936514..2d09c626b 100644 --- a/test/org/traccar/protocol/MeiligaoFrameDecoderTest.java +++ b/test/org/traccar/protocol/MeiligaoFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/MeitrackFrameDecoderTest.java b/test/org/traccar/protocol/MeitrackFrameDecoderTest.java index f0afee02e..53749816e 100644 --- a/test/org/traccar/protocol/MeitrackFrameDecoderTest.java +++ b/test/org/traccar/protocol/MeitrackFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/MeitrackProtocolEncoderTest.java b/test/org/traccar/protocol/MeitrackProtocolEncoderTest.java index a2b0ae6fd..d5ecbb488 100644 --- a/test/org/traccar/protocol/MeitrackProtocolEncoderTest.java +++ b/test/org/traccar/protocol/MeitrackProtocolEncoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; diff --git a/test/org/traccar/protocol/MiniFinderProtocolEncoderTest.java b/test/org/traccar/protocol/MiniFinderProtocolEncoderTest.java index d446c228c..e9422da9f 100644 --- a/test/org/traccar/protocol/MiniFinderProtocolEncoderTest.java +++ b/test/org/traccar/protocol/MiniFinderProtocolEncoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; diff --git a/test/org/traccar/protocol/NvsFrameDecoderTest.java b/test/org/traccar/protocol/NvsFrameDecoderTest.java index b03e3826c..8a00207eb 100644 --- a/test/org/traccar/protocol/NvsFrameDecoderTest.java +++ b/test/org/traccar/protocol/NvsFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/Pt502FrameDecoderTest.java b/test/org/traccar/protocol/Pt502FrameDecoderTest.java index d654c0c61..04180abb0 100644 --- a/test/org/traccar/protocol/Pt502FrameDecoderTest.java +++ b/test/org/traccar/protocol/Pt502FrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/Pt502ProtocolEncoderTest.java b/test/org/traccar/protocol/Pt502ProtocolEncoderTest.java index 5438eb366..9d591bceb 100644 --- a/test/org/traccar/protocol/Pt502ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Pt502ProtocolEncoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; diff --git a/test/org/traccar/protocol/TelicFrameDecoderTest.java b/test/org/traccar/protocol/TelicFrameDecoderTest.java index 711014c46..dd0efacb6 100644 --- a/test/org/traccar/protocol/TelicFrameDecoderTest.java +++ b/test/org/traccar/protocol/TelicFrameDecoderTest.java @@ -1,7 +1,5 @@ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index 6b38035ed..565f6bb88 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -1,11 +1,8 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; -import org.traccar.TestIdentityManager; import org.traccar.model.Command; -import org.traccar.model.Device; import static org.junit.Assert.assertEquals; diff --git a/test/org/traccar/protocol/TmgFrameDecoderTest.java b/test/org/traccar/protocol/TmgFrameDecoderTest.java index 7a6c47fb9..4f7be1b28 100644 --- a/test/org/traccar/protocol/TmgFrameDecoderTest.java +++ b/test/org/traccar/protocol/TmgFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/TotemFrameDecoderTest.java b/test/org/traccar/protocol/TotemFrameDecoderTest.java index 0d3e69465..8fb5f8d54 100644 --- a/test/org/traccar/protocol/TotemFrameDecoderTest.java +++ b/test/org/traccar/protocol/TotemFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/TotemProtocolEncoderTest.java b/test/org/traccar/protocol/TotemProtocolEncoderTest.java index d17f70405..5a47f74cc 100644 --- a/test/org/traccar/protocol/TotemProtocolEncoderTest.java +++ b/test/org/traccar/protocol/TotemProtocolEncoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; diff --git a/test/org/traccar/protocol/TzoneProtocolDecoderTest.java b/test/org/traccar/protocol/TzoneProtocolDecoderTest.java index 68c8bbdbc..c309abce1 100644 --- a/test/org/traccar/protocol/TzoneProtocolDecoderTest.java +++ b/test/org/traccar/protocol/TzoneProtocolDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import static org.junit.Assert.assertNotNull; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/UlbotechFrameDecoderTest.java b/test/org/traccar/protocol/UlbotechFrameDecoderTest.java index b0af435b8..d3d0429d6 100644 --- a/test/org/traccar/protocol/UlbotechFrameDecoderTest.java +++ b/test/org/traccar/protocol/UlbotechFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/Vt200FrameDecoderTest.java b/test/org/traccar/protocol/Vt200FrameDecoderTest.java index a9fff6c33..9422f6d74 100644 --- a/test/org/traccar/protocol/Vt200FrameDecoderTest.java +++ b/test/org/traccar/protocol/Vt200FrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/VtfmsFrameDecoderTest.java b/test/org/traccar/protocol/VtfmsFrameDecoderTest.java index c9ee8ae20..a5eb0b49b 100644 --- a/test/org/traccar/protocol/VtfmsFrameDecoderTest.java +++ b/test/org/traccar/protocol/VtfmsFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/WatchProtocolEncoderTest.java b/test/org/traccar/protocol/WatchProtocolEncoderTest.java index a87afbfc5..a201b7860 100644 --- a/test/org/traccar/protocol/WatchProtocolEncoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolEncoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; diff --git a/test/org/traccar/protocol/WondexFrameDecoderTest.java b/test/org/traccar/protocol/WondexFrameDecoderTest.java index 8eb80da9a..642473f2d 100644 --- a/test/org/traccar/protocol/WondexFrameDecoderTest.java +++ b/test/org/traccar/protocol/WondexFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/protocol/WondexProtocolEncoderTest.java b/test/org/traccar/protocol/WondexProtocolEncoderTest.java index 7c5fa572c..3d76cb677 100644 --- a/test/org/traccar/protocol/WondexProtocolEncoderTest.java +++ b/test/org/traccar/protocol/WondexProtocolEncoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; diff --git a/test/org/traccar/protocol/XexunFrameDecoderTest.java b/test/org/traccar/protocol/XexunFrameDecoderTest.java index cdf30965c..8fc628bdb 100644 --- a/test/org/traccar/protocol/XexunFrameDecoderTest.java +++ b/test/org/traccar/protocol/XexunFrameDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; -import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; diff --git a/test/org/traccar/reports/ReportUtilsTest.java b/test/org/traccar/reports/ReportUtilsTest.java index bdf45e9d5..a8ff653c9 100644 --- a/test/org/traccar/reports/ReportUtilsTest.java +++ b/test/org/traccar/reports/ReportUtilsTest.java @@ -15,7 +15,6 @@ import java.util.Iterator; import java.util.List; import java.util.TimeZone; -import org.junit.Assert; import org.junit.Test; import org.traccar.BaseTest; import org.traccar.model.Position; -- cgit v1.2.3 From 22f792139406e381fdda5a02a61b61a577b33656 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 1 Feb 2018 14:12:06 +0500 Subject: Provide access to media files received from devices --- src/org/traccar/api/MediaFilter.java | 104 ++++++++++++++++++++++ src/org/traccar/protocol/Gt06ProtocolDecoder.java | 3 +- src/org/traccar/web/WebServer.java | 18 ++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/org/traccar/api/MediaFilter.java (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/api/MediaFilter.java b/src/org/traccar/api/MediaFilter.java new file mode 100644 index 000000000..c07175d40 --- /dev/null +++ b/src/org/traccar/api/MediaFilter.java @@ -0,0 +1,104 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2018 Andrey Kunitsyn (andrey@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 java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.ws.rs.NotAuthorizedException; + +import org.traccar.Context; +import org.traccar.api.resource.SessionResource; +import org.traccar.helper.Log; +import org.traccar.model.Device; + +public class MediaFilter implements Filter { + + private boolean dirAllowed; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + dirAllowed = Context.getConfig().getBoolean("media.dirAllowed"); + } + + private static void formatError(HttpServletResponse response, Exception e) throws IOException { + if (e instanceof SecurityException) { + response.setStatus(HttpServletResponse.SC_FORBIDDEN); + } else if (e instanceof IllegalArgumentException) { + response.setStatus(HttpServletResponse.SC_NOT_FOUND); + } else if (e instanceof NotAuthorizedException) { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } else { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + } + response.getWriter().println(Log.exceptionStack(e)); + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + try { + HttpSession session = ((HttpServletRequest) request).getSession(false); + Long userId = null; + if (session != null) { + userId = (Long) session.getAttribute(SessionResource.USER_ID_KEY); + if (userId != null) { + Context.getPermissionsManager().checkUserEnabled(userId); + Context.getStatisticsManager().registerRequest(userId); + } + } + if (userId == null) { + throw new NotAuthorizedException("Not authorized"); + } + + String[] parts = ((HttpServletRequest) request).getPathInfo().split("/"); + if (parts.length < 2) { + if (dirAllowed) { + Context.getPermissionsManager().checkAdmin(userId); + } else { + throw new SecurityException("Wrong path"); + } + } else if (parts.length == 2 && !dirAllowed) { + throw new SecurityException("Wrong path"); + } else { + Device device = Context.getIdentityManager().getByUniqueId(parts[1]); + if (device != null) { + Context.getPermissionsManager().checkDevice(userId, device.getId()); + } else { + throw new IllegalArgumentException("Device not found"); + } + } + + chain.doFilter(request, response); + } catch (Exception e) { + formatError((HttpServletResponse) response, e); + } + } + + @Override + public void destroy() { + } + +} diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java index e11a6580b..4173b4d5b 100644 --- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -704,7 +704,8 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { sendPhotoRequest(channel, pictureId); } else { Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId()); - Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg"); + position.set( + Position.KEY_IMAGE, Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg")); photos.remove(pictureId); } diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index e145ff554..8b0696081 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -25,6 +25,7 @@ import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.ResourceHandler; import org.eclipse.jetty.server.session.HashSessionManager; +import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.webapp.WebAppContext; @@ -35,6 +36,7 @@ import org.traccar.Config; import org.traccar.Context; import org.traccar.api.AsyncSocketServlet; import org.traccar.api.CorsResponseFilter; +import org.traccar.api.MediaFilter; import org.traccar.api.ObjectMapperProvider; import org.traccar.api.ResourceErrorHandler; import org.traccar.api.SecurityRequestFilter; @@ -42,6 +44,7 @@ import org.traccar.api.resource.ServerResource; import org.traccar.helper.Log; import javax.naming.InitialContext; +import javax.servlet.DispatcherType; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -49,6 +52,7 @@ import javax.sql.DataSource; import java.io.IOException; import java.io.Writer; import java.net.InetSocketAddress; +import java.util.EnumSet; public class WebServer { @@ -80,6 +84,7 @@ public class WebServer { } initServer(); + initMedia(); initApi(); if (config.getBoolean("web.console")) { initConsole(); @@ -156,6 +161,19 @@ public class WebServer { handlers.addHandler(app); } + private void initMedia() { + ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); + servletHandler.setContextPath("/api/media"); + servletHandler.getSessionHandler().setSessionManager(sessionManager); + + ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class); + servletHolder.setInitParameter("resourceBase", config.getString("media.path")); + servletHolder.setInitParameter("dirAllowed", config.getString("media.dirAllowed", "false")); + servletHandler.addServlet(servletHolder, "/*"); + servletHandler.addFilter(MediaFilter.class, "/*", EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST)); + handlers.addHandler(servletHandler); + } + private void initApi() { ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); servletHandler.setContextPath("/api"); -- cgit v1.2.3 From 195ec5aed9e3a6a499cf5a21773235563806a0c1 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 2 Feb 2018 11:15:12 +0500 Subject: - Move media servlet to api context - Cleanup and optimization --- src/org/traccar/api/MediaFilter.java | 41 +++++++++++++----------------------- src/org/traccar/web/WebServer.java | 24 +++++++-------------- 2 files changed, 23 insertions(+), 42 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/api/MediaFilter.java b/src/org/traccar/api/MediaFilter.java index c07175d40..b3dcbbd66 100644 --- a/src/org/traccar/api/MediaFilter.java +++ b/src/org/traccar/api/MediaFilter.java @@ -36,24 +36,8 @@ import org.traccar.model.Device; public class MediaFilter implements Filter { - private boolean dirAllowed; - @Override public void init(FilterConfig filterConfig) throws ServletException { - dirAllowed = Context.getConfig().getBoolean("media.dirAllowed"); - } - - private static void formatError(HttpServletResponse response, Exception e) throws IOException { - if (e instanceof SecurityException) { - response.setStatus(HttpServletResponse.SC_FORBIDDEN); - } else if (e instanceof IllegalArgumentException) { - response.setStatus(HttpServletResponse.SC_NOT_FOUND); - } else if (e instanceof NotAuthorizedException) { - response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - } else { - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); - } - response.getWriter().println(Log.exceptionStack(e)); } @Override @@ -73,15 +57,10 @@ public class MediaFilter implements Filter { throw new NotAuthorizedException("Not authorized"); } - String[] parts = ((HttpServletRequest) request).getPathInfo().split("/"); - if (parts.length < 2) { - if (dirAllowed) { - Context.getPermissionsManager().checkAdmin(userId); - } else { - throw new SecurityException("Wrong path"); - } - } else if (parts.length == 2 && !dirAllowed) { - throw new SecurityException("Wrong path"); + String path = ((HttpServletRequest) request).getPathInfo(); + String[] parts = path.split("/"); + if (parts.length < 2 || parts.length == 2 && !path.endsWith("/")) { + Context.getPermissionsManager().checkAdmin(userId); } else { Device device = Context.getIdentityManager().getByUniqueId(parts[1]); if (device != null) { @@ -93,7 +72,17 @@ public class MediaFilter implements Filter { chain.doFilter(request, response); } catch (Exception e) { - formatError((HttpServletResponse) response, e); + HttpServletResponse httpResponse = (HttpServletResponse) response; + if (e instanceof SecurityException) { + httpResponse.setStatus(HttpServletResponse.SC_FORBIDDEN); + } else if (e instanceof IllegalArgumentException) { + httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND); + } else if (e instanceof NotAuthorizedException) { + httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } else { + httpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST); + } + response.getWriter().println(Log.exceptionStack(e)); } } diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 8b0696081..88b38c1ab 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 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. @@ -84,7 +84,6 @@ public class WebServer { } initServer(); - initMedia(); initApi(); if (config.getBoolean("web.console")) { initConsole(); @@ -161,19 +160,6 @@ public class WebServer { handlers.addHandler(app); } - private void initMedia() { - ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); - servletHandler.setContextPath("/api/media"); - servletHandler.getSessionHandler().setSessionManager(sessionManager); - - ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class); - servletHolder.setInitParameter("resourceBase", config.getString("media.path")); - servletHolder.setInitParameter("dirAllowed", config.getString("media.dirAllowed", "false")); - servletHandler.addServlet(servletHolder, "/*"); - servletHandler.addFilter(MediaFilter.class, "/*", EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST)); - handlers.addHandler(servletHandler); - } - private void initApi() { ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); servletHandler.setContextPath("/api"); @@ -181,11 +167,17 @@ public class WebServer { servletHandler.addServlet(new ServletHolder(new AsyncSocketServlet()), "/socket"); + ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class); + servletHolder.setInitParameter("resourceBase", config.getString("media.path")); + servletHolder.setInitParameter("dirAllowed", config.getString("media.directoryAllow", "false")); + servletHolder.setInitParameter("pathInfoOnly", "true"); + servletHandler.addServlet(servletHolder, "/media/*"); + servletHandler.addFilter(MediaFilter.class, "/media/*", EnumSet.allOf(DispatcherType.class)); + ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig.registerClasses(JacksonFeature.class, ObjectMapperProvider.class, ResourceErrorHandler.class); resourceConfig.registerClasses(SecurityRequestFilter.class, CorsResponseFilter.class); resourceConfig.packages(ServerResource.class.getPackage().getName()); - servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*"); handlers.addHandler(servletHandler); -- cgit v1.2.3 From 8f4d1c67d3996657c57ed868f34da4066095480c Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 2 Feb 2018 12:39:22 +0500 Subject: Add switch to enable media web access --- src/org/traccar/web/WebServer.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 88b38c1ab..5c3fc8aaa 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -167,12 +167,14 @@ public class WebServer { servletHandler.addServlet(new ServletHolder(new AsyncSocketServlet()), "/socket"); - ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class); - servletHolder.setInitParameter("resourceBase", config.getString("media.path")); - servletHolder.setInitParameter("dirAllowed", config.getString("media.directoryAllow", "false")); - servletHolder.setInitParameter("pathInfoOnly", "true"); - servletHandler.addServlet(servletHolder, "/media/*"); - servletHandler.addFilter(MediaFilter.class, "/media/*", EnumSet.allOf(DispatcherType.class)); + if (config.getBoolean("media.web.enable")) { + ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class); + servletHolder.setInitParameter("resourceBase", config.getString("media.path")); + servletHolder.setInitParameter("dirAllowed", config.getString("media.web.directoryAllow", "false")); + servletHolder.setInitParameter("pathInfoOnly", "true"); + servletHandler.addServlet(servletHolder, "/media/*"); + servletHandler.addFilter(MediaFilter.class, "/media/*", EnumSet.allOf(DispatcherType.class)); + } ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig.registerClasses(JacksonFeature.class, ObjectMapperProvider.class, ResourceErrorHandler.class); -- cgit v1.2.3 From af49fada37f21df9ed5ceb67504c45b714ec24d4 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 2 Feb 2018 13:53:49 +0500 Subject: - Use "media.path" as switch - Handle if MediaManager undefined --- src/org/traccar/protocol/Gt06ProtocolDecoder.java | 8 +++++--- src/org/traccar/protocol/MeitrackProtocolDecoder.java | 4 +++- src/org/traccar/protocol/WatchProtocolDecoder.java | 8 ++++++-- src/org/traccar/web/WebServer.java | 4 ++-- 4 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java index 4173b4d5b..e1bb6f96c 100644 --- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -703,9 +703,11 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { if (photo.writableBytes() > 0) { sendPhotoRequest(channel, pictureId); } else { - Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId()); - position.set( - Position.KEY_IMAGE, Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg")); + if (Context.getMediaManager() != null) { + Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId()); + position.set(Position.KEY_IMAGE, + Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg")); + } photos.remove(pictureId); } diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index 11e38194f..4d42683b1 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -477,7 +477,9 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg")); + if (Context.getMediaManager() != null) { + position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg")); + } photo = null; return position; diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index fe62874b5..cc0364094 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -246,7 +246,9 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { int timeIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); buf.readerIndex(timeIndex + 12 + 2); - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(id, buf, "jpg")); + if (Context.getMediaManager() != null) { + position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(id, buf, "jpg")); + } return position; @@ -257,7 +259,9 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); - position.set(Position.KEY_AUDIO, Context.getMediaManager().writeFile(id, buf, "amr")); + if (Context.getMediaManager() != null) { + position.set(Position.KEY_AUDIO, Context.getMediaManager().writeFile(id, buf, "amr")); + } return position; diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 5c3fc8aaa..06894552d 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -167,10 +167,10 @@ public class WebServer { servletHandler.addServlet(new ServletHolder(new AsyncSocketServlet()), "/socket"); - if (config.getBoolean("media.web.enable")) { + if (config.hasKey("media.path")) { ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class); servletHolder.setInitParameter("resourceBase", config.getString("media.path")); - servletHolder.setInitParameter("dirAllowed", config.getString("media.web.directoryAllow", "false")); + servletHolder.setInitParameter("dirAllowed", config.getString("media.directoryAllow", "false")); servletHolder.setInitParameter("pathInfoOnly", "true"); servletHandler.addServlet(servletHolder, "/media/*"); servletHandler.addFilter(MediaFilter.class, "/media/*", EnumSet.allOf(DispatcherType.class)); -- cgit v1.2.3 From 654116f06161a6f73d23a6bb610f5420cebfdfc9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 3 Feb 2018 08:08:07 +1300 Subject: Revert back parameter name --- src/org/traccar/web/WebServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 06894552d..13eebf34f 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -170,7 +170,7 @@ public class WebServer { if (config.hasKey("media.path")) { ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class); servletHolder.setInitParameter("resourceBase", config.getString("media.path")); - servletHolder.setInitParameter("dirAllowed", config.getString("media.directoryAllow", "false")); + servletHolder.setInitParameter("dirAllowed", config.getString("media.dirAllowed", "false")); servletHolder.setInitParameter("pathInfoOnly", "true"); servletHandler.addServlet(servletHolder, "/media/*"); servletHandler.addFilter(MediaFilter.class, "/media/*", EnumSet.allOf(DispatcherType.class)); -- cgit v1.2.3