aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/api
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-16 16:20:13 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-16 16:20:13 -0700
commitb100cb211161d1014dfaa1ab532f1670c699e80d (patch)
tree48b9191e793391f87e9fe5399d6ddf966040c184 /src/main/java/org/traccar/api
parentb47eca7a90438e738eb1486664717cccc277865f (diff)
downloadtrackermap-server-b100cb211161d1014dfaa1ab532f1670c699e80d.tar.gz
trackermap-server-b100cb211161d1014dfaa1ab532f1670c699e80d.tar.bz2
trackermap-server-b100cb211161d1014dfaa1ab532f1670c699e80d.zip
Improve web injection
Diffstat (limited to 'src/main/java/org/traccar/api')
-rw-r--r--src/main/java/org/traccar/api/AsyncSocketServlet.java2
-rw-r--r--src/main/java/org/traccar/api/MediaFilter.java47
-rw-r--r--src/main/java/org/traccar/api/ObjectMapperProvider.java13
3 files changed, 39 insertions, 23 deletions
diff --git a/src/main/java/org/traccar/api/AsyncSocketServlet.java b/src/main/java/org/traccar/api/AsyncSocketServlet.java
index 317ec469e..7c532179b 100644
--- a/src/main/java/org/traccar/api/AsyncSocketServlet.java
+++ b/src/main/java/org/traccar/api/AsyncSocketServlet.java
@@ -24,9 +24,11 @@ import org.traccar.config.Keys;
import org.traccar.session.ConnectionManager;
import javax.inject.Inject;
+import javax.inject.Singleton;
import javax.servlet.http.HttpSession;
import java.time.Duration;
+@Singleton
public class AsyncSocketServlet extends JettyWebSocketServlet {
private final ObjectMapper objectMapper;
diff --git a/src/main/java/org/traccar/api/MediaFilter.java b/src/main/java/org/traccar/api/MediaFilter.java
index e0609871a..6d95c66a8 100644
--- a/src/main/java/org/traccar/api/MediaFilter.java
+++ b/src/main/java/org/traccar/api/MediaFilter.java
@@ -16,19 +16,7 @@
*/
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 org.traccar.Main;
+import com.google.inject.Provider;
import org.traccar.api.resource.SessionResource;
import org.traccar.api.security.PermissionsService;
import org.traccar.database.StatisticsManager;
@@ -40,8 +28,35 @@ import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Request;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+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 java.io.IOException;
+
+@Singleton
public class MediaFilter implements Filter {
+ private final Storage storage;
+ private final StatisticsManager statisticsManager;
+ private final Provider<PermissionsService> permissionsServiceProvider;
+
+ @Inject
+ public MediaFilter(
+ Storage storage, StatisticsManager statisticsManager,
+ Provider<PermissionsService> permissionsServiceProvider) {
+ this.storage = storage;
+ this.statisticsManager = statisticsManager;
+ this.permissionsServiceProvider = permissionsServiceProvider;
+ }
+
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@@ -50,10 +65,6 @@ public class MediaFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
- PermissionsService permissionsService = Main.getInjector().getInstance(PermissionsService.class);
- Storage storage = Main.getInjector().getInstance(Storage.class);
- StatisticsManager statisticsManager = Main.getInjector().getInstance(StatisticsManager.class);
-
HttpServletResponse httpResponse = (HttpServletResponse) response;
try {
HttpSession session = ((HttpServletRequest) request).getSession(false);
@@ -75,7 +86,7 @@ public class MediaFilter implements Filter {
Device device = storage.getObject(Device.class, new Request(
new Columns.All(), new Condition.Equals("uniqueId", "uniqueId", parts[1])));
if (device != null) {
- permissionsService.checkPermission(Device.class, userId, device.getId());
+ permissionsServiceProvider.get().checkPermission(Device.class, userId, device.getId());
chain.doFilter(request, response);
return;
}
diff --git a/src/main/java/org/traccar/api/ObjectMapperProvider.java b/src/main/java/org/traccar/api/ObjectMapperProvider.java
index cb7adab6c..4d8b266fa 100644
--- a/src/main/java/org/traccar/api/ObjectMapperProvider.java
+++ b/src/main/java/org/traccar/api/ObjectMapperProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2022 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.
@@ -16,17 +16,20 @@
package org.traccar.api;
import com.fasterxml.jackson.databind.ObjectMapper;
-import org.traccar.Main;
import javax.ws.rs.ext.ContextResolver;
-import javax.ws.rs.ext.Provider;
-@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
+ private final ObjectMapper objectMapper;
+
+ public ObjectMapperProvider(ObjectMapper objectMapper) {
+ this.objectMapper = objectMapper;
+ }
+
@Override
public ObjectMapper getContext(Class<?> type) {
- return Main.getInjector().getInstance(ObjectMapper.class);
+ return objectMapper;
}
}