From 5fab783b50e56811cd4a51fd8cf2063da9517ec5 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Fri, 19 Aug 2016 15:34:15 -0400 Subject: [PATCH] NIFI-2609: Ensure that we handle URIs for Remote Process Groups that do not have a path of /nifi or /nifi/ This closes #902. Signed-off-by: Bryan Bende --- .../remote/StandardRemoteProcessGroup.java | 17 ++++-- .../TestStandardRemoteProcessGroup.java | 59 +++++++++++++++++++ 2 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/remote/TestStandardRemoteProcessGroup.java diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/remote/StandardRemoteProcessGroup.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/remote/StandardRemoteProcessGroup.java index 27b6da2c87..5fd0f1a8e4 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/remote/StandardRemoteProcessGroup.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/remote/StandardRemoteProcessGroup.java @@ -146,14 +146,17 @@ public class StandardRemoteProcessGroup implements RemoteProcessGroup { this.flowController = requireNonNull(flowController); final URI uri; try { - uri = new URI(requireNonNull(targetUri)); + uri = new URI(requireNonNull(targetUri.trim())); // Trim the trailing / String uriPath = uri.getPath(); - if (uriPath.endsWith("/")) { + if (uriPath == null || uriPath.equals("/") || uriPath.trim().isEmpty()) { + uriPath = "/nifi"; + } else if (uriPath.endsWith("/")) { uriPath = uriPath.substring(0, uriPath.length() - 1); } - final String apiPath = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + uriPath + "-api"; + + final String apiPath = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + uriPath.trim() + "-api"; apiUri = new URI(apiPath); } catch (final URISyntaxException e) { throw new IllegalArgumentException(e); @@ -856,7 +859,7 @@ public class StandardRemoteProcessGroup implements RemoteProcessGroup { writeLock.unlock(); } - throw new CommunicationsException("Unable to communicate with Remote NiFi at URI " + apiUri + " due to: " + e.getMessage()); + throw new CommunicationsException("Unable to communicate with Remote NiFi at URI " + getApiUri() + " due to: " + e.getMessage()); } writeLock.lock(); @@ -927,12 +930,16 @@ public class StandardRemoteProcessGroup implements RemoteProcessGroup { private SiteToSiteRestApiClient getSiteToSiteRestApiClient() { SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(sslContext, new HttpProxy(proxyHost, proxyPort, proxyUser, proxyPassword)); - apiClient.setBaseUrl(apiUri.toString()); + apiClient.setBaseUrl(getApiUri()); apiClient.setConnectTimeoutMillis(getCommunicationsTimeout(TimeUnit.MILLISECONDS)); apiClient.setReadTimeoutMillis(getCommunicationsTimeout(TimeUnit.MILLISECONDS)); return apiClient; } + protected String getApiUri() { + return apiUri.toString(); + } + /** * Converts a set of ports into a set of remote process group ports. * diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/remote/TestStandardRemoteProcessGroup.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/remote/TestStandardRemoteProcessGroup.java new file mode 100644 index 0000000000..69d38e9239 --- /dev/null +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/remote/TestStandardRemoteProcessGroup.java @@ -0,0 +1,59 @@ +/* + * 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.remote; + +import static org.junit.Assert.assertEquals; + +import org.apache.nifi.controller.FlowController; +import org.apache.nifi.groups.ProcessGroup; +import org.apache.nifi.util.NiFiProperties; +import org.junit.Test; +import org.mockito.Mockito; + +public class TestStandardRemoteProcessGroup { + + @Test + public void testApiUri() { + final NiFiProperties properties = Mockito.mock(NiFiProperties.class); + final FlowController controller = Mockito.mock(FlowController.class); + final ProcessGroup group = Mockito.mock(ProcessGroup.class); + + final String expectedUri = "http://localhost:8080/nifi-api"; + StandardRemoteProcessGroup rpg = new StandardRemoteProcessGroup("id", "http://localhost:8080/nifi", group, controller, null, properties); + assertEquals(expectedUri, rpg.getApiUri()); + + rpg = new StandardRemoteProcessGroup("id", "http://localhost:8080/nifi/", group, controller, null, properties); + assertEquals(expectedUri, rpg.getApiUri()); + + rpg = new StandardRemoteProcessGroup("id", "http://localhost:8080/nifi/ ", group, controller, null, properties); + assertEquals(expectedUri, rpg.getApiUri()); + + rpg = new StandardRemoteProcessGroup("id", " http://localhost:8080/nifi/ ", group, controller, null, properties); + assertEquals(expectedUri, rpg.getApiUri()); + + rpg = new StandardRemoteProcessGroup("id", "http://localhost:8080/", group, controller, null, properties); + assertEquals(expectedUri, rpg.getApiUri()); + + rpg = new StandardRemoteProcessGroup("id", "http://localhost:8080", group, controller, null, properties); + assertEquals(expectedUri, rpg.getApiUri()); + + rpg = new StandardRemoteProcessGroup("id", "http://localhost:8080 ", group, controller, null, properties); + assertEquals(expectedUri, rpg.getApiUri()); + } + +}