public class GenericUrl extends GenericData
URL builder in which the query parameters are specified as generic data key/value pairs, based on the specification RFC 3986: Uniform Resource Identifier (URI).
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.
Static Methods
toPathParts(String encodedPath)
public static List<String> toPathParts(String encodedPath)
Returns the decoded path parts for the given encoded path.
Parameter | |
---|---|
Name | Description |
encodedPath |
String slash-prefixed encoded path, for example |
Returns | |
---|---|
Type | Description |
List<String> |
decoded path parts, with each part assumed to be preceded by a |
toPathParts(String encodedPath, boolean verbatim)
public static List<String> toPathParts(String encodedPath, boolean verbatim)
Returns the path parts (decoded if not verbatim
).
Parameters | |
---|---|
Name | Description |
encodedPath |
String slash-prefixed encoded path, for example |
verbatim |
boolean flag, to specify if URL should be used as is (without encoding, decoding and escaping) |
Returns | |
---|---|
Type | Description |
List<String> |
path parts (decoded if not |
Constructors
GenericUrl()
public GenericUrl()
GenericUrl(String encodedUrl)
public GenericUrl(String encodedUrl)
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 MalformedURLException is wrapped in an IllegalArgumentException.
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.
Parameter | |
---|---|
Name | Description |
encodedUrl |
String encoded URL, including any existing query parameters that should be parsed |
GenericUrl(String encodedUrl, boolean verbatim)
public GenericUrl(String encodedUrl, boolean verbatim)
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 MalformedURLException is wrapped in an IllegalArgumentException.
Parameters | |
---|---|
Name | Description |
encodedUrl |
String encoded URL, including any existing query parameters that should be parsed |
verbatim |
boolean flag, to specify if URL should be used as is (without encoding, decoding and escaping) |
GenericUrl(URI uri)
public GenericUrl(URI uri)
Constructs from a URI.
Parameter | |
---|---|
Name | Description |
uri |
URI URI |
GenericUrl(URI uri, boolean verbatim)
public GenericUrl(URI uri, boolean verbatim)
Constructs from a URI.
Parameters | |
---|---|
Name | Description |
uri |
URI URI |
verbatim |
boolean flag, to specify if URL should be used as is (without encoding, decoding and escaping) |
GenericUrl(URL url)
public GenericUrl(URL url)
Constructs from a URL.
Parameter | |
---|---|
Name | Description |
url |
URL URL |
GenericUrl(URL url, boolean verbatim)
public GenericUrl(URL url, boolean verbatim)
Constructs from a URL.
Parameters | |
---|---|
Name | Description |
url |
URL URL |
verbatim |
boolean flag, to specify if URL should be used as is (without encoding, decoding and escaping) |
Methods
appendRawPath(String encodedPath)
public void appendRawPath(String encodedPath)
Appends the given raw encoded path to the current #pathParts, setting field only if it
is null
or empty.
The last part of the #pathParts is 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"
.
Parameter | |
---|---|
Name | Description |
encodedPath |
String raw encoded path or |
build()
public final String build()
Constructs the string representation of the URL, including the path specified by #pathParts and the query parameters specified by this generic URL.
Returns | |
---|---|
Type | Description |
String |
buildAuthority()
public final String 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 | |
---|---|
Type | Description |
String |
scheme://[user-info@]host[:port] |
buildRelativeUrl()
public final String 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 | |
---|---|
Type | Description |
String |
path with with leading '/' if the path is non-empty, query parameters and fragment |
clone()
public GenericUrl clone()
Makes a "deep" clone of the generic data, in which the clone is completely independent of the original.
Returns | |
---|---|
Type | Description |
GenericUrl |
equals(Object obj)
public boolean equals(Object obj)
Parameter | |
---|---|
Name | Description |
obj |
Object |
Returns | |
---|---|
Type | Description |
boolean |
getAll(String name)
public Collection<Object> getAll(String name)
Returns all query parameter values for the given query parameter name.
Parameter | |
---|---|
Name | Description |
name |
String query parameter name |
Returns | |
---|---|
Type | Description |
Collection<Object> |
unmodifiable collection of query parameter values (possibly empty) |
getFirst(String name)
public Object getFirst(String name)
Returns the first query parameter value for the given query parameter name.
Parameter | |
---|---|
Name | Description |
name |
String query parameter name |
Returns | |
---|---|
Type | Description |
Object |
first query parameter value |
getFragment()
public String getFragment()
Returns the fragment component or null
for none.
Returns | |
---|---|
Type | Description |
String |
getHost()
public String getHost()
Returns the host, for example "www.google.com"
.
Returns | |
---|---|
Type | Description |
String |
getPathParts()
public List<String> getPathParts()
Returns the decoded path component by parts with each part separated by a '/'
or
null
for none.
Returns | |
---|---|
Type | Description |
List<String> |
getPort()
public int getPort()
Returns the port number or -1
if undefined, for example 443
.
Returns | |
---|---|
Type | Description |
int |
getRawPath()
public String getRawPath()
Returns the raw encoded path computed from the #pathParts.
Returns | |
---|---|
Type | Description |
String |
raw encoded path computed from the #pathParts or |
getScheme()
public final String getScheme()
Returns the scheme (lowercase), for example "https"
.
Returns | |
---|---|
Type | Description |
String |
getUserInfo()
public final String getUserInfo()
Returns the user info or null
for none, for example "username:password"
.
Returns | |
---|---|
Type | Description |
String |
hashCode()
public int hashCode()
Returns | |
---|---|
Type | Description |
int |
set(String fieldName, Object value)
public GenericUrl set(String fieldName, Object value)
Sets the given field value (may be null
) for the given field name. Any existing value
for the field will be overwritten. It may be more slightly more efficient than #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.
Parameters | |
---|---|
Name | Description |
fieldName |
String |
value |
Object |
Returns | |
---|---|
Type | Description |
GenericUrl |
setFragment(String fragment)
public final void setFragment(String fragment)
Sets the fragment component or null
for none.
Parameter | |
---|---|
Name | Description |
fragment |
String |
setHost(String host)
public final void setHost(String host)
Sets the host, for example "www.google.com"
.
Parameter | |
---|---|
Name | Description |
host |
String |
setPathParts(List<String> pathParts)
public void setPathParts(List<String> pathParts)
Sets the decoded path component by parts with each part separated by a '/'
or
null
for 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.
Parameter | |
---|---|
Name | Description |
pathParts |
List<String> |
setPort(int port)
public final void setPort(int port)
Sets the port number, for example 443
.
Parameter | |
---|---|
Name | Description |
port |
int |
setRawPath(String encodedPath)
public void setRawPath(String encodedPath)
Sets the #pathParts from the given raw encoded path.
Parameter | |
---|---|
Name | Description |
encodedPath |
String raw encoded path or |
setScheme(String scheme)
public final void setScheme(String scheme)
Sets the scheme (lowercase), for example "https"
.
Parameter | |
---|---|
Name | Description |
scheme |
String |
setUserInfo(String userInfo)
public final void setUserInfo(String userInfo)
Sets the user info or null
for none, for example "username:password"
.
Parameter | |
---|---|
Name | Description |
userInfo |
String |
toString()
public String toString()
Returns | |
---|---|
Type | Description |
String |
toURI()
public final URI toURI()
Constructs the URI based on the string representation of the URL from #build().
Any URISyntaxException is wrapped in an IllegalArgumentException.
Returns | |
---|---|
Type | Description |
URI |
new URI instance |
toURL()
public final URL toURL()
Constructs the URL based on the string representation of the URL from #build().
Any MalformedURLException is wrapped in an IllegalArgumentException.
Returns | |
---|---|
Type | Description |
URL |
new URL instance |
toURL(String relativeUrl)
public final URL toURL(String relativeUrl)
Constructs the URL based on URL#URL(URL, String) with this URL representation from #toURL() and a relative url.
Any MalformedURLException is wrapped in an IllegalArgumentException.
Parameter | |
---|---|
Name | Description |
relativeUrl |
String |
Returns | |
---|---|
Type | Description |
URL |
new URL instance |