aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-18 10:08:52 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-18 10:08:52 -0700
commit22bd8bcc80b850dc2308be50cf2886c39b0655da (patch)
tree01f98cf67e21f06f94f85b8cc38ae63b8ec39df9 /src
parentc248ed30047a0525bf792730a0fbd4de0c89ad8e (diff)
downloadtrackermap-server-22bd8bcc80b850dc2308be50cf2886c39b0655da.tar.gz
trackermap-server-22bd8bcc80b850dc2308be50cf2886c39b0655da.tar.bz2
trackermap-server-22bd8bcc80b850dc2308be50cf2886c39b0655da.zip
Refactor device password
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/traccar/BaseProtocolEncoder.java20
-rw-r--r--src/main/java/org/traccar/config/Keys.java7
-rw-r--r--src/main/java/org/traccar/database/DeviceManager.java18
-rw-r--r--src/main/java/org/traccar/database/IdentityManager.java2
-rw-r--r--src/main/java/org/traccar/helper/model/AttributeUtil.java21
-rw-r--r--src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java5
-rw-r--r--src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java5
-rw-r--r--src/test/java/org/traccar/BaseTest.java6
8 files changed, 55 insertions, 29 deletions
diff --git a/src/main/java/org/traccar/BaseProtocolEncoder.java b/src/main/java/org/traccar/BaseProtocolEncoder.java
index 10b780fc8..612d91c57 100644
--- a/src/main/java/org/traccar/BaseProtocolEncoder.java
+++ b/src/main/java/org/traccar/BaseProtocolEncoder.java
@@ -15,7 +15,6 @@
*/
package org.traccar;
-import com.google.inject.Inject;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
@@ -24,7 +23,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.database.IdentityManager;
import org.traccar.helper.NetworkUtil;
+import org.traccar.helper.model.AttributeUtil;
import org.traccar.model.Command;
+import org.traccar.session.cache.CacheManager;
+
+import javax.inject.Inject;
public abstract class BaseProtocolEncoder extends ChannelOutboundHandlerAdapter {
@@ -34,12 +37,23 @@ public abstract class BaseProtocolEncoder extends ChannelOutboundHandlerAdapter
private final Protocol protocol;
+ private CacheManager cacheManager;
+
private IdentityManager identityManager;
public BaseProtocolEncoder(Protocol protocol) {
this.protocol = protocol;
}
+ public CacheManager getCacheManager() {
+ return cacheManager;
+ }
+
+ @Inject
+ public void setCacheManager(CacheManager cacheManager) {
+ this.cacheManager = cacheManager;
+ }
+
public IdentityManager getIdentityManager() {
return identityManager;
}
@@ -59,8 +73,8 @@ public abstract class BaseProtocolEncoder extends ChannelOutboundHandlerAdapter
protected void initDevicePassword(Command command, String defaultPassword) {
if (!command.getAttributes().containsKey(Command.KEY_DEVICE_PASSWORD)) {
- String password = identityManager
- .getDevicePassword(command.getDeviceId(), getProtocolName(), defaultPassword);
+ String password = AttributeUtil.getDevicePassword(
+ cacheManager, command.getDeviceId(), getProtocolName(), defaultPassword);
command.set(Command.KEY_DEVICE_PASSWORD, password);
}
}
diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java
index 465751d38..410947079 100644
--- a/src/main/java/org/traccar/config/Keys.java
+++ b/src/main/java/org/traccar/config/Keys.java
@@ -72,6 +72,13 @@ public final class Keys {
/**
* Device password. Commonly used in some protocol for sending commands.
*/
+ public static final ConfigKey<String> DEVICE_PASSWORD = new StringConfigKey(
+ "devicePassword",
+ List.of(KeyType.DEVICE));
+
+ /**
+ * Device password. Commonly used in some protocol for sending commands.
+ */
public static final ConfigSuffix<String> PROTOCOL_DEVICE_PASSWORD = new StringConfigSuffix(
".devicePassword",
List.of(KeyType.CONFIG));
diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java
index a3e04f920..eee19c4c2 100644
--- a/src/main/java/org/traccar/database/DeviceManager.java
+++ b/src/main/java/org/traccar/database/DeviceManager.java
@@ -100,24 +100,6 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
}
@Override
- public String getDevicePassword(long id, String protocol, String defaultPassword) {
-
- String password = lookupAttributeString(id, Command.KEY_DEVICE_PASSWORD, null, false, false);
- if (password != null) {
- return password;
- }
-
- if (protocol != null) {
- password = Context.getConfig().getString(Keys.PROTOCOL_DEVICE_PASSWORD.withPrefix(protocol));
- if (password != null) {
- return password;
- }
- }
-
- return defaultPassword;
- }
-
- @Override
public Set<Long> getAllItems() {
Set<Long> result = super.getAllItems();
if (result.isEmpty()) {
diff --git a/src/main/java/org/traccar/database/IdentityManager.java b/src/main/java/org/traccar/database/IdentityManager.java
index 10a64ebd9..1897c637f 100644
--- a/src/main/java/org/traccar/database/IdentityManager.java
+++ b/src/main/java/org/traccar/database/IdentityManager.java
@@ -24,8 +24,6 @@ public interface IdentityManager {
Device getByUniqueId(String uniqueId) throws Exception;
- String getDevicePassword(long id, String protocol, String defaultPassword);
-
Position getLastPosition(long deviceId);
boolean isLatestPosition(Position position);
diff --git a/src/main/java/org/traccar/helper/model/AttributeUtil.java b/src/main/java/org/traccar/helper/model/AttributeUtil.java
index 5b3fc1cbe..225089d5c 100644
--- a/src/main/java/org/traccar/helper/model/AttributeUtil.java
+++ b/src/main/java/org/traccar/helper/model/AttributeUtil.java
@@ -15,8 +15,11 @@
*/
package org.traccar.helper.model;
+import org.traccar.Context;
import org.traccar.config.ConfigKey;
import org.traccar.config.KeyType;
+import org.traccar.config.Keys;
+import org.traccar.model.Command;
import org.traccar.model.Device;
import org.traccar.model.Group;
import org.traccar.session.cache.CacheManager;
@@ -69,4 +72,22 @@ public final class AttributeUtil {
return key.getDefaultValue();
}
+ public static String getDevicePassword(
+ CacheManager cacheManager, long deviceId, String protocol, String defaultPassword) {
+
+ String password = lookup(cacheManager, Keys.DEVICE_PASSWORD, deviceId);
+ if (password != null) {
+ return password;
+ }
+
+ if (protocol != null) {
+ password = cacheManager.getConfig().getString(Keys.PROTOCOL_DEVICE_PASSWORD.withPrefix(protocol));
+ if (password != null) {
+ return password;
+ }
+ }
+
+ return defaultPassword;
+ }
+
}
diff --git a/src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java b/src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java
index 3ed828fc7..569f4a809 100644
--- a/src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java
+++ b/src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java
@@ -20,6 +20,7 @@ import io.netty.buffer.Unpooled;
import org.traccar.BaseProtocolEncoder;
import org.traccar.Protocol;
import org.traccar.helper.Checksum;
+import org.traccar.helper.model.AttributeUtil;
import org.traccar.model.Command;
import java.nio.charset.StandardCharsets;
@@ -68,8 +69,8 @@ public class Gt06ProtocolEncoder extends BaseProtocolEncoder {
boolean alternative = getIdentityManager().lookupAttributeBoolean(
command.getDeviceId(), getProtocolName() + ".alternative", false, false, true);
- String password = getIdentityManager()
- .getDevicePassword(command.getDeviceId(), getProtocolName(), "123456");
+ String password = AttributeUtil.getDevicePassword(
+ getCacheManager(), command.getDeviceId(), getProtocolName(), "123456");
switch (command.getType()) {
case Command.TYPE_ENGINE_STOP:
diff --git a/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java b/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java
index c55c0624d..e9570ee11 100644
--- a/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java
@@ -17,6 +17,7 @@ package org.traccar.protocol;
import io.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
+import org.traccar.helper.model.AttributeUtil;
import org.traccar.session.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
@@ -252,8 +253,8 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder {
sendAcknowledge(status, event, checksum, channel, remoteAddress);
- String devicePassword = getIdentityManager()
- .getDevicePassword(deviceSession.getDeviceId(), getProtocolName(), DEFAULT_DEVICE_PASSWORD);
+ String devicePassword = AttributeUtil.getDevicePassword(
+ getCacheManager(), deviceSession.getDeviceId(), getProtocolName(), DEFAULT_DEVICE_PASSWORD);
sendEventResponse(event, devicePassword, channel, remoteAddress);
}
diff --git a/src/test/java/org/traccar/BaseTest.java b/src/test/java/org/traccar/BaseTest.java
index add423cdd..54035553f 100644
--- a/src/test/java/org/traccar/BaseTest.java
+++ b/src/test/java/org/traccar/BaseTest.java
@@ -66,9 +66,11 @@ public class BaseTest {
var device = mock(Device.class);
when(device.getId()).thenReturn(1L);
when(device.getUniqueId()).thenReturn("123456789012345");
+ var cacheManager = mock(CacheManager.class);
+ when(cacheManager.getConfig()).thenReturn(mock(Config.class));
+ when(cacheManager.getObject(eq(Device.class), anyLong())).thenReturn(device);
+ encoder.setCacheManager(cacheManager);
var identityManager = mock(IdentityManager.class);
- when(identityManager.getDevicePassword(anyLong(), any(), any()))
- .thenAnswer(invocation -> invocation.getArguments()[2]);
when(identityManager.getById(anyLong())).thenReturn(device);
when(identityManager.getByUniqueId(any())).thenReturn(device);
encoder.setIdentityManager(identityManager);