Search in sources :

Example 1 with BeatsFrame

use of org.apache.nifi.processors.beats.frame.BeatsFrame in project nifi by apache.

the class BeatsSocketChannelHandler 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 packet 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<BeatsFrame> frames = decoder.getFrames();
                for (BeatsFrame frame : frames) {
                    logger.debug("Received Beats frame with transaction {} and command {}", new Object[] { frame.getSeqNumber(), frame.getSeqNumber() });
                    // Ignore the WINDOW SIZE 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 BeatsFrameException rfe) {
        logger.error("Error reading Beats 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);
    }
}
Also used : BeatsFrame(org.apache.nifi.processors.beats.frame.BeatsFrame) SocketChannelResponder(org.apache.nifi.processor.util.listen.response.socket.SocketChannelResponder) BeatsFrameException(org.apache.nifi.processors.beats.frame.BeatsFrameException) InetAddress(java.net.InetAddress)

Example 2 with BeatsFrame

use of org.apache.nifi.processors.beats.frame.BeatsFrame in project nifi by apache.

the class BeatsSSLSocketChannelHandler 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 packet 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<BeatsFrame> frames = decoder.getFrames();
                // A list of events has been generated
                for (BeatsFrame frame : frames) {
                    logger.debug("Received Beats 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 BeatsFrameException rfe) {
        logger.error("Error reading Beats 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);
    }
}
Also used : BeatsFrame(org.apache.nifi.processors.beats.frame.BeatsFrame) BeatsFrameException(org.apache.nifi.processors.beats.frame.BeatsFrameException) SSLSocketChannelResponder(org.apache.nifi.processor.util.listen.response.socket.SSLSocketChannelResponder) InetAddress(java.net.InetAddress)

Example 3 with BeatsFrame

use of org.apache.nifi.processors.beats.frame.BeatsFrame 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)

Example 4 with BeatsFrame

use of org.apache.nifi.processors.beats.frame.BeatsFrame in project nifi by apache.

the class TestBeatsFrameHandler method testWindow.

@Test
public void testWindow() throws IOException, InterruptedException {
    final BeatsFrame openFrame = new BeatsFrame.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);
}
Also used : BeatsFrame(org.apache.nifi.processors.beats.frame.BeatsFrame) Test(org.junit.Test)

Aggregations

BeatsFrame (org.apache.nifi.processors.beats.frame.BeatsFrame)4 InetAddress (java.net.InetAddress)2 BeatsFrameException (org.apache.nifi.processors.beats.frame.BeatsFrameException)2 Test (org.junit.Test)2 SSLSocketChannelResponder (org.apache.nifi.processor.util.listen.response.socket.SSLSocketChannelResponder)1 SocketChannelResponder (org.apache.nifi.processor.util.listen.response.socket.SocketChannelResponder)1 BeatsEvent (org.apache.nifi.processors.beats.event.BeatsEvent)1