public class URIBuilder extends Object implements Cloneable
set, add
and remove methods affect this class' internal URI
representation. All mutator methods support chaining, e.g.
new URIBuilder("http://www.google.com/")
.setScheme( "https" )
.setPort( 443 )
.setPath( "some/path" )
.toString();
A slightly more 'Groovy' version would be:
new URIBuilder('http://www.google.com/').with {
scheme = 'https'
port = 443
path = 'some/path'
query = [p1:1, p2:'two']
return it
}.toString()
| Constructor and Description |
|---|
URIBuilder(String url) |
URIBuilder(URI uri) |
URIBuilder(URL url) |
| Modifier and Type | Method and Description |
|---|---|
protected URIBuilder |
addQueryParam(NameValuePair nvp) |
URIBuilder |
addQueryParam(String param,
Object value)
This will append a query parameter to the existing query string.
|
protected URIBuilder |
addQueryParams(List<NameValuePair> nvp) |
URIBuilder |
addQueryParams(Map<?,?> params)
Add these parameters to the URIBuilder's existing query string.
|
Object |
asType(Class<?> type)
Implementation of Groovy's
as operator, to allow type
conversion. |
protected URIBuilder |
clone()
Create a copy of this URIBuilder instance.
|
static URI |
convertToURI(Object uri)
Utility method to convert a number of type to a URI instance.
|
boolean |
equals(Object obj)
Determine if this URIBuilder is equal to another URIBuilder instance.
|
String |
getFragment()
|
String |
getHost()
See
URI.getHost() |
String |
getPath()
Note that this property is not necessarily reflexive
with the
setPath(String) method! URIBuilder.setPath()
will resolve a relative path, whereas this method will always return the
full, absolute path. |
int |
getPort()
See
URI.getPort() |
Map<String,Object> |
getQuery()
Get the query string as a map for convenience.
|
protected List<NameValuePair> |
getQueryNVP() |
String |
getScheme()
Get the scheme for this URI.
|
String |
getUserInfo()
|
boolean |
hasQueryParam(String name)
Indicates if the given parameter is already part of this URI's query
string.
|
URIBuilder |
removeQueryParam(String param)
Remove the given query parameter from this URI's query string.
|
URIBuilder |
setFragment(String fragment)
The document fragment, without a preceeding '#'.
|
URIBuilder |
setHost(String host)
Set the host portion of this URI.
|
URIBuilder |
setPath(String path)
Set the path component of this URI.
|
URIBuilder |
setPort(int port)
Set the port for this URI, or
-1 to unset the port. |
URIBuilder |
setQuery(Map<?,?> params)
Set the query portion of the URI.
|
protected URIBuilder |
setQueryNVP(List<NameValuePair> nvp) |
URIBuilder |
setRawQuery(String query)
Set the raw, already-escaped query string.
|
URIBuilder |
setScheme(String scheme)
Set the URI scheme, AKA the 'protocol.' e.g.
|
URIBuilder |
setUserInfo(String userInfo)
Set the userInfo portion of the URI, or
null if the URI
should have no user information. |
String |
toString()
Print this builder's URI representation.
|
URI |
toURI()
Convenience method to convert this object to a URI instance.
|
URL |
toURL()
Convenience method to convert this object to a URL instance.
|
protected URI |
update(String scheme,
String userInfo,
String host,
int port,
String path,
String query,
String fragment) |
public URIBuilder(String url) throws URISyntaxException
URISyntaxExceptionpublic URIBuilder(URL url) throws URISyntaxException
URISyntaxExceptionpublic URIBuilder(URI uri) throws IllegalArgumentException
uri - IllegalArgumentException - if uri is nullpublic static URI convertToURI(Object uri) throws URISyntaxException
uri - a URI, URL or any object that produces a
valid URI string from its toString() result.URISyntaxExceptionprotected URI update(String scheme, String userInfo, String host, int port, String path, String query, String fragment) throws URISyntaxException
URISyntaxExceptionpublic URIBuilder setScheme(String scheme) throws URISyntaxException
setScheme('https')URISyntaxException - if the given scheme contains illegal characters.public String getScheme()
URI.getScheme()public URIBuilder setPort(int port) throws URISyntaxException
-1 to unset the port.port - URISyntaxExceptionpublic int getPort()
URI.getPort()public URIBuilder setHost(String host) throws URISyntaxException
host - URISyntaxException - if the host parameter contains illegal characters.public String getHost()
URI.getHost()public URIBuilder setPath(String path) throws URISyntaxException
def uri = new URIBuilder( 'http://localhost/p1/p2?a=1' ) uri.path = '/p3/p2' assert uri.toString() == 'http://localhost/p3/p2?a=1' uri.path = 'p2a' assert uri.toString() == 'http://localhost/p3/p2a?a=1' uri.path = '../p4' assert uri.toString() == 'http://localhost/p4?a=1&b=2&c=3#frag'
path - the path portion of this URI, relative to the current URI.URISyntaxException - if the given path contains characters that
cannot be converted to a valid URIpublic String getPath()
setPath(String) method! URIBuilder.setPath()
will resolve a relative path, whereas this method will always return the
full, absolute path.
See URI.getPath()protected URIBuilder setQueryNVP(List<NameValuePair> nvp) throws URISyntaxException
URISyntaxExceptionpublic URIBuilder setQuery(Map<?,?> params) throws URISyntaxException
uri.query = [ p1:'val1', p2:['val2', 'val3'] ] // will produce a query string of ?p1=val1&p2=val2&p2=val3
params - a Map of parameters that will be transformed into the query stringURISyntaxExceptionpublic URIBuilder setRawQuery(String query) throws URISyntaxException
query - URISyntaxExceptionpublic Map<String,Object> getQuery()
p1=one&p1=two) both values will be
inserted into a list for that paramter key ([p1 : ['one','two']]
). Note that this is not a "live" map. Therefore, you cannot
call
uri.query.a = 'BCD'You will not modify the query string but instead the generated map of parameters. Instead, you need to use
removeQueryParam(String)
first, then addQueryParam(String, Object), or call
setQuery(Map) which will set the entire query string.protected List<NameValuePair> getQueryNVP()
public boolean hasQueryParam(String name)
name - the query parameter namepublic URIBuilder removeQueryParam(String param) throws URISyntaxException
param - the query name to removeURISyntaxExceptionprotected URIBuilder addQueryParam(NameValuePair nvp) throws URISyntaxException
URISyntaxExceptionpublic URIBuilder addQueryParam(String param, Object value) throws URISyntaxException
removeQueryParam(String) first, or use getQuery(),
modify the value in the map, then call setQuery(Map).param - query parameter namevalue - query parameter value (will be converted to a string if
not null. If value is null, it will be set as the empty
string.URISyntaxException - if the query parameter values cannot be
converted to a valid URI.setQuery(Map)protected URIBuilder addQueryParams(List<NameValuePair> nvp) throws URISyntaxException
URISyntaxExceptionpublic URIBuilder addQueryParams(Map<?,?> params) throws URISyntaxException
uriBuilder.addQueryParams( [one:1,two:2] ) uriBuilder.addQueryParams( three : 3 )If any of the parameters already exist in the URI query, these values will not replace them. Multiple values for the same query parameter may be added by putting them in a list. See
setQuery(Map).params - parameters to add to the existing URI query (if any).URISyntaxExceptionpublic URIBuilder setFragment(String fragment) throws URISyntaxException
null
to use no document fragment.fragment - URISyntaxException - if the given value contains illegal characters.public String getFragment()
public URIBuilder setUserInfo(String userInfo) throws URISyntaxException
null if the URI
should have no user information.userInfo - URISyntaxException - if the given value contains illegal characters.public String getUserInfo()
null if it
is not specified.public URL toURL() throws MalformedURLException
MalformedURLException - if the underlying URI does not represent a
valid URL.public URI toURI()
public Object asType(Class<?> type) throws MalformedURLException
as operator, to allow type
conversion.type - URL, URL, or String.MalformedURLException - if type is URL and this
URIBuilder instance does not represent a valid URL.protected URIBuilder clone()
public boolean equals(Object obj)
equals in class Objectobj is a URIBuilder instance whose underlying
URI implementation is equal to this one's.URI.equals(Object)Copyright © 2008-2014. All Rights Reserved.