bejancsaba commented on code in PR #7125:
URL: https://github.com/apache/nifi/pull/7125#discussion_r1162473024


##########
c2/c2-client-bundle/c2-client-http/src/test/java/org/apache/nifi/c2/client/http/url/C2UrlProviderFactoryTest.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.c2.client.http.url;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
+import static org.mockito.Mockito.lenient;
+import static org.mockito.Mockito.when;
+
+import org.apache.nifi.c2.client.C2ClientConfig;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class C2UrlProviderFactoryTest {
+
+    private static final String C2_REST_BASE = "https://host:8080/c2/api";;
+    private static final String HEARTBEAT_PATH = "/heartbeat";
+    private static final String ACKNOWLEDGE_PATH = "/acknowledge";
+
+    @Mock
+    private C2ClientConfig clientConfig;
+    @InjectMocks
+    private C2UrlProviderFactory testC2UrlProviderFactory;
+
+    @Test
+    public void testProxyAwareC2UrlProviderIsCreated() {
+        // given
+        when(clientConfig.getC2RestPathBase()).thenReturn(C2_REST_BASE);
+        when(clientConfig.getC2RestPathHeartbeat()).thenReturn(HEARTBEAT_PATH);
+        
when(clientConfig.getC2RestPathAcknowledge()).thenReturn(ACKNOWLEDGE_PATH);
+
+        // when
+        C2UrlProvider c2UrlProvider = testC2UrlProviderFactory.create();
+
+        // then
+        assertInstanceOf(ProxyAwareC2UrlProvider.class, c2UrlProvider);
+    }
+
+    @Test
+    public void testLegacyC2UrlProviderIsCreated() {
+        // given
+        when(clientConfig.getC2Url()).thenReturn(C2_REST_BASE + 
HEARTBEAT_PATH);
+        when(clientConfig.getC2AckUrl()).thenReturn(C2_REST_BASE + 
ACKNOWLEDGE_PATH);
+
+        // when
+        C2UrlProvider c2UrlProvider = testC2UrlProviderFactory.create();
+
+        // then
+        assertInstanceOf(LegacyC2UrlProvider.class, c2UrlProvider);
+    }
+
+    @Test
+    public void testProxyAwareProviderTakesPrecedenceOverLegacy() {

Review Comment:
   I was looking for this test, thank you!



##########
c2/c2-client-bundle/c2-client-api/src/main/java/org/apache/nifi/c2/client/api/C2Client.java:
##########
@@ -57,4 +57,14 @@ public interface C2Client {
      * @return optional error message if any issues occurred
      */
     Optional<String> uploadBundle(String callbackUrl, byte[] bundle);
+
+    /**
+     * Creates a callback URL according to proxy aware C2 settings
+     *
+     * @param absoluteUrl absolute url sent by C2 server
+     * @param relativeUrl relative url sent by C2 server
+     * @return finalised callback url
+     * @throws Exception when the callback url can not be created as per the 
current configuration and parameters
+     */
+    String getCallbackUrl(String absoluteUrl, String relativeUrl) throws 
Exception;

Review Comment:
   Thanks looks good



##########
c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/url/ProxyAwareC2UrlProvider.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.c2.client.http.url;
+
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class ProxyAwareC2UrlProvider implements C2UrlProvider {
+
+    private final String c2RestApi;
+    private final String c2RestPathHeartbeat;
+    private final String c2RestPathAcknowledge;
+
+    ProxyAwareC2UrlProvider(String c2RestApi, String c2RestPathHeartbeat, 
String c2RestPathAcknowledge) {
+        this.c2RestApi = c2RestApi;
+        this.c2RestPathHeartbeat = c2RestPathHeartbeat;
+        this.c2RestPathAcknowledge = c2RestPathAcknowledge;
+    }
+
+    @Override
+    public String getHeartbeatUrl() {
+        return toAbsoluteUrl(c2RestPathHeartbeat);
+    }
+
+    @Override
+    public String getAcknowledgeUrl() {
+        return toAbsoluteUrl(c2RestPathAcknowledge);
+    }
+
+    @Override
+    public String getCallbackUrl(String absoluteUrl, String relativeUrl) 
throws Exception {

Review Comment:
   Ok, it was just an idea didn't wanted to change the whole approach just 
elevate some logic to the C2Client. FOr me it made sense that way but I'm ok 
with the current approach as well.



##########
minifi/minifi-nar-bundles/minifi-framework-bundle/minifi-framework/minifi-resources/src/main/resources/conf/bootstrap.conf:
##########
@@ -134,8 +134,13 @@ java.arg.14=-Djava.awt.headless=true
 # Enabling C2 Uncomment each of the following options
 #c2.enable=true
 ## define protocol parameters
+# legacy properties
 #c2.rest.url=
 #c2.rest.url.ack=
+# new c2 properties
+#c2.rest.api=

Review Comment:
   Okay sounds good, thank you



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to