Class GenericUrl
The query parameters are specified with the data key name as the parameter name, and the data
value as the parameter value. Subclasses can declare fields for known query parameters using the
Key annotation. null parameter names are not allowed, but null query
values are allowed.
Query parameter values are parsed using UrlEncodedParser.parse(String, Object).
Implementation is not thread-safe.
- Since:
- 1.0
-
Nested Class Summary
Nested classes/interfaces inherited from class GenericData
GenericData.FlagsNested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate StringFragment component ornullfor none.private StringHost, for example"www.google.com".Decoded path component by parts with each part separated by a'/'ornullfor none, for example"/m8/feeds/contacts/default/full"is represented by"", "m8", "feeds", "contacts", "default", "full".private intPort number or-1if undefined, for example443.private StringScheme (lowercase), for example"https".private static final Escaperprivate StringUser info ornullfor none, for example"username:password".private booleanIf true, the URL string originally given is used as is (without encoding, decoding and escaping) whenever referenced; otherwise, part of the URL string may be encoded or decoded as deemed appropriate or necessary. -
Constructor Summary
ConstructorsModifierConstructorDescriptionGenericUrl(String encodedUrl) Constructs a GenericUrl from a URL encoded string.GenericUrl(String encodedUrl, boolean verbatim) Constructs a GenericUrl from a string.privateGenericUrl(String scheme, String host, int port, String path, String fragment, String query, String userInfo, boolean verbatim) GenericUrl(URI uri) Constructs from a URI.GenericUrl(URI uri, boolean verbatim) Constructs from a URI.GenericUrl(URL url) Constructs from a URL.GenericUrl(URL url, boolean verbatim) Constructs from a URL. -
Method Summary
Modifier and TypeMethodDescription(package private) static voidaddQueryParams(Set<Map.Entry<String, Object>> entrySet, StringBuilder buf, boolean verbatim) Adds query parameters from the provided entrySet into the buffer.private static booleanappendParam(boolean first, StringBuilder buf, String name, Object value, boolean verbatim) voidappendRawPath(String encodedPath) Appends the given raw encoded path to the currentpathParts, setting field only if it isnullor empty.private voidfinal Stringbuild()Constructs the string representation of the URL, including the path specified bypathPartsand the query parameters specified by this generic URL.final StringConstructs the portion of the URL containing the scheme, host and port.final StringConstructs the portion of the URL beginning at the rooted path.clone()Makes a "deep" clone of the generic data, in which the clone is completely independent of the original.booleanReturns all query parameter values for the given query parameter name.Returns the first query parameter value for the given query parameter name.Returns the fragment component ornullfor none.getHost()Returns the host, for example"www.google.com".Returns the decoded path component by parts with each part separated by a'/'ornullfor none.intgetPort()Returns the port number or-1if undefined, for example443.Returns the raw encoded path computed from thepathParts.final StringReturns the scheme (lowercase), for example"https".final StringReturns the user info ornullfor none, for example"username:password".inthashCode()private static URLReturns the URI for the given encoded URL.Sets the given field value (may benull) for the given field name.final voidsetFragment(String fragment) Sets the fragment component ornullfor none.final voidSets the host, for example"www.google.com".voidsetPathParts(List<String> pathParts) Sets the decoded path component by parts with each part separated by a'/'ornullfor none.final voidsetPort(int port) Sets the port number, for example443.voidsetRawPath(String encodedPath) Sets thepathPartsfrom the given raw encoded path.final voidSets the scheme (lowercase), for example"https".final voidsetUserInfo(String userInfo) Sets the user info ornullfor none, for example"username:password".toPathParts(String encodedPath) Returns the decoded path parts for the given encoded path.toPathParts(String encodedPath, boolean verbatim) Returns the path parts (decoded if notverbatim).toString()final URItoURI()Constructs the URI based on the string representation of the URL frombuild().private static URIReturns the URI for the given encoded URL.final URLtoURL()Constructs the URL based on the string representation of the URL frombuild().final URLConstructs the URL based onURL(URL, String)with this URL representation fromtoURL()and a relative url.Methods inherited from class GenericData
entrySet, get, getClassInfo, getUnknownKeys, put, putAll, remove, setUnknownKeysMethods inherited from class AbstractMap
clear, containsKey, containsValue, isEmpty, keySet, size, valuesMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
URI_FRAGMENT_ESCAPER
-
scheme
Scheme (lowercase), for example"https". -
host
Host, for example"www.google.com". -
userInfo
User info ornullfor none, for example"username:password". -
port
private int portPort number or-1if undefined, for example443. -
pathParts
Decoded path component by parts with each part separated by a'/'ornullfor none, for example"/m8/feeds/contacts/default/full"is represented by"", "m8", "feeds", "contacts", "default", "full".Use
appendRawPath(String)to append to the path, which ensures that no extra slash is added. -
fragment
Fragment component ornullfor none. -
verbatim
private boolean verbatimIf true, the URL string originally given is used as is (without encoding, decoding and escaping) whenever referenced; otherwise, part of the URL string may be encoded or decoded as deemed appropriate or necessary.
-
-
Constructor Details
-
GenericUrl
public GenericUrl() -
GenericUrl
Constructs a GenericUrl from a URL encoded string.Any known query parameters with pre-defined fields as data keys will be parsed based on their data type. Any unrecognized query parameter will always be parsed as a string.
Any
MalformedURLExceptionis wrapped in anIllegalArgumentException.Upgrade warning: starting in version 1.18 this parses the encodedUrl using new URL(encodedUrl). In previous versions it used new URI(encodedUrl). In particular, this means that only a limited set of schemes are allowed such as "http" and "https", but that parsing is compliant with, at least, RFC 3986.
- Parameters:
encodedUrl- encoded URL, including any existing query parameters that should be parsed- Throws:
IllegalArgumentException- if the URL has a syntax error
-
GenericUrl
Constructs a GenericUrl from a string.Any known query parameters with pre-defined fields as data keys will be parsed based on their data type. Any unrecognized query parameter will always be parsed as a string.
Any
MalformedURLExceptionis wrapped in anIllegalArgumentException.- Parameters:
encodedUrl- encoded URL, including any existing query parameters that should be parsedverbatim- flag, to specify if URL should be used as is (without encoding, decoding and escaping)- Throws:
IllegalArgumentException- if URL has a syntax error
-
GenericUrl
-
GenericUrl
Constructs from a URI.- Parameters:
uri- URIverbatim- flag, to specify if URL should be used as is (without encoding, decoding and escaping)
-
GenericUrl
-
GenericUrl
Constructs from a URL.- Parameters:
url- URLverbatim- flag, to specify if URL should be used as is (without encoding, decoding and escaping)- Since:
- 1.14
-
GenericUrl
-
-
Method Details
-
hashCode
-
equals
-
toString
- Overrides:
toStringin classGenericData
-
clone
Description copied from class:GenericDataMakes a "deep" clone of the generic data, in which the clone is completely independent of the original.- Overrides:
clonein classGenericData
-
set
Description copied from class:GenericDataSets the given field value (may benull) for the given field name. Any existing value for the field will be overwritten. It may be more slightly more efficient thanGenericData.put(String, Object)because it avoids accessing the field's original value.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
- Overrides:
setin classGenericData
-
getScheme
-
setScheme
Sets the scheme (lowercase), for example"https".- Since:
- 1.5
-
getHost
-
setHost
-
getUserInfo
Returns the user info ornullfor none, for example"username:password".- Since:
- 1.15
-
setUserInfo
Sets the user info ornullfor none, for example"username:password".- Since:
- 1.15
-
getPort
public int getPort()Returns the port number or-1if undefined, for example443.- Since:
- 1.5
-
setPort
public final void setPort(int port) Sets the port number, for example443.- Since:
- 1.5
-
getPathParts
-
setPathParts
Sets the decoded path component by parts with each part separated by a'/'ornullfor none.For example
"/m8/feeds/contacts/default/full"is represented by"", "m8", "feeds", "contacts", "default", "full".Use
appendRawPath(String)to append to the path, which ensures that no extra slash is added.- Since:
- 1.5
-
getFragment
-
setFragment
Sets the fragment component ornullfor none.- Since:
- 1.5
-
build
-
buildAuthority
Constructs the portion of the URL containing the scheme, host and port.For the URL
"http://example.com/something?action=add"this method would return"http://example.com".- Returns:
- scheme://[user-info@]host[:port]
- Since:
- 1.9
-
buildRelativeUrl
Constructs the portion of the URL beginning at the rooted path.For the URL
"http://example.com/something?action=add"this method would return"/something?action=add".- Returns:
- path with with leading '/' if the path is non-empty, query parameters and fragment
- Since:
- 1.9
-
toURI
Constructs the URI based on the string representation of the URL frombuild().Any
URISyntaxExceptionis wrapped in anIllegalArgumentException.- Returns:
- new URI instance
- Since:
- 1.14
-
toURL
Constructs the URL based on the string representation of the URL frombuild().Any
MalformedURLExceptionis wrapped in anIllegalArgumentException.- Returns:
- new URL instance
- Since:
- 1.14
-
toURL
Constructs the URL based onURL(URL, String)with this URL representation fromtoURL()and a relative url.Any
MalformedURLExceptionis wrapped in anIllegalArgumentException.- Returns:
- new URL instance
- Since:
- 1.14
-
getFirst
-
getAll
Returns all query parameter values for the given query parameter name.- Parameters:
name- query parameter name- Returns:
- unmodifiable collection of query parameter values (possibly empty)
-
getRawPath
-
setRawPath
-
appendRawPath
Appends the given raw encoded path to the currentpathParts, setting field only if it isnullor empty.The last part of the
pathPartsis merged with the first part of the path parts computed from the given encoded path. Thus, if the current raw encoded path is"a", and the given encoded path is"b", then the resulting raw encoded path is"ab".- Parameters:
encodedPath- raw encoded path ornullto ignore
-
toPathParts
Returns the decoded path parts for the given encoded path.- Parameters:
encodedPath- slash-prefixed encoded path, for example"/m8/feeds/contacts/default/full"- Returns:
- decoded path parts, with each part assumed to be preceded by a
'/', for example"", "m8", "feeds", "contacts", "default", "full", ornullfornullor""input
-
toPathParts
Returns the path parts (decoded if notverbatim).- Parameters:
encodedPath- slash-prefixed encoded path, for example"/m8/feeds/contacts/default/full"verbatim- flag, to specify if URL should be used as is (without encoding, decoding and escaping)- Returns:
- path parts (decoded if not
verbatim), with each part assumed to be preceded by a'/', for example"", "m8", "feeds", "contacts", "default", "full", ornullfornullor""input
-
appendRawPathFromParts
-
addQueryParams
static void addQueryParams(Set<Map.Entry<String, Object>> entrySet, StringBuilder buf, boolean verbatim) Adds query parameters from the provided entrySet into the buffer. -
appendParam
private static boolean appendParam(boolean first, StringBuilder buf, String name, Object value, boolean verbatim) -
toURI
Returns the URI for the given encoded URL.Any
URISyntaxExceptionis wrapped in anIllegalArgumentException.- Parameters:
encodedUrl- encoded URL- Returns:
- URI
-
parseURL
Returns the URI for the given encoded URL.Any
MalformedURLExceptionis wrapped in anIllegalArgumentException.- Parameters:
encodedUrl- encoded URL- Returns:
- URL
-