diff options
author | Anton Tananaev <anton@traccar.org> | 2022-05-30 11:49:39 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-05-30 11:49:39 -0700 |
commit | 154ff3b2175e67b3fac531cb9c5c5c68880f5e12 (patch) | |
tree | f448a59c75b64deea60b1bd6e79d66790f62a4a8 /src | |
parent | b1a971eedb18a11af2cdc70ba5f8004bae89defd (diff) | |
download | trackermap-server-154ff3b2175e67b3fac531cb9c5c5c68880f5e12.tar.gz trackermap-server-154ff3b2175e67b3fac531cb9c5c5c68880f5e12.tar.bz2 trackermap-server-154ff3b2175e67b3fac531cb9c5c5c68880f5e12.zip |
Inject media manager
Diffstat (limited to 'src')
20 files changed, 56 insertions, 78 deletions
diff --git a/src/main/java/org/traccar/BasePipelineFactory.java b/src/main/java/org/traccar/BasePipelineFactory.java index 89ef76a80..1f383f211 100644 --- a/src/main/java/org/traccar/BasePipelineFactory.java +++ b/src/main/java/org/traccar/BasePipelineFactory.java @@ -109,7 +109,9 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { pipeline.addLast(new StandardLoggingHandler(protocol)); addProtocolHandlers(handler -> { - if (!(handler instanceof BaseProtocolDecoder || handler instanceof BaseProtocolEncoder)) { + if (handler instanceof BaseProtocolDecoder || handler instanceof BaseProtocolEncoder) { + Main.getInjector().injectMembers(handler); + } else { if (handler instanceof ChannelInboundHandler) { handler = new WrapperInboundHandler((ChannelInboundHandler) handler); } else { @@ -144,9 +146,8 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { AlertEventHandler.class, IgnitionEventHandler.class, MaintenanceEventHandler.class, - DriverEventHandler.class); - - pipeline.addLast(new MainEventHandler()); + DriverEventHandler.class, + MainEventHandler.class); } } diff --git a/src/main/java/org/traccar/BaseProtocolDecoder.java b/src/main/java/org/traccar/BaseProtocolDecoder.java index a40756796..505e7926f 100644 --- a/src/main/java/org/traccar/BaseProtocolDecoder.java +++ b/src/main/java/org/traccar/BaseProtocolDecoder.java @@ -15,6 +15,8 @@ */ package org.traccar; +import com.google.inject.Inject; +import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; import io.netty.channel.socket.DatagramChannel; import io.netty.handler.codec.http.HttpRequestDecoder; @@ -25,6 +27,7 @@ import org.traccar.config.Keys; import org.traccar.database.CommandsManager; import org.traccar.database.ConnectionManager; import org.traccar.database.IdentityManager; +import org.traccar.database.MediaManager; import org.traccar.database.StatisticsManager; import org.traccar.helper.UnitsConverter; import org.traccar.model.Command; @@ -53,11 +56,22 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { private final StatisticsManager statisticsManager; private final Protocol protocol; + private MediaManager mediaManager; + public BaseProtocolDecoder(Protocol protocol) { this.protocol = protocol; statisticsManager = Main.getInjector() != null ? Main.getInjector().getInstance(StatisticsManager.class) : null; } + @Inject + public void setMediaManager(MediaManager mediaManager) { + this.mediaManager = mediaManager; + } + + public String writeMediaFile(String uniqueId, ByteBuf buf, String extension) { + return mediaManager.writeFile(uniqueId, buf, extension); + } + public String getProtocolName() { return protocol != null ? protocol.getName() : PROTOCOL_UNKNOWN; } diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index ee14f8a1a..237a34624 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -37,7 +37,6 @@ import org.traccar.database.IdentityManager; import org.traccar.database.LdapProvider; import org.traccar.database.MailManager; import org.traccar.database.MaintenancesManager; -import org.traccar.database.MediaManager; import org.traccar.database.NotificationManager; import org.traccar.database.OrderManager; import org.traccar.database.PermissionsManager; @@ -114,12 +113,6 @@ public final class Context { return mailManager; } - private static MediaManager mediaManager; - - public static MediaManager getMediaManager() { - return mediaManager; - } - private static UsersManager usersManager; public static UsersManager getUsersManager() { @@ -313,8 +306,6 @@ public final class Context { mailManager = new MailManager(); - mediaManager = new MediaManager(config.getString(Keys.MEDIA_PATH)); - if (dataManager != null) { usersManager = new UsersManager(dataManager); groupsManager = new GroupsManager(dataManager); @@ -390,13 +381,12 @@ public final class Context { velocityEngine.init(velocityProperties); } - public static void init(IdentityManager testIdentityManager, MediaManager testMediaManager) { + public static void init(IdentityManager testIdentityManager) { config = new Config(); objectMapper = new ObjectMapper(); objectMapper.registerModule(new JSR353Module()); client = ClientBuilder.newClient().register(new ObjectMapperContextResolver()); identityManager = testIdentityManager; - mediaManager = testMediaManager; } public static <T extends BaseModel> BaseObjectManager<T> getManager(Class<T> clazz) { diff --git a/src/main/java/org/traccar/MainEventHandler.java b/src/main/java/org/traccar/MainEventHandler.java index f1f2527bc..91706222a 100644 --- a/src/main/java/org/traccar/MainEventHandler.java +++ b/src/main/java/org/traccar/MainEventHandler.java @@ -30,6 +30,7 @@ import org.traccar.helper.NetworkUtil; import org.traccar.model.Position; import org.traccar.storage.StorageException; +import javax.inject.Inject; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedHashSet; @@ -42,6 +43,7 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter { private final Set<String> connectionlessProtocols = new HashSet<>(); private final Set<String> logAttributes = new LinkedHashSet<>(); + @Inject public MainEventHandler() { String connectionlessProtocolList = Context.getConfig().getString(Keys.STATUS_IGNORE_OFFLINE); if (connectionlessProtocolList != null) { diff --git a/src/main/java/org/traccar/database/GroupsManager.java b/src/main/java/org/traccar/database/GroupsManager.java index 9322dd80a..4df848042 100644 --- a/src/main/java/org/traccar/database/GroupsManager.java +++ b/src/main/java/org/traccar/database/GroupsManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2022 Anton Tananaev (anton@traccar.org) * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,7 +19,6 @@ package org.traccar.database; import java.util.HashSet; import java.util.Set; -import org.traccar.Context; import org.traccar.model.Group; import org.traccar.storage.StorageException; diff --git a/src/main/java/org/traccar/database/MediaManager.java b/src/main/java/org/traccar/database/MediaManager.java index edade5766..5f3fdcdf7 100644 --- a/src/main/java/org/traccar/database/MediaManager.java +++ b/src/main/java/org/traccar/database/MediaManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 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. @@ -18,7 +18,10 @@ package org.traccar.database; import io.netty.buffer.ByteBuf; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.traccar.config.Config; +import org.traccar.config.Keys; +import javax.inject.Inject; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -34,10 +37,11 @@ public class MediaManager { private static final Logger LOGGER = LoggerFactory.getLogger(MediaManager.class); - private String path; + private final String path; - public MediaManager(String path) { - this.path = path; + @Inject + public MediaManager(Config config) { + this.path = config.getString(Keys.MEDIA_PATH); } private File createFile(String uniqueId, String name) throws IOException { diff --git a/src/main/java/org/traccar/protocol/AtrackProtocolDecoder.java b/src/main/java/org/traccar/protocol/AtrackProtocolDecoder.java index 186b81470..247a1b696 100644 --- a/src/main/java/org/traccar/protocol/AtrackProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/AtrackProtocolDecoder.java @@ -626,7 +626,7 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, new Date(time * 1000)); - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(String.valueOf(id), photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(String.valueOf(id), photo, "jpg")); photo.release(); photo = null; diff --git a/src/main/java/org/traccar/protocol/DualcamProtocolDecoder.java b/src/main/java/org/traccar/protocol/DualcamProtocolDecoder.java index c64b8171f..3c15d41eb 100644 --- a/src/main/java/org/traccar/protocol/DualcamProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/DualcamProtocolDecoder.java @@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.Context; import org.traccar.DeviceSession; import org.traccar.NetworkMessage; import org.traccar.Protocol; @@ -98,7 +97,7 @@ public class DualcamProtocolDecoder extends BaseProtocolDecoder { position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); try { - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(uniqueId, photo, "jpg")); } finally { photo.release(); photo = null; diff --git a/src/main/java/org/traccar/protocol/FifotrackProtocolDecoder.java b/src/main/java/org/traccar/protocol/FifotrackProtocolDecoder.java index 5f9326a61..53f35c3cd 100644 --- a/src/main/java/org/traccar/protocol/FifotrackProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/FifotrackProtocolDecoder.java @@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.Context; import org.traccar.DeviceSession; import org.traccar.NetworkMessage; import org.traccar.Protocol; @@ -381,7 +380,7 @@ public class FifotrackProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(getProtocolName()); position.setDeviceId(getDeviceSession(channel, remoteAddress, imei).getDeviceId()); getLastLocation(position, null); - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(imei, photo, "jpg")); photo.release(); photo = null; return position; diff --git a/src/main/java/org/traccar/protocol/GalileoProtocolDecoder.java b/src/main/java/org/traccar/protocol/GalileoProtocolDecoder.java index f29fb9850..eb553c5a9 100644 --- a/src/main/java/org/traccar/protocol/GalileoProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/GalileoProtocolDecoder.java @@ -329,7 +329,7 @@ public class GalileoProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(uniqueId, photo, "jpg")); photo.release(); photo = null; diff --git a/src/main/java/org/traccar/protocol/Gps103ProtocolDecoder.java b/src/main/java/org/traccar/protocol/Gps103ProtocolDecoder.java index d74f19179..d5aa45b9c 100644 --- a/src/main/java/org/traccar/protocol/Gps103ProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/Gps103ProtocolDecoder.java @@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.Context; import org.traccar.DeviceSession; import org.traccar.NetworkMessage; import org.traccar.Protocol; @@ -361,7 +360,7 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); try { - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(imei, photo, "jpg")); } finally { photoPackets = 0; photo.release(); diff --git a/src/main/java/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/main/java/org/traccar/protocol/Gt06ProtocolDecoder.java index 89931f614..832645374 100644 --- a/src/main/java/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -1028,8 +1028,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { sendPhotoRequest(channel, pictureId); } else { Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId()); - position.set( - Position.KEY_IMAGE, Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(device.getUniqueId(), photo, "jpg")); photos.remove(pictureId).release(); } @@ -1265,8 +1264,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, new Date(timestamp)); Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId()); - position.set(Position.KEY_IMAGE, - Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(device.getUniqueId(), photo, "jpg")); photos.remove(mediaId).release(); } } diff --git a/src/main/java/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/main/java/org/traccar/protocol/MeiligaoProtocolDecoder.java index d38e5d1c3..a25cab06f 100644 --- a/src/main/java/org/traccar/protocol/MeiligaoProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/MeiligaoProtocolDecoder.java @@ -472,7 +472,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { String uniqueId = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getUniqueId(); ByteBuf photo = photos.remove(imageIndex); try { - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(uniqueId, photo, "jpg")); } finally { photo.release(); } diff --git a/src/main/java/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/main/java/org/traccar/protocol/MeitrackProtocolDecoder.java index 013e297c0..6fed56fb6 100644 --- a/src/main/java/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -589,7 +589,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(imei, photo, "jpg")); photo.release(); photo = null; diff --git a/src/main/java/org/traccar/protocol/Pt502ProtocolDecoder.java b/src/main/java/org/traccar/protocol/Pt502ProtocolDecoder.java index ff92b51f1..5e24cacdc 100644 --- a/src/main/java/org/traccar/protocol/Pt502ProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/Pt502ProtocolDecoder.java @@ -182,7 +182,7 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(uniqueId, photo, "jpg")); photo.release(); photo = null; diff --git a/src/main/java/org/traccar/protocol/RuptelaProtocolDecoder.java b/src/main/java/org/traccar/protocol/RuptelaProtocolDecoder.java index 5a0383358..7abb52bd0 100644 --- a/src/main/java/org/traccar/protocol/RuptelaProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/RuptelaProtocolDecoder.java @@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.Context; import org.traccar.DeviceSession; import org.traccar.NetworkMessage; import org.traccar.Protocol; @@ -298,7 +297,7 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(imei, photo, "jpg")); photo.release(); photo = null; return position; diff --git a/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java b/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java index f83a49941..61a61b900 100644 --- a/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java @@ -148,7 +148,7 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { String uniqueId = Context.getIdentityManager().getById(position.getDeviceId()).getUniqueId(); photos.remove(photoId); try { - position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); + position.set(Position.KEY_IMAGE, writeMediaFile(uniqueId, photo, "jpg")); } finally { photo.release(); } diff --git a/src/main/java/org/traccar/protocol/WatchProtocolDecoder.java b/src/main/java/org/traccar/protocol/WatchProtocolDecoder.java index 420866578..3967fb804 100644 --- a/src/main/java/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/WatchProtocolDecoder.java @@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.Context; import org.traccar.DeviceSession; import org.traccar.NetworkMessage; import org.traccar.Protocol; @@ -313,7 +312,7 @@ 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")); + position.set(Position.KEY_IMAGE, writeMediaFile(id, buf, "jpg")); return position; @@ -339,7 +338,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); - position.set(Position.KEY_AUDIO, Context.getMediaManager().writeFile(id, audio, "amr")); + position.set(Position.KEY_AUDIO, writeMediaFile(id, audio, "amr")); audio.release(); audio = null; return position; @@ -356,7 +355,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); - position.set(Position.KEY_AUDIO, Context.getMediaManager().writeFile(id, buf, "amr")); + position.set(Position.KEY_AUDIO, writeMediaFile(id, buf, "amr")); return position; diff --git a/src/test/java/org/traccar/BaseTest.java b/src/test/java/org/traccar/BaseTest.java index 0b2c616ce..1dddbb03b 100644 --- a/src/test/java/org/traccar/BaseTest.java +++ b/src/test/java/org/traccar/BaseTest.java @@ -1,34 +1,9 @@ package org.traccar; -import io.netty.buffer.ByteBuf; -import org.traccar.database.MediaManager; - -import java.util.HashMap; -import java.util.Map; - public class BaseTest { - public static class MockMediaManager extends MediaManager { - Map<String, ByteBuf> files = new HashMap<>(); - - MockMediaManager() { - super(""); - } - - @Override - public String writeFile(String uniqueId, ByteBuf buf, String extension) { - String fileName = uniqueId + "/mock." + extension; - files.put(fileName, buf); - return fileName; - } - - public ByteBuf readFile(String fileName) { - return files.get(fileName); - } - } - static { - Context.init(new TestIdentityManager(), new MockMediaManager()); + Context.init(new TestIdentityManager()); } } diff --git a/src/test/java/org/traccar/protocol/WatchProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/WatchProtocolDecoderTest.java index ef6c33da9..50baaa81a 100644 --- a/src/test/java/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -1,12 +1,11 @@ package org.traccar.protocol; -import io.netty.buffer.ByteBuf; import org.junit.Test; -import org.traccar.Context; import org.traccar.ProtocolTest; +import org.traccar.database.MediaManager; import org.traccar.model.Position; -import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; public class WatchProtocolDecoderTest extends ProtocolTest { @@ -148,14 +147,15 @@ public class WatchProtocolDecoderTest extends ProtocolTest { var decoder = new WatchProtocolDecoder(null); - verifyNull(decoder.decode(null, null, buffer("[CS*1234567890*0004*TK,1]"))); + var mediaManager = mock(MediaManager.class); + when(mediaManager.writeFile(any(), any(), any())).thenReturn("mock.amr"); + decoder.setMediaManager(mediaManager); - ByteBuf data = binary("7d5b5d2c2aff"); + verifyAttribute(decoder, concatenateBuffers( + buffer("[CS*1234567890*000e*TK,#!AMR"), binary("7d5b5d2c2aff"), buffer("]")), + Position.KEY_AUDIO, "mock.amr"); - Object decodedObject = decoder.decode(null, null, concatenateBuffers(buffer("[CS*1234567890*000e*TK,#!AMR"), data.resetReaderIndex(), buffer("]"))); - assertEquals("1234567890/mock.amr", ((Position) decodedObject).getAttributes().get("audio")); - - verifyFrame(concatenateBuffers(buffer("#!AMR"), data.resetReaderIndex()), ((MockMediaManager) Context.getMediaManager()).readFile("1234567890/mock.amr")); + verify(mediaManager).writeFile(any(), any(), any()); } |