diff --git a/nifi-commons/nifi-site-to-site-client/src/main/java/org/apache/nifi/remote/util/SiteToSiteRestApiClient.java b/nifi-commons/nifi-site-to-site-client/src/main/java/org/apache/nifi/remote/util/SiteToSiteRestApiClient.java index cb3a55a15c..e81dc5b8b0 100644 --- a/nifi-commons/nifi-site-to-site-client/src/main/java/org/apache/nifi/remote/util/SiteToSiteRestApiClient.java +++ b/nifi-commons/nifi-site-to-site-client/src/main/java/org/apache/nifi/remote/util/SiteToSiteRestApiClient.java @@ -90,14 +90,17 @@ import java.io.IOException; import java.io.InputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; +import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.Arrays; import java.util.Collection; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; @@ -823,7 +826,15 @@ public class SiteToSiteRestApiClient implements Closeable { private String execute(final HttpGet get) throws IOException { final CloseableHttpClient httpClient = getHttpClient(); + if (logger.isTraceEnabled()) { + Arrays.stream(get.getAllHeaders()).forEach(h -> logger.debug("REQ| {}", h)); + } + try (final CloseableHttpResponse response = httpClient.execute(get)) { + if (logger.isTraceEnabled()) { + Arrays.stream(response.getAllHeaders()).forEach(h -> logger.debug("RES| {}", h)); + } + final StatusLine statusLine = response.getStatusLine(); final int statusCode = statusLine.getStatusCode(); if (RESPONSE_CODE_OK != statusCode) { @@ -865,7 +876,12 @@ public class SiteToSiteRestApiClient implements Closeable { final ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); - return mapper.readValue(responseMessage, entityClass); + try { + return mapper.readValue(responseMessage, entityClass); + } catch (JsonParseException e) { + logger.warn("Failed to parse Json, response={}", responseMessage); + throw e; + } } public String getBaseUrl() { @@ -906,8 +922,13 @@ public class SiteToSiteRestApiClient implements Closeable { return resolveBaseUrl(scheme, host, port, "/nifi-api"); } - public String resolveBaseUrl(final String scheme, final String host, final int port, final String path) { - final String baseUri = scheme + "://" + host + ":" + port + path; + private String resolveBaseUrl(final String scheme, final String host, final int port, final String path) { + final String baseUri; + try { + baseUri = new URL(scheme, host, port, path).toURI().toString(); + } catch (MalformedURLException|URISyntaxException e) { + throw new IllegalArgumentException(e); + } this.setBaseUrl(baseUri); return baseUri; } diff --git a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/util/TestSiteToSiteRestApiClient.java b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/util/TestSiteToSiteRestApiClient.java new file mode 100644 index 0000000000..1ae964cc7e --- /dev/null +++ b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/util/TestSiteToSiteRestApiClient.java @@ -0,0 +1,67 @@ +/* + * 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.util; + +import org.junit.Assert; +import org.junit.Test; + +public class TestSiteToSiteRestApiClient { + + @Test + public void testResolveBaseUrlHttp() throws Exception{ + + final SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(null, null); + + final String baseUrl = apiClient.resolveBaseUrl("http://nifi.example.com/nifi"); + Assert.assertEquals("http://nifi.example.com/nifi-api", baseUrl); + } + + @Test + public void testResolveBaseUrlHttpSub() throws Exception{ + + final SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(null, null); + + final String baseUrl = apiClient.resolveBaseUrl("http://nifi.example.com/foo/bar/baz/nifi"); + Assert.assertEquals("http://nifi.example.com/foo/bar/baz/nifi-api", baseUrl); + } + + @Test + public void testResolveBaseUrlHttpPort() { + final SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(null, null); + + final String baseUrl = apiClient.resolveBaseUrl("http://nifi.example.com:8080/nifi"); + Assert.assertEquals("http://nifi.example.com:8080/nifi-api", baseUrl); + } + + @Test + public void testResolveBaseUrlHttps() throws Exception{ + + final SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(null, null); + + final String baseUrl = apiClient.resolveBaseUrl("https://nifi.example.com/nifi"); + Assert.assertEquals("https://nifi.example.com/nifi-api", baseUrl); + } + + @Test + public void testResolveBaseUrlHttpsPort() { + final SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(null, null); + + final String baseUrl = apiClient.resolveBaseUrl("https://nifi.example.com:8443/nifi"); + Assert.assertEquals("https://nifi.example.com:8443/nifi-api", baseUrl); + } + +}