|
1 | 1 | package com.taosdata.jdbc.utils; |
2 | 2 |
|
3 | 3 | import com.taosdata.jdbc.TSDBDriver; |
| 4 | +import com.taosdata.jdbc.TSDBError; |
| 5 | +import com.taosdata.jdbc.TSDBErrorNumbers; |
4 | 6 |
|
| 7 | +import java.sql.SQLException; |
5 | 8 | import java.util.Properties; |
6 | 9 | import java.util.StringTokenizer; |
7 | 10 |
|
@@ -31,62 +34,109 @@ public static boolean isNumeric(String str) { |
31 | 34 | return true; |
32 | 35 | } |
33 | 36 |
|
34 | | - public static Properties parseUrl(String url, Properties defaults) { |
| 37 | + public static Properties parseHostPort(String hostPortDb, boolean isNative) throws SQLException { |
| 38 | + Properties properties = new Properties(); |
| 39 | + |
| 40 | + // parse host and port |
| 41 | + String host = hostPortDb; |
| 42 | + String port = null; |
| 43 | + |
| 44 | + // ipv6 address |
| 45 | + if (hostPortDb.startsWith("[")) { |
| 46 | + int endBracket = hostPortDb.indexOf(']'); |
| 47 | + if (endBracket != -1) { |
| 48 | + // extract host |
| 49 | + if (isNative){ |
| 50 | + host = hostPortDb.substring(1, endBracket); |
| 51 | + } else { |
| 52 | + host = hostPortDb.substring(0, endBracket + 1); |
| 53 | + } |
| 54 | + |
| 55 | + // if have port |
| 56 | + if (endBracket + 1 < hostPortDb.length() && |
| 57 | + hostPortDb.charAt(endBracket + 1) == ':') { |
| 58 | + port = hostPortDb.substring(endBracket + 2); |
| 59 | + } |
| 60 | + } else { |
| 61 | + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "Invalid IPv6 address: " + hostPortDb); |
| 62 | + } |
| 63 | + } |
| 64 | + // ipv4 address or hostname |
| 65 | + else { |
| 66 | + // find last ':' |
| 67 | + int lastColon = hostPortDb.lastIndexOf(':'); |
| 68 | + |
| 69 | + if (lastColon != -1) { |
| 70 | + host = hostPortDb.substring(0, lastColon); |
| 71 | + port = hostPortDb.substring(lastColon + 1); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // handle Scope Identifier like fe80::1%eth0 |
| 76 | + if (host.contains("%")) { |
| 77 | + host = host.replace("%", "%25"); |
| 78 | + } |
| 79 | + |
| 80 | + // set property |
| 81 | + if (!host.trim().isEmpty()) { |
| 82 | + properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host); |
| 83 | + } |
| 84 | + if (port != null && !port.trim().isEmpty()) { |
| 85 | + properties.setProperty(TSDBDriver.PROPERTY_KEY_PORT, port); |
| 86 | + } |
| 87 | + |
| 88 | + return properties; |
| 89 | + } |
| 90 | + |
| 91 | + public static Properties parseUrl(String url, Properties defaults, boolean isNative) throws SQLException { |
35 | 92 | Properties urlProps = (defaults != null) ? defaults : new Properties(); |
36 | 93 | if (StringUtils.isEmpty(url)) { |
37 | 94 | return urlProps; |
38 | 95 | } |
39 | 96 |
|
40 | | - // parse properties in url |
41 | | - int beginningOfSlashes = url.indexOf("//"); |
42 | | - int index = url.indexOf("?"); |
43 | | - if (index != -1) { |
44 | | - String paramString = url.substring(index + 1); |
45 | | - url = url.substring(0, index); |
| 97 | + // parse parameters |
| 98 | + int paramStart = url.indexOf("?"); |
| 99 | + if (paramStart != -1) { |
| 100 | + String paramString = url.substring(paramStart + 1); |
| 101 | + url = url.substring(0, paramStart); |
46 | 102 | StringTokenizer queryParams = new StringTokenizer(paramString, "&"); |
47 | 103 | while (queryParams.hasMoreElements()) { |
48 | | - String parameterValuePair = queryParams.nextToken(); |
49 | | - int indexOfEqual = parameterValuePair.indexOf("="); |
50 | | - String parameter = null; |
51 | | - String value = null; |
52 | | - if (indexOfEqual != -1) { |
53 | | - parameter = parameterValuePair.substring(0, indexOfEqual); |
54 | | - if (indexOfEqual + 1 < parameterValuePair.length()) { |
55 | | - value = parameterValuePair.substring(indexOfEqual + 1); |
| 104 | + String pair = queryParams.nextToken(); |
| 105 | + int eqIdx = pair.indexOf("="); |
| 106 | + if (eqIdx != -1) { |
| 107 | + String key = pair.substring(0, eqIdx); |
| 108 | + String value = eqIdx + 1 < pair.length() ? pair.substring(eqIdx + 1) : ""; |
| 109 | + if (!key.isEmpty() && !value.isEmpty()) { |
| 110 | + urlProps.setProperty(key, value); |
56 | 111 | } |
57 | 112 | } |
58 | | - if (value != null && value.length() > 0 && parameter.length() > 0) { |
59 | | - urlProps.setProperty(parameter, value); |
60 | | - } |
61 | 113 | } |
62 | 114 | } |
63 | 115 |
|
64 | | - // parse Product Name |
65 | | - String dbProductName = url.substring(0, beginningOfSlashes); |
| 116 | + // parse product name |
| 117 | + int slashesStart = url.indexOf("//"); |
| 118 | + String dbProductName = url.substring(0, slashesStart); |
66 | 119 | dbProductName = dbProductName.substring(dbProductName.indexOf(":") + 1); |
67 | 120 | dbProductName = dbProductName.substring(0, dbProductName.indexOf(":")); |
68 | 121 | urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PRODUCT_NAME, dbProductName); |
| 122 | + |
| 123 | + // extract host,port and dbname |
| 124 | + String hostPortDb = url.substring(slashesStart + 2); |
| 125 | + |
69 | 126 | // parse dbname |
70 | | - url = url.substring(beginningOfSlashes + 2); |
71 | | - int indexOfSlash = url.indexOf("/"); |
72 | | - if (indexOfSlash != -1) { |
73 | | - if (indexOfSlash + 1 < url.length()) { |
74 | | - urlProps.setProperty(TSDBDriver.PROPERTY_KEY_DBNAME, url.substring(indexOfSlash + 1).toLowerCase()); |
| 127 | + int dbStart = hostPortDb.indexOf("/"); |
| 128 | + if (dbStart != -1) { |
| 129 | + if (dbStart + 1 < hostPortDb.length()) { |
| 130 | + urlProps.setProperty(TSDBDriver.PROPERTY_KEY_DBNAME, |
| 131 | + hostPortDb.substring(dbStart + 1).toLowerCase()); |
75 | 132 | } |
76 | | - url = url.substring(0, indexOfSlash); |
77 | | - } |
78 | | - // parse port |
79 | | - int indexOfColon = url.indexOf(":"); |
80 | | - if (indexOfColon != -1) { |
81 | | - if (indexOfColon + 1 < url.length()) { |
82 | | - urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PORT, url.substring(indexOfColon + 1)); |
83 | | - } |
84 | | - url = url.substring(0, indexOfColon); |
85 | | - } |
86 | | - // parse host |
87 | | - if (url.length() > 0 && url.trim().length() > 0) { |
88 | | - urlProps.setProperty(TSDBDriver.PROPERTY_KEY_HOST, url); |
| 133 | + hostPortDb = hostPortDb.substring(0, dbStart); |
89 | 134 | } |
| 135 | + |
| 136 | + // parse host and port |
| 137 | + Properties hostPortProps = parseHostPort(hostPortDb, isNative); |
| 138 | + urlProps.putAll(hostPortProps); |
| 139 | + |
90 | 140 | return urlProps; |
91 | 141 | } |
92 | 142 |
|
|
0 commit comments