Class GenericUrl
- java.lang.Object
-
- java.util.AbstractMap<java.lang.String,java.lang.Object>
-
- com.google.api.client.util.GenericData
-
- com.google.api.client.http.GenericUrl
-
- All Implemented Interfaces:
java.lang.Cloneable,java.util.Map<java.lang.String,java.lang.Object>
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
Keyannotation.nullparameter names are not allowed, butnullquery 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 com.google.api.client.util.GenericData
GenericData.Flags
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringfragmentFragment component ornullfor none.private java.lang.StringhostHost, for example"www.google.com".private java.util.List<java.lang.String>pathPartsDecoded 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 intportPort number or-1if undefined, for example443.private java.lang.StringschemeScheme (lowercase), for example"https".private static EscaperURI_FRAGMENT_ESCAPERprivate java.lang.StringuserInfoUser info ornullfor none, for example"username:password".private booleanverbatimIf 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
Constructors Modifier Constructor Description GenericUrl()GenericUrl(java.lang.String encodedUrl)Constructs a GenericUrl from a URL encoded string.GenericUrl(java.lang.String encodedUrl, boolean verbatim)Constructs a GenericUrl from a string.privateGenericUrl(java.lang.String scheme, java.lang.String host, int port, java.lang.String path, java.lang.String fragment, java.lang.String query, java.lang.String userInfo, boolean verbatim)GenericUrl(java.net.URI uri)Constructs from a URI.GenericUrl(java.net.URI uri, boolean verbatim)Constructs from a URI.GenericUrl(java.net.URL url)Constructs from a URL.GenericUrl(java.net.URL url, boolean verbatim)Constructs from a URL.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static voidaddQueryParams(java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet, java.lang.StringBuilder buf, boolean verbatim)Adds query parameters from the provided entrySet into the buffer.private static booleanappendParam(boolean first, java.lang.StringBuilder buf, java.lang.String name, java.lang.Object value, boolean verbatim)voidappendRawPath(java.lang.String encodedPath)Appends the given raw encoded path to the currentpathParts, setting field only if it isnullor empty.private voidappendRawPathFromParts(java.lang.StringBuilder buf)java.lang.Stringbuild()Constructs the string representation of the URL, including the path specified bypathPartsand the query parameters specified by this generic URL.java.lang.StringbuildAuthority()Constructs the portion of the URL containing the scheme, host and port.java.lang.StringbuildRelativeUrl()Constructs the portion of the URL beginning at the rooted path.GenericUrlclone()Makes a "deep" clone of the generic data, in which the clone is completely independent of the original.booleanequals(java.lang.Object obj)java.util.Collection<java.lang.Object>getAll(java.lang.String name)Returns all query parameter values for the given query parameter name.java.lang.ObjectgetFirst(java.lang.String name)Returns the first query parameter value for the given query parameter name.java.lang.StringgetFragment()Returns the fragment component ornullfor none.java.lang.StringgetHost()Returns the host, for example"www.google.com".java.util.List<java.lang.String>getPathParts()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.java.lang.StringgetRawPath()Returns the raw encoded path computed from thepathParts.java.lang.StringgetScheme()Returns the scheme (lowercase), for example"https".java.lang.StringgetUserInfo()Returns the user info ornullfor none, for example"username:password".inthashCode()private static java.net.URLparseURL(java.lang.String encodedUrl)Returns the URI for the given encoded URL.GenericUrlset(java.lang.String fieldName, java.lang.Object value)Sets the given field value (may benull) for the given field name.voidsetFragment(java.lang.String fragment)Sets the fragment component ornullfor none.voidsetHost(java.lang.String host)Sets the host, for example"www.google.com".voidsetPathParts(java.util.List<java.lang.String> pathParts)Sets the decoded path component by parts with each part separated by a'/'ornullfor none.voidsetPort(int port)Sets the port number, for example443.voidsetRawPath(java.lang.String encodedPath)Sets thepathPartsfrom the given raw encoded path.voidsetScheme(java.lang.String scheme)Sets the scheme (lowercase), for example"https".voidsetUserInfo(java.lang.String userInfo)Sets the user info ornullfor none, for example"username:password".static java.util.List<java.lang.String>toPathParts(java.lang.String encodedPath)Returns the decoded path parts for the given encoded path.static java.util.List<java.lang.String>toPathParts(java.lang.String encodedPath, boolean verbatim)Returns the path parts (decoded if notverbatim).java.lang.StringtoString()java.net.URItoURI()Constructs the URI based on the string representation of the URL frombuild().private static java.net.URItoURI(java.lang.String encodedUrl)Returns the URI for the given encoded URL.java.net.URLtoURL()Constructs the URL based on the string representation of the URL frombuild().java.net.URLtoURL(java.lang.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
-
-
-
-
Field Detail
-
URI_FRAGMENT_ESCAPER
private static final Escaper URI_FRAGMENT_ESCAPER
-
scheme
private java.lang.String scheme
Scheme (lowercase), for example"https".
-
host
private java.lang.String host
Host, for example"www.google.com".
-
userInfo
private java.lang.String userInfo
User info ornullfor none, for example"username:password".
-
port
private int port
Port number or-1if undefined, for example443.
-
pathParts
private java.util.List<java.lang.String> 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
private java.lang.String fragment
Fragment component ornullfor none.
-
verbatim
private boolean verbatim
If 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 Detail
-
GenericUrl
public GenericUrl()
-
GenericUrl
public GenericUrl(java.lang.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
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:
java.lang.IllegalArgumentException- if the URL has a syntax error
-
GenericUrl
public GenericUrl(java.lang.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
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:
java.lang.IllegalArgumentException- if URL has a syntax error
-
GenericUrl
public GenericUrl(java.net.URI uri)
Constructs from a URI.- Parameters:
uri- URI- Since:
- 1.14
-
GenericUrl
public GenericUrl(java.net.URI uri, boolean verbatim)Constructs from a URI.- Parameters:
uri- URIverbatim- flag, to specify if URL should be used as is (without encoding, decoding and escaping)
-
GenericUrl
public GenericUrl(java.net.URL url)
Constructs from a URL.- Parameters:
url- URL- Since:
- 1.14
-
GenericUrl
public GenericUrl(java.net.URL url, boolean verbatim)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
private GenericUrl(java.lang.String scheme, java.lang.String host, int port, java.lang.String path, java.lang.String fragment, java.lang.String query, java.lang.String userInfo, boolean verbatim)
-
-
Method Detail
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfacejava.util.Map<java.lang.String,java.lang.Object>- Overrides:
hashCodein classGenericData
-
equals
public boolean equals(java.lang.Object obj)
- Specified by:
equalsin interfacejava.util.Map<java.lang.String,java.lang.Object>- Overrides:
equalsin classGenericData
-
toString
public java.lang.String toString()
- Overrides:
toStringin classGenericData
-
clone
public GenericUrl 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
public GenericUrl set(java.lang.String fieldName, java.lang.Object value)
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
public final java.lang.String getScheme()
Returns the scheme (lowercase), for example"https".- Since:
- 1.5
-
setScheme
public final void setScheme(java.lang.String scheme)
Sets the scheme (lowercase), for example"https".- Since:
- 1.5
-
getHost
public java.lang.String getHost()
Returns the host, for example"www.google.com".- Since:
- 1.5
-
setHost
public final void setHost(java.lang.String host)
Sets the host, for example"www.google.com".- Since:
- 1.5
-
getUserInfo
public final java.lang.String getUserInfo()
Returns the user info ornullfor none, for example"username:password".- Since:
- 1.15
-
setUserInfo
public final void setUserInfo(java.lang.String userInfo)
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
public java.util.List<java.lang.String> getPathParts()
Returns the decoded path component by parts with each part separated by a'/'ornullfor none.- Since:
- 1.5
-
setPathParts
public void setPathParts(java.util.List<java.lang.String> pathParts)
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
public java.lang.String getFragment()
Returns the fragment component ornullfor none.- Since:
- 1.5
-
setFragment
public final void setFragment(java.lang.String fragment)
Sets the fragment component ornullfor none.- Since:
- 1.5
-
build
public final java.lang.String build()
Constructs the string representation of the URL, including the path specified bypathPartsand the query parameters specified by this generic URL.
-
buildAuthority
public final java.lang.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 java.lang.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 java.net.URI 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
public final java.net.URL 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
public final java.net.URL toURL(java.lang.String relativeUrl)
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
public java.lang.Object getFirst(java.lang.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 java.util.Collection<java.lang.Object> getAll(java.lang.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)
-
getRawPath
public java.lang.String getRawPath()
Returns the raw encoded path computed from thepathParts.
-
setRawPath
public void setRawPath(java.lang.String encodedPath)
Sets thepathPartsfrom the given raw encoded path.- Parameters:
encodedPath- raw encoded path ornullto setpathPartstonull
-
appendRawPath
public void appendRawPath(java.lang.String encodedPath)
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
public static java.util.List<java.lang.String> toPathParts(java.lang.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", ornullfornullor""input
-
toPathParts
public static java.util.List<java.lang.String> toPathParts(java.lang.String encodedPath, boolean verbatim)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
private void appendRawPathFromParts(java.lang.StringBuilder buf)
-
addQueryParams
static void addQueryParams(java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet, java.lang.StringBuilder buf, boolean verbatim)Adds query parameters from the provided entrySet into the buffer.
-
appendParam
private static boolean appendParam(boolean first, java.lang.StringBuilder buf, java.lang.String name, java.lang.Object value, boolean verbatim)
-
toURI
private static java.net.URI toURI(java.lang.String encodedUrl)
Returns the URI for the given encoded URL.Any
URISyntaxExceptionis wrapped in anIllegalArgumentException.- Parameters:
encodedUrl- encoded URL- Returns:
- URI
-
parseURL
private static java.net.URL parseURL(java.lang.String encodedUrl)
Returns the URI for the given encoded URL.Any
MalformedURLExceptionis wrapped in anIllegalArgumentException.- Parameters:
encodedUrl- encoded URL- Returns:
- URL
-
-