Class GenericUrl
- java.lang.Object
-
- java.util.AbstractMap<String,Object>
-
- com.google.api.client.util.GenericData
-
- com.google.api.client.http.GenericUrl
-
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, butnull
query values are allowed.Query parameter values are parsed using
UrlEncodedParser.parse(String, Object)
.Implementation is not thread-safe.
- Since:
- 1.0
- Author:
- Yaniv Inbar
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.api.client.util.GenericData
GenericData.Flags
-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor Description GenericUrl()
GenericUrl(String encodedUrl)
Constructs from an encoded URL.GenericUrl(URI uri)
Constructs from a URI.GenericUrl(URL url)
Constructs from a URL.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
appendRawPath(String encodedPath)
Appends the given raw encoded path to the currentpathParts
, setting field only if it isnull
or empty.String
build()
Constructs the string representation of the URL, including the path specified bypathParts
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 ornull
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'/'
ornull
for none.int
getPort()
Returns the port number or-1
if undefined, for example443
.String
getRawPath()
Returns the raw encoded path computed from thepathParts
.String
getScheme()
Returns the scheme (lowercase), for example"https"
.String
getUserInfo()
Returns the user info ornull
for none, for example"username:password"
.int
hashCode()
GenericUrl
set(String fieldName, Object value)
Sets the given field value (may benull
) for the given field name.void
setFragment(String fragment)
Sets the fragment component ornull
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'/'
ornull
for none.void
setPort(int port)
Sets the port number, for example443
.void
setRawPath(String encodedPath)
Sets thepathParts
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 ornull
for none, for example"username:password"
.static List<String>
toPathParts(String encodedPath)
Returns the decoded path parts for the given encoded path.String
toString()
URI
toURI()
Constructs the URI based on the string representation of the URL frombuild()
.URL
toURL()
Constructs the URL based on the string representation of the URL frombuild()
.URL
toURL(String relativeUrl)
Constructs the URL based onURL(URL, String)
with this URL representation fromtoURL()
and a relative url.-
Methods inherited from class com.google.api.client.util.GenericData
entrySet, get, getClassInfo, getUnknownKeys, put, putAll, remove, setUnknownKeys
-
Methods inherited from class java.util.AbstractMap
clear, containsKey, containsValue, isEmpty, keySet, size, values
-
-
-
-
Constructor Detail
-
GenericUrl
public GenericUrl()
-
GenericUrl
public GenericUrl(String encodedUrl)
Constructs from an encoded URL.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 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 URL has a syntax error
-
GenericUrl
public GenericUrl(URI uri)
Constructs from a URI.- Parameters:
uri
- URI- Since:
- 1.14
-
GenericUrl
public GenericUrl(URL url)
Constructs from a URL.- Parameters:
url
- URL- Since:
- 1.14
-
-
Method Detail
-
hashCode
public int hashCode()
-
equals
public boolean equals(Object obj)
-
toString
public String toString()
- Overrides:
toString
in classAbstractMap<String,Object>
-
clone
public GenericUrl clone()
Description copied from class:GenericData
Makes a "deep" clone of the generic data, in which the clone is completely independent of the original.- Overrides:
clone
in classGenericData
-
set
public GenericUrl set(String fieldName, Object value)
Description copied from class:GenericData
Sets 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:
set
in classGenericData
-
getScheme
public final String getScheme()
Returns the scheme (lowercase), for example"https"
.- Since:
- 1.5
-
setScheme
public final void setScheme(String scheme)
Sets the scheme (lowercase), for example"https"
.- Since:
- 1.5
-
getHost
public String getHost()
Returns the host, for example"www.google.com"
.- Since:
- 1.5
-
setHost
public final void setHost(String host)
Sets the host, for example"www.google.com"
.- Since:
- 1.5
-
getUserInfo
public final String getUserInfo()
Returns the user info ornull
for none, for example"username:password"
.- Since:
- 1.15
-
setUserInfo
public final void setUserInfo(String userInfo)
Sets the user info ornull
for none, for example"username:password"
.- Since:
- 1.15
-
getPort
public int getPort()
Returns the port number or-1
if undefined, for example443
.- Since:
- 1.5
-
setPort
public final void setPort(int port)
Sets the port number, for example443
.- Since:
- 1.5
-
getPathParts
public List<String> getPathParts()
Returns the decoded path component by parts with each part separated by a'/'
ornull
for none.- Since:
- 1.5
-
setPathParts
public void setPathParts(List<String> pathParts)
Sets the decoded path component by parts with each part separated by a'/'
ornull
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.- Since:
- 1.5
-
getFragment
public String getFragment()
Returns the fragment component ornull
for none.- Since:
- 1.5
-
setFragment
public final void setFragment(String fragment)
Sets the fragment component ornull
for none.- Since:
- 1.5
-
build
public final String build()
Constructs the string representation of the URL, including the path specified bypathParts
and the query parameters specified by this generic URL.
-
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:
- scheme://[user-info@]host[:port]
- Since:
- 1.9
-
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:
- path with with leading '/' if the path is non-empty, query parameters and fragment
- Since:
- 1.9
-
toURI
public final URI toURI()
Constructs the URI based on the string representation of the URL frombuild()
.Any
URISyntaxException
is wrapped in anIllegalArgumentException
.- Returns:
- new URI instance
- Since:
- 1.14
-
toURL
public final URL toURL()
Constructs the URL based on the string representation of the URL frombuild()
.Any
MalformedURLException
is wrapped in anIllegalArgumentException
.- Returns:
- new URL instance
- Since:
- 1.14
-
toURL
public final URL toURL(String relativeUrl)
Constructs the URL based onURL(URL, String)
with this URL representation fromtoURL()
and a relative url.Any
MalformedURLException
is wrapped in anIllegalArgumentException
.- Returns:
- new URL instance
- Since:
- 1.14
-
getFirst
public Object getFirst(String name)
Returns the first query parameter value for the given query parameter name.- Parameters:
name
- query parameter name- Returns:
- first query parameter value
-
getAll
public Collection<Object> getAll(String name)
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)
-
setRawPath
public void setRawPath(String encodedPath)
Sets thepathParts
from the given raw encoded path.- Parameters:
encodedPath
- raw encoded path ornull
to setpathParts
tonull
-
appendRawPath
public void appendRawPath(String encodedPath)
Appends the given raw encoded path to the currentpathParts
, setting field only if it isnull
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"
.- Parameters:
encodedPath
- raw encoded path ornull
to ignore
-
toPathParts
public static List<String> toPathParts(String encodedPath)
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"
, ornull
fornull
or""
input
-
-