public class UrlBuilder extends Object
Simple class that encapsulates the process of building up a URL from a path fragment and a zero or more parameters. Parameters can be single valued, array valued or collection valued. In the case of arrays and collections, each value in the array or collection will be added as a separate URL parameter (with the same name). The assembled URL can then be retrieved by calling toString().
While not immediately obvious, it is possible to add a single parameter with multiple values by invoking the addParameter() method that uses varargs, and supplying a Collection as the single parameter value to the method.
| Modifier | Constructor and Description |
|---|---|
protected |
UrlBuilder(Charset charset,
boolean isForPage)
Sets the locale and sets the parameter separator based on the value of
isForPage. |
|
UrlBuilder(Charset charset,
String url,
boolean isForPage)
Constructs a UrlBuilder with the path to a resource.
|
| Modifier and Type | Method and Description |
|---|---|
UrlBuilder |
addParameter(String name,
Object... values)
Appends one or more values of a parameter to the URL.
|
UrlBuilder |
addParameters(Map<? extends Object,? extends Object> parameters)
Appends one or more parameters to the URL.
|
protected String |
build()
Build and return the URL
|
protected String |
format(Object value)
Attempts to format an object.
|
String |
getAnchor()
Gets the anchor, if any, that will be appended to the URL.
|
String |
getParameterSeparator()
Returns the string that will be used to separate parameters in the query string.
|
UrlBuilder |
setAnchor(String anchor)
Sets the anchor, if any, that will be appended to the URL.
|
void |
setParameterSeparator(String parameterSeparator)
Sets the string that will be used to separate parameters.
|
String |
toString()
Returns the URL composed thus far as a String.
|
public UrlBuilder(Charset charset, String url, boolean isForPage)
charset - the Charset to use when formatting URLsurl - the path part of the URLisForPage - true if the URL is to be embedded in a page (e.g. in an anchor of img
tag), false if for some other purpose.protected UrlBuilder(Charset charset, boolean isForPage)
isForPage.charset - the Charset to use when formatting URLsisForPage - true if the URL is to be embedded in a page (e.g. in an anchor of img tag),
false if for some other purpose.public String getParameterSeparator()
public void setParameterSeparator(String parameterSeparator)
public UrlBuilder addParameter(String name, Object... values)
Appends one or more values of a parameter to the URL. Checks to see if each value is null, and if so generates a parameter with no value. URL Encodes the parameter values to make sure that it is safe to insert into the URL.
If any parameter value passed is a Collection or an Array then this method is called recursively with the contents of the collection or array. As a result you can pass arbitrarily nested arrays and collections to this method and it will recurse through them adding all scalar values as parameters to the URL.
.name - the name of the request parameter being addedvalues - one or more values for the parameter suppliedpublic UrlBuilder addParameters(Map<? extends Object,? extends Object> parameters)
parameters - a non-null Map as described abovepublic String getAnchor()
public UrlBuilder setAnchor(String anchor)
anchor - the anchor with or without the leading pound sign, or null to disablepublic String toString()
protected String format(Object value)
value will
be formatted as an empty string.value - the object to be formattedprotected String build()
Copyright © 2022 MANYDESIGNS s.r.l.. All rights reserved.