public class Utils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
Utils.Parse |
Modifier and Type | Field and Description |
---|---|
private static char[] |
hexArray |
private static java.util.regex.Pattern |
IP_V4 |
private static java.util.regex.Pattern |
IP_V6 |
private static java.util.regex.Pattern |
IP_V6_COMPRESSED |
private static SocketHandlerFunction |
socketHandler |
Constructor and Description |
---|
Utils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
byteArrayToHexString(byte[] bytes) |
static byte[] |
copyRange(byte[] orig,
int from,
int to)
Copies from original byte array to a new byte array.
|
static byte[] |
copyWithLength(byte[] orig,
int length)
Copies the original byte array content to a new byte array.
|
static java.net.Socket |
createSocket(Options options,
java.lang.String host)
Create socket accordingly to options.
|
static byte[] |
encryptPassword(java.lang.String password,
byte[] seed,
java.lang.String passwordCharacterEncoding)
Encrypts a password.
|
static java.lang.String |
escapeString(java.lang.String value,
boolean noBackslashEscapes)
Escape String.
|
private static java.lang.String |
getHex(byte[] raw) |
private static Protocol |
getProxyLoggingIfNeeded(UrlParser urlParser,
Protocol protocol) |
static java.util.TimeZone |
getTimeZone(java.lang.String id)
Get timezone from Id.
|
static java.lang.String |
hexdump(byte[]... bytes)
Hexdump.
|
static java.lang.String |
hexdump(int maxQuerySizeToLog,
int offset,
int length,
byte[]... byteArr)
Hexdump.
|
static boolean |
isIPv4(java.lang.String ip) |
static boolean |
isIPv6(java.lang.String ip) |
static java.lang.String |
nativeSql(java.lang.String sql,
Protocol protocol)
Escape sql String.
|
static java.lang.String |
parseSessionVariables(java.lang.String sessionVariable)
Parse the option "sessionVariable" to ensure having no injection.
|
private static java.lang.String |
replaceFunctionParameter(java.lang.String functionString,
Protocol protocol)
Helper function to replace function parameters in escaped string.
|
private static java.lang.String |
resolveEscapes(java.lang.String escaped,
Protocol protocol) |
static Protocol |
retrieveProxy(UrlParser urlParser,
GlobalStateInfo globalInfo)
Retrieve protocol corresponding to the failover options.
|
static java.net.Socket |
standardSocket(Options options,
java.lang.String host)
Use standard socket implementation.
|
static int |
transactionFromString(java.lang.String txIsolation)
Traduce a String value of transaction isolation to corresponding java value.
|
static boolean |
validateFileName(java.lang.String sql,
ParameterHolder[] parameters,
java.lang.String fileName)
Validate that file name correspond to send query.
|
private static void |
writeHex(byte[] bytes,
int offset,
int dataLength,
java.lang.StringBuilder outputBuilder)
Write bytes/hexadecimal value of a byte array to a StringBuilder.
|
private static final char[] hexArray
private static final java.util.regex.Pattern IP_V4
private static final java.util.regex.Pattern IP_V6
private static final java.util.regex.Pattern IP_V6_COMPRESSED
private static final SocketHandlerFunction socketHandler
public static java.net.Socket standardSocket(Options options, java.lang.String host) throws java.io.IOException
options
- url optionshost
- host to connectjava.io.IOException
- in case of error establishing socket.public static java.lang.String escapeString(java.lang.String value, boolean noBackslashEscapes)
value
- value to escapenoBackslashEscapes
- must backslash be escapedpublic static byte[] encryptPassword(java.lang.String password, byte[] seed, java.lang.String passwordCharacterEncoding) throws java.security.NoSuchAlgorithmException, java.io.UnsupportedEncodingException
protocol for authentication is like this: 1. Server sends a random array of bytes (the seed) 2. client makes a sha1 digest of the password 3. client hashes the output of 2 4. client digests the seed 5. client updates the digest with the output from 3 6. an xor of the output of 5 and 2 is sent to server 7. server does the same thing and verifies that the scrambled passwords match
password
- the password to encryptseed
- the seed to usepasswordCharacterEncoding
- password character encodingjava.security.NoSuchAlgorithmException
- if SHA1 is not available on the platform we are usingjava.io.UnsupportedEncodingException
- if passwordCharacterEncoding is not a valid charset namepublic static byte[] copyWithLength(byte[] orig, int length)
orig
- the original byte arraylength
- how big the resulting byte array will bepublic static byte[] copyRange(byte[] orig, int from, int to)
orig
- the original byte arrayfrom
- index of first byte in original byte array which will be copiedto
- index of last byte in original byte array which will be copied. This can be outside
of the original byte arrayprivate static java.lang.String replaceFunctionParameter(java.lang.String functionString, Protocol protocol)
caution: this use MariaDB server conversion: 'SELECT CONVERT('2147483648', INTEGER)' will return a BIGINT. MySQL will throw a syntax error.
functionString
- input stringprotocol
- protocolprivate static java.lang.String resolveEscapes(java.lang.String escaped, Protocol protocol) throws java.sql.SQLException
java.sql.SQLException
public static java.lang.String nativeSql(java.lang.String sql, Protocol protocol) throws java.sql.SQLException
sql
- initial sqlprotocol
- protocoljava.sql.SQLException
- if escape sequence is incorrect.public static Protocol retrieveProxy(UrlParser urlParser, GlobalStateInfo globalInfo) throws java.sql.SQLException
urlParser
- urlParser corresponding to connection url string.globalInfo
- global variable informationjava.sql.SQLException
- if any error occur during connectionprivate static Protocol getProxyLoggingIfNeeded(UrlParser urlParser, Protocol protocol)
public static java.util.TimeZone getTimeZone(java.lang.String id) throws java.sql.SQLException
id
- timezone idjava.sql.SQLException
- if no timezone is found for this Idpublic static java.net.Socket createSocket(Options options, java.lang.String host) throws java.io.IOException
options
- Url optionshost
- hostName ( mandatory only for named pipe)java.io.IOException
- if connection error occurpublic static java.lang.String hexdump(byte[]... bytes)
bytes
- byte arrayspublic static java.lang.String hexdump(int maxQuerySizeToLog, int offset, int length, byte[]... byteArr)
String output example :
+--------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------------------------------------------------+------------------+
| 11 00 00 02 00 00 00 02 40 00 00 00 08 01 06 05 | ........@....... |
| 74 65 73 74 6A | testj |
+--------------------------------------------------+------------------+
maxQuerySizeToLog
- max log sizeoffset
- offset of last byte arraylength
- length of last byte arraybyteArr
- byte arrays. if many, only the last may have offset and size limitation others
will be displayed completely.private static void writeHex(byte[] bytes, int offset, int dataLength, java.lang.StringBuilder outputBuilder)
String output example :
+--------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------------------------------------------------+------------------+
| 5F 00 00 00 03 73 65 74 20 61 75 74 6F 63 6F 6D | _....set autocom |
| 6D 69 74 3D 31 2C 20 73 65 73 73 69 6F 6E 5F 74 | mit=1, session_t |
| 72 61 63 6B 5F 73 63 68 65 6D 61 3D 31 2C 20 73 | rack_schema=1, s |
| 71 6C 5F 6D 6F 64 65 20 3D 20 63 6F 6E 63 61 74 | ql_mode = concat |
| 28 40 40 73 71 6C 5F 6D 6F 64 65 2C 27 2C 53 54 | (@@sql_mode,',ST |
| 52 49 43 54 5F 54 52 41 4E 53 5F 54 41 42 4C 45 | RICT_TRANS_TABLE |
| 53 27 29 | S') |
+--------------------------------------------------+------------------+
bytes
- byte arrayoffset
- offsetdataLength
- byte length to writeoutputBuilder
- string builderprivate static java.lang.String getHex(byte[] raw)
public static java.lang.String byteArrayToHexString(byte[] bytes)
public static java.lang.String parseSessionVariables(java.lang.String sessionVariable)
sessionVariable
- option valuepublic static boolean isIPv4(java.lang.String ip)
public static boolean isIPv6(java.lang.String ip)
public static int transactionFromString(java.lang.String txIsolation) throws java.sql.SQLException
txIsolation
- String valuejava.sql.SQLException
- if String value doesn't correspond
to @@tx_isolation/@@transaction_isolation possible valuepublic static boolean validateFileName(java.lang.String sql, ParameterHolder[] parameters, java.lang.String fileName)
sql
- sql commandparameters
- sql parameterfileName
- server file name