Search in sources :

Example 1 with WebSocketConnectedMessage

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

the class AbstractWebSocketGatewayProcessor method connected.

@Override
public void connected(WebSocketSessionInfo sessionInfo) {
    final WebSocketMessage message = new WebSocketConnectedMessage(sessionInfo);
    sessionInfo.setTransitUri(getTransitUri(sessionInfo));
    enqueueMessage(message);
}
Also used : WebSocketConnectedMessage(org.apache.nifi.websocket.WebSocketConnectedMessage) WebSocketMessage(org.apache.nifi.websocket.WebSocketMessage)

Example 2 with WebSocketConnectedMessage

use of org.apache.nifi.websocket.WebSocketConnectedMessage 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)

Aggregations

WebSocketConnectedMessage (org.apache.nifi.websocket.WebSocketConnectedMessage)2 WebSocketMessage (org.apache.nifi.websocket.WebSocketMessage)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