Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/main/java/netty/syslog/DecoderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,32 @@ static byte peek(ByteBuf buffer) {
}

static void expect(ByteBuf buffer, char c) {
if (buffer.readByte() != c) {
throw new DecoderException("Expected " + c + " at index " + buffer.readerIndex());
byte readByte = buffer.readByte();
if (readByte != c) {
throw new DecoderException("Expected " + c + " at index " + buffer.readerIndex() + " got "+ (char)readByte);
}
}

static void skipStructuredData(ByteBuf buffer, boolean checkNull) {
if (checkNull && peek(buffer) == '-') {
buffer.readByte();
return;
}
expect(buffer, '[');
int length = -1;
for (int i = buffer.readerIndex(); i < buffer.capacity(); i++) {
if (buffer.getByte(i) == ' ' && buffer.getByte(i-1) == ']') {
length = i - buffer.readerIndex();
break;
}
}
if (length < 0) {
length = buffer.readableBytes();
}
buffer.skipBytes(length);
return;
}

static String readStringToSpace(ByteBuf buffer, boolean checkNull) {
if (checkNull && peek(buffer) == '-') {
buffer.readByte();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/netty/syslog/MessageDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.DecoderException;
import io.netty.util.CharsetUtil;

import java.time.ZonedDateTime;
import java.util.List;
Expand All @@ -28,6 +29,7 @@
import static netty.syslog.DecoderUtil.peek;
import static netty.syslog.DecoderUtil.readDigit;
import static netty.syslog.DecoderUtil.readStringToSpace;
import static netty.syslog.DecoderUtil.skipStructuredData;

public class MessageDecoder extends ByteToMessageDecoder {

Expand Down Expand Up @@ -85,8 +87,8 @@ protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object
messageBuilder.messageId(readStringToSpace(buffer, true));
expect(buffer, ' ');

// TODO Decode structured data
expect(buffer, '-');
// TODO Decode structured data discard for now
skipStructuredData(buffer, true);
expect(buffer, ' ');

final int length = buffer.readableBytes();
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/netty/syslog/MessageDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,23 @@ public void decode() throws Exception {
assertEquals(message.content().toString(StandardCharsets.UTF_8), "SHLVL : 1");
}

@Test
public void decodeStructuredData() throws Exception {
final MessageDecoder decoder = new MessageDecoder();
final List<Object> messageList = new ArrayList<>();

decoder.decode(null, Unpooled.wrappedBuffer("<14>1 2014-03-20T20:14:14+00:00 loggregator 20d38e29-85bb-4833-81c8-99ba7d0c1b09 [App/0] - [joe=fred dude=garbage][bob=joe] SHLVL : 1".getBytes()), messageList);
assertEquals(messageList.size(), 1);
final Message message = ((Message) messageList.remove(0));
assertEquals(message.getFacility(), Message.Facility.USER_LEVEL);
assertEquals(message.getSeverity(), Message.Severity.INFORMATION);
assertEquals(message.getTimestamp(), ZonedDateTime.parse("2014-03-20T20:14:14Z"));
assertEquals(message.getHostname(), "loggregator");
assertEquals(message.getApplicationName(), "20d38e29-85bb-4833-81c8-99ba7d0c1b09");
assertEquals(message.getProcessId(), "[App/0]");
assertNull(message.getMessageId());
assertEquals(message.getStructuredData().size(), 0);
assertEquals(message.content().toString(StandardCharsets.UTF_8), "SHLVL : 1");
}

}