Class UriBuilder
It can parse an exiting URI and modify it, or construct a blank URI from scratch.
Details
Most aspects are typical, but there are two significant features to note:- secure flag : In the event that the URI you want to generate is "http" vs "https" (or "ws" vs "wss"), but you don't want to have external logic to select the correct scheme string, simply use any version of the scheme and then use the secure flag to have it set correctly in the final URI.
- variables : If a path contains a macro, such as {name},
then it will be replaced with the corresponding variable value.
This is an easy way to inject variables into path segments. For example:
builder.setPath("/users/{userId}/").setVar("userId", 1234); // results in a path of "/users/1234/"
Example #1 (starting from blank)
URI uri = new UriBuilder()
.setScheme("http")
.setHost("example.com")
.setPort(12345)
.setSecure(true) // changes the scheme from "http" to "https"
.setPath("/path/{var1}/page.html")
.setVar("var1", "to") // causes variable substitution to take place
.setFragment("fragment")
.addParam("param1", "value1")
.addParam("param2", "value 2") // note that the space character is encoded
.addParam("param3", null) // just the parameter without an associated value
.toURI();
assertThat(uri.toString(), equalTo("https://example.com:12345/path/to/page.html#fragment?param1=value1¶m2=value+2¶m3");
Example #2 (starting from an existing URI string)
URI uri = new UriBuilder("https://mysite.com:443/path/to/page")
.setSecure(false)
.setPort(null)
.addParam("param1", "value1")
.toURI();
assertThat(uri.toString(), equalTo("http://mysite.com/path/to/page?param1=value1");
- Since:
- 1.0
- Version:
- 2022-10-05
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty URI builder.UriBuilder
(String str) Constructs a URI builder initialized from the specified string.UriBuilder
(URI uri) Creates a URI builder initialized from the specified URI. -
Method Summary
Modifier and TypeMethodDescriptionAdds the specified parameter to the URI.Adds the specified parameter to the URI.Adds the specified parameters to the URI.Adds a path to the URI.Adds paths to the URI.Adds paths to the URI.getHost()
getPort()
Removes all parameters from the URI.removeParam
(String name) Removes all instances of the specified parameter from the URI.setFragment
(String fragment) Sets the fragment for the URI.Sets the host of the URI.Removes all instances of the given parameter and replaces it with the new parameter.Removes all instances of the given parameter and replaces it with the new parameter.Replaces all existing parameters with the new list of parameters.Sets the path for the URI.Sets the path for the URI.Sets the path for the URI.Sets the port number of the URI.Sets thescheme
of the URI.Sets thesecure
flag.Replaces the {name} macro in the path with the specified value.toString()
Equivalent to callingtoURI().toString()
.toURI()
Returns a URI from the current builder configuration.
-
Constructor Details
-
UriBuilder
public UriBuilder()Constructs an empty URI builder. -
UriBuilder
Constructs a URI builder initialized from the specified string.- Parameters:
str
- the initial URI for the builder
-
UriBuilder
Creates a URI builder initialized from the specified URI.- Parameters:
uri
- the initial URI for the builder
-
-
Method Details
-
setSecure
Sets thesecure
flag.If this flag is set to either
true
orfalse
, then the configuredscheme
will be converted to the form that matches it. For example:- If this flag is
true
, then "http" becomes "https" and "ws" becomes "wss" - If this flag is
false
, then "https" becomes "http" and "wss" becomes "ws" - If this flag is
null
, then no change is made to the scheme
- Parameters:
secure
- true enables security, false disables it, null indicates "no change"
- If this flag is
-
setScheme
Sets thescheme
of the URI.If "http/https" or "ws/wss", then the actual scheme can be controlled using the setSecure() method, in cases where computing the correct form of the scheme is inconvenient.
- Parameters:
scheme
- the scheme [http|https|ws|wss] of the URI
-
setHost
Sets the host of the URI.- Parameters:
host
- the host of the URI
-
setPort
Sets the port number of the URI. Use null to remove the explicit port number.- Parameters:
port
- the port number of the URI
-
setPath
Sets the path for the URI.This removes all existing path segments before setting the specified path. The path can contain multiple segments such as "foo/bar", as well as macro expansions such as "users/{userId}".
- Parameters:
path
- the path of the URI
-
setPaths
Sets the path for the URI.This removes all existing path segements and replaces then with the specified path. Each path can contain multiple segments such as "foo/bar", as well as macro expansions such as "users/{userId}".
- Parameters:
paths
- the new path segments of the URI
-
setPaths
Sets the path for the URI. This will remove all existing paths and replace with the specified path. Each path can contain multiple segments such as "foo/bar" as well as macro expansions such as "users/{userId}"- Parameters:
paths
- the new paths
-
addPath
Adds a path to the URI. The path can contain multiple segments such as "foo/bar" as well as macro expansions such as "users/{userId}"- Parameters:
path
- the path to add
-
addPaths
Adds paths to the URI. The paths can contain multiple segments such as "foo/bar" as well as macro expansions such as "users/{userId}"- Parameters:
paths
- the paths to add
-
addPaths
Adds paths to the URI. The paths can contain multiple segments such as "foo/bar" as well as macro expansions such as "users/{userId}"- Parameters:
paths
- the paths to add
-
setVar
Replaces the {name} macro in the path with the specified value. This replaces every instance of the macro name in the path. If any macros exists without variables, then exception is thrown whentoURI()
is called.- Parameters:
name
- name of the variableval
- value of the variable
-
removeAllParams
Removes all parameters from the URI. -
removeParam
Removes all instances of the specified parameter from the URI.- Parameters:
name
- the name of the parameter to remove
-
setParams
Replaces all existing parameters with the new list of parameters.- Parameters:
params
- new parameter list
-
addParams
Adds the specified parameters to the URI.- Parameters:
params
- the parameters to add
-
setParam
Removes all instances of the given parameter and replaces it with the new parameter.- Parameters:
param
- the replacement parameter
-
setParam
Removes all instances of the given parameter and replaces it with the new parameter.- Parameters:
name
- the parameter namevalue
- the parmaeter value
-
addParam
Adds the specified parameter to the URI.- Parameters:
param
- the parameter to add
-
addParam
Adds the specified parameter to the URI.- Parameters:
name
- the parameter namevalue
- the parameter value
-
setFragment
Sets the fragment for the URI.- Parameters:
fragment
- the fragment
-
toURI
Returns a URI from the current builder configuration. -
toString
Equivalent to callingtoURI().toString()
. -
getScheme
-
getHost
-
getPort
-
getFragment
-