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'] }.toString()
Constructor and Description |
---|
URIBuilder(URI uri,
boolean urlEncodingEnabled,
EncoderConfig config) |
Modifier and Type | Method and Description |
---|---|
protected URIBuilder |
addQueryParams(List<org.apache.http.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.
|
static String |
encode(String content,
String encoding) |
boolean |
equals(Object obj)
Determine if this URIBuilder is equal to another URIBuilder instance.
|
Map<String,Object> |
getQuery()
Get the query string as a map for convenience.
|
protected List<org.apache.http.NameValuePair> |
getQueryNVP() |
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) |
URIBuilder |
setPath(String path)
Set the path component of this URI.
|
URIBuilder |
setPort(int port) |
URIBuilder |
setQuery(Map<?,?> params)
Set the query portion of the URI.
|
protected URIBuilder |
setQueryNVP(List<org.apache.http.NameValuePair> nvp) |
URIBuilder |
setScheme(String scheme)
Set the URI scheme, AKA the 'protocol.' e.g.
|
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 base
public URIBuilder(URI uri, boolean urlEncodingEnabled, EncoderConfig config) 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.URISyntaxException
public URIBuilder setScheme(String scheme) throws URISyntaxException
setScheme('https')
URISyntaxException
- if the given scheme contains illegal characters.public URIBuilder setPort(int port) throws URISyntaxException
URISyntaxException
public URIBuilder setHost(String host) throws URISyntaxException
URISyntaxException
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 URIprotected URIBuilder setQueryNVP(List<org.apache.http.NameValuePair> nvp) throws URISyntaxException
URISyntaxException
public 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 stringURISyntaxException
public 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 call setQuery(Map)
which will set the entire query string.protected List<org.apache.http.NameValuePair> getQueryNVP()
public boolean hasQueryParam(String name)
name
- the query parameter namepublic URIBuilder removeQueryParam(String param) throws URISyntaxException
param
- the query name to removeURISyntaxException
protected URIBuilder addQueryParams(List<org.apache.http.NameValuePair> nvp) throws URISyntaxException
URISyntaxException
public 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).URISyntaxException
public URIBuilder setFragment(String fragment) throws URISyntaxException
fragment
- URISyntaxException
- if the given value contains illegal characters.public String toString()
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 Object
obj
is a URIBuilder instance whose underlying
URI implementation is equal to this one's.URI.equals(Object)
Copyright © 2010–2019. All rights reserved.