public class Url
extends java.lang.Object
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.
| Modifier and Type | Field and Description |
|---|---|
protected java.lang.String |
host
The url host.
|
protected java.lang.String |
original
The url string as supplied to the constructor, with 'http://localhost/' prepended if the constructor url arg did not contain a host
|
protected JSNode |
params
A case insensitive map of query string name/value pairs that preserves iteration order
|
protected Path |
path
The part of the url after the host/port before the query string.
|
protected int |
port
The url port number if a custom port was provided
|
protected java.lang.String |
protocol
The url protocol, either http or https
|
| Constructor and Description |
|---|
Url(java.lang.String url)
Parses
url into its protocol, host, port, path and query string param parts. |
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
clearParams(java.lang.String... tokens)
Removes any param that has one of
tokens as a whole word case insensitive substring in the key. |
boolean |
equals(java.lang.Object url)
Checks url equality based on type and toString equality
|
java.lang.String |
findKey(java.lang.String... tokens)
Finds a key that has any one of
tokens as a whole word case insensitive substring |
java.lang.String |
findKeyValue(java.lang.String... tokens)
Finds the value associated with
findKey(tokens) |
java.lang.String |
getDomain() |
java.lang.String |
getFile()
Gets the last url path part if it exists
|
java.lang.String |
getHost() |
java.lang.String |
getOriginal() |
java.lang.String |
getParam(java.lang.String key)
Gets the param value with
key based on a case insensitive match. |
java.util.Map<java.lang.String,java.lang.String> |
getParams() |
Path |
getPath() |
int |
getPort() |
java.lang.String |
getProtocol() |
java.lang.String |
getQuery()
Deprecated.
replaced by
getQueryString() |
java.lang.String |
getQueryString()
Generates a URL encode query string for
params |
void |
replaceParam(java.lang.String key,
java.lang.String value)
Replaces any existing param that has
key as a whole word case insensitive substring in its key. |
static java.lang.String |
toQueryString(java.util.Map params)
Creates a UTF-8 url encoded query string, not including a leading "?" with key value pairs separated by a '&'
|
java.lang.String |
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.
|
Url |
withHost(java.lang.String host) |
Url |
withParam(java.lang.String name,
java.lang.String value)
Adds name/value to
params overwriting any preexisting key/value pair
on a key case insensitive basis. |
Url |
withParams(java.util.Map<java.lang.String,java.lang.String> newParams)
Adds all key/value pairs from
newParams to params
overwriting any exiting keys on a case insensitive basis |
Url |
withParams(java.lang.String... nvpairs) |
Url |
withPath(Path path) |
Url |
withPort(int port) |
Url |
withProtocol(java.lang.String protocol) |
Url |
withQueryString(java.lang.String queryString)
Parses
queryString and replace params |
protected final java.lang.String original
protected java.lang.String protocol
protected java.lang.String host
protected int port
protected Path path
protected JSNode params
Implementation 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.
public Url(java.lang.String url)
url into its protocol, host, port, path and query string param parts.
If url does not start with "http://" or "https://" then "http://localhost/" will be prepended.
url - url stringpublic static java.lang.String toQueryString(java.util.Map params)
params - the key/value pairs to encodeURLEncoder.encode(String, String)public java.lang.String toString()
toString in class java.lang.Objectpublic boolean equals(java.lang.Object url)
equals in class java.lang.Objecturl is a Url with a matching toStringpublic java.lang.String getDomain()
public java.lang.String getQueryString()
paramsparams.size() is @gt; 0 else empty stringtoQueryString(Map)public java.lang.String getQuery()
getQueryString()public java.lang.String getHost()
public Url withHost(java.lang.String host)
public int getPort()
public Url withPort(int port)
public java.lang.String getProtocol()
public Url withProtocol(java.lang.String protocol)
public Path getPath()
public java.lang.String getFile()
public Url withQueryString(java.lang.String queryString)
queryString and replace paramsqueryString - query prams to addpublic Url withParams(java.util.Map<java.lang.String,java.lang.String> newParams)
newParams to params
overwriting any exiting keys on a case insensitive basisnewParams - name/value pairs to add to the query string params mappublic Url withParam(java.lang.String name, java.lang.String value)
params overwriting any preexisting key/value pair
on a key case insensitive basis.name - the key to add or overwrite, may not be nullvalue - the value, may be nullpublic Url withParams(java.lang.String... nvpairs)
public void replaceParam(java.lang.String key,
java.lang.String value)
key as a whole word case insensitive substring in its key.
If the key/value pair "eq(dog,Fido)" = null is in the map replaceParam("DOG", "Fifi")
would cause it to be removed and the pair "DOG" / "Fifi" would be added.
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 nullUtils.containsToken(java.lang.String, java.lang.String)public java.lang.String clearParams(java.lang.String... tokens)
tokens as a whole word case insensitive substring in the key.tokens - string tokens when found in a param key will cause them to be removedtokensUtils.containsToken(java.lang.String, java.lang.String)public java.lang.String findKey(java.lang.String... tokens)
tokens as a whole word case insensitive substringtokens - substrings to search params.keySet() fortokens as a whole word case insensitive substringpublic java.lang.String findKeyValue(java.lang.String... tokens)
findKey(tokens)tokens - substrings to search params.keySet() fortokens as a whole word case insensitive substringfindKey(String...)public java.lang.String getParam(java.lang.String key)
key based on a case insensitive match.key - the key to getkey based on a case insensitive match.public java.util.Map<java.lang.String,java.lang.String> getParams()
paramspublic java.lang.String getOriginal()
Copyright © 2021 Rocket Partners, LLC. All rights reserved.