10
10
import java .security .KeyStore ;
11
11
import java .sql .SQLException ;
12
12
import java .util .ArrayList ;
13
+ import java .util .Arrays ;
13
14
import java .util .HashMap ;
15
+ import java .util .HashSet ;
14
16
import java .util .List ;
15
17
import java .util .Locale ;
16
18
import java .util .Map ;
17
19
import java .util .Optional ;
18
20
import java .util .Properties ;
21
+ import java .util .Set ;
22
+ import java .util .stream .Collectors ;
19
23
20
24
import static com .intellij .DriverPropertyInfoHelper .ENABLE_SSL ;
21
25
import static com .intellij .DriverPropertyInfoHelper .ENABLE_SSL_DEFAULT ;
26
+ import static com .intellij .DriverPropertyInfoHelper .PASSWORD ;
27
+ import static com .intellij .DriverPropertyInfoHelper .USER ;
22
28
import static com .intellij .DriverPropertyInfoHelper .isTrue ;
23
29
24
30
class CouchbaseClientURI {
25
31
static final String PREFIX = "jdbc:couchbase:" ;
26
32
27
- private final String hosts ;
28
- private final String keyspace ;
29
- private final String collection ;
33
+ private static final Set < String > JDBC_KEYS = new HashSet <>( Arrays . asList ( USER , PASSWORD , ENABLE_SSL )) ;
34
+
35
+ private final String connectionString ;
30
36
private final String uri ;
37
+ private final String hosts ;
31
38
private final String userName ;
32
39
private final String password ;
33
40
private final boolean sslEnabled ;
34
41
35
42
public CouchbaseClientURI (String uri , Properties info ) {
36
43
this .uri = uri ;
37
- if (!uri .startsWith (PREFIX ))
44
+ if (!uri .startsWith (PREFIX )) {
38
45
throw new IllegalArgumentException ("URI needs to start with " + PREFIX );
46
+ }
39
47
40
- uri = uri .substring (PREFIX .length ());
41
-
42
-
43
- String serverPart ;
44
- String nsPart ;
48
+ String trimmedUri = uri .substring (PREFIX .length ());
45
49
Map <String , List <String >> options = null ;
50
+ String serverPart ;
46
51
47
- {
48
- int lastSlashIndex = uri .lastIndexOf ("/" );
49
- if (lastSlashIndex < 0 ) {
50
- if (uri .contains ("?" )) {
51
- throw new IllegalArgumentException ("URI contains options without trailing slash" );
52
- }
53
- serverPart = uri ;
54
- nsPart = null ;
55
- } else {
56
- serverPart = uri .substring (0 , lastSlashIndex );
57
- nsPart = uri .substring (lastSlashIndex + 1 );
58
-
59
- int questionMarkIndex = nsPart .indexOf ("?" );
60
- if (questionMarkIndex >= 0 ) {
61
- options = parseOptions (nsPart .substring (questionMarkIndex + 1 ));
62
- nsPart = nsPart .substring (0 , questionMarkIndex );
63
- }
64
- }
65
- }
66
-
67
- hosts = serverPart ;
68
- this .userName = getOption (info , options , "user" , null );
69
- this .password = getOption (info , options , "password" , null );
70
- String sslEnabledOption = getOption (info , options , ENABLE_SSL , ENABLE_SSL_DEFAULT );
71
- this .sslEnabled = isTrue (sslEnabledOption );
72
-
73
- if (nsPart != null && nsPart .length () != 0 ) { // keyspace._collection
74
- int dotIndex = nsPart .indexOf ("." );
75
- if (dotIndex < 0 ) {
76
- keyspace = nsPart ;
77
- collection = null ;
78
- } else {
79
- keyspace = nsPart .substring (0 , dotIndex );
80
- collection = nsPart .substring (dotIndex + 1 );
81
- }
52
+ int optionsStartIndex = trimmedUri .indexOf ("?" );
53
+ if (optionsStartIndex < 0 ) {
54
+ serverPart = trimmedUri ;
82
55
} else {
83
- keyspace = null ;
84
- collection = null ;
56
+ serverPart = trimmedUri . substring ( 0 , optionsStartIndex ) ;
57
+ options = parseOptions ( trimmedUri . substring ( optionsStartIndex + 1 )) ;
85
58
}
59
+
60
+ this .userName = getOption (info , options , USER , null );
61
+ this .password = getOption (info , options , PASSWORD , null );
62
+ this .sslEnabled = isTrue (getOption (info , options , ENABLE_SSL , ENABLE_SSL_DEFAULT ));
63
+ this .hosts = serverPart ;
64
+ this .connectionString = createConnectionString (serverPart , options );
86
65
}
87
66
88
67
/**
@@ -114,7 +93,7 @@ Cluster createCluster() throws SQLException {
114
93
}
115
94
authenticator = PasswordAuthenticator .create (userName , password );
116
95
}
117
- return Cluster .connect (hosts , ClusterOptions .clusterOptions (authenticator ));
96
+ return Cluster .connect (connectionString , ClusterOptions .clusterOptions (authenticator ));
118
97
}
119
98
120
99
private String getLastValue (final Map <String , List <String >> optionsMap , final String key ) {
@@ -127,7 +106,7 @@ private String getLastValue(final Map<String, List<String>> optionsMap, final St
127
106
private Map <String , List <String >> parseOptions (String optionsPart ) {
128
107
Map <String , List <String >> optionsMap = new HashMap <>();
129
108
130
- for (String _part : optionsPart .split ("[&;] " )) {
109
+ for (String _part : optionsPart .split ("& " )) {
131
110
int idx = _part .indexOf ("=" );
132
111
if (idx >= 0 ) {
133
112
String key = _part .substring (0 , idx ).toLowerCase (Locale .ENGLISH );
@@ -144,6 +123,15 @@ private Map<String, List<String>> parseOptions(String optionsPart) {
144
123
return optionsMap ;
145
124
}
146
125
126
+ private String createConnectionString (String hosts , Map <String , List <String >> optionsMap ) {
127
+ if (optionsMap == null ) {
128
+ return hosts ;
129
+ }
130
+ return optionsMap .keySet ().stream ()
131
+ .filter (key -> !JDBC_KEYS .contains (key ))
132
+ .map (key -> key + "=" + getLastValue (optionsMap , key ))
133
+ .collect (Collectors .joining ("&" , hosts + "?" , "" ));
134
+ }
147
135
148
136
// ---------------------------------
149
137
@@ -175,31 +163,21 @@ public Boolean getSslEnabled() {
175
163
}
176
164
177
165
/**
178
- * Gets the list of hosts
166
+ * Gets the list of hosts and params sent directly to Java SDK
179
167
*
180
168
* @return the host list
181
169
*/
182
- public String getHosts () {
183
- return hosts ;
170
+ public String getConnectionString () {
171
+ return connectionString ;
184
172
}
185
173
186
174
/**
187
- * Gets the keyspace name
188
- *
189
- * @return the keyspace name
190
- */
191
- public String getKeyspace () {
192
- return keyspace ;
193
- }
194
-
195
-
196
- /**
197
- * Gets the collection name
175
+ * Gets the list of hosts
198
176
*
199
- * @return the collection name
177
+ * @return the host list
200
178
*/
201
- public String getCollection () {
202
- return collection ;
179
+ public String getHosts () {
180
+ return hosts ;
203
181
}
204
182
205
183
/**
0 commit comments