Search in sources :

Example 1 with LumberjackEvent

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));
}
Also used : LumberjackFrame(org.apache.nifi.processors.lumberjack.frame.LumberjackFrame) LumberjackEvent(org.apache.nifi.processors.lumberjack.event.LumberjackEvent) Test(org.junit.Test)

Example 2 with LumberjackEvent

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;
}
Also used : LumberjackEvent(org.apache.nifi.processors.lumberjack.event.LumberjackEvent) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with LumberjackEvent

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);
}
Also used : LumberjackEventFactory(org.apache.nifi.processors.lumberjack.event.LumberjackEventFactory) LumberjackSocketChannelHandlerFactory(org.apache.nifi.processors.lumberjack.handler.LumberjackSocketChannelHandlerFactory) Charset(java.nio.charset.Charset) SSLContext(javax.net.ssl.SSLContext) ByteBuffer(java.nio.ByteBuffer) AsyncChannelDispatcher(org.apache.nifi.processor.util.listen.dispatcher.AsyncChannelDispatcher) LumberjackEvent(org.apache.nifi.processors.lumberjack.event.LumberjackEvent) SSLContextService(org.apache.nifi.ssl.SSLContextService) RestrictedSSLContextService(org.apache.nifi.ssl.RestrictedSSLContextService) SocketChannelDispatcher(org.apache.nifi.processor.util.listen.dispatcher.SocketChannelDispatcher)

Aggregations

LumberjackEvent (org.apache.nifi.processors.lumberjack.event.LumberjackEvent)3 Gson (com.google.gson.Gson)1 ByteBuffer (java.nio.ByteBuffer)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SSLContext (javax.net.ssl.SSLContext)1 AsyncChannelDispatcher (org.apache.nifi.processor.util.listen.dispatcher.AsyncChannelDispatcher)1 SocketChannelDispatcher (org.apache.nifi.processor.util.listen.dispatcher.SocketChannelDispatcher)1 LumberjackEventFactory (org.apache.nifi.processors.lumberjack.event.LumberjackEventFactory)1 LumberjackFrame (org.apache.nifi.processors.lumberjack.frame.LumberjackFrame)1 LumberjackSocketChannelHandlerFactory (org.apache.nifi.processors.lumberjack.handler.LumberjackSocketChannelHandlerFactory)1 RestrictedSSLContextService (org.apache.nifi.ssl.RestrictedSSLContextService)1 SSLContextService (org.apache.nifi.ssl.SSLContextService)1 Test (org.junit.Test)1