Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit a0a5ff2

Browse files
authored
Merge pull request #243 from pontusmelke/4.0-routing-context
Handle valid URL
2 parents 7f3190f + 5ae075c commit a0a5ff2

File tree

13 files changed

+90
-169
lines changed

13 files changed

+90
-169
lines changed

cypher-shell/src/integration-test/java/org/neo4j/shell/MainIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void wrongPortWithBolt() throws Exception
271271
{
272272
// given
273273
CliArgs cliArgs = new CliArgs();
274-
cliArgs.setScheme( "bolt://", "" );
274+
cliArgs.setScheme( "bolt", "" );
275275
cliArgs.setPort( 1234 );
276276

277277
ShellAndConnection sac = getShell( cliArgs );
@@ -288,7 +288,7 @@ public void wrongPortWithNeo4j() throws Exception
288288
{
289289
// given
290290
CliArgs cliArgs = new CliArgs();
291-
cliArgs.setScheme( "neo4j://", "" );
291+
cliArgs.setScheme( "neo4j", "" );
292292
cliArgs.setPort( 1234 );
293293

294294
ShellAndConnection sac = getShell( cliArgs );

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ abstract class CypherShellIntegrationTest
1212
CypherShell shell;
1313

1414
void connect(String password) throws CommandException {
15-
shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", password, Encryption.DEFAULT, ABSENT_DB_NAME ) );
15+
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", password, Encryption.DEFAULT, ABSENT_DB_NAME ) );
1616
}
1717
}

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellMultiDatabaseIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void setUp() throws Exception
4646
beginCommand = new Begin( shell );
4747
rollbackCommand = new Rollback( shell );
4848

49-
shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
49+
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
5050

5151
// Multiple databases are only available from 4.0
5252
assumeTrue( majorVersion( shell.getServerVersion() ) >= 4 );
@@ -138,7 +138,7 @@ public void switchingToNonExistingDatabaseShouldGiveErrorResponseFromServerInter
138138
{
139139
shell = new CypherShell( linePrinter, new PrettyConfig( Format.PLAIN, true, 1000 ), true, new ShellParameterMap() );
140140
useCommand = new Use( shell );
141-
shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
141+
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
142142

143143
useCommand.execute( SYSTEM_DB_NAME );
144144

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellProtocolIntegrationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public class CypherShellProtocolIntegrationTest{
2121
@Test
2222
public void shouldConnectWithBoltProtocol() throws Exception {
2323
CypherShell shell = new CypherShell( new StringLinePrinter(), new PrettyConfig( Format.PLAIN, true, 1000), false, new ShellParameterMap());
24-
shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
24+
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
2525
assertTrue(shell.isConnected());
2626
}
2727

2828
@Test
2929
public void shouldConnectWithNeo4jProtocol() throws Exception {
3030
CypherShell shell = new CypherShell( new StringLinePrinter(), new PrettyConfig( Format.PLAIN, true, 1000), false, new ShellParameterMap());
3131
// This should work even on older databases without the neo4j protocol, by falling back to bolt
32-
shell.connect( new ConnectionConfig( "neo4j://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
32+
shell.connect( new ConnectionConfig( "neo4j", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
3333
assertTrue(shell.isConnected());
3434
}
3535

@@ -38,7 +38,7 @@ public void shouldConnectWithBoltSSCProtocol() throws Exception {
3838
CypherShell shell = new CypherShell( new StringLinePrinter(), new PrettyConfig( Format.PLAIN, true, 1000), false, new ShellParameterMap());
3939
// Given 3.X series where X > 1, where SSC are the default. Hard to test in 4.0 sadly.
4040
onlyIn3_2to3_6( shell);
41-
shell.connect( new ConnectionConfig( "bolt+ssc://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
41+
shell.connect( new ConnectionConfig( "bolt+ssc", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
4242
assertTrue(shell.isConnected());
4343
}
4444

@@ -48,15 +48,15 @@ public void shouldConnectWithNeo4jSSCProtocol() throws Exception {
4848
// Given 3.X series where X > 1, where SSC are the default. Hard to test in 4.0 sadly.
4949
onlyIn3_2to3_6( shell);
5050
// This should work by falling back to bolt+ssc
51-
shell.connect( new ConnectionConfig( "neo4j+ssc://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
51+
shell.connect( new ConnectionConfig( "neo4j+ssc", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
5252
assertTrue(shell.isConnected());
5353
}
5454

5555
// Here should be tests for "neo4j+s" and "bolt+s", but we don't have the infrastructure for those.
5656

5757
private void onlyIn3_2to3_6( CypherShell shell) throws Exception {
5858
// Default connection settings
59-
shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
59+
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
6060
assumeTrue( majorVersion( shell.getServerVersion() ) == 3 );
6161
assumeTrue( minorVersion( shell.getServerVersion() ) > 1 );
6262
shell.disconnect();

cypher-shell/src/main/java/org/neo4j/shell/ConnectionConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public String newPassword() {
7676

7777
@Nonnull
7878
public String driverUrl() {
79-
return String.format("%s%s:%d", scheme(), host(), port());
79+
return String.format("%s://%s:%d", scheme(), host(), port());
8080
}
8181

8282
@Nonnull

cypher-shell/src/main/java/org/neo4j/shell/cli/CliArgHelper.java

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import net.sourceforge.argparse4j.inf.Namespace;
1515

1616
import java.io.PrintWriter;
17+
import java.net.URI;
18+
import java.net.URISyntaxException;
1719
import java.util.regex.Matcher;
1820
import java.util.regex.Pattern;
1921
import javax.annotation.Nonnull;
@@ -31,9 +33,6 @@
3133
*/
3234
public class CliArgHelper {
3335

34-
static final Pattern ADDRESS_ARG_PATTERN =
35-
Pattern.compile("\\s*(?<scheme>[a-zA-Z0-9+\\-.]+://)?((?<username>\\w+):(?<password>[^\\s]+)@)?(?<host>[a-zA-Z\\d\\-.]+)?(:(?<port>\\d+))?\\s*");
36-
3736
/**
3837
* @param args to parse
3938
* @return null in case of error, commandline arguments otherwise
@@ -70,22 +69,21 @@ public static CliArgs parseAndThrow( @Nonnull String... args ) throws ArgumentPa
7069
private static CliArgs getCliArgs( CliArgs cliArgs, ArgumentParser parser, Namespace ns )
7170
{
7271
// Parse address string, returns null on error
73-
final Matcher addressMatcher = parseAddressMatcher( parser, ns.getString( "address"));
72+
final URI uri = parseURI( parser, ns.getString( "address"));
7473

75-
if (addressMatcher == null) {
74+
if (uri == null) {
7675
return null;
7776
}
7877

7978
//---------------------
8079
// Connection arguments
81-
cliArgs.setScheme(addressMatcher.group("scheme"), "bolt://");
82-
cliArgs.setHost(addressMatcher.group("host"), "localhost");
80+
cliArgs.setScheme(uri.getScheme(), "bolt");
81+
cliArgs.setHost(uri.getHost(), "localhost");
8382
// Safe, regex only matches integers
84-
String portString = addressMatcher.group("port");
85-
cliArgs.setPort(portString == null ? 7687 : Integer.parseInt(portString));
83+
int port = uri.getPort();
84+
cliArgs.setPort(port == -1 ? 7687 : port);
8685
// Also parse username and password from address if available
87-
cliArgs.setUsername(addressMatcher.group("username"), "");
88-
cliArgs.setPassword(addressMatcher.group("password"), "");
86+
parseUserInfo( uri, cliArgs );
8987

9088
// Only overwrite user/pass from address string if the arguments were specified
9189
String user = ns.getString("username");
@@ -125,19 +123,46 @@ private static CliArgs getCliArgs( CliArgs cliArgs, ArgumentParser parser, Names
125123
return cliArgs;
126124
}
127125

126+
private static void parseUserInfo(URI uri, CliArgs cliArgs)
127+
{
128+
String userInfo = uri.getUserInfo();
129+
String user = null;
130+
String password = null;
131+
if (userInfo != null)
132+
{
133+
String[] split = userInfo.split( ":" );
134+
if (split.length == 0)
135+
{
136+
user = userInfo;
137+
} else if (split.length == 2)
138+
{
139+
user = split[0];
140+
password = split[1];
141+
} else {
142+
throw new IllegalArgumentException("Cannot parse user and password from " + userInfo);
143+
}
144+
145+
}
146+
cliArgs.setUsername(user, "");
147+
cliArgs.setPassword(password, "");
148+
}
149+
128150
@Nullable
129-
private static Matcher parseAddressMatcher(ArgumentParser parser, String address) {
130-
Matcher matcher = ADDRESS_ARG_PATTERN.matcher(address);
131-
if (!matcher.matches()) {
132-
// Match behavior in built-in error handling
133-
PrintWriter printWriter = new PrintWriter(System.err);
134-
parser.printUsage(printWriter);
135-
printWriter.println("cypher-shell: error: Failed to parse address: '" + address + "'");
136-
printWriter.println("\n Address should be of the form: [scheme://][username:password@][host][:port]");
151+
static URI parseURI( ArgumentParser parser, String address )
152+
{
153+
try
154+
{
155+
return new URI( address );
156+
}
157+
catch ( URISyntaxException e )
158+
{
159+
PrintWriter printWriter = new PrintWriter( System.err );
160+
parser.printUsage( printWriter );
161+
printWriter.println( "cypher-shell: error: Failed to parse address: '" + address + "'" );
162+
printWriter.println( "\n Address should be of the form: [scheme://][username:password@][host][:port]" );
137163
printWriter.flush();
138164
return null;
139165
}
140-
return matcher;
141166
}
142167

143168
private static ArgumentParser setupParser(ParameterMap parameterMap)

cypher-shell/src/main/java/org/neo4j/shell/cli/CliArgs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import static org.neo4j.shell.DatabaseManager.ABSENT_DB_NAME;
1111

1212
public class CliArgs {
13-
private static final String DEFAULT_SCHEME = "bolt://";
13+
private static final String DEFAULT_SCHEME = "bolt";
1414
private static final String DEFAULT_HOST = "localhost";
1515
private static final int DEFAULT_PORT = 7687;
1616
static final int DEFAULT_NUM_SAMPLE_ROWS = 1000;

cypher-shell/src/main/java/org/neo4j/shell/state/BoltStateHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,20 @@ public void connect( @Nonnull ConnectionConfig connectionConfig, ThrowingAction<
169169
String fallbackScheme;
170170
switch ( scheme )
171171
{
172-
case Scheme.NEO4J_URI_SCHEME + "://":
172+
case Scheme.NEO4J_URI_SCHEME:
173173
fallbackScheme = Scheme.BOLT_URI_SCHEME;
174174
break;
175-
case Scheme.NEO4J_LOW_TRUST_URI_SCHEME + "://":
175+
case Scheme.NEO4J_LOW_TRUST_URI_SCHEME:
176176
fallbackScheme = Scheme.BOLT_LOW_TRUST_URI_SCHEME;
177177
break;
178-
case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME + "://":
178+
case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME:
179179
fallbackScheme = Scheme.BOLT_HIGH_TRUST_URI_SCHEME;
180180
break;
181181
default:
182182
throw e;
183183
}
184184
connectionConfig = new ConnectionConfig(
185-
fallbackScheme + "://",
185+
fallbackScheme,
186186
connectionConfig.host(),
187187
connectionConfig.port(),
188188
connectionConfig.username(),

cypher-shell/src/test/java/org/neo4j/shell/ConnectionConfigTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public class ConnectionConfigTest {
1818
= new EnvironmentVariables();
1919

2020
private Logger logger = mock(Logger.class);
21-
private ConnectionConfig config = new ConnectionConfig("bolt://", "localhost", 1, "bob",
21+
private ConnectionConfig config = new ConnectionConfig("bolt", "localhost", 1, "bob",
2222
"pass", Encryption.DEFAULT, "db");
2323

2424

2525
@Test
2626
public void scheme() {
27-
assertEquals("bolt://", config.scheme());
27+
assertEquals("bolt", config.scheme());
2828
}
2929

3030
@Test
@@ -45,7 +45,7 @@ public void username() {
4545
@Test
4646
public void usernameDefaultsToEnvironmentVar() {
4747
environmentVariables.set(ConnectionConfig.USERNAME_ENV_VAR, "alice");
48-
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt://", "localhost", 1, "",
48+
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt", "localhost", 1, "",
4949
"", Encryption.DEFAULT, ABSENT_DB_NAME);
5050
assertEquals("alice", configWithEmptyParams.username());
5151
}
@@ -58,7 +58,7 @@ public void password() {
5858
@Test
5959
public void passwordDefaultsToEnvironmentVar() {
6060
environmentVariables.set(ConnectionConfig.PASSWORD_ENV_VAR, "ssap");
61-
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt://", "localhost", 1, "",
61+
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt", "localhost", 1, "",
6262
"", Encryption.DEFAULT, ABSENT_DB_NAME);
6363
assertEquals("ssap", configWithEmptyParams.password());
6464
}
@@ -71,7 +71,7 @@ public void database() {
7171
@Test
7272
public void databaseDefaultsToEnvironmentVar() {
7373
environmentVariables.set(ConnectionConfig.DATABASE_ENV_VAR, "funnyDB");
74-
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt://", "localhost", 1, "",
74+
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt", "localhost", 1, "",
7575
"", Encryption.DEFAULT, ABSENT_DB_NAME);
7676
assertEquals("funnyDB", configWithEmptyParams.database());
7777
}
@@ -82,8 +82,8 @@ public void driverUrlDefaultScheme() {
8282

8383
@Test
8484
public void encryption() {
85-
assertEquals(Encryption.DEFAULT, new ConnectionConfig("bolt://", "", -1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME).encryption());
86-
assertEquals(Encryption.TRUE, new ConnectionConfig("bolt://", "", -1, "", "", Encryption.TRUE, ABSENT_DB_NAME).encryption());
87-
assertEquals(Encryption.FALSE, new ConnectionConfig("bolt://", "", -1, "", "", Encryption.FALSE, ABSENT_DB_NAME).encryption());
85+
assertEquals(Encryption.DEFAULT, new ConnectionConfig("bolt", "", -1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME).encryption());
86+
assertEquals(Encryption.TRUE, new ConnectionConfig("bolt", "", -1, "", "", Encryption.TRUE, ABSENT_DB_NAME).encryption());
87+
assertEquals(Encryption.FALSE, new ConnectionConfig("bolt", "", -1, "", "", Encryption.FALSE, ABSENT_DB_NAME).encryption());
8888
}
8989
}

cypher-shell/src/test/java/org/neo4j/shell/CypherShellTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void setup() {
6363

6464
@Test
6565
public void verifyDelegationOfConnectionMethods() throws CommandException {
66-
ConnectionConfig cc = new ConnectionConfig("bolt://", "", 1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME);
66+
ConnectionConfig cc = new ConnectionConfig("bolt", "", 1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME);
6767
CypherShell shell = new CypherShell(logger, mockedBoltStateHandler, mockedPrettyPrinter, new ShellParameterMap());
6868

6969
shell.connect(cc);

0 commit comments

Comments
 (0)