diff --git a/community/server/src/main/java/org/neo4j/server/rest/discovery/CommunityDiscoverableURIs.java b/community/server/src/main/java/org/neo4j/server/rest/discovery/CommunityDiscoverableURIs.java index 9fcd19a132838..7b36c0118ebb3 100644 --- a/community/server/src/main/java/org/neo4j/server/rest/discovery/CommunityDiscoverableURIs.java +++ b/community/server/src/main/java/org/neo4j/server/rest/discovery/CommunityDiscoverableURIs.java @@ -23,6 +23,8 @@ import org.neo4j.kernel.configuration.ConnectorPortRegister; import org.neo4j.server.configuration.ServerSettings; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.NORMAL; + public class CommunityDiscoverableURIs { /** @@ -31,8 +33,8 @@ public class CommunityDiscoverableURIs public static DiscoverableURIs communityDiscoverableURIs( Config config, ConnectorPortRegister portRegister ) { return new DiscoverableURIs.Builder() - .add( "data", config.get( ServerSettings.rest_api_path ).getPath() + "/", DiscoverableURIs.NORMAL ) - .add( "management", config.get( ServerSettings.management_api_path ).getPath() + "/", DiscoverableURIs.NORMAL ) + .add( "data", config.get( ServerSettings.rest_api_path ).getPath() + "/", NORMAL ) + .add( "management", config.get( ServerSettings.management_api_path ).getPath() + "/", NORMAL ) .addBoltConnectorFromConfig( "bolt", "bolt", config, ServerSettings.bolt_discoverable_address, portRegister ) .build(); } diff --git a/community/server/src/main/java/org/neo4j/server/rest/discovery/DiscoverableURIs.java b/community/server/src/main/java/org/neo4j/server/rest/discovery/DiscoverableURIs.java index 768af7b2d3e55..734bb3d19c21c 100644 --- a/community/server/src/main/java/org/neo4j/server/rest/discovery/DiscoverableURIs.java +++ b/community/server/src/main/java/org/neo4j/server/rest/discovery/DiscoverableURIs.java @@ -25,7 +25,6 @@ import java.util.Collection; import java.util.Comparator; import java.util.List; -import java.util.Optional; import java.util.function.BiConsumer; import java.util.stream.Collectors; @@ -35,16 +34,24 @@ import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.configuration.ConnectorPortRegister; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.HIGH; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.HIGHEST; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.LOW; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.LOWEST; + /** * Repository of URIs that the REST API publicly advertises at the root endpoint. */ public class DiscoverableURIs { - public static final int HIGHEST = 5; - public static final int HIGH = 4; - public static final int NORMAL = 3; - public static final int LOW = 2; - public static final int LOWEST = 1; + public enum Precedence + { + LOWEST, + LOW, + NORMAL, + HIGH, + HIGHEST + } private final Collection entries; @@ -64,10 +71,10 @@ public void forEach( BiConsumer consumer ) private static class URIEntry { private String key; - private int precedence; + private Precedence precedence; private URI uri; - private URIEntry( String key, URI uri, int precedence ) + private URIEntry( String key, URI uri, Precedence precedence ) { this.key = key; this.uri = uri; @@ -89,21 +96,19 @@ public Builder( DiscoverableURIs copy ) entries = new ArrayList<>( copy.entries ); } - public Builder add( String key, URI uri, int precedence ) + public Builder add( String key, URI uri, Precedence precedence ) { - Optional entry = entries.stream().filter( e -> e.key.equals( key ) && e.precedence == precedence ).findFirst(); - - if ( entry.isPresent() ) + if ( entries.stream().anyMatch( e -> e.key.equals( key ) && e.precedence == precedence ) ) { throw new InvalidSettingException( - String.format( "Unable to add two entries with the same precedence using key '%s' and precedence '%d'", key, precedence ) ); + String.format( "Unable to add two entries with the same precedence using key '%s' and precedence '%s'", key, precedence ) ); } entries.add( new URIEntry( key, uri, precedence ) ); return this; } - public Builder add( String key, String uri, int precedence ) + public Builder add( String key, String uri, Precedence precedence ) { try { @@ -116,7 +121,7 @@ public Builder add( String key, String uri, int precedence ) } } - public Builder add( String key, String scheme, String hostname, int port, int precedence ) + public Builder add( String key, String scheme, String hostname, int port, Precedence precedence ) { try { diff --git a/community/server/src/test/java/org/neo4j/server/rest/discovery/DiscoverableURIsTest.java b/community/server/src/test/java/org/neo4j/server/rest/discovery/DiscoverableURIsTest.java index 8522537d6365b..1a1e18ff33edf 100644 --- a/community/server/src/test/java/org/neo4j/server/rest/discovery/DiscoverableURIsTest.java +++ b/community/server/src/test/java/org/neo4j/server/rest/discovery/DiscoverableURIsTest.java @@ -39,6 +39,11 @@ import static org.mockito.Mockito.only; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.HIGH; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.HIGHEST; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.LOW; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.LOWEST; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.NORMAL; public class DiscoverableURIsTest { @@ -59,9 +64,9 @@ public void shouldInvokeConsumerForEachKey() throws URISyntaxException { DiscoverableURIs discoverables = new DiscoverableURIs.Builder() - .add( "a", "/test", DiscoverableURIs.NORMAL ) - .add( "b", "/data", DiscoverableURIs.NORMAL ) - .add( "c", "http://www.example.com", DiscoverableURIs.LOW ) + .add( "a", "/test", NORMAL ) + .add( "b", "/data", NORMAL ) + .add( "c", "http://www.example.com", LOW ) .build(); discoverables.forEach( consumer ); @@ -78,8 +83,8 @@ public void shouldThrowWhenAddingTwoEntriesWithSamePrecedence() { DiscoverableURIs discoverables = new DiscoverableURIs.Builder() - .add( "a", "/test", DiscoverableURIs.NORMAL ) - .add( "a", "/data", DiscoverableURIs.NORMAL ) + .add( "a", "/test", NORMAL ) + .add( "a", "/data", NORMAL ) .build(); fail( "exception expected" ); @@ -96,12 +101,12 @@ public void shouldInvokeConsumerForEachKeyWithHighestPrecedence() throws URISynt { DiscoverableURIs discoverables = new DiscoverableURIs.Builder() - .add( "c", "bolt://localhost:7687", DiscoverableURIs.HIGHEST ) - .add( "a", "/test", DiscoverableURIs.NORMAL ) - .add( "b", "/data", DiscoverableURIs.NORMAL ) - .add( "b", "/data2", DiscoverableURIs.LOWEST ) - .add( "a", "/test2", DiscoverableURIs.HIGHEST ) - .add( "c", "bolt://localhost:7688", DiscoverableURIs.LOW ) + .add( "c", "bolt://localhost:7687", HIGHEST ) + .add( "a", "/test", NORMAL ) + .add( "b", "/data", NORMAL ) + .add( "b", "/data2", LOWEST ) + .add( "a", "/test2", HIGHEST ) + .add( "c", "bolt://localhost:7688", LOW ) .build(); discoverables.forEach( consumer ); @@ -116,11 +121,11 @@ public void shouldInvokeConsumerForEachKeyWithHighestPrecedenceOnce() throws URI { DiscoverableURIs discoverables = new DiscoverableURIs.Builder() - .add( "a", "/test1", DiscoverableURIs.LOWEST ) - .add( "a", "/test2", DiscoverableURIs.LOW ) - .add( "a", "/data3", DiscoverableURIs.NORMAL ) - .add( "a", "/test4", DiscoverableURIs.HIGH ) - .add( "a", "/test5", DiscoverableURIs.HIGHEST ) + .add( "a", "/test1", LOWEST ) + .add( "a", "/test2", LOW ) + .add( "a", "/data3", NORMAL ) + .add( "a", "/test4", HIGH ) + .add( "a", "/test5", HIGHEST ) .build(); discoverables.forEach( consumer ); @@ -132,7 +137,7 @@ public void shouldInvokeConsumerForEachKeyWithHighestPrecedenceOnce() throws URI public void shouldConvertStringIntoURI() throws URISyntaxException { DiscoverableURIs empty = new DiscoverableURIs.Builder() - .add( "a", "bolt://localhost:7687", DiscoverableURIs.NORMAL ) + .add( "a", "bolt://localhost:7687", NORMAL ) .build(); empty.forEach( consumer ); @@ -144,7 +149,7 @@ public void shouldConvertStringIntoURI() throws URISyntaxException public void shouldConvertSchemeHostPortIntoURI() throws URISyntaxException { DiscoverableURIs empty = new DiscoverableURIs.Builder() - .add( "a", "bolt", "www.example.com", 8888, DiscoverableURIs.NORMAL ) + .add( "a", "bolt", "www.example.com", 8888, NORMAL ) .build(); empty.forEach( consumer ); @@ -158,7 +163,7 @@ public void shouldUsePassedURI() throws URISyntaxException URI uri = new URI( "bolt://www.example.com:9999" ); DiscoverableURIs empty = new DiscoverableURIs.Builder() - .add( "a", uri, DiscoverableURIs.NORMAL ) + .add( "a", uri, NORMAL ) .build(); empty.forEach( consumer ); @@ -171,7 +176,7 @@ public void shouldOverrideLowestForAbsolute() throws URISyntaxException { URI override = new URI( "http://www.example.com:9999" ); DiscoverableURIs empty = new DiscoverableURIs.Builder() - .add( "a", "bolt://localhost:8989", DiscoverableURIs.LOWEST ) + .add( "a", "bolt://localhost:8989", LOWEST ) .overrideAbsolutesFromRequest( override ) .build(); @@ -185,10 +190,10 @@ public void shouldNotOverrideOtherThanLowestForAbsolute() throws URISyntaxExcept { URI override = new URI( "http://www.example.com:9999" ); DiscoverableURIs empty = new DiscoverableURIs.Builder() - .add( "a", "bolt://localhost:8989", DiscoverableURIs.LOW ) - .add( "b", "bolt://localhost:8990", DiscoverableURIs.NORMAL ) - .add( "c", "bolt://localhost:8991", DiscoverableURIs.HIGH ) - .add( "d", "bolt://localhost:8992", DiscoverableURIs.HIGHEST ) + .add( "a", "bolt://localhost:8989", LOW ) + .add( "b", "bolt://localhost:8990", NORMAL ) + .add( "c", "bolt://localhost:8991", HIGH ) + .add( "d", "bolt://localhost:8992", HIGHEST ) .overrideAbsolutesFromRequest( override ) .build(); diff --git a/community/server/src/test/java/org/neo4j/server/rest/repr/DiscoveryRepresentationTest.java b/community/server/src/test/java/org/neo4j/server/rest/repr/DiscoveryRepresentationTest.java index 43861dedd180e..02e3d0fc1d345 100644 --- a/community/server/src/test/java/org/neo4j/server/rest/repr/DiscoveryRepresentationTest.java +++ b/community/server/src/test/java/org/neo4j/server/rest/repr/DiscoveryRepresentationTest.java @@ -29,6 +29,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.NORMAL; public class DiscoveryRepresentationTest { @@ -39,9 +40,9 @@ public void shouldCreateAMapContainingDataAndManagementURIs() throws URISyntaxEx String dataUri = "/data"; DiscoveryRepresentation dr = new DiscoveryRepresentation( new DiscoverableURIs.Builder() - .add( "management", managementUri, DiscoverableURIs.NORMAL ) - .add( "data", dataUri, DiscoverableURIs.NORMAL ) - .add( "bolt", new URI( "bolt://localhost:7687" ), DiscoverableURIs.NORMAL ).build() ); + .add( "management", managementUri, NORMAL ) + .add( "data", dataUri, NORMAL ) + .add( "bolt", new URI( "bolt://localhost:7687" ), NORMAL ).build() ); Map mapOfUris = RepresentationTestAccess.serialize( dr );