Skip to content

Commit

Permalink
NIFI-14013 Add disconnected Relationship to ConnectWebSocket (apache#…
Browse files Browse the repository at this point in the history
…9533)

Signed-off-by: David Handermann <[email protected]>
  • Loading branch information
POliveNokia authored Dec 14, 2024
1 parent 8d2db60 commit e4cb5a3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.nifi.websocket.WebSocketClientService;
import org.apache.nifi.websocket.WebSocketConfigurationException;
import org.apache.nifi.websocket.WebSocketConnectedMessage;
import org.apache.nifi.websocket.WebSocketDisconnectedMessage;
import org.apache.nifi.websocket.WebSocketMessage;
import org.apache.nifi.websocket.WebSocketService;
import org.apache.nifi.websocket.WebSocketSessionInfo;
Expand Down Expand Up @@ -67,6 +68,12 @@ public abstract class AbstractWebSocketGatewayProcessor extends AbstractSessionF
.description("The WebSocket session is established")
.build();

public static final Relationship REL_DISCONNECTED = new Relationship.Builder()
.name("disconnected")
.description("The WebSocket session is disconnected")
.autoTerminateDefault(true)
.build();

public static final Relationship REL_MESSAGE_TEXT = new Relationship.Builder()
.name("text message")
.description("The WebSocket text message output")
Expand All @@ -92,6 +99,7 @@ public abstract class AbstractWebSocketGatewayProcessor extends AbstractSessionF
static Set<Relationship> getAbstractRelationships() {
final Set<Relationship> relationships = new HashSet<>();
relationships.add(REL_CONNECTED);
relationships.add(REL_DISCONNECTED);
relationships.add(REL_MESSAGE_TEXT);
relationships.add(REL_MESSAGE_BINARY);
return relationships;
Expand All @@ -114,6 +122,13 @@ public void connected(WebSocketSessionInfo sessionInfo) {
enqueueMessage(message);
}

@Override
public void disconnected(WebSocketSessionInfo sessionInfo) {
final WebSocketMessage message = new WebSocketDisconnectedMessage(sessionInfo);
sessionInfo.setTransitUri(getTransitUri(sessionInfo));
enqueueMessage(message);
}

@Override
public void consume(WebSocketSessionInfo sessionInfo, String messageStr) {
final WebSocketMessage message = new WebSocketMessage(sessionInfo);
Expand Down Expand Up @@ -252,6 +267,8 @@ private void enqueueMessage(final WebSocketMessage incomingMessage) {

if (incomingMessage instanceof WebSocketConnectedMessage) {
session.transfer(messageFlowFile, REL_CONNECTED);
} else if (incomingMessage instanceof WebSocketDisconnectedMessage) {
session.transfer(messageFlowFile, REL_DISCONNECTED);
} else {
switch (Objects.requireNonNull(messageType)) {
case TEXT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void testSuccess() throws Exception {
processor.consume(webSocketSession, binaryMessage, 0, binaryMessage.length);
processor.consume(webSocketSession, binaryMessage, 0, binaryMessage.length);
processor.consume(webSocketSession, binaryMessage, 0, binaryMessage.length);
processor.disconnected(webSocketSession);
return null;
}).when(service).connect(endpointId);
runner.addControllerService(serviceId, service);
Expand Down Expand Up @@ -119,7 +120,7 @@ public void testSuccess() throws Exception {
binaryFlowFiles.forEach(ff -> assertFlowFile(webSocketSession, serviceId, endpointId, ff, WebSocketMessage.Type.BINARY));

final List<ProvenanceEventRecord> provenanceEvents = sharedSessionState.getProvenanceEvents();
assertEquals(6, provenanceEvents.size());
assertEquals(7, provenanceEvents.size());
assertTrue(provenanceEvents.stream().allMatch(event -> ProvenanceEventType.RECEIVE.equals(event.getEventType())));
}

Expand Down Expand Up @@ -201,6 +202,56 @@ void testDynamicUrlsParsedFromFlowFileButNotAbleToConnect() throws Initializatio
runner.stop();
}

@Test
void testDynamicUrlsParsedFromFlowFileAndAbleToConnectAndDisconnect() throws InitializationException {
// Start websocket server
final TestRunner webSocketListener = TestRunners.newTestRunner(ListenWebSocket.class);

final String serverId = "ws-server-service";
JettyWebSocketServer server = new JettyWebSocketServer();
webSocketListener.addControllerService(serverId, server);
webSocketListener.setProperty(server, JettyWebSocketServer.LISTEN_PORT, "0");
webSocketListener.enableControllerService(server);

webSocketListener.setProperty(ListenWebSocket.PROP_WEBSOCKET_SERVER_SERVICE, serverId);
webSocketListener.setProperty(ListenWebSocket.PROP_SERVER_URL_PATH, "/test");

webSocketListener.run(1, false);
final int listeningPort = server.getListeningPort();

final TestRunner runner = TestRunners.newTestRunner(ConnectWebSocket.class);

final String clientId = "ws-service";
final String endpointId = "client-1";

MockFlowFile flowFile = getFlowFile();
runner.enqueue(flowFile);

JettyWebSocketClient client = new JettyWebSocketClient();


runner.addControllerService(clientId, client);
runner.setProperty(client, JettyWebSocketClient.WS_URI, String.format("ws://localhost:%s/${dynamicUrlPart}", listeningPort));
runner.enableControllerService(client);

runner.setProperty(ConnectWebSocket.PROP_WEBSOCKET_CLIENT_SERVICE, clientId);
runner.setProperty(ConnectWebSocket.PROP_WEBSOCKET_CLIENT_ID, endpointId);

runner.run(1, false);

final List<MockFlowFile> flowFilesForConnectedRelationship = runner.getFlowFilesForRelationship(ConnectWebSocket.REL_CONNECTED);
assertEquals(1, flowFilesForConnectedRelationship.size());

final List<MockFlowFile> flowFilesForSuccess = runner.getFlowFilesForRelationship(ConnectWebSocket.REL_SUCCESS);
assertEquals(1, flowFilesForSuccess.size());

webSocketListener.disableControllerService(server);

final List<MockFlowFile> flowFilesForDisconnectedRelationship = runner.getFlowFilesForRelationship(ConnectWebSocket.REL_DISCONNECTED);
assertEquals(1, flowFilesForDisconnectedRelationship.size());

runner.stop();
}

private MockFlowFile getFlowFile() {
Map<String, String> attributes = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
*/
public interface ConnectedListener {
void connected(final WebSocketSessionInfo sessionInfo);
void disconnected(final WebSocketSessionInfo sessionInfo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.websocket;

public class WebSocketDisconnectedMessage extends WebSocketMessage {
public WebSocketDisconnectedMessage(final WebSocketSessionInfo sessionInfo) {
super(sessionInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public void captureSession(final WebSocketSession session) {
}

public void onWebSocketClose(final String sessionId, final int statusCode, final String reason) {
if (processor instanceof ConnectedListener connectedListener) {
connectedListener.disconnected(getSessionOrFail(sessionId));
}
sessions.remove(sessionId);
}

Expand Down

0 comments on commit e4cb5a3

Please sign in to comment.