Top Description Inners Fields Constructors Methods
com.mysql.cj.conf

public Class ConnectionUrlParser

extends Object
implements DatabaseUrlContainer
Class Inheritance
All Implemented Interfaces
com.mysql.cj.conf.DatabaseUrlContainer
Static Imports
com.mysql.cj.util.StringUtils.isNullOrEmpty, .StringUtils.safeTrim

This class parses a connection string using the general URI structure defined in RFC 3986. Instead of using a URI instance to ensure the correct syntax of the connection string, this implementation uses regular expressions which is faster but also less strict in terms of validations. This actually works better because database URLs don't exactly stick to the RFC 3986 rules.

scheme://authority/path?query#fragment

This results in splitting the connection string URL and processing its internal parts:

scheme
The protocol and subprotocol identification. Usually "jdbc:mysql:" or "mysqlx:".
authority
Contains information about the user credentials and/or the host and port information. Unlike its definition in the RFC 3986 specification, there can be multiple authority sections separated by a single comma (,) in a connection string. It is also possible to use an alternative syntax for the user and/or host identification, that also allows setting per host connection properties, in the form of "[user[:password]@]address=(key1=value)[(key2=value)]...[,address=(key3=value)[(key4=value)]...]...".
path
Corresponds to the database identification.
query
The connection properties, written as "propertyName1[=[propertyValue1]][&propertyName2[=[propertyValue2]]]..."
fragment
The fragment section is ignored in Connector/J connection strings.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ConnectionUrlParser.Pair<
left part type
T
,
right part type
U
>

This class is a simple container for two elements.

Field Summary

Modifier and TypeField and Description
private static final String
private static final Pattern
private String
private final String
private static final Pattern
private static final String
private static final Pattern
private static final Pattern
private static final String
private static final String
private static final String
private static final String
private static final String
private static final Pattern
private List<HostInfo>
private Map<String, String>
private String
private static final Pattern
private String
private String
private static final Pattern
private static final String
private static final String

Constructor Summary

AccessConstructor and Description
private
ConnectionUrlParser(String
the connection string to parse
connString
)

Constructs a connection string parser for the given connection string.

Method Summary

Modifier and TypeMethod and Description
private HostInfo

Returns:

the HostInfo instance containing the parsed information or null if the host part is not empty
buildHostInfoForEmptyHost
(String
the user to include in the final HostInfo
user
,
String
the password to include in the final HostInfo
password
,
String
the string containing the host information part
hostInfo
)

Builds an HostInfo instance for empty host authority segments.

private HostInfo

Returns:

the HostInfo instance containing the parsed information or null if unable to parse the host information
buildHostInfoResortingToAddressEqualsSyntaxParser
(String
the user to include in the resulting HostInfo
user
,
String
the password to include in the resulting HostInfo
password
,
String
the string containing the host information part
hostInfo
)

Parses the host information using the alternate syntax "address=(key1=value1)(key2=value2)...".

private HostInfo

Returns:

the HostInfo instance containing the parsed information or null if unable to parse the host information
buildHostInfoResortingToGenericSyntaxParser
(String
the user to include in the resulting HostInfo
user
,
String
the password to include in the resulting HostInfo
password
,
String
the string containing the host information part
hostInfo
)

Parses the host information using the generic syntax "host:port".

private HostInfo

Returns:

the HostInfo instance containing the parsed information or null if unable to parse the host information
buildHostInfoResortingToKeyValueSyntaxParser
(String
the user to include in the resulting HostInfo
user
,
String
the password to include in the resulting HostInfo
password
,
String
the string containing the host information part
hostInfo
)

Parses the host information using the alternate syntax "(key1=value1, key2=value2, ...)".

private List<HostInfo>

Returns:

a list with all HostInfo instances containing the parsed information or null if unable to parse the host information
buildHostInfoResortingToSubHostsListParser
(String
the user to include in all the resulting HostInfo
user
,
String
the password to include in all the resulting HostInfo
password
,
String
the string containing the host information part
hostInfo
)

Parses the host information using the alternate sub hosts lists syntax "[host1, host2, ...]".

private HostInfo

Returns:

the HostInfo instance containing the parsed information or null if unable to parse the host information
buildHostInfoResortingToUriParser
(String
the user to include in the final HostInfo
user
,
String
the password to include in the final HostInfo
password
,
String
the string containing the host information part
hostInfo
)

Parses the host information resorting to a URI object.

private static String

Returns:

the decoded string
decode
(String
the string to decode
text
)

URL-decode the given string.

private static String

Returns:

the decoded string
decodeSkippingPlusSign
(String
the string to decode
text
)

URL-decode the given string skipping all occurrences of the plus sign.

public String

Returns:

the authority section
getAuthority
()

Returns the authority section.

public String

Returns:

the original database URL
getDatabaseUrl
()

Implements com.mysql.cj.conf.DatabaseUrlContainer.getDatabaseUrl.

Returns the original database URL that produced this connection string parser.

public List<HostInfo>

Returns:

the hosts information
getHosts
()

Returns the hosts information.

public String

Returns:

the path section
getPath
()

Returns the path section.

public Map<String, String>

Returns:

the properties map
getProperties
()

Returns the properties map contained in this connection string.

public String

Returns:

the query section
getQuery
()

Returns the query section.

public String

Returns:

the scheme section
getScheme
()

Returns the scheme section.

public static boolean

Returns:

true if supported
isConnectionStringSupported
(String
connection string
connString
)

Checks if the scheme part of given connection string matches one of the Types supported by Connector/J.

private void
parseAuthoritySection()

Parses the authority section (user and/or host identification) of the connection string URI.

private void
parseAuthoritySegment(String
the string containing the authority segment
authSegment
)

Parses the given sub authority segment, which can take one of the following syntaxes:

  • _user_:_password_@_host_:_port_
  • _user_:_password_@(key1=value1,key2=value2,...)
  • _user_:_password_@address=(key1=value1)(key2=value2)...
  • _user_:_password_@[_any_of_the_above_1_,_any_of_the_above_2_,...]
public static ConnectionUrlParser

Returns:

an instance of ConnectionUrlParser
parseConnectionString
(String
The connection string to parse.
connString
)

Static factory method for constructing instances of this class.

private void
parseConnectionString()

Splits the connection string in its main sections.

public static ConnectionUrlParser.Pair<String, Integer>

Returns:

a Pair containing the host and port information or null if the host information can't be parsed
parseHostPortPair
(String
the host:pair to parse
hostInfo
)

Parses a host:port pair and returns the two elements in a Pair

private void
parseQuerySection()

Parses the connection properties section and stores the extracted key/value pairs into a local map.

public static ConnectionUrlParser.Pair<String, String>

Returns:

a Pair containing the user and password information or null if the user information can't be parsed
parseUserInfo
(String
the string containing the user information
userInfo
)

Parses the given user information which is formed by the parts [user][:password].

private Map<String, String>

Returns:

a key/value map containing the matched values
processKeyValuePattern
(Pattern
the regular expression pattern to match against to
pattern
,
String
the input string
input
)

Takes a two-matching-groups (respectively named "key" and "value") pattern which is successively tested against the given string and produces a key/value map with the matched values.

private ConnectionUrlParser.Pair<String, String>

Returns:

a Pair containing the user information in the left side and the host information in the right
splitByUserInfoAndHostInfo
(String
the string containing the authority segment, i.e., the user and host information parts
authSegment
)

Splits the given authority segment in the user information part and the host part.

public String

Returns:

a string representation of this object
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of this object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

ADDRESS_EQUALS_HOST_INFO_PREFIXback to summary
private static final String ADDRESS_EQUALS_HOST_INFO_PREFIX
ADDRESS_EQUALS_HOST_PTRNback to summary
private static final Pattern ADDRESS_EQUALS_HOST_PTRN
authorityback to summary
private String authority
baseConnectionStringback to summary
private final String baseConnectionString
CONNECTION_STRING_PTRNback to summary
private static final Pattern CONNECTION_STRING_PTRN
DUMMY_SCHEMAback to summary
private static final String DUMMY_SCHEMA
GENERIC_HOST_PTRNback to summary
private static final Pattern GENERIC_HOST_PTRN
HOST_LIST_PTRNback to summary
private static final Pattern HOST_LIST_PTRN
HOSTS_LIST_CLOSING_MARKERSback to summary
private static final String HOSTS_LIST_CLOSING_MARKERS
HOSTS_LIST_OPENING_MARKERSback to summary
private static final String HOSTS_LIST_OPENING_MARKERS
HOSTS_SEPARATORback to summary
private static final String HOSTS_SEPARATOR
KEY_VALUE_HOST_INFO_CLOSING_MARKERback to summary
private static final String KEY_VALUE_HOST_INFO_CLOSING_MARKER
KEY_VALUE_HOST_INFO_OPENING_MARKERback to summary
private static final String KEY_VALUE_HOST_INFO_OPENING_MARKER
KEY_VALUE_HOST_PTRNback to summary
private static final Pattern KEY_VALUE_HOST_PTRN
parsedHostsback to summary
private List<HostInfo> parsedHosts
parsedPropertiesback to summary
private Map<String, String> parsedProperties
pathback to summary
private String path
PROPERTIES_PTRNback to summary
private static final Pattern PROPERTIES_PTRN
queryback to summary
private String query
schemeback to summary
private String scheme
SCHEME_PTRNback to summary
private static final Pattern SCHEME_PTRN
USER_HOST_SEPARATORback to summary
private static final String USER_HOST_SEPARATOR
USER_PASS_SEPARATORback to summary
private static final String USER_PASS_SEPARATOR

Constructor Detail

ConnectionUrlParserback to summary
private ConnectionUrlParser(String connString)

Constructs a connection string parser for the given connection string.

Parameters
connString:String

the connection string to parse

Method Detail

buildHostInfoForEmptyHostback to summary
private HostInfo buildHostInfoForEmptyHost(String user, String password, String hostInfo)

Builds an HostInfo instance for empty host authority segments.

Parameters
user:String

the user to include in the final HostInfo

password:String

the password to include in the final HostInfo

hostInfo:String

the string containing the host information part

Returns:HostInfo

the HostInfo instance containing the parsed information or null if the host part is not empty

buildHostInfoResortingToAddressEqualsSyntaxParserback to summary
private HostInfo buildHostInfoResortingToAddressEqualsSyntaxParser(String user, String password, String hostInfo)

Parses the host information using the alternate syntax "address=(key1=value1)(key2=value2)...".

Parameters
user:String

the user to include in the resulting HostInfo

password:String

the password to include in the resulting HostInfo

hostInfo:String

the string containing the host information part

Returns:HostInfo

the HostInfo instance containing the parsed information or null if unable to parse the host information

buildHostInfoResortingToGenericSyntaxParserback to summary
private HostInfo buildHostInfoResortingToGenericSyntaxParser(String user, String password, String hostInfo)

Parses the host information using the generic syntax "host:port".

Parameters
user:String

the user to include in the resulting HostInfo

password:String

the password to include in the resulting HostInfo

hostInfo:String

the string containing the host information part

Returns:HostInfo

the HostInfo instance containing the parsed information or null if unable to parse the host information

buildHostInfoResortingToKeyValueSyntaxParserback to summary
private HostInfo buildHostInfoResortingToKeyValueSyntaxParser(String user, String password, String hostInfo)

Parses the host information using the alternate syntax "(key1=value1, key2=value2, ...)".

Parameters
user:String

the user to include in the resulting HostInfo

password:String

the password to include in the resulting HostInfo

hostInfo:String

the string containing the host information part

Returns:HostInfo

the HostInfo instance containing the parsed information or null if unable to parse the host information

buildHostInfoResortingToSubHostsListParserback to summary
private List<HostInfo> buildHostInfoResortingToSubHostsListParser(String user, String password, String hostInfo)

Parses the host information using the alternate sub hosts lists syntax "[host1, host2, ...]".

Parameters
user:String

the user to include in all the resulting HostInfo

password:String

the password to include in all the resulting HostInfo

hostInfo:String

the string containing the host information part

Returns:List<HostInfo>

a list with all HostInfo instances containing the parsed information or null if unable to parse the host information

buildHostInfoResortingToUriParserback to summary
private HostInfo buildHostInfoResortingToUriParser(String user, String password, String hostInfo)

Parses the host information resorting to a URI object. This process handles most single-host well formed addresses.

Parameters
user:String

the user to include in the final HostInfo

password:String

the password to include in the final HostInfo

hostInfo:String

the string containing the host information part

Returns:HostInfo

the HostInfo instance containing the parsed information or null if unable to parse the host information

decodeback to summary
private static String decode(String text)

URL-decode the given string.

Parameters
text:String

the string to decode

Returns:String

the decoded string

decodeSkippingPlusSignback to summary
private static String decodeSkippingPlusSign(String text)

URL-decode the given string skipping all occurrences of the plus sign.

Parameters
text:String

the string to decode

Returns:String

the decoded string

getAuthorityback to summary
public String getAuthority()

Returns the authority section.

Returns:String

the authority section

getDatabaseUrlback to summary
public String getDatabaseUrl()

Implements com.mysql.cj.conf.DatabaseUrlContainer.getDatabaseUrl.

Returns the original database URL that produced this connection string parser.

Returns:String

the original database URL

Annotations
@Override
getHostsback to summary
public List<HostInfo> getHosts()

Returns the hosts information.

Returns:List<HostInfo>

the hosts information

getPathback to summary
public String getPath()

Returns the path section.

Returns:String

the path section

getPropertiesback to summary
public Map<String, String> getProperties()

Returns the properties map contained in this connection string.

Returns:Map<String, String>

the properties map

getQueryback to summary
public String getQuery()

Returns the query section.

Returns:String

the query section

getSchemeback to summary
public String getScheme()

Returns the scheme section.

Returns:String

the scheme section

isConnectionStringSupportedback to summary
public static boolean isConnectionStringSupported(String connString)

Checks if the scheme part of given connection string matches one of the Types supported by Connector/J. Throws WrongArgumentException if connString is null.

Parameters
connString:String

connection string

Returns:boolean

true if supported

parseAuthoritySectionback to summary
private void parseAuthoritySection()

Parses the authority section (user and/or host identification) of the connection string URI.

parseAuthoritySegmentback to summary
private void parseAuthoritySegment(String authSegment)

Parses the given sub authority segment, which can take one of the following syntaxes:

  • _user_:_password_@_host_:_port_
  • _user_:_password_@(key1=value1,key2=value2,...)
  • _user_:_password_@address=(key1=value1)(key2=value2)...
  • _user_:_password_@[_any_of_the_above_1_,_any_of_the_above_2_,...]
Most of the above placeholders can be omitted, representing a null, empty, or default value. The placeholder _host_, can be a host name, IPv4 or IPv6. This parser doesn't check IP syntax. IPv6 addresses are enclosed by square brackets ([::1]). The placeholder _any_of_the_above_?_ can be any of the above except for the user information part (_user_:_password_@). When the symbol ":" is not used, it means an null/empty password or a default (HostInfo.NO_PORT) port, respectively. When the symbol "@" is not used, it means that the authority part doesn't contain user information (depending on the scheme type can still be provided via key=value pairs).
Parameters
authSegment:String

the string containing the authority segment

parseConnectionStringback to summary
public static ConnectionUrlParser parseConnectionString(String connString)

Static factory method for constructing instances of this class.

Parameters
connString:String

The connection string to parse.

Returns:ConnectionUrlParser

an instance of ConnectionUrlParser

parseConnectionStringback to summary
private void parseConnectionString()

Splits the connection string in its main sections.

parseHostPortPairback to summary
public static ConnectionUrlParser.Pair<String, Integer> parseHostPortPair(String hostInfo)

Parses a host:port pair and returns the two elements in a Pair

Parameters
hostInfo:String

the host:pair to parse

Returns:ConnectionUrlParser.Pair<String, Integer>

a Pair containing the host and port information or null if the host information can't be parsed

parseQuerySectionback to summary
private void parseQuerySection()

Parses the connection properties section and stores the extracted key/value pairs into a local map.

parseUserInfoback to summary
public static ConnectionUrlParser.Pair<String, String> parseUserInfo(String userInfo)

Parses the given user information which is formed by the parts [user][:password].

Parameters
userInfo:String

the string containing the user information

Returns:ConnectionUrlParser.Pair<String, String>

a Pair containing the user and password information or null if the user information can't be parsed

processKeyValuePatternback to summary
private Map<String, String> processKeyValuePattern(Pattern pattern, String input)

Takes a two-matching-groups (respectively named "key" and "value") pattern which is successively tested against the given string and produces a key/value map with the matched values. The given pattern must ensure that there are no leftovers between successive tests, i.e., the end of the previous match must coincide with the beginning of the next.

Parameters
pattern:Pattern

the regular expression pattern to match against to

input:String

the input string

Returns:Map<String, String>

a key/value map containing the matched values

splitByUserInfoAndHostInfoback to summary
private ConnectionUrlParser.Pair<String, String> splitByUserInfoAndHostInfo(String authSegment)

Splits the given authority segment in the user information part and the host part.

Parameters
authSegment:String

the string containing the authority segment, i.e., the user and host information parts

Returns:ConnectionUrlParser.Pair<String, String>

a Pair containing the user information in the left side and the host information in the right

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of this object.

Returns:String

a string representation of this object

Annotations
@Override
com.mysql.cj.conf back to summary

public Class ConnectionUrlParser.Pair<T, U>

extends Object
Class Inheritance
Type Parameters
<T>
left part type
<U>
right part type

This class is a simple container for two elements.

Field Summary

Modifier and TypeField and Description
public final T
public final U

Constructor Summary

AccessConstructor and Description
public
Pair(T left, U right)

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

leftback to summary
public final T left
rightback to summary
public final U right

Constructor Detail

Pairback to summary
public Pair(T left, U right)

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override