diff options
Diffstat (limited to 'src/main/java/org/traccar/handler/network')
4 files changed, 25 insertions, 14 deletions
diff --git a/src/main/java/org/traccar/handler/network/AcknowledgementHandler.java b/src/main/java/org/traccar/handler/network/AcknowledgementHandler.java index e87f5d34c..7ba625e00 100644 --- a/src/main/java/org/traccar/handler/network/AcknowledgementHandler.java +++ b/src/main/java/org/traccar/handler/network/AcknowledgementHandler.java @@ -92,12 +92,10 @@ public class AcknowledgementHandler extends ChannelOutboundHandlerAdapter { if (queue == null) { queue = new LinkedList<>(); } - } else if (msg instanceof EventDecoded) { - EventDecoded event = (EventDecoded) msg; + } else if (msg instanceof EventDecoded event) { LOGGER.debug("Event decoded {}", event.getObjects().size()); waiting.addAll(event.getObjects()); - } else if (msg instanceof EventHandled) { - EventHandled event = (EventHandled) msg; + } else if (msg instanceof EventHandled event) { LOGGER.debug("Event handled"); waiting.remove(event.getObject()); } diff --git a/src/main/java/org/traccar/handler/network/NetworkMessageHandler.java b/src/main/java/org/traccar/handler/network/NetworkMessageHandler.java index c2fb9016a..f151c91a9 100644 --- a/src/main/java/org/traccar/handler/network/NetworkMessageHandler.java +++ b/src/main/java/org/traccar/handler/network/NetworkMessageHandler.java @@ -32,16 +32,14 @@ public class NetworkMessageHandler extends ChannelDuplexHandler { if (ctx.channel() instanceof DatagramChannel) { DatagramPacket packet = (DatagramPacket) msg; ctx.fireChannelRead(new NetworkMessage(packet.content(), packet.sender())); - } else if (msg instanceof ByteBuf) { - ByteBuf buffer = (ByteBuf) msg; + } else if (msg instanceof ByteBuf buffer) { ctx.fireChannelRead(new NetworkMessage(buffer, ctx.channel().remoteAddress())); } } @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { - if (msg instanceof NetworkMessage) { - NetworkMessage message = (NetworkMessage) msg; + if (msg instanceof NetworkMessage message) { if (ctx.channel() instanceof DatagramChannel) { InetSocketAddress recipient = (InetSocketAddress) message.getRemoteAddress(); InetSocketAddress sender = (InetSocketAddress) ctx.channel().localAddress(); diff --git a/src/main/java/org/traccar/handler/network/RemoteAddressHandler.java b/src/main/java/org/traccar/handler/network/RemoteAddressHandler.java index c52bb2be1..9d5504ce1 100644 --- a/src/main/java/org/traccar/handler/network/RemoteAddressHandler.java +++ b/src/main/java/org/traccar/handler/network/RemoteAddressHandler.java @@ -44,8 +44,7 @@ public class RemoteAddressHandler extends ChannelInboundHandlerAdapter { InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); String hostAddress = remoteAddress != null ? remoteAddress.getAddress().getHostAddress() : null; - if (msg instanceof Position) { - Position position = (Position) msg; + if (msg instanceof Position position) { position.set(Position.KEY_IP, hostAddress); } } diff --git a/src/main/java/org/traccar/handler/network/StandardLoggingHandler.java b/src/main/java/org/traccar/handler/network/StandardLoggingHandler.java index dae93655d..8e63cae4d 100644 --- a/src/main/java/org/traccar/handler/network/StandardLoggingHandler.java +++ b/src/main/java/org/traccar/handler/network/StandardLoggingHandler.java @@ -24,22 +24,33 @@ import jakarta.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.NetworkMessage; +import org.traccar.config.Config; +import org.traccar.config.Keys; +import org.traccar.helper.BufferUtil; import org.traccar.helper.NetworkUtil; import org.traccar.model.LogRecord; import org.traccar.session.ConnectionManager; +import java.nio.charset.StandardCharsets; + public class StandardLoggingHandler extends ChannelDuplexHandler { private static final Logger LOGGER = LoggerFactory.getLogger(StandardLoggingHandler.class); private final String protocol; private ConnectionManager connectionManager; + private boolean decodeTextData; public StandardLoggingHandler(String protocol) { this.protocol = protocol; } @Inject + public void setConfig(Config config) { + decodeTextData = config.getBoolean(Keys.LOGGER_TEXT_PROTOCOL); + } + + @Inject public void setConnectionManager(ConnectionManager connectionManager) { this.connectionManager = connectionManager; } @@ -61,12 +72,17 @@ public class StandardLoggingHandler extends ChannelDuplexHandler { } private LogRecord createLogRecord(ChannelHandlerContext ctx, Object msg) { - if (msg instanceof NetworkMessage) { - NetworkMessage networkMessage = (NetworkMessage) msg; - if (networkMessage.getMessage() instanceof ByteBuf) { + if (msg instanceof NetworkMessage networkMessage) { + if (networkMessage.getMessage() instanceof ByteBuf data) { LogRecord record = new LogRecord(ctx.channel().localAddress(), networkMessage.getRemoteAddress()); record.setProtocol(protocol); - record.setData(ByteBufUtil.hexDump((ByteBuf) networkMessage.getMessage())); + if (decodeTextData && BufferUtil.isPrintable(data, data.readableBytes())) { + record.setData(data.getCharSequence( + data.readerIndex(), data.readableBytes(), StandardCharsets.US_ASCII).toString() + .replace("\r", "\\r").replace("\n", "\\n")); + } else { + record.setData(ByteBufUtil.hexDump(data)); + } return record; } } |