diff --git a/community/dbms/src/main/java/org/neo4j/commandline/dbms/ImportCommand.java b/community/dbms/src/main/java/org/neo4j/commandline/dbms/ImportCommand.java index d4d39084f0454..e4e3e8031b0d7 100644 --- a/community/dbms/src/main/java/org/neo4j/commandline/dbms/ImportCommand.java +++ b/community/dbms/src/main/java/org/neo4j/commandline/dbms/ImportCommand.java @@ -147,7 +147,7 @@ public List possibleArguments() public String description() { return "Import a collection of CSV files with --mode=csv (default), or a database from " + - "a pre-3.0 installation with --mode=database."; + "a pre-3.0 installation with --mode=database."; } @Override diff --git a/community/kernel/src/main/java/org/neo4j/helpers/SocketAddress.java b/community/kernel/src/main/java/org/neo4j/helpers/SocketAddress.java index c606903cefb73..26336a77f2332 100644 --- a/community/kernel/src/main/java/org/neo4j/helpers/SocketAddress.java +++ b/community/kernel/src/main/java/org/neo4j/helpers/SocketAddress.java @@ -31,7 +31,7 @@ public class SocketAddress private final String hostname; private final int port; - SocketAddress( String hostname, int port ) + public SocketAddress( String hostname, int port ) { this.hostname = hostname; this.port = port; @@ -73,8 +73,7 @@ public boolean equals( Object o ) return false; } SocketAddress that = (SocketAddress) o; - return port == that.port && - Objects.equals( hostname, that.hostname ); + return port == that.port && Objects.equals( hostname, that.hostname ); } @Override diff --git a/community/kernel/src/test/java/org/neo4j/helpers/SocketAddressFormatTest.java b/community/kernel/src/test/java/org/neo4j/helpers/SocketAddressFormatTest.java index e1168e2d594a9..e7897605ad456 100644 --- a/community/kernel/src/test/java/org/neo4j/helpers/SocketAddressFormatTest.java +++ b/community/kernel/src/test/java/org/neo4j/helpers/SocketAddressFormatTest.java @@ -33,7 +33,7 @@ public void shouldCreateAdvertisedSocketAddressWithLeadingWhitespace() throws Ex String addressString = whitespace( 1 ) + "localhost:9999"; // when - AdvertisedSocketAddress address = SocketAddressFormat.socketAddress( addressString, AdvertisedSocketAddress::new ); + SocketAddress address = SocketAddressFormat.socketAddress( addressString, SocketAddress::new ); // then assertEquals( "localhost", address.getHostname() ); @@ -47,7 +47,7 @@ public void shouldCreateAdvertisedSocketAddressWithTrailingWhitespace() throws E String addressString = "localhost:9999" + whitespace( 1 ); // when - AdvertisedSocketAddress address = SocketAddressFormat.socketAddress( addressString, AdvertisedSocketAddress::new ); + SocketAddress address = SocketAddressFormat.socketAddress( addressString, SocketAddress::new ); // then assertEquals( "localhost", address.getHostname() ); @@ -60,7 +60,7 @@ public void shouldFailToCreateSocketAddressWithMixedInWhitespace() String addressString = "localhost" + whitespace( 1 ) + ":9999"; try { - SocketAddressFormat.socketAddress( addressString, AdvertisedSocketAddress::new ); + SocketAddressFormat.socketAddress( addressString, SocketAddress::new ); fail( "Should have thrown an exception" ); } catch ( IllegalArgumentException e ) @@ -75,7 +75,7 @@ public void shouldFailToCreateSocketWithTrailingNonNumbers() String addressString = "localhost:9999abc"; try { - SocketAddressFormat.socketAddress( addressString, AdvertisedSocketAddress::new ); + SocketAddressFormat.socketAddress( addressString, SocketAddress::new ); fail( "Should have thrown an exception" ); } catch ( IllegalArgumentException e ) @@ -90,7 +90,7 @@ public void shouldFailOnMissingPort() String addressString = "localhost:"; try { - SocketAddressFormat.socketAddress( addressString, AdvertisedSocketAddress::new ); + SocketAddressFormat.socketAddress( addressString, SocketAddress::new ); fail( "Should have thrown an exception" ); } catch ( IllegalArgumentException e ) diff --git a/community/server/src/main/java/org/neo4j/server/rest/discovery/DiscoveryService.java b/community/server/src/main/java/org/neo4j/server/rest/discovery/DiscoveryService.java index eb293c8070f26..3229050f9048b 100644 --- a/community/server/src/main/java/org/neo4j/server/rest/discovery/DiscoveryService.java +++ b/community/server/src/main/java/org/neo4j/server/rest/discovery/DiscoveryService.java @@ -19,6 +19,7 @@ */ package org.neo4j.server.rest.discovery; +import java.net.URI; import java.net.URISyntaxException; import java.util.Optional; import javax.ws.rs.GET; @@ -28,6 +29,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; import org.neo4j.helpers.AdvertisedSocketAddress; import org.neo4j.kernel.configuration.Config; @@ -56,7 +58,7 @@ public DiscoveryService( @Context Config config, @Context OutputFormat outputFor @GET @Produces( MediaType.APPLICATION_JSON ) - public Response getDiscoveryDocument( @HeaderParam( "host" ) String host ) throws URISyntaxException + public Response getDiscoveryDocument( @Context UriInfo uriInfo ) throws URISyntaxException { String managementUri = config.get( ServerSettings.management_api_path ).getPath() + "/"; String dataUri = config.get( ServerSettings.rest_api_path ).getPath() + "/"; @@ -71,7 +73,8 @@ public Response getDiscoveryDocument( @HeaderParam( "host" ) String host ) throw { // Use the port specified in the config, but not the host return outputFormat.ok( new DiscoveryRepresentation( managementUri, dataUri, - fabricateBoltAddress( host, advertisedSocketAddress.getPort() ) ) ); + new AdvertisedSocketAddress( uriInfo.getBaseUri().getHost(), + advertisedSocketAddress.getPort() ) ) ); } else { @@ -84,16 +87,11 @@ public Response getDiscoveryDocument( @HeaderParam( "host" ) String host ) throw else { // There's no config, compute possible endpoint using host header and default bolt port. - return outputFormat - .ok( new DiscoveryRepresentation( managementUri, dataUri, fabricateBoltAddress( host, 7687 ) ) ); + return outputFormat.ok( new DiscoveryRepresentation( managementUri, dataUri, + new AdvertisedSocketAddress( uriInfo.getBaseUri().getHost(), 7687 ) ) ); } } - private AdvertisedSocketAddress fabricateBoltAddress( String host, int port ) - { - return new AdvertisedSocketAddress( "bolt://" + host, port ); - } - @GET @Produces( MediaType.WILDCARD ) public Response redirectToBrowser() diff --git a/community/server/src/test/java/org/neo4j/server/BoltIT.java b/community/server/src/test/java/org/neo4j/server/BoltIT.java index da85c4dbf999b..246a981111a47 100644 --- a/community/server/src/test/java/org/neo4j/server/BoltIT.java +++ b/community/server/src/test/java/org/neo4j/server/BoltIT.java @@ -37,6 +37,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertFalse; import static org.neo4j.graphdb.factory.GraphDatabaseSettings.boltConnector; import static org.neo4j.server.helpers.CommunityServerBuilder.server; @@ -95,6 +96,7 @@ public void boltAddressShouldAppearToComeFromTheSameOriginAsTheHttpAddressEvenTh // Then Map map = JsonHelper.jsonToMap( response.getEntity() ); assertThat( String.valueOf( map.get( "bolt" ) ), containsString( "bolt://" + host ) ); + assertFalse( String.valueOf( map.get( "bolt" ) ).contains( "bolt://bolt://" ) ); } @Test diff --git a/community/server/src/test/java/org/neo4j/server/rest/discovery/DiscoveryServiceTest.java b/community/server/src/test/java/org/neo4j/server/rest/discovery/DiscoveryServiceTest.java index c3525b8315147..91e84adc456b7 100644 --- a/community/server/src/test/java/org/neo4j/server/rest/discovery/DiscoveryServiceTest.java +++ b/community/server/src/test/java/org/neo4j/server/rest/discovery/DiscoveryServiceTest.java @@ -26,6 +26,7 @@ import java.net.URISyntaxException; import java.util.HashMap; import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; import org.neo4j.graphdb.factory.GraphDatabaseSettings; import org.neo4j.helpers.AdvertisedSocketAddress; @@ -85,7 +86,7 @@ private DiscoveryService testDiscoveryService() throws URISyntaxException @Test public void shouldReturnValidJSON() throws Exception { - Response response = testDiscoveryService().getDiscoveryDocument( "localhost" ); + Response response = testDiscoveryService().getDiscoveryDocument( uriInfo( "localhost" ) ); String json = new String( (byte[]) response.getEntity() ); assertNotNull( json ); @@ -94,10 +95,18 @@ public void shouldReturnValidJSON() throws Exception assertThat( json, is( not( "null" ) ) ); } + private UriInfo uriInfo( String host ) + { + URI uri = URI.create( host ); + UriInfo uriInfo = mock( UriInfo.class ); + when( uriInfo.getBaseUri() ).thenReturn( uri ); + return uriInfo; + } + @Test public void shouldReturnBoltURI() throws Exception { - Response response = testDiscoveryService().getDiscoveryDocument( "localhost" ); + Response response = testDiscoveryService().getDiscoveryDocument( uriInfo( "localhost" ) ); String json = new String( (byte[]) response.getEntity() ); assertThat( json, containsString( "\"bolt\" : \"bolt://" + boltAddress ) ); } @@ -105,7 +114,7 @@ public void shouldReturnBoltURI() throws Exception @Test public void shouldReturnDataURI() throws Exception { - Response response = testDiscoveryService().getDiscoveryDocument( "localhost" ); + Response response = testDiscoveryService().getDiscoveryDocument( uriInfo( "localhost" ) ); String json = new String( (byte[]) response.getEntity() ); assertThat( json, containsString( "\"data\" : \"" + baseUri + dataUri + "/\"" ) ); } @@ -113,7 +122,7 @@ public void shouldReturnDataURI() throws Exception @Test public void shouldReturnManagementURI() throws Exception { - Response response = testDiscoveryService().getDiscoveryDocument( "localhost" ); + Response response = testDiscoveryService().getDiscoveryDocument( uriInfo( "localhost" ) ); String json = new String( (byte[]) response.getEntity() ); assertThat( json, containsString( "\"management\" : \"" + baseUri + managementUri + "/\"" ) ); }