public class GenericUrl extends GenericData
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.
GenericData.Flags
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
GenericUrl() |
GenericUrl(String encodedUrl)
Constructs from an encoded URL.
|
GenericUrl(String encodedUrl,
boolean verbatim)
Constructs from an encoded URL.
|
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.
|
Modifier and Type | Method and Description |
---|---|
void |
appendRawPath(String encodedPath)
Appends the given raw encoded path to the current
pathParts , setting field only if it
is null or empty. |
String |
build()
Constructs the string representation of the URL, including the path specified by
pathParts and the query parameters specified by this generic URL. |
String |
buildAuthority()
Constructs the portion of the URL containing the scheme, host and port.
|
String |
buildRelativeUrl()
Constructs the portion of the URL beginning at the rooted path.
|
GenericUrl |
clone()
Makes a "deep" clone of the generic data, in which the clone is completely independent of the
original.
|
boolean |
equals(Object obj) |
Collection<Object> |
getAll(String name)
Returns all query parameter values for the given query parameter name.
|
Object |
getFirst(String name)
Returns the first query parameter value for the given query parameter name.
|
String |
getFragment()
Returns the fragment component or
null for none. |
String |
getHost()
Returns the host, for example
"www.google.com" . |
List<String> |
getPathParts()
Returns the decoded path component by parts with each part separated by a
'/' or null for none. |
int |
getPort()
Returns the port number or
-1 if undefined, for example 443 . |
String |
getRawPath()
Returns the raw encoded path computed from the
pathParts . |
String |
getScheme()
Returns the scheme (lowercase), for example
"https" . |
String |
getUserInfo()
Returns the user info or
null for none, for example "username:password" . |
int |
hashCode() |
GenericUrl |
set(String fieldName,
Object value)
Sets the given field value (may be
null ) for the given field name. |
void |
setFragment(String fragment)
Sets the fragment component or
null for none. |
void |
setHost(String host)
Sets the host, for example
"www.google.com" . |
void |
setPathParts(List<String> pathParts)
Sets the decoded path component by parts with each part separated by a
'/' or null for none. |
void |
setPort(int port)
Sets the port number, for example
443 . |
void |
setRawPath(String encodedPath)
Sets the
pathParts from the given raw encoded path. |
void |
setScheme(String scheme)
Sets the scheme (lowercase), for example
"https" . |
void |
setUserInfo(String userInfo)
Sets the user info or
null for none, for example "username:password" . |
static List<String> |
toPathParts(String encodedPath)
Returns the decoded path parts for the given encoded path.
|
static List<String> |
toPathParts(String encodedPath,
boolean verbatim)
Returns the path parts (decoded if not
verbatim ). |
String |
toString() |
URI |
toURI()
Constructs the URI based on the string representation of the URL from
build() . |
URL |
toURL()
Constructs the URL based on the string representation of the URL from
build() . |
URL |
toURL(String relativeUrl)
Constructs the URL based on
URL.URL(URL, String) with this URL representation from
toURL() and a relative url. |
entrySet, get, getClassInfo, getUnknownKeys, put, putAll, remove, setUnknownKeys
clear, containsKey, containsValue, isEmpty, keySet, size, values
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public GenericUrl()
public GenericUrl(String encodedUrl)
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.
encodedUrl
- encoded URL, including any existing query parameters that should be parsedIllegalArgumentException
- if URL has a syntax errorpublic GenericUrl(String encodedUrl, boolean verbatim)
Any known query parameters with pre-defined fields as data keys are parsed based on their data type. Any unrecognized query parameter are always parsed as a string.
Any MalformedURLException
is wrapped in an IllegalArgumentException
.
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)IllegalArgumentException
- if URL has a syntax errorpublic GenericUrl(URI uri)
uri
- URIpublic GenericUrl(URI uri, boolean verbatim)
uri
- URIverbatim
- flag, to specify if URL should be used as is (without encoding, decoding and
escaping)public GenericUrl(URL url)
url
- URLpublic GenericUrl(URL url, boolean verbatim)
url
- URLverbatim
- flag, to specify if URL should be used as is (without encoding, decoding and
escaping)public int hashCode()
public boolean equals(Object obj)
public String toString()
toString
in class GenericData
public GenericUrl clone()
GenericData
clone
in class GenericData
public GenericUrl set(String fieldName, Object value)
GenericData
null
) for the given field name. Any existing value
for the field will be overwritten. It may be more slightly more efficient than GenericData.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.
set
in class GenericData
public final String getScheme()
"https"
.public final void setScheme(String scheme)
"https"
.public String getHost()
"www.google.com"
.public final void setHost(String host)
"www.google.com"
.public final String getUserInfo()
null
for none, for example "username:password"
.public final void setUserInfo(String userInfo)
null
for none, for example "username:password"
.public int getPort()
-1
if undefined, for example 443
.public final void setPort(int port)
443
.public List<String> getPathParts()
'/'
or null
for none.public void setPathParts(List<String> pathParts)
'/'
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.
public String getFragment()
null
for none.public final void setFragment(String fragment)
null
for none.public final String build()
pathParts
and the query parameters specified by this generic URL.public final String buildAuthority()
For the URL "http://example.com/something?action=add"
this method would return
"http://example.com"
.
public final String buildRelativeUrl()
For the URL "http://example.com/something?action=add"
this method would return
"/something?action=add"
.
public final URI toURI()
build()
.
Any URISyntaxException
is wrapped in an IllegalArgumentException
.
public final URL toURL()
build()
.
Any MalformedURLException
is wrapped in an IllegalArgumentException
.
public final URL toURL(String relativeUrl)
URL.URL(URL, String)
with this URL representation from
toURL()
and a relative url.
Any MalformedURLException
is wrapped in an IllegalArgumentException
.
public Object getFirst(String name)
name
- query parameter namepublic Collection<Object> getAll(String name)
name
- query parameter namepublic void setRawPath(String encodedPath)
pathParts
from the given raw encoded path.encodedPath
- raw encoded path or null
to set pathParts
to null
public void appendRawPath(String encodedPath)
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"
.
encodedPath
- raw encoded path or null
to ignorepublic static List<String> toPathParts(String encodedPath)
encodedPath
- slash-prefixed encoded path, for example "/m8/feeds/contacts/default/full"
'/'
, for example
"", "m8", "feeds", "contacts", "default", "full"
, or null
for null
or ""
inputpublic static List<String> toPathParts(String encodedPath, boolean verbatim)
verbatim
).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)verbatim
), with each part assumed to be preceded by
a '/'
, for example "", "m8", "feeds", "contacts", "default", "full"
, or
null
for null
or ""
inputCopyright © 2011–2019 Google. All rights reserved.