use of org.apache.nifi.processors.lumberjack.frame.LumberjackFrame in project nifi by apache.
the class TestLumberjackFrameHandler method testData.
@Test
public void testData() throws IOException, InterruptedException {
final byte[] payload = new byte[] { // Number of pairs
0x00, // Number of pairs
0x00, // Number of pairs
0x00, // Number of pairs
0x02, // Length of first pair key ('line')
0x00, // Length of first pair key ('line')
0x00, // Length of first pair key ('line')
0x00, // Length of first pair key ('line')
0x04, // 'line'
0x6C, // 'line'
0x69, // 'line'
0x6E, // 'line'
0x65, // Length of 'test-content'
0x00, // Length of 'test-content'
0x00, // Length of 'test-content'
0x00, // Length of 'test-content'
0x0C, //
0x74, //
0x65, //
0x73, //
0x74, // 'test-content'
0x2d, // 'test-content'
0x63, // 'test-content'
0x6f, // 'test-content'
0x6e, //
0x74, //
0x65, //
0x6e, //
0x74, // Length of 2nd pair key (field)
0x00, // Length of 2nd pair key (field)
0x00, // Length of 2nd pair key (field)
0x00, // Length of 2nd pair key (field)
0x05, //
0x66, //
0x69, //
0x65, //
0x6c, // 'field'
0x64, // Length of 'value'
0x00, // Length of 'value'
0x00, // Length of 'value'
0x00, // Length of 'value'
0x05, // 'value'
0x76, // 'value'
0x61, // 'value'
0x6c, // 'value'
0x75, 0x65 };
final LumberjackFrame dataFrame = new LumberjackFrame.Builder().version((byte) 0x31).frameType((byte) 0x44).seqNumber(1).payload(payload).build();
final String sender = "sender1";
final CapturingChannelResponder responder = new CapturingChannelResponder();
// call the handler and verify respond() was called once with once response
frameHandler.handle(dataFrame, responder, sender);
// No response expected
Assert.assertEquals(0, responder.responded);
// But events should contain one event
Assert.assertEquals(1, events.size());
final LumberjackEvent event = events.poll();
Assert.assertEquals("{\"field\":\"value\"}", new String((event.getFields())));
Assert.assertEquals("test-content", new String(event.getData(), charset));
}
use of org.apache.nifi.processors.lumberjack.frame.LumberjackFrame in project nifi by apache.
the class LumberjackSSLSocketChannelHandler method processBuffer.
@Override
protected void processBuffer(final SSLSocketChannel sslSocketChannel, final SocketChannel socketChannel, final int bytesRead, final byte[] buffer) throws InterruptedException, IOException {
final InetAddress sender = socketChannel.socket().getInetAddress();
try {
// go through the buffer parsing the Lumberjack command
for (int i = 0; i < bytesRead; i++) {
byte currByte = buffer[i];
// if we found the end of a frame, handle the frame and mark the buffer
if (decoder.process(currByte)) {
final List<LumberjackFrame> frames = decoder.getFrames();
// A list of events has been generated
for (LumberjackFrame frame : frames) {
logger.debug("Received Lumberjack frame with transaction {} and command {}", new Object[] { frame.getSeqNumber(), frame.getSeqNumber() });
// Ignore the WINDOWS type frames as they contain no payload.
if (frame.getFrameType() != 0x57) {
final SSLSocketChannelResponder responder = new SSLSocketChannelResponder(socketChannel, sslSocketChannel);
frameHandler.handle(frame, responder, sender.toString());
}
}
}
}
logger.debug("Done processing buffer");
} catch (final LumberjackFrameException rfe) {
logger.error("Error reading Lumberjack frames due to {}", new Object[] { rfe.getMessage() }, rfe);
// if an invalid frame or bad data was sent then the decoder will be left in a
// corrupted state, so lets close the connection and cause the client to re-establish
dispatcher.completeConnection(key);
}
}
use of org.apache.nifi.processors.lumberjack.frame.LumberjackFrame in project nifi by apache.
the class LumberjackSocketChannelHandler method processBuffer.
@Override
protected void processBuffer(final SocketChannel socketChannel, final ByteBuffer socketBuffer) throws InterruptedException, IOException {
// get total bytes in buffer
final int total = socketBuffer.remaining();
final InetAddress sender = socketChannel.socket().getInetAddress();
try {
// go through the buffer parsing the Lumberjack command
for (int i = 0; i < total; i++) {
byte currByte = socketBuffer.get();
// if we found the end of a frame, handle the frame and mark the buffer
if (decoder.process(currByte)) {
final List<LumberjackFrame> frames = decoder.getFrames();
for (LumberjackFrame frame : frames) {
// TODO: Clean this
logger.debug("Received Lumberjack frame with transaction {} and command {}", new Object[] { frame.getSeqNumber(), frame.getSeqNumber() });
// Ignore the WINDOWS type frames as they contain no payload.
if (frame.getFrameType() != 0x57) {
final SocketChannelResponder responder = new SocketChannelResponder(socketChannel);
frameHandler.handle(frame, responder, sender.toString());
}
}
socketBuffer.mark();
}
}
logger.debug("Done processing buffer");
} catch (final LumberjackFrameException rfe) {
logger.error("Error reading Lumberjack frames due to {}", new Object[] { rfe.getMessage() }, rfe);
// if an invalid frame or bad data was sent then the decoder will be left in a
// corrupted state, so lets close the connection and cause the client to re-establish
dispatcher.completeConnection(key);
}
}
use of org.apache.nifi.processors.lumberjack.frame.LumberjackFrame in project nifi by apache.
the class TestLumberjackFrameHandler method testWindow.
@Test
public void testWindow() throws IOException, InterruptedException {
final LumberjackFrame openFrame = new LumberjackFrame.Builder().version((byte) 0x31).frameType((byte) 0x57).seqNumber(-1).payload(Integer.toString(1).getBytes()).build();
final String sender = "sender1";
final CapturingChannelResponder responder = new CapturingChannelResponder();
// call the handler and verify respond() was called once with once response
frameHandler.handle(openFrame, responder, sender);
// No response expected
Assert.assertEquals(0, responder.responded);
}
Aggregations