You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2021/06/22 12:34:22 UTC

[GitHub] [nifi] tpalfy commented on a change in pull request #5130: NIFI-8639: Add custom headers and dynamic URLs to ConnectWebsocket

tpalfy commented on a change in pull request #5130:
URL: https://github.com/apache/nifi/pull/5130#discussion_r656148733



##########
File path: nifi-nar-bundles/nifi-websocket-bundle/nifi-websocket-services-api/src/main/java/org/apache/nifi/websocket/WebSocketClientService.java
##########
@@ -32,7 +33,7 @@
 
     void stopClient() throws Exception;
 
-    void connect(final String clientId) throws IOException;
+    void connect(final String clientId, final Map<String, String> flowFileAttributes) throws IOException;

Review comment:
       ```suggestion
       void connect(final String clientId) throws IOException;
       
       default void connect(final String clientId, final Map<String, String> flowFileAttributes) throws IOException {
           connect(clientId);
       };
   ```

##########
File path: nifi-nar-bundles/nifi-websocket-bundle/nifi-websocket-services-jetty/src/test/java/org/apache/nifi/websocket/jetty/ITJettyWebSocketCommunication.java
##########
@@ -234,7 +235,7 @@ public void testClientServerCommunicationRecovery() throws Exception {
 
         clientService.registerProcessor(clientId, clientProcessor);
 
-        clientService.connect(clientId);
+        clientService.connect(clientId, Collections.emptyMap());

Review comment:
       If the interface is made backward-compatible, this change can be reverted.

##########
File path: nifi-nar-bundles/nifi-websocket-bundle/nifi-websocket-processors/src/main/resources/docs/org.apache.nifi.processors.websocket.ConnectWebSocket/additionalDetails.html
##########
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<html lang="en">
+<!--
+  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.
+-->
+<head>
+    <meta charset="utf-8"/>
+    <title>ConnectWebSocket</title>
+    <link rel="stylesheet" href="../../../../../css/component-usage.css" type="text/css"/>
+</head>
+
+<body>
+<h2>Summary</h2>
+<p>
+    This processor acts as a WebSocket client endpoint to interact with a remote WebSocket server.
+    It is capable of receiving messages from a websocket server and it transfers them to downstream relationships
+    according to the received message types. Dynamic properties and custom header attributes can be configured by
+    incoming
+    flowfile attributes.
+</p>
+<p>
+    Any flowfile attributes can be referenced in the processor and in its WebSocket Client Service. For dynamic
+    properties
+    use the NiFi Expression Language and reference a flowfile attribute. For instance to the "dynamic.url =
+    currentValue"
+    flowfile attribute can be referenced in the WebSocket Client Service with the ${dynamic.url} expression.

Review comment:
       ```suggestion
       The processor may have an incoming relationship, in which case flowfile attributes are passed down to its WebSocket Client Service. This can be used to fine-tune connection configuration (url and headers for example). For example the "dynamic.url =
       currentValue"
       flowfile attribute can be referenced in the WebSocket Client Service with the ${dynamic.url} expression.
   ```

##########
File path: nifi-nar-bundles/nifi-websocket-bundle/nifi-websocket-processors/src/main/resources/docs/org.apache.nifi.processors.websocket.ConnectWebSocket/additionalDetails.html
##########
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<html lang="en">
+<!--
+  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.
+-->
+<head>
+    <meta charset="utf-8"/>
+    <title>ConnectWebSocket</title>
+    <link rel="stylesheet" href="../../../../../css/component-usage.css" type="text/css"/>
+</head>
+
+<body>
+<h2>Summary</h2>
+<p>
+    This processor acts as a WebSocket client endpoint to interact with a remote WebSocket server.
+    It is capable of receiving messages from a websocket server and it transfers them to downstream relationships
+    according to the received message types. Dynamic properties and custom header attributes can be configured by
+    incoming
+    flowfile attributes.
+</p>
+<p>
+    Any flowfile attributes can be referenced in the processor and in its WebSocket Client Service. For dynamic
+    properties
+    use the NiFi Expression Language and reference a flowfile attribute. For instance to the "dynamic.url =
+    currentValue"
+    flowfile attribute can be referenced in the WebSocket Client Service with the ${dynamic.url} expression.
+</p>
+<p>
+    You can define custom websocket headers in the incoming flowfile as additional attributes. The attribute key
+    shall start with "header." and continue with they header key. For example: "header.Authorization". The attribute
+    value will be the corresponding header value.
+<ol>
+    <li>header.Autorization | AuthorizationPassword</li>

Review comment:
       ```suggestion
       <li>header.Authorization | Basic base64UserNamePassWord</li>
   ```

##########
File path: nifi-nar-bundles/nifi-websocket-bundle/nifi-websocket-services-jetty/src/test/java/org/apache/nifi/websocket/jetty/ITJettyWebSocketCommunication.java
##########
@@ -165,7 +166,7 @@ public void testClientServerCommunication() throws Exception {
 
         clientService.registerProcessor(clientId, clientProcessor);
 
-        clientService.connect(clientId);
+        clientService.connect(clientId, Collections.emptyMap());

Review comment:
       If the interface is made backward-compatible, this change can be reverted.

##########
File path: nifi-nar-bundles/nifi-websocket-bundle/nifi-websocket-processors/src/test/java/org/apache/nifi/processors/websocket/TestConnectWebSocket.java
##########
@@ -86,7 +95,7 @@ public void testSuccess() throws Exception {
             processor.consume(webSocketSession, binaryMessage, 0, binaryMessage.length);
             processor.consume(webSocketSession, binaryMessage, 0, binaryMessage.length);
             return null;
-        }).when(service).connect(endpointId);
+        }).when(service).connect(endpointId, Collections.emptyMap());

Review comment:
       If the interface is made backward-compatible, this change can be reverted.

##########
File path: nifi-nar-bundles/nifi-websocket-bundle/nifi-websocket-services-jetty/src/test/java/org/apache/nifi/websocket/util/HeaderMapExtractorTest.java
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.util;
+
+import org.junit.Test;
+import util.HeaderMapExtractor;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class HeaderMapExtractorTest {
+
+    private static final Map<String, String> FLOW_FILE_ATTRIBUTES;
+    private static final Map<String, List<String>> EXPECTED_HEADER_MAP;
+
+    static {
+        final Map<String, String> attributes = new HashMap<>();
+        attributes.put("header.AUTHORIZATION", "AUTH_VALUE");
+        attributes.put("header.MULTI_HEADER_KEY", "FIRST, SECOND   ,THIRD");
+        attributes.put("headers.dots.dots.dots.DOTS_KEY", "DOTS_VALUE");
+        attributes.put("something.else.SOMETHING_ELSE_KEY", "SOMETHING_ELSE_VALUE");
+        attributes.put("header.EMPTY_VALUE_KEY", "");
+        FLOW_FILE_ATTRIBUTES = Collections.unmodifiableMap(attributes);
+    }
+
+    static {
+        final Map<String, List<String>> headers = new HashMap<>();
+        headers.put("AUTHORIZATION", Collections.singletonList("AUTH_VALUE"));
+        headers.put("MULTI_HEADER_KEY", Arrays.asList("FIRST", "SECOND", "THIRD"));
+        headers.put("dots.dots.dots.DOTS_KEY", Collections.singletonList("DOTS_VALUE"));
+        EXPECTED_HEADER_MAP = Collections.unmodifiableMap(headers);
+    }
+
+    @Test
+    public void testMapExtractor() {
+        final Map<String, List<String>> headerMap = HeaderMapExtractor.getHeaderMap(FLOW_FILE_ATTRIBUTES);
+
+        assertEquals(EXPECTED_HEADER_MAP.size(), headerMap.size());
+        for (Map.Entry<String, List<String>> entry : headerMap.entrySet()) {
+            assertTrue(EXPECTED_HEADER_MAP.containsKey(entry.getKey()));
+            final List<String> actualHeaderValues = entry.getValue();
+            final List<String> expectedHeaderValues = EXPECTED_HEADER_MAP.get(entry.getKey());
+            for (int i = 0; i < actualHeaderValues.size(); i++) {
+                assertEquals(expectedHeaderValues.get(i), actualHeaderValues.get(i));
+            }
+        }
+    }

Review comment:
       Using static constants to define the input and expected values of a single test case is not recommended.
   If the scope of the data is a single test case, so should be the corresponding variables. Also makes it less complex overall.
   
   The assertion can be simplified and made more declarative. The usual Java Map implementations have "smart" equals() methods which we can leverage.
   
   Below is a suggestion. Note I added one more header example as well:
   `attributes.put("headerButNotReally.UNRECOGNIZED", "NOT_A_HEADER_VALUE");`
   
   
   
   ```suggestion
       @Test
       public void testMapExtractor() {
           // GIVEN
           final Map<String, String> attributes = new HashMap<>();
           attributes.put("header.AUTHORIZATION", "AUTH_VALUE");
           attributes.put("header.MULTI_HEADER_KEY", "FIRST, SECOND   ,THIRD");
           attributes.put("header.dots.dots.dots.DOTS_KEY", "DOTS_VALUE");
           attributes.put("something.else.SOMETHING_ELSE_KEY", "SOMETHING_ELSE_VALUE");
           attributes.put("header.EMPTY_VALUE_KEY", "");
           attributes.put("headerButNotReally.UNRECOGNIZED", "NOT_A_HEADER_VALUE");
   
           final Map<String, List<String>> expected = new HashMap<>();
           expected.put("AUTHORIZATION", Collections.singletonList("AUTH_VALUE"));
           expected.put("MULTI_HEADER_KEY", Arrays.asList("FIRST", "SECOND", "THIRD"));
           expected.put("dots.dots.dots.DOTS_KEY", Collections.singletonList("DOTS_VALUE"));
   
           // WHEN
           final Map<String, List<String>> actual = HeaderMapExtractor.getHeaderMap(attributes);
   
           // THEN
           assertEquals(expected, actual);
       }
   ```

##########
File path: nifi-nar-bundles/nifi-websocket-bundle/nifi-websocket-services-jetty/src/main/java/util/HeaderMapExtractor.java
##########
@@ -0,0 +1,41 @@
+/*
+ * 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 util;
+
+import org.apache.nifi.util.StringUtils;
+
+import java.util.AbstractMap;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class HeaderMapExtractor {
+
+    public static Map<String, List<String>> getHeaderMap(final Map<String, String> flowFileAttributes) {
+        return flowFileAttributes.entrySet().stream()
+                .filter(entry -> entry.getKey().startsWith("header"))
+                .filter(entry -> StringUtils.isNotBlank(entry.getValue()))
+                .map(entry -> new AbstractMap.SimpleImmutableEntry<>(StringUtils.substringAfter(entry.getKey(), "."), entry.getValue()))
+                .collect(Collectors.toMap(Map.Entry::getKey, HeaderMapExtractor::headerValueMapper));
+    }

Review comment:
       ```suggestion
       public static final String HEADER_PREFIX = "header.";
   
       public static Map<String, List<String>> getHeaderMap(final Map<String, String> flowFileAttributes) {
           return flowFileAttributes.entrySet().stream()
                   .filter(entry -> entry.getKey().startsWith(HEADER_PREFIX))
                   .filter(entry -> StringUtils.isNotBlank(entry.getValue()))
                   .map(entry -> new AbstractMap.SimpleImmutableEntry<>(entry.getKey().substring(HEADER_PREFIX.length()), entry.getValue()))
                   .collect(Collectors.toMap(Map.Entry::getKey, HeaderMapExtractor::headerValueMapper));
       }
   ```

##########
File path: nifi-nar-bundles/nifi-websocket-bundle/nifi-websocket-processors/src/main/resources/docs/org.apache.nifi.processors.websocket.ConnectWebSocket/additionalDetails.html
##########
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<html lang="en">
+<!--
+  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.
+-->
+<head>
+    <meta charset="utf-8"/>
+    <title>ConnectWebSocket</title>
+    <link rel="stylesheet" href="../../../../../css/component-usage.css" type="text/css"/>
+</head>
+
+<body>
+<h2>Summary</h2>
+<p>
+    This processor acts as a WebSocket client endpoint to interact with a remote WebSocket server.
+    It is capable of receiving messages from a websocket server and it transfers them to downstream relationships
+    according to the received message types. Dynamic properties and custom header attributes can be configured by
+    incoming
+    flowfile attributes.

Review comment:
       ```suggestion
       according to the received message types.
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org