groovyx.net.http
Class URIBuilder

java.lang.Object
  extended by groovyx.net.http.URIBuilder

public class URIBuilder
extends Object

This class implements a mutable URI. All 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()
 

Author:
Tom Nichols

Field Summary
protected  URI base
           
 
Constructor Summary
URIBuilder(String url)
           
URIBuilder(URI url)
           
URIBuilder(URL url)
           
 
Method Summary
 URIBuilder addQueryParam(String param, Object value)
          This will append a param to the existing query string.
 URIBuilder addQueryParams(Map<String,?> params)
           
static URI convertToURI(Object uri)
          Attempts to convert a URL or String to a URI.
 Map<String,String> getQuery()
          Get the query string as a map
 boolean hasQueryParam(String name)
           
 URIBuilder removeQueryParam(String param)
           
 URIBuilder setFragment(String fragment)
          The document fragment, without a preceeding '#'
 URIBuilder setHost(String host)
           
 URIBuilder setPath(String path)
           
 URIBuilder setPort(int port)
           
 URIBuilder setQuery(Map<String,?> params)
          Set the query portion of the URI
 URIBuilder setScheme(String scheme)
          AKA protocol
 String toString()
           
 URI toURI()
           
 URL toURL()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

base

protected URI base
Constructor Detail

URIBuilder

public URIBuilder(String url)
           throws URISyntaxException
Throws:
URISyntaxException

URIBuilder

public URIBuilder(URL url)
           throws URISyntaxException
Throws:
URISyntaxException

URIBuilder

public URIBuilder(URI url)
Method Detail

convertToURI

public static URI convertToURI(Object uri)
                        throws URISyntaxException
Attempts to convert a URL or String to a URI.

Parameters:
uri - a URI, URL or any object that produces a parse-able URI string from its toString() result.
Returns:
a valid URI parsed from the given object
Throws:
URISyntaxException

setScheme

public URIBuilder setScheme(String scheme)
                     throws URISyntaxException
AKA protocol

Throws:
URISyntaxException

setPort

public URIBuilder setPort(int port)
                   throws URISyntaxException
Throws:
URISyntaxException

setHost

public URIBuilder setHost(String host)
                   throws URISyntaxException
Throws:
URISyntaxException

setPath

public URIBuilder setPath(String path)
                   throws URISyntaxException
Throws:
URISyntaxException

setQuery

public URIBuilder setQuery(Map<String,?> params)
                    throws URISyntaxException
Set the query portion of the URI

Parameters:
params - a Map of parameters that will be transformed into the query string
Returns:
Throws:
URISyntaxException

getQuery

public Map<String,String> getQuery()
Get the query string as a map

Returns:

hasQueryParam

public boolean hasQueryParam(String name)

removeQueryParam

public URIBuilder removeQueryParam(String param)
                            throws URISyntaxException
Throws:
URISyntaxException

addQueryParam

public URIBuilder addQueryParam(String param,
                                Object value)
                         throws URISyntaxException
This will append a param to the existing query string. If the given param is already part of the query string, it will be replaced.

Parameters:
param -
value -
Throws:
URISyntaxException

addQueryParams

public URIBuilder addQueryParams(Map<String,?> params)
                          throws URISyntaxException
Throws:
URISyntaxException

setFragment

public URIBuilder setFragment(String fragment)
                       throws URISyntaxException
The document fragment, without a preceeding '#'

Parameters:
fragment -
Throws:
URISyntaxException

toString

public String toString()
Overrides:
toString in class Object

toURL

public URL toURL()
          throws MalformedURLException
Throws:
MalformedURLException

toURI

public URI toURI()


Copyright © 2008-2009. All Rights Reserved.