Search in sources :

Example 1 with WebSocketSessionInfo

use of org.apache.nifi.websocket.WebSocketSessionInfo in project nifi by apache.

the class AbstractWebSocketGatewayProcessor method enqueueMessage.

private void enqueueMessage(final WebSocketMessage incomingMessage) {
    final ProcessSession session = processSessionFactory.createSession();
    try {
        FlowFile messageFlowFile = session.create();
        final Map<String, String> attrs = new HashMap<>();
        attrs.put(ATTR_WS_CS_ID, webSocketService.getIdentifier());
        final WebSocketSessionInfo sessionInfo = incomingMessage.getSessionInfo();
        attrs.put(ATTR_WS_SESSION_ID, sessionInfo.getSessionId());
        attrs.put(ATTR_WS_ENDPOINT_ID, endpointId);
        attrs.put(ATTR_WS_LOCAL_ADDRESS, sessionInfo.getLocalAddress().toString());
        attrs.put(ATTR_WS_REMOTE_ADDRESS, sessionInfo.getRemoteAddress().toString());
        final WebSocketMessage.Type messageType = incomingMessage.getType();
        if (messageType != null) {
            attrs.put(ATTR_WS_MESSAGE_TYPE, messageType.name());
        }
        messageFlowFile = session.putAllAttributes(messageFlowFile, attrs);
        final byte[] payload = incomingMessage.getPayload();
        if (payload != null) {
            messageFlowFile = session.write(messageFlowFile, out -> {
                out.write(payload, incomingMessage.getOffset(), incomingMessage.getLength());
            });
        }
        session.getProvenanceReporter().receive(messageFlowFile, getTransitUri(sessionInfo));
        if (incomingMessage instanceof WebSocketConnectedMessage) {
            session.transfer(messageFlowFile, REL_CONNECTED);
        } else {
            switch(messageType) {
                case TEXT:
                    session.transfer(messageFlowFile, REL_MESSAGE_TEXT);
                    break;
                case BINARY:
                    session.transfer(messageFlowFile, REL_MESSAGE_BINARY);
                    break;
            }
        }
        session.commit();
    } catch (Exception e) {
        logger.error("Unable to fully process input due to " + e, e);
        session.rollback();
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) ATTR_WS_LOCAL_ADDRESS(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_LOCAL_ADDRESS) TextMessageConsumer(org.apache.nifi.websocket.TextMessageConsumer) HashMap(java.util.HashMap) ComponentLog(org.apache.nifi.logging.ComponentLog) StringUtils(org.apache.commons.lang3.StringUtils) ProcessException(org.apache.nifi.processor.exception.ProcessException) ATTR_WS_SESSION_ID(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_SESSION_ID) HashSet(java.util.HashSet) WebSocketConnectedMessage(org.apache.nifi.websocket.WebSocketConnectedMessage) WebSocketSessionInfo(org.apache.nifi.websocket.WebSocketSessionInfo) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) WebSocketConfigurationException(org.apache.nifi.websocket.WebSocketConfigurationException) TriggerSerially(org.apache.nifi.annotation.behavior.TriggerSerially) ConnectedListener(org.apache.nifi.websocket.ConnectedListener) ATTR_WS_ENDPOINT_ID(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_ENDPOINT_ID) FlowFile(org.apache.nifi.flowfile.FlowFile) ProcessContext(org.apache.nifi.processor.ProcessContext) WebSocketMessage(org.apache.nifi.websocket.WebSocketMessage) ATTR_WS_REMOTE_ADDRESS(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_REMOTE_ADDRESS) Set(java.util.Set) ProcessSession(org.apache.nifi.processor.ProcessSession) IOException(java.io.IOException) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) WebSocketService(org.apache.nifi.websocket.WebSocketService) BinaryMessageConsumer(org.apache.nifi.websocket.BinaryMessageConsumer) ATTR_WS_CS_ID(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_CS_ID) ATTR_WS_MESSAGE_TYPE(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_MESSAGE_TYPE) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) WebSocketClientService(org.apache.nifi.websocket.WebSocketClientService) FlowFile(org.apache.nifi.flowfile.FlowFile) WebSocketSessionInfo(org.apache.nifi.websocket.WebSocketSessionInfo) WebSocketConnectedMessage(org.apache.nifi.websocket.WebSocketConnectedMessage) HashMap(java.util.HashMap) WebSocketMessage(org.apache.nifi.websocket.WebSocketMessage) ProcessException(org.apache.nifi.processor.exception.ProcessException) WebSocketConfigurationException(org.apache.nifi.websocket.WebSocketConfigurationException) IOException(java.io.IOException)

Example 2 with WebSocketSessionInfo

use of org.apache.nifi.websocket.WebSocketSessionInfo in project nifi by apache.

the class ITJettyWebSocketCommunication method assertConsumeBinaryMessage.

protected Object assertConsumeBinaryMessage(CountDownLatch latch, String expectedMessage, InvocationOnMock invocation) {
    final WebSocketSessionInfo sessionInfo = invocation.getArgumentAt(0, WebSocketSessionInfo.class);
    assertNotNull(sessionInfo.getLocalAddress());
    assertNotNull(sessionInfo.getRemoteAddress());
    assertNotNull(sessionInfo.getSessionId());
    assertEquals(isSecure(), sessionInfo.isSecure());
    final byte[] receivedMessage = invocation.getArgumentAt(1, byte[].class);
    final byte[] expectedBinary = expectedMessage.getBytes();
    final int offset = invocation.getArgumentAt(2, Integer.class);
    final int length = invocation.getArgumentAt(3, Integer.class);
    assertNotNull(receivedMessage);
    assertEquals(expectedBinary.length, receivedMessage.length);
    assertEquals(expectedMessage, new String(receivedMessage));
    assertEquals(0, offset);
    assertEquals(expectedBinary.length, length);
    latch.countDown();
    return null;
}
Also used : WebSocketSessionInfo(org.apache.nifi.websocket.WebSocketSessionInfo) Matchers.anyString(org.mockito.Matchers.anyString)

Example 3 with WebSocketSessionInfo

use of org.apache.nifi.websocket.WebSocketSessionInfo in project nifi by apache.

the class ITJettyWebSocketCommunication method assertConnectedEvent.

protected Object assertConnectedEvent(CountDownLatch latch, AtomicReference<String> sessionIdRef, InvocationOnMock invocation) {
    final WebSocketSessionInfo sessionInfo = invocation.getArgumentAt(0, WebSocketSessionInfo.class);
    assertNotNull(sessionInfo.getLocalAddress());
    assertNotNull(sessionInfo.getRemoteAddress());
    assertNotNull(sessionInfo.getSessionId());
    assertEquals(isSecure(), sessionInfo.isSecure());
    sessionIdRef.set(sessionInfo.getSessionId());
    latch.countDown();
    return null;
}
Also used : WebSocketSessionInfo(org.apache.nifi.websocket.WebSocketSessionInfo)

Example 4 with WebSocketSessionInfo

use of org.apache.nifi.websocket.WebSocketSessionInfo in project nifi by apache.

the class ITJettyWebSocketCommunication method assertConsumeTextMessage.

protected Object assertConsumeTextMessage(CountDownLatch latch, String expectedMessage, InvocationOnMock invocation) {
    final WebSocketSessionInfo sessionInfo = invocation.getArgumentAt(0, WebSocketSessionInfo.class);
    assertNotNull(sessionInfo.getLocalAddress());
    assertNotNull(sessionInfo.getRemoteAddress());
    assertNotNull(sessionInfo.getSessionId());
    assertEquals(isSecure(), sessionInfo.isSecure());
    final String receivedMessage = invocation.getArgumentAt(1, String.class);
    assertNotNull(receivedMessage);
    assertEquals(expectedMessage, receivedMessage);
    latch.countDown();
    return null;
}
Also used : WebSocketSessionInfo(org.apache.nifi.websocket.WebSocketSessionInfo) Matchers.anyString(org.mockito.Matchers.anyString)

Aggregations

WebSocketSessionInfo (org.apache.nifi.websocket.WebSocketSessionInfo)4 Matchers.anyString (org.mockito.Matchers.anyString)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 StringUtils (org.apache.commons.lang3.StringUtils)1 TriggerSerially (org.apache.nifi.annotation.behavior.TriggerSerially)1 OnStopped (org.apache.nifi.annotation.lifecycle.OnStopped)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 AbstractSessionFactoryProcessor (org.apache.nifi.processor.AbstractSessionFactoryProcessor)1 ProcessContext (org.apache.nifi.processor.ProcessContext)1 ProcessSession (org.apache.nifi.processor.ProcessSession)1 ProcessSessionFactory (org.apache.nifi.processor.ProcessSessionFactory)1 ProcessorInitializationContext (org.apache.nifi.processor.ProcessorInitializationContext)1 Relationship (org.apache.nifi.processor.Relationship)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 ATTR_WS_CS_ID (org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_CS_ID)1