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

Commit 98f7dcb

Browse files
authored
Merge pull request #248 from pontusmelke/4.2-routing-context
Handle valid URL
2 parents 74e580e + 424ea4c commit 98f7dcb

File tree

13 files changed

+94
-172
lines changed

13 files changed

+94
-172
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
@@ -273,7 +273,7 @@ public void wrongPortWithBolt() throws Exception
273273
{
274274
// given
275275
CliArgs cliArgs = new CliArgs();
276-
cliArgs.setScheme( "bolt://", "" );
276+
cliArgs.setScheme( "bolt", "" );
277277
cliArgs.setPort( 1234 );
278278

279279
ShellAndConnection sac = getShell( cliArgs );
@@ -290,7 +290,7 @@ public void wrongPortWithNeo4j() throws Exception
290290
{
291291
// given
292292
CliArgs cliArgs = new CliArgs();
293-
cliArgs.setScheme( "neo4j://", "" );
293+
cliArgs.setScheme( "neo4j", "" );
294294
cliArgs.setPort( 1234 );
295295

296296
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: 47 additions & 22 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");
83-
// Safe, regex only matches integers
84-
String portString = addressMatcher.group("port");
85-
cliArgs.setPort(portString == null ? CliArgs.DEFAULT_PORT : Integer.parseInt(portString));
80+
cliArgs.setScheme(uri.getScheme(), "bolt");
81+
cliArgs.setHost(uri.getHost(), "localhost");
82+
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)
@@ -153,7 +178,7 @@ private static ArgumentParser setupParser(ParameterMap parameterMap)
153178
ArgumentGroup connGroup = parser.addArgumentGroup("connection arguments");
154179
connGroup.addArgument("-a", "--address")
155180
.help("address and port to connect to")
156-
.setDefault(String.format("%s%s:%d", CliArgs.DEFAULT_SCHEME, CliArgs.DEFAULT_HOST, CliArgs.DEFAULT_PORT));
181+
.setDefault(String.format("%s://%s:%d", CliArgs.DEFAULT_SCHEME, CliArgs.DEFAULT_HOST, CliArgs.DEFAULT_PORT));
157182
connGroup.addArgument("-u", "--username")
158183
.setDefault("")
159184
.help("username to connect as. Can also be specified using environment variable " + ConnectionConfig.USERNAME_ENV_VAR);

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-
static final String DEFAULT_SCHEME = "neo4j://";
13+
static final String DEFAULT_SCHEME = "neo4j";
1414
static final String DEFAULT_HOST = "localhost";
1515
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
@@ -195,20 +195,20 @@ public void connect( @Nonnull ConnectionConfig connectionConfig, ThrowingAction<
195195
String fallbackScheme;
196196
switch ( scheme )
197197
{
198-
case Scheme.NEO4J_URI_SCHEME + "://":
198+
case Scheme.NEO4J_URI_SCHEME:
199199
fallbackScheme = Scheme.BOLT_URI_SCHEME;
200200
break;
201-
case Scheme.NEO4J_LOW_TRUST_URI_SCHEME + "://":
201+
case Scheme.NEO4J_LOW_TRUST_URI_SCHEME:
202202
fallbackScheme = Scheme.BOLT_LOW_TRUST_URI_SCHEME;
203203
break;
204-
case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME + "://":
204+
case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME:
205205
fallbackScheme = Scheme.BOLT_HIGH_TRUST_URI_SCHEME;
206206
break;
207207
default:
208208
throw e;
209209
}
210210
connectionConfig = new ConnectionConfig(
211-
fallbackScheme + "://",
211+
fallbackScheme,
212212
connectionConfig.host(),
213213
connectionConfig.port(),
214214
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)