NIFI-2386 This closes #716. Site-to-Site fails without port no

It fails if a given URL doesn't have port in it.
This fixes its behavior with default http 80 and https 443 port.
This commit is contained in:
Koji Kawamura 2016-07-25 22:03:18 +09:00 committed by joewitt
parent 986f951ae8
commit b396867847
2 changed files with 91 additions and 3 deletions

View File

@ -90,14 +90,17 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.PipedInputStream; import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -823,7 +826,15 @@ public class SiteToSiteRestApiClient implements Closeable {
private String execute(final HttpGet get) throws IOException { private String execute(final HttpGet get) throws IOException {
final CloseableHttpClient httpClient = getHttpClient(); final CloseableHttpClient httpClient = getHttpClient();
if (logger.isTraceEnabled()) {
Arrays.stream(get.getAllHeaders()).forEach(h -> logger.debug("REQ| {}", h));
}
try (final CloseableHttpResponse response = httpClient.execute(get)) { 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 StatusLine statusLine = response.getStatusLine();
final int statusCode = statusLine.getStatusCode(); final int statusCode = statusLine.getStatusCode();
if (RESPONSE_CODE_OK != statusCode) { if (RESPONSE_CODE_OK != statusCode) {
@ -865,7 +876,12 @@ public class SiteToSiteRestApiClient implements Closeable {
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
return mapper.readValue(responseMessage, entityClass); return mapper.readValue(responseMessage, entityClass);
} catch (JsonParseException e) {
logger.warn("Failed to parse Json, response={}", responseMessage);
throw e;
}
} }
public String getBaseUrl() { public String getBaseUrl() {
@ -906,8 +922,13 @@ public class SiteToSiteRestApiClient implements Closeable {
return resolveBaseUrl(scheme, host, port, "/nifi-api"); return resolveBaseUrl(scheme, host, port, "/nifi-api");
} }
public String resolveBaseUrl(final String scheme, final String host, final int port, final String path) { private String resolveBaseUrl(final String scheme, final String host, final int port, final String path) {
final String baseUri = scheme + "://" + host + ":" + port + 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); this.setBaseUrl(baseUri);
return baseUri; return baseUri;
} }

View File

@ -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);
}
}