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();
}
}
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;
}
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;
}
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;
}
Aggregations