Package io.inversion.utils
Class Url
java.lang.Object
io.inversion.utils.Url
Utility class for parsing and working with HTTP(S) URLs.
Not for use with non HTTP(S) urls.
A number of different utility methods are provided to make it simple to find or remove different query string keys.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringThe url host.protected final StringThe url string as supplied to the constructor, with 'http://localhost/' prepended if the constructor url arg did not contain a hostprotected JSNodeA case insensitive map of query string name/value pairs that preserves iteration orderprotected PathThe part of the url after the host/port before the query string.protected intThe url port number if a custom port was providedprotected StringThe url protocol, either http or https -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclearParams(String... tokens) Removes any param that has one oftokensas a whole word case insensitive substring in the key.booleanChecks url equality based on type and toString equalityFinds a key that has any one oftokensas a whole word case insensitive substringfindKeyValue(String... tokens) Finds the value associated withfindKey(tokens)getFile()Gets the last url path part if it existsgetHost()Gets the param value withkeybased on a case insensitive match.getPath()intgetPort()getQuery()Deprecated.Generates a URL encode query string forparamsvoidreplaceParam(String key, String value) Replaces any existing param that haskeyas a whole word case insensitive substring in its key.static StringtoQueryString(Map params) Creates a UTF-8 url encoded query string, not including a leading "?" with key value pairs separated by a '&'toString()Generates a string string representation of this url with any query string parameters URL encoded and port number included only if it differs from the standard protocol port.Adds name/value toparamsoverwriting any preexisting key/value pair on a key case insensitive basis.withParams(String... nvpairs) withParams(Map<String, String> newParams) Adds all key/value pairs fromnewParamstoparamsoverwriting any exiting keys on a case insensitive basiswithPort(int port) withProtocol(String protocol) withQueryString(String queryString) ParsesqueryStringand replaceparams
-
Field Details
-
original
The url string as supplied to the constructor, with 'http://localhost/' prepended if the constructor url arg did not contain a host -
protocol
The url protocol, either http or https -
host
The url host. -
port
protected int portThe url port number if a custom port was provided -
path
The part of the url after the host/port before the query string. -
params
A case insensitive map of query string name/value pairs that preserves iteration orderImplementation Node:
paramsis not actually JSON, JSNode is used as the map implementation here simply because it is an affective case insensitive map that preserves the original key case key iteration order.
-
-
Constructor Details
-
Url
Parsesurlinto its protocol, host, port, path and query string param parts.If
urldoes not start with "http://" or "https://" then "http://localhost/" will be prepended.- Parameters:
url- url string
-
-
Method Details
-
toQueryString
Creates a UTF-8 url encoded query string, not including a leading "?" with key value pairs separated by a '&'- Parameters:
params- the key/value pairs to encode- Returns:
- a UTF-8 url encoded query string
- See Also:
-
toString
Generates a string string representation of this url with any query string parameters URL encoded and port number included only if it differs from the standard protocol port. -
equals
Checks url equality based on type and toString equality -
getDomain
-
getQueryString
Generates a URL encode query string forparams- Returns:
- a URL encoded query string if
params.size() is @gt; 0else empty string - See Also:
-
getQuery
Deprecated.replaced bygetQueryString() -
getHost
-
withHost
-
getPort
public int getPort() -
withPort
-
getProtocol
-
withProtocol
-
getPath
-
withPath
-
getFile
Gets the last url path part if it exists- Returns:
- path.last() if it exists otherwise null
-
withQueryString
ParsesqueryStringand replaceparams- Parameters:
queryString- query prams to add- Returns:
- this
-
withParams
Adds all key/value pairs fromnewParamstoparamsoverwriting any exiting keys on a case insensitive basis- Parameters:
newParams- name/value pairs to add to the query string params map- Returns:
- this
-
withParam
Adds name/value toparamsoverwriting any preexisting key/value pair on a key case insensitive basis.- Parameters:
name- the key to add or overwrite, may not be nullvalue- the value, may be null- Returns:
- this
-
withParams
-
replaceParam
Replaces any existing param that haskeyas a whole word case insensitive substring in its key.If the key/value pair
"eq(dog,Fido)" = nullis in the mapreplaceParam("DOG", "Fifi")would cause it to be removed and the pair "DOG" / "Fifi" would be added.- Parameters:
key- the key to add overwrite, may not be null, also used a a regex whole world case insensitive token to search for other keys to remove.value- the value, may be null- See Also:
-
clearParams
Removes any param that has one oftokensas a whole word case insensitive substring in the key.- Parameters:
tokens- string tokens when found in a param key will cause them to be removed- Returns:
- the first value found that contained any one of
tokens - See Also:
-
findKey
Finds a key that has any one oftokensas a whole word case insensitive substring- Parameters:
tokens- substrings to search params.keySet() for- Returns:
- the first param key that has anyone of
tokensas a whole word case insensitive substring
-
findKeyValue
Finds the value associated withfindKey(tokens)- Parameters:
tokens- substrings to search params.keySet() for- Returns:
- the value for the first param key that has anyone of
tokensas a whole word case insensitive substring - See Also:
-
getParam
Gets the param value withkeybased on a case insensitive match.- Parameters:
key- the key to get- Returns:
- the param value for
keybased on a case insensitive match.
-
getParams
- Returns:
- a new case insensitive order preserving map copy of
params
-
getOriginal
- Returns:
- the url string used in constructing this Url.
-
getQueryString()