16
16
17
17
package com .google .cloud .sql .sqlserver ;
18
18
19
+ import com .google .cloud .sql .core .CloudSqlInstanceName ;
19
20
import com .google .cloud .sql .core .ConnectionConfig ;
20
21
import com .google .cloud .sql .core .InternalConnectorRegistry ;
21
22
import com .google .common .annotations .VisibleForTesting ;
25
26
import java .net .InetAddress ;
26
27
import java .net .Socket ;
27
28
import java .net .URLDecoder ;
28
- import java .nio .charset .StandardCharsets ;
29
29
import java .util .List ;
30
30
import java .util .Properties ;
31
31
@@ -43,14 +43,20 @@ public class SocketFactory extends javax.net.SocketFactory {
43
43
44
44
// props are protected, not private, so that they can be accessed from unit tests
45
45
@ VisibleForTesting protected Properties props = new Properties ();
46
+ @ VisibleForTesting protected String domainName ;
46
47
47
48
/**
48
49
* Implements the {@link SocketFactory} constructor, which can be used to create authenticated
49
50
* connections to a Cloud SQL instance.
50
51
*/
51
52
public SocketFactory (String socketFactoryConstructorArg ) throws UnsupportedEncodingException {
52
53
List <String > s = Splitter .on ('?' ).splitToList (socketFactoryConstructorArg );
53
- this .props .setProperty (ConnectionConfig .CLOUD_SQL_INSTANCE_PROPERTY , s .get (0 ));
54
+ final String instanceOrDomainName = s .get (0 );
55
+ if (CloudSqlInstanceName .isValidInstanceName (instanceOrDomainName )) {
56
+ this .props .setProperty (ConnectionConfig .CLOUD_SQL_INSTANCE_PROPERTY , instanceOrDomainName );
57
+ } else {
58
+ domainName = instanceOrDomainName ;
59
+ }
54
60
if (s .size () == 2 && s .get (1 ).length () > 0 ) {
55
61
Iterable <String > queryParams = Splitter .on ('&' ).split (s .get (1 ));
56
62
for (String param : queryParams ) {
@@ -62,8 +68,8 @@ public SocketFactory(String socketFactoryConstructorArg) throws UnsupportedEncod
62
68
String .format ("Malformed query param in socketFactoryConstructorArg : %s" , param ));
63
69
}
64
70
this .props .setProperty (
65
- URLDecoder .decode (splitParam .get (0 ), StandardCharsets . UTF_8 . name () ),
66
- URLDecoder .decode (splitParam .get (1 ), StandardCharsets . UTF_8 . name () ));
71
+ URLDecoder .decode (splitParam .get (0 ), "utf-8" ),
72
+ URLDecoder .decode (splitParam .get (1 ), "utf-8" ));
67
73
}
68
74
} else if (s .size () > 2 ) {
69
75
throw new IllegalArgumentException (
@@ -75,7 +81,7 @@ public SocketFactory(String socketFactoryConstructorArg) throws UnsupportedEncod
75
81
public Socket createSocket () throws IOException {
76
82
try {
77
83
return InternalConnectorRegistry .getInstance ()
78
- .connect (ConnectionConfig .fromConnectionProperties (props ));
84
+ .connect (ConnectionConfig .fromConnectionProperties (props , domainName ));
79
85
} catch (InterruptedException e ) {
80
86
throw new RuntimeException (e );
81
87
}
0 commit comments