Skip to content

Commit 0981b01

Browse files
committed
Added trace logging of message content
Enabled easier debugging by logging decrypted messages when the trace level is enabled.
1 parent 8f5773a commit 0981b01

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@
4545
<version>0.1.0</version>
4646
</dependency>
4747

48-
<dependency>
49-
<groupId>org.slf4j</groupId>
50-
<artifactId>slf4j-api</artifactId>
51-
<version>1.7.10</version>
52-
</dependency>
53-
5448
<dependency>
5549
<groupId>javax.json</groupId>
5650
<artifactId>javax.json-api</artifactId>
@@ -69,6 +63,12 @@
6963
<version>3.4.1</version>
7064
</dependency>
7165

66+
<dependency>
67+
<groupId>commons-io</groupId>
68+
<artifactId>commons-io</artifactId>
69+
<version>2.4</version>
70+
</dependency>
71+
7272
</dependencies>
7373

7474
<build>

src/main/java/com/beowulfe/hap/impl/http/impl/BinaryHandler.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import io.netty.channel.ChannelHandlerContext;
66
import io.netty.handler.codec.ByteToMessageCodec;
77

8+
import java.io.ByteArrayOutputStream;
89
import java.util.List;
910

11+
import org.apache.commons.io.HexDump;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
1214

@@ -29,6 +31,7 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out)
2931
if (started) {
3032
byte[] b = new byte[msg.readableBytes()];
3133
msg.readBytes(b);
34+
traceData("Sending data", b, ctx);
3235
out.writeBytes(connection.encryptResponse(b));
3336
} else {
3437
out.writeBytes(msg);
@@ -41,6 +44,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in,
4144
byte[] b = new byte[in.readableBytes()];
4245
in.readBytes(b);
4346
byte[] decrypted = connection.decryptRequest(b);
47+
traceData("Received data", decrypted, ctx);
4448
out.add(Unpooled.copiedBuffer(decrypted));
4549
started = true;
4650
}
@@ -51,5 +55,16 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
5155
logger.error("Exception in binary handler", cause);
5256
super.exceptionCaught(ctx, cause);
5357
}
58+
59+
private void traceData(String msg, byte[] b, ChannelHandlerContext ctx) throws Exception {
60+
if (logger.isTraceEnabled()) {
61+
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
62+
HexDump.dump(b, 0, stream, 0);
63+
stream.flush();
64+
logger.trace(String.format("%s [%s]:\n%s\n", msg, ctx.channel().remoteAddress().toString(),
65+
stream.toString()));
66+
}
67+
}
68+
}
5469

5570
}

0 commit comments

Comments
 (0)