Search in sources :

Example 1 with BeatsEvent

use of org.apache.nifi.processors.beats.event.BeatsEvent in project nifi by apache.

the class ListenBeats method createDispatcher.

@Override
protected ChannelDispatcher createDispatcher(final ProcessContext context, final BlockingQueue<BeatsEvent> events) throws IOException {
    final EventFactory<BeatsEvent> eventFactory = new BeatsEventFactory();
    final ChannelHandlerFactory<BeatsEvent, AsyncChannelDispatcher> handlerFactory = new BeatsSocketChannelHandlerFactory<>();
    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 : BeatsEventFactory(org.apache.nifi.processors.beats.event.BeatsEventFactory) BeatsEvent(org.apache.nifi.processors.beats.event.BeatsEvent) Charset(java.nio.charset.Charset) SSLContext(javax.net.ssl.SSLContext) ByteBuffer(java.nio.ByteBuffer) AsyncChannelDispatcher(org.apache.nifi.processor.util.listen.dispatcher.AsyncChannelDispatcher) BeatsSocketChannelHandlerFactory(org.apache.nifi.processors.beats.handler.BeatsSocketChannelHandlerFactory) SSLContextService(org.apache.nifi.ssl.SSLContextService) RestrictedSSLContextService(org.apache.nifi.ssl.RestrictedSSLContextService) SocketChannelDispatcher(org.apache.nifi.processor.util.listen.dispatcher.SocketChannelDispatcher)

Example 2 with BeatsEvent

use of org.apache.nifi.processors.beats.event.BeatsEvent in project nifi by apache.

the class TestBeatsFrameHandler method testJson.

@Test
public void testJson() throws IOException, InterruptedException {
    final byte[] jsonPayload = new byte[] { // Payload eq { "message": "test-content", "field": "value"}
    0x7b, 0x22, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x3a, 0x20, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x22, 0x2c, 0x20, 0x22, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x22, 0x3a, 0x20, 0x22, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x7d };
    final BeatsFrame jsonFrame = new BeatsFrame.Builder().version((byte) 0x32).frameType((byte) 0x4a).seqNumber(1).dataSize(45).payload(jsonPayload).build();
    final String sender = "sender1";
    final CapturingChannelResponder responder = new CapturingChannelResponder();
    // call the handler and verify respond() was called once with once response
    frameHandler.handle(jsonFrame, responder, sender);
    // No response expected
    Assert.assertEquals(0, responder.responded);
    // But events should contain one event
    Assert.assertEquals(1, events.size());
    final BeatsEvent event = events.poll();
    Assert.assertEquals("{\"message\": \"test-content\", \"field\": \"value\"}", new String(event.getData(), charset));
}
Also used : BeatsFrame(org.apache.nifi.processors.beats.frame.BeatsFrame) BeatsEvent(org.apache.nifi.processors.beats.event.BeatsEvent) Test(org.junit.Test)

Aggregations

BeatsEvent (org.apache.nifi.processors.beats.event.BeatsEvent)2 ByteBuffer (java.nio.ByteBuffer)1 Charset (java.nio.charset.Charset)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 BeatsEventFactory (org.apache.nifi.processors.beats.event.BeatsEventFactory)1 BeatsFrame (org.apache.nifi.processors.beats.frame.BeatsFrame)1 BeatsSocketChannelHandlerFactory (org.apache.nifi.processors.beats.handler.BeatsSocketChannelHandlerFactory)1 RestrictedSSLContextService (org.apache.nifi.ssl.RestrictedSSLContextService)1 SSLContextService (org.apache.nifi.ssl.SSLContextService)1 Test (org.junit.Test)1