use of org.apache.nifi.processors.lumberjack.event.LumberjackEvent 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.event.LumberjackEvent in project nifi by apache.
the class ListenLumberjack method getAttributes.
@Override
protected Map<String, String> getAttributes(FlowFileEventBatch batch) {
final List<LumberjackEvent> events = batch.getEvents();
// the sender and command will be the same for all events based on the batch key
final String sender = events.get(0).getSender();
final int numAttributes = events.size() == 1 ? 5 : 4;
final Map<String, String> attributes = new HashMap<>(numAttributes);
attributes.put(LumberjackAttributes.SENDER.key(), sender);
attributes.put(LumberjackAttributes.PORT.key(), String.valueOf(port));
attributes.put(CoreAttributes.MIME_TYPE.key(), "text/plain");
// NOTE: we could pass on all the transaction ids joined together
if (events.size() == 1) {
attributes.put(LumberjackAttributes.SEQNUMBER.key(), String.valueOf(events.get(0).getSeqNumber()));
// Convert the serialized fields from JSON
String serialFields = String.valueOf(events.get(0).getFields());
Gson jsonObject = new Gson();
Map<String, String> fields = jsonObject.fromJson(serialFields, Map.class);
for (Map.Entry<String, String> entry : fields.entrySet()) {
attributes.put(LumberjackAttributes.FIELDS.key().concat(".").concat(entry.getKey()), entry.getValue());
}
}
return attributes;
}
use of org.apache.nifi.processors.lumberjack.event.LumberjackEvent in project nifi by apache.
the class ListenLumberjack method createDispatcher.
@Override
protected ChannelDispatcher createDispatcher(final ProcessContext context, final BlockingQueue<LumberjackEvent> events) throws IOException {
final EventFactory<LumberjackEvent> eventFactory = new LumberjackEventFactory();
final ChannelHandlerFactory<LumberjackEvent, AsyncChannelDispatcher> handlerFactory = new LumberjackSocketChannelHandlerFactory<>();
final int maxConnections = context.getProperty(MAX_CONNECTIONS).asInteger();
final int bufferSize = context.getProperty(RECV_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
final Charset charSet = Charset.forName(context.getProperty(CHARSET).getValue());
// initialize the buffer pool based on max number of connections and the buffer size
final BlockingQueue<ByteBuffer> bufferPool = createBufferPool(maxConnections, bufferSize);
// if an SSLContextService was provided then create an SSLContext to pass down to the dispatcher
SSLContext sslContext = null;
final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
if (sslContextService != null) {
sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.REQUIRED);
}
// if we decide to support SSL then get the context and pass it in here
return new SocketChannelDispatcher<>(eventFactory, handlerFactory, bufferPool, events, getLogger(), maxConnections, sslContext, charSet);
}
Aggregations