@DefaultAnnotation(value=edu.umd.cs.findbugs.annotations.NonNull.class) @NotThreadSafe public final class UriBuilder extends Object
This class complements the immutable URI class by enabling its
clients to compose a URI from its components which can get read or written
as independent properties.
Each URI is composed of the five components scheme,
authority, path,
query and fragment.
When done with setting the properties for the URI components, the resulting
URI can be composed by calling any of the methods getUri(),
toUri(), getString() or toString().
This class quotes illegal characters wherever required for the respective URI component. As a deviation from RFC 2396, non-US-ASCII characters get preserved when encoding.
Note that using this class is superior to the five argument URI constructor
new
because the URI constructor does not quote all paths correctly.
For example, URI(scheme, authority, path, query, fragment)new URI(null, null, "foo:bar", null, null) does not
quote the colon before parsing so the resulting URI will have a scheme
component foo and a path component bar instead of just a
path component foo:bar.
URI u it is generally true that
new UriBuilder(u).toUri().equals(u);
and
new UriBuilder().uri(u).toUri().equals(u);
and
new UriBuilder()
.scheme(u.getScheme())
.authority(u.getAuthority())
.path(u.isOpaque() ? u.getSchemeSpecificPart() : u.getPath())
.query(u.getQuery())
.fragment(u.getFragment())
.toUri()
.equals(u);
These identity productions still apply even if the method getUri()
is substituted with the method toUri().UriEncoder| Modifier and Type | Field and Description |
|---|---|
private String |
authority |
private StringBuilder |
builder |
private UriEncoder |
encoder |
private String |
fragment |
private String |
path |
private String |
query |
private String |
scheme |
| Constructor and Description |
|---|
UriBuilder()
Constructs a new URI builder.
|
UriBuilder(URI uri)
Constructs a new URI builder.
|
| Modifier and Type | Method and Description |
|---|---|
UriBuilder |
authority(String authority)
Sets the URI authority component.
|
UriBuilder |
clear()
Clears the state of this URI builder.
|
UriBuilder |
fragment(String fragment)
Sets the URI fragment component.
|
String |
getAuthority()
Returns the URI authority component.
|
String |
getFragment()
Returns the URI fragment component.
|
String |
getPath()
Returns the URI path component.
|
String |
getQuery()
Returns the URI query component.
|
String |
getScheme()
Returns the URI scheme component.
|
String |
getString()
|
URI |
getUri()
|
private static URISyntaxException |
newURISyntaxException(CharBuffer input,
String reason) |
UriBuilder |
path(String path)
Sets the URI path component.
|
UriBuilder |
query(String query)
Sets the URI query component.
|
private static String |
quote(Object s) |
private StringBuilder |
resetBuilder() |
UriBuilder |
scheme(String scheme)
Sets the URI scheme component.
|
void |
setAuthority(String authority)
Sets the URI authority component.
|
void |
setFragment(String fragment)
Sets the URI fragment component.
|
void |
setPath(String path)
Sets the URI path component.
|
void |
setQuery(String query)
Sets the URI query component.
|
void |
setScheme(String scheme)
Sets the URI scheme component.
|
void |
setString(String uri)
Initializes all URI components from the given URI string.
|
void |
setUri(URI uri)
Initializes all URI components from the given URI.
|
UriBuilder |
string(String uri)
Initializes all URI components from the given URI string.
|
String |
toString()
|
URI |
toUri()
|
UriBuilder |
uri(URI uri)
Initializes all URI components from the given URI.
|
private static void |
validateScheme(CharBuffer input) |
static void |
validateScheme(String scheme)
Checks the given string to conform to the syntax constraints for URI
schemes in
RFC 2396
|
private final UriEncoder encoder
@CheckForNull private StringBuilder builder
@CheckForNull private String scheme
@CheckForNull private String authority
@CheckForNull private String path
@CheckForNull private String query
@CheckForNull private String fragment
public UriBuilder()
public UriBuilder(URI uri)
uri - the uri for initializing the initial state.public UriBuilder clear()
null.thispublic String toString()
toString in class ObjectIllegalStateException - if composing a valid URI is not possible.getString()public String getString() throws URISyntaxException
URISyntaxException - if composing a valid URI is not possible due
to an invalid scheme.toString()private StringBuilder resetBuilder()
public static void validateScheme(String scheme) throws URISyntaxException
scheme - the string to validate.URISyntaxException - if scheme does not conform to the
syntax constraints for URI schemes in
RFC 2396.private static void validateScheme(CharBuffer input) throws URISyntaxException
URISyntaxExceptionprivate static URISyntaxException newURISyntaxException(CharBuffer input, String reason)
public void setString(String uri)
uri - the URI string.IllegalArgumentException - if uri does not conform to the
syntax constraints of the URI class.public UriBuilder string(String uri)
uri - the URI string.thisIllegalArgumentException - if uri does not conform to the
syntax constraints of the URI class.public URI toUri()
IllegalStateException - if composing a valid URI is not possible.getUri()public URI getUri() throws URISyntaxException
URISyntaxException - if composing a valid URI is not possible.toUri()public void setUri(URI uri)
uri - the URI.public UriBuilder uri(URI uri)
uri - the URI.this@CheckForNull public String getScheme()
public void setScheme(@CheckForNull
String scheme)
scheme - the URI scheme component.public UriBuilder scheme(@CheckForNull String scheme)
scheme - the URI scheme component.this@CheckForNull public String getAuthority()
initialized from an
opaque URI, then this property is null.public void setAuthority(@CheckForNull
String authority)
authority - the URI authority component.public UriBuilder authority(@CheckForNull String authority)
authority - the URI authority component.this@CheckForNull public String getPath()
initialized from an
opaque URI, then this property contains the
scheme specific part of the URI.public void setPath(@CheckForNull
String path)
path - the URI path component.public UriBuilder path(@CheckForNull String path)
path - the URI path component.this@CheckForNull public String getQuery()
initialized from an
opaque URI, then this property is null.public void setQuery(@CheckForNull
String query)
query - the URI query component.public UriBuilder query(@CheckForNull String query)
query - the URI query component.this@CheckForNull public String getFragment()
public void setFragment(@CheckForNull
String fragment)
fragment - the URI fragment component.public UriBuilder fragment(@CheckForNull String fragment)
fragment - the URI fragment component.thisCopyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.