Class URIBuilder
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumDefines the encoding policy for URI components inURIBuilder. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Charsetprivate Stringprivate Stringprivate Stringprivate Stringprivate Stringprivate Stringprivate URIBuilder.EncodingPolicyprivate Stringprivate Stringprivate static final charprivate static final charprivate booleanprivate booleanprivate intprivate Stringprivate static final charprivate static final Tokenizer.Delimiterprivate static final Tokenizer.Delimiterprivate List<NameValuePair> private Stringprivate String -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty instance.URIBuilder(String uriString) Constructs an instance from the string which must be a valid URI.URIBuilder(String uriString, Charset charset) Constructs an instance from the string which must be a valid URI.URIBuilder(URI uri) Constructs an instance from the provided URI.URIBuilder(URI uri, Charset charset) Constructs an instance from the provided URI. -
Method Summary
Modifier and TypeMethodDescriptionaddParameter(String param, String value) Adds parameter to URI query.addParameter(NameValuePair nameValuePair) Adds parameter to URI query.addParameters(List<NameValuePair> nameValuePairs) Adds URI query parameters.appendPath(String path) Appends path to URI.appendPathSegments(String... pathSegments) Appends segments URI path.appendPathSegments(List<String> pathSegments) Appends segments to URI path.build()Builds aURIinstance.private StringClears URI query parameters.private void(package private) static voidformatPath(StringBuilder buf, Iterable<String> segments, boolean rootless, Charset charset) (package private) static voidformatPath(StringBuilder buf, Iterable<String> segments, boolean rootless, Charset charset, BitSet safechars) (package private) static voidformatQuery(StringBuilder buf, Iterable<? extends NameValuePair> params, Charset charset, boolean blankAsPlus) (package private) static voidformatQuery(StringBuilder buf, Iterable<? extends NameValuePair> params, Charset charset, BitSet safechars, boolean blankAsPlus) Gets the authority.Gets the Charset.getFirstQueryParam(String name) Gets the firstNameValuePairfor a given name.Gets the fragments.getHost()Gets the host portion of theURI.getPath()Gets the path.Gets the path segments.intgetPort()Gets the port.Gets the query parameters as a List.Gets the scheme.Gets the scheme specific part.Gets the user info.booleanTests whether the URI is absolute.booleanisOpaque()Tests whether the URI is opaque.booleanTests whether the path is empty.booleanTests whether the query is empty.static URIBuilderCreates a new builder for the hostInetAddress.getLocalHost().static URIBuilderCreates a new builder for the hostInetAddress.getLoopbackAddress().Deprecated.do not use this method.optimize()Optimizes URI components if the URI is considered non-opaque (the path component has a root): characters of scheme and host components are converted to lower case dot segments of the path component are removed if the path has a root percent encoding of all components is re-appliedparsePath(CharSequence s, Charset charset) (package private) static List<NameValuePair> parseQuery(CharSequence s, Charset charset, boolean plusAsBlank) removeParameter(String param) Removes parameter of URI query if set.Removes URI query.setAuthority(NamedEndpoint authority) Sets the authority.setAuthority(URIAuthority authority) Sets the authority.setCharset(Charset charset) Sets the Charset.setCustomQuery(String query) Sets custom URI query.setEncodingPolicy(URIBuilder.EncodingPolicy encodingPolicy) Sets the encoding policy for thisURIBuilder.setFragment(String fragment) Sets URI fragment.Sets URI host.setHost(InetAddress host) Sets URI host.setHttpHost(HttpHost httpHost) Sets the scheme, host name, and port.setParameter(String param, String value) Sets parameter of URI query overriding existing value if set.setParameters(List<NameValuePair> nameValuePairs) Sets URI query parameters.setParameters(NameValuePair... nameValuePairs) Sets URI query parameters.Sets URI path.setPathSegments(String... pathSegments) Sets URI path.setPathSegments(List<String> pathSegments) Sets URI path.setPathSegmentsRootless(String... pathSegments) Sets rootless URI path (the first segment does not start with a /).setPathSegmentsRootless(List<String> pathSegments) Sets rootless URI path (the first segment does not start with a /).setPlusAsBlank(boolean plusAsBlank) Sets whether the plus sign ('+') should be interpreted as a blank space (' ') when parsing the query parameters of the URI.setPort(int port) Sets URI port.Sets URI scheme.setSchemeSpecificPart(String schemeSpecificPart) Sets the URI scheme specific part.setSchemeSpecificPart(String schemeSpecificPart, List<NameValuePair> nvps) Sets the URI scheme specific part and append a list of NameValuePair to this part.setSchemeSpecificPart(String schemeSpecificPart, NameValuePair... nvps) Sets the URI scheme specific part and append a variable arguments list of NameValuePair instance(s) to this part.setUserInfo(String userInfo) Sets URI user info.setUserInfo(String username, String password) Deprecated.The use of clear-text passwords inURIs has been deprecated and is strongly discouraged.toString()Converts this instance to a URI string.
-
Field Details
-
scheme
-
encodedSchemeSpecificPart
-
encodedAuthority
-
userInfo
-
encodedUserInfo
-
host
-
port
private int port -
encodedPath
-
pathRootless
private boolean pathRootless -
pathSegments
-
encodedQuery
-
queryParams
-
query
-
charset
-
fragment
-
encodedFragment
-
encodingPolicy
-
plusAsBlank
private boolean plusAsBlank -
QUERY_PARAM_SEPARATOR
private static final char QUERY_PARAM_SEPARATOR- See Also:
-
PARAM_VALUE_SEPARATOR
private static final char PARAM_VALUE_SEPARATOR- See Also:
-
PATH_SEPARATOR
private static final char PATH_SEPARATOR- See Also:
-
QUERY_PARAM_SEPARATORS
-
QUERY_VALUE_SEPARATORS
-
-
Constructor Details
-
URIBuilder
public URIBuilder()Constructs an empty instance. -
URIBuilder
Constructs an instance from the string which must be a valid URI.- Parameters:
uriString- a valid URI in string form.- Throws:
URISyntaxException- if the input is not a valid URI.
-
URIBuilder
-
URIBuilder
Constructs an instance from the string which must be a valid URI.- Parameters:
uriString- a valid URI in string form.- Throws:
URISyntaxException- if the input is not a valid URI
-
URIBuilder
-
-
Method Details
-
localhost
Creates a new builder for the hostInetAddress.getLocalHost().- Returns:
- a new builder.
- Throws:
UnknownHostException- if the local host name could not be resolved into an address.
-
loopbackAddress
Creates a new builder for the hostInetAddress.getLoopbackAddress(). -
setAuthority
Sets the authority.- Parameters:
authority- the authority.- Returns:
- this instance.
- Since:
- 5.2
-
setAuthority
Sets the authority.- Parameters:
authority- the authority.- Returns:
- this instance.
- Since:
- 5.2
-
setCharset
Sets the Charset.- Parameters:
charset- the Charset.- Returns:
- this instance.
-
setEncodingPolicy
Sets the encoding policy for thisURIBuilder. The encoding policy determines how URI components (e.g., query, fragment) are percent-encoded when building the URI string. If not set, the default policy isURIBuilder.EncodingPolicy.RFC_3986.- Parameters:
encodingPolicy- the encoding policy to apply, ornullto reset to the default (URIBuilder.EncodingPolicy.ALL_RESERVED)- Returns:
- this
URIBuilderinstance for method chaining - Since:
- 5.4
-
getAuthority
-
getCharset
-
setPlusAsBlank
Sets whether the plus sign ('+') should be interpreted as a blank space (' ') when parsing the query parameters of the URI.In HTTP URLs, query strings may contain spaces encoded as '+' characters or as '%20'. This flag controls whether '+' is interpreted as a space or remains as a plus sign.
If the query string was already set, calling this method will re-parse the query using the updated flag. This ensures that the query parameters are processed correctly based on the specified interpretation of the '+' character.
- Parameters:
plusAsBlank-trueto interpret '+' as a space,falseto keep '+' as a literal plus sign.- Returns:
- this
URIBuilderinstance for method chaining. - Since:
- 5.4
-
parseQuery
-
splitPath
-
parsePath
-
formatPath
static void formatPath(StringBuilder buf, Iterable<String> segments, boolean rootless, Charset charset, BitSet safechars) -
formatPath
static void formatPath(StringBuilder buf, Iterable<String> segments, boolean rootless, Charset charset) -
formatQuery
static void formatQuery(StringBuilder buf, Iterable<? extends NameValuePair> params, Charset charset, BitSet safechars, boolean blankAsPlus) -
formatQuery
static void formatQuery(StringBuilder buf, Iterable<? extends NameValuePair> params, Charset charset, boolean blankAsPlus) -
build
-
buildString
-
digestURI
-
setScheme
-
setSchemeSpecificPart
Sets the URI scheme specific part.- Parameters:
schemeSpecificPart-- Returns:
- this instance.
- Since:
- 5.1
-
setSchemeSpecificPart
Sets the URI scheme specific part and append a variable arguments list of NameValuePair instance(s) to this part.- Parameters:
schemeSpecificPart-nvps- Optional, can be null. Variable arguments list of NameValuePair query parameters to be reused by the specific scheme part- Returns:
- this instance.
- Since:
- 5.1
-
setSchemeSpecificPart
Sets the URI scheme specific part and append a list of NameValuePair to this part.- Parameters:
schemeSpecificPart-nvps- Optional, can be null. List of query parameters to be reused by the specific scheme part- Returns:
- this instance.
- Since:
- 5.1
-
setUserInfo
Sets URI user info. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
setUserInfo
Deprecated.The use of clear-text passwords inURIs has been deprecated and is strongly discouraged.Sets URI user info as a combination of username and password. These values are expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
setHost
-
setHost
Sets URI host. The input value must not already be URI encoded, for example::1is valid however[::1]is not. It is dangerous to calluriBuilder.setHost(uri.getHost())due toURI.getHost()returning URI encoded values.- Returns:
- this instance.
-
setHttpHost
Sets the scheme, host name, and port.- Parameters:
httpHost- the scheme, host name, and port.- Returns:
- this instance.
-
setPort
-
setPath
Sets URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
appendPath
Appends path to URI. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
setPathSegments
Sets URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
appendPathSegments
Appends segments URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
setPathSegmentsRootless
Sets rootless URI path (the first segment does not start with a /). The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
- Since:
- 5.1
-
setPathSegments
Sets URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
appendPathSegments
Appends segments to URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
setPathSegmentsRootless
Sets rootless URI path (the first segment does not start with a /). The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
- Since:
- 5.1
-
removeQuery
-
setParameters
Sets URI query parameters. The parameter name / values are expected to be unescaped and may contain non ASCII characters.Please note query parameters and custom query component are mutually exclusive. This method will remove custom query if present.
- Returns:
- this instance.
-
addParameters
Adds URI query parameters. The parameter name / values are expected to be unescaped and may contain non ASCII characters.Please note query parameters and custom query component are mutually exclusive. This method will remove custom query if present.
- Returns:
- this instance.
-
setParameters
Sets URI query parameters. The parameter name / values are expected to be unescaped and may contain non ASCII characters.Please note query parameters and custom query component are mutually exclusive. This method will remove custom query if present.
- Returns:
- this instance.
-
addParameter
Adds parameter to URI query. The parameter name and value are expected to be unescaped and may contain non ASCII characters.Please note query parameters and custom query component are mutually exclusive. This method will remove custom query if present.
- Returns:
- this instance.
-
addParameter
Adds parameter to URI query. The parameter name and value are expected to be unescaped and may contain non ASCII characters.Please note query parameters and custom query component are mutually exclusive. This method will remove custom query if present.
- Returns:
- this instance.
- Since:
- 5.2
-
removeParameter
Removes parameter of URI query if set. The parameter name is expected to be unescaped and may contain non ASCII characters.Please note query parameters and custom query component are mutually exclusive. This method will remove custom query if present, even when no parameter was actually removed.
- Returns:
- this instance.
- Since:
- 5.2
-
setParameter
Sets parameter of URI query overriding existing value if set. The parameter name and value are expected to be unescaped and may contain non ASCII characters.Please note query parameters and custom query component are mutually exclusive. This method will remove custom query if present.
- Returns:
- this instance.
-
clearParameters
-
setCustomQuery
Sets custom URI query. The value is expected to be unescaped and may contain non ASCII characters.Please note query parameters and custom query component are mutually exclusive. This method will remove query parameters if present.
- Returns:
- this instance.
-
setFragment
Sets URI fragment. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
isAbsolute
public boolean isAbsolute()Tests whether the URI is absolute.- Returns:
- whether the URI is absolute.
-
isOpaque
public boolean isOpaque()Tests whether the URI is opaque.- Returns:
- whether the URI is opaque.
-
getScheme
-
getSchemeSpecificPart
-
getUserInfo
-
getHost
Gets the host portion of theURI. This method returns unencoded IPv6 addresses (without brackets). This behavior differs from values returned byURI.getHost().- Returns:
- The host portion of the URI.
-
getPort
public int getPort()Gets the port.- Returns:
- the port.
-
isPathEmpty
public boolean isPathEmpty()Tests whether the path is empty.- Returns:
- whether the path is empty.
-
getPathSegments
-
getPath
-
isQueryEmpty
public boolean isQueryEmpty()Tests whether the query is empty.- Returns:
- whether the query is empty.
-
getQueryParams
Gets the query parameters as a List.- Returns:
- the query parameters as a List.
-
getFirstQueryParam
Gets the firstNameValuePairfor a given name.- Parameters:
name- the name- Returns:
- the first named
NameValuePairor null if not found. - Since:
- 5.2
-
getFragment
-
normalizeSyntax
-
optimize
Optimizes URI components if the URI is considered non-opaque (the path component has a root):- characters of scheme and host components are converted to lower case
- dot segments of the path component are removed if the path has a root
- percent encoding of all components is re-applied
Please note some URI consumers may consider the optimized URI components produced by this method as semantically different from the original ones.
- Since:
- 5.3
-
toString
-