Class URIBuilder
- java.lang.Object
-
- org.apache.hc.core5.net.URIBuilder
-
public class URIBuilder extends java.lang.ObjectBuilder forURIinstances.- Since:
- 5.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classURIBuilder.EncodingPolicyDefines the encoding policy for URI components inURIBuilder.
-
Field Summary
Fields Modifier and Type Field Description private java.nio.charset.Charsetcharsetprivate java.lang.StringencodedAuthorityprivate java.lang.StringencodedFragmentprivate java.lang.StringencodedPathprivate java.lang.StringencodedQueryprivate java.lang.StringencodedSchemeSpecificPartprivate java.lang.StringencodedUserInfoprivate URIBuilder.EncodingPolicyencodingPolicyprivate java.lang.Stringfragmentprivate java.lang.Stringhostprivate static charPARAM_VALUE_SEPARATORprivate static charPATH_SEPARATORprivate booleanpathRootlessprivate java.util.List<java.lang.String>pathSegmentsprivate booleanplusAsBlankprivate intportprivate java.lang.Stringqueryprivate static charQUERY_PARAM_SEPARATORprivate static Tokenizer.DelimiterQUERY_PARAM_SEPARATORSprivate static Tokenizer.DelimiterQUERY_VALUE_SEPARATORSprivate java.util.List<NameValuePair>queryParamsprivate java.lang.Stringschemeprivate java.lang.StringuserInfo
-
Constructor Summary
Constructors Constructor Description URIBuilder()Constructs an empty instance.URIBuilder(java.lang.String uriString)Constructs an instance from the string which must be a valid URI.URIBuilder(java.lang.String uriString, java.nio.charset.Charset charset)Constructs an instance from the string which must be a valid URI.URIBuilder(java.net.URI uri)Constructs an instance from the provided URI.URIBuilder(java.net.URI uri, java.nio.charset.Charset charset)Constructs an instance from the provided URI.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description URIBuilderaddParameter(java.lang.String param, java.lang.String value)Adds parameter to URI query.URIBuilderaddParameter(NameValuePair nameValuePair)Adds parameter to URI query.URIBuilderaddParameters(java.util.List<NameValuePair> nameValuePairs)Adds URI query parameters.URIBuilderappendPath(java.lang.String path)Appends path to URI.URIBuilderappendPathSegments(java.lang.String... pathSegments)Appends segments URI path.URIBuilderappendPathSegments(java.util.List<java.lang.String> pathSegments)Appends segments to URI path.java.net.URIbuild()Builds aURIinstance.private java.lang.StringbuildString()URIBuilderclearParameters()Clears URI query parameters.private voiddigestURI(java.net.URI uri, java.nio.charset.Charset charset)(package private) static voidformatPath(java.lang.StringBuilder buf, java.lang.Iterable<java.lang.String> segments, boolean rootless, java.nio.charset.Charset charset)(package private) static voidformatPath(java.lang.StringBuilder buf, java.lang.Iterable<java.lang.String> segments, boolean rootless, java.nio.charset.Charset charset, java.util.BitSet safechars)(package private) static voidformatQuery(java.lang.StringBuilder buf, java.lang.Iterable<? extends NameValuePair> params, java.nio.charset.Charset charset, boolean blankAsPlus)(package private) static voidformatQuery(java.lang.StringBuilder buf, java.lang.Iterable<? extends NameValuePair> params, java.nio.charset.Charset charset, java.util.BitSet safechars, boolean blankAsPlus)URIAuthoritygetAuthority()Gets the authority.java.nio.charset.CharsetgetCharset()Gets the Charset.NameValuePairgetFirstQueryParam(java.lang.String name)Gets the firstNameValuePairfor a given name.java.lang.StringgetFragment()Gets the fragments.java.lang.StringgetHost()Gets the host portion of theURI.java.lang.StringgetPath()Gets the path.java.util.List<java.lang.String>getPathSegments()Gets the path segments.intgetPort()Gets the port.java.util.List<NameValuePair>getQueryParams()Gets the query parameters as a List.java.lang.StringgetScheme()Gets the scheme.java.lang.StringgetSchemeSpecificPart()Gets the scheme specific part.java.lang.StringgetUserInfo()Gets the user info.booleanisAbsolute()Tests whether the URI is absolute.booleanisOpaque()Tests whether the URI is opaque.booleanisPathEmpty()Tests whether the path is empty.booleanisQueryEmpty()Tests whether the query is empty.static URIBuilderlocalhost()Creates a new builder for the hostInetAddress.getLocalHost().static URIBuilderloopbackAddress()Creates a new builder for the hostInetAddress.getLoopbackAddress().URIBuildernormalizeSyntax()Deprecated.do not use this method.URIBuilderoptimize()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(package private) static java.util.List<java.lang.String>parsePath(java.lang.CharSequence s, java.nio.charset.Charset charset)(package private) static java.util.List<NameValuePair>parseQuery(java.lang.CharSequence s, java.nio.charset.Charset charset, boolean plusAsBlank)URIBuilderremoveParameter(java.lang.String param)Removes parameter of URI query if set.URIBuilderremoveQuery()Removes URI query.URIBuildersetAuthority(NamedEndpoint authority)Sets the authority.URIBuildersetAuthority(URIAuthority authority)Sets the authority.URIBuildersetCharset(java.nio.charset.Charset charset)Sets the Charset.URIBuildersetCustomQuery(java.lang.String query)Sets custom URI query.URIBuildersetEncodingPolicy(URIBuilder.EncodingPolicy encodingPolicy)Sets the encoding policy for thisURIBuilder.URIBuildersetFragment(java.lang.String fragment)Sets URI fragment.URIBuildersetHost(java.lang.String host)Sets URI host.URIBuildersetHost(java.net.InetAddress host)Sets URI host.URIBuildersetHttpHost(HttpHost httpHost)Sets the scheme, host name, and port.URIBuildersetParameter(java.lang.String param, java.lang.String value)Sets parameter of URI query overriding existing value if set.URIBuildersetParameters(java.util.List<NameValuePair> nameValuePairs)Sets URI query parameters.URIBuildersetParameters(NameValuePair... nameValuePairs)Sets URI query parameters.URIBuildersetPath(java.lang.String path)Sets URI path.URIBuildersetPathSegments(java.lang.String... pathSegments)Sets URI path.URIBuildersetPathSegments(java.util.List<java.lang.String> pathSegments)Sets URI path.URIBuildersetPathSegmentsRootless(java.lang.String... pathSegments)Sets rootless URI path (the first segment does not start with a /).URIBuildersetPathSegmentsRootless(java.util.List<java.lang.String> pathSegments)Sets rootless URI path (the first segment does not start with a /).URIBuildersetPlusAsBlank(boolean plusAsBlank)Sets whether the plus sign ('+') should be interpreted as a blank space (' ') when parsing the query parameters of the URI.URIBuildersetPort(int port)Sets URI port.URIBuildersetScheme(java.lang.String scheme)Sets URI scheme.URIBuildersetSchemeSpecificPart(java.lang.String schemeSpecificPart)Sets the URI scheme specific part.URIBuildersetSchemeSpecificPart(java.lang.String schemeSpecificPart, java.util.List<NameValuePair> nvps)Sets the URI scheme specific part and append a list of NameValuePair to this part.URIBuildersetSchemeSpecificPart(java.lang.String schemeSpecificPart, NameValuePair... nvps)Sets the URI scheme specific part and append a variable arguments list of NameValuePair instance(s) to this part.URIBuildersetUserInfo(java.lang.String userInfo)Sets URI user info.URIBuildersetUserInfo(java.lang.String username, java.lang.String password)Deprecated.The use of clear-text passwords inURIs has been deprecated and is strongly discouraged.(package private) static java.util.List<java.lang.String>splitPath(java.lang.CharSequence s)java.lang.StringtoString()Converts this instance to a URI string.
-
-
-
Field Detail
-
scheme
private java.lang.String scheme
-
encodedSchemeSpecificPart
private java.lang.String encodedSchemeSpecificPart
-
encodedAuthority
private java.lang.String encodedAuthority
-
userInfo
private java.lang.String userInfo
-
encodedUserInfo
private java.lang.String encodedUserInfo
-
host
private java.lang.String host
-
port
private int port
-
encodedPath
private java.lang.String encodedPath
-
pathRootless
private boolean pathRootless
-
pathSegments
private java.util.List<java.lang.String> pathSegments
-
encodedQuery
private java.lang.String encodedQuery
-
queryParams
private java.util.List<NameValuePair> queryParams
-
query
private java.lang.String query
-
charset
private java.nio.charset.Charset charset
-
fragment
private java.lang.String fragment
-
encodedFragment
private java.lang.String encodedFragment
-
encodingPolicy
private URIBuilder.EncodingPolicy encodingPolicy
-
plusAsBlank
private boolean plusAsBlank
-
QUERY_PARAM_SEPARATOR
private static final char QUERY_PARAM_SEPARATOR
- See Also:
- Constant Field Values
-
PARAM_VALUE_SEPARATOR
private static final char PARAM_VALUE_SEPARATOR
- See Also:
- Constant Field Values
-
PATH_SEPARATOR
private static final char PATH_SEPARATOR
- See Also:
- Constant Field Values
-
QUERY_PARAM_SEPARATORS
private static final Tokenizer.Delimiter QUERY_PARAM_SEPARATORS
-
QUERY_VALUE_SEPARATORS
private static final Tokenizer.Delimiter QUERY_VALUE_SEPARATORS
-
-
Constructor Detail
-
URIBuilder
public URIBuilder()
Constructs an empty instance.
-
URIBuilder
public URIBuilder(java.lang.String uriString) throws java.net.URISyntaxExceptionConstructs an instance from the string which must be a valid URI.- Parameters:
uriString- a valid URI in string form.- Throws:
java.net.URISyntaxException- if the input is not a valid URI.
-
URIBuilder
public URIBuilder(java.net.URI uri)
Constructs an instance from the provided URI.- Parameters:
uri- a URI.
-
URIBuilder
public URIBuilder(java.lang.String uriString, java.nio.charset.Charset charset) throws java.net.URISyntaxExceptionConstructs an instance from the string which must be a valid URI.- Parameters:
uriString- a valid URI in string form.- Throws:
java.net.URISyntaxException- if the input is not a valid URI
-
URIBuilder
public URIBuilder(java.net.URI uri, java.nio.charset.Charset charset)Constructs an instance from the provided URI.- Parameters:
uri- a URI.
-
-
Method Detail
-
localhost
public static URIBuilder localhost() throws java.net.UnknownHostException
Creates a new builder for the hostInetAddress.getLocalHost().- Returns:
- a new builder.
- Throws:
java.net.UnknownHostException- if the local host name could not be resolved into an address.
-
loopbackAddress
public static URIBuilder loopbackAddress()
Creates a new builder for the hostInetAddress.getLoopbackAddress().
-
setAuthority
public URIBuilder setAuthority(NamedEndpoint authority)
Sets the authority.- Parameters:
authority- the authority.- Returns:
- this instance.
- Since:
- 5.2
-
setAuthority
public URIBuilder setAuthority(URIAuthority authority)
Sets the authority.- Parameters:
authority- the authority.- Returns:
- this instance.
- Since:
- 5.2
-
setCharset
public URIBuilder setCharset(java.nio.charset.Charset charset)
Sets the Charset.- Parameters:
charset- the Charset.- Returns:
- this instance.
-
setEncodingPolicy
public URIBuilder setEncodingPolicy(URIBuilder.EncodingPolicy encodingPolicy)
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
public URIAuthority getAuthority()
Gets the authority.- Returns:
- the authority.
- Since:
- 5.2
-
getCharset
public java.nio.charset.Charset getCharset()
Gets the Charset.- Returns:
- the Charset.
-
setPlusAsBlank
public URIBuilder setPlusAsBlank(boolean plusAsBlank)
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
static java.util.List<NameValuePair> parseQuery(java.lang.CharSequence s, java.nio.charset.Charset charset, boolean plusAsBlank)
-
splitPath
static java.util.List<java.lang.String> splitPath(java.lang.CharSequence s)
-
parsePath
static java.util.List<java.lang.String> parsePath(java.lang.CharSequence s, java.nio.charset.Charset charset)
-
formatPath
static void formatPath(java.lang.StringBuilder buf, java.lang.Iterable<java.lang.String> segments, boolean rootless, java.nio.charset.Charset charset, java.util.BitSet safechars)
-
formatPath
static void formatPath(java.lang.StringBuilder buf, java.lang.Iterable<java.lang.String> segments, boolean rootless, java.nio.charset.Charset charset)
-
formatQuery
static void formatQuery(java.lang.StringBuilder buf, java.lang.Iterable<? extends NameValuePair> params, java.nio.charset.Charset charset, java.util.BitSet safechars, boolean blankAsPlus)
-
formatQuery
static void formatQuery(java.lang.StringBuilder buf, java.lang.Iterable<? extends NameValuePair> params, java.nio.charset.Charset charset, boolean blankAsPlus)
-
build
public java.net.URI build() throws java.net.URISyntaxExceptionBuilds aURIinstance.- Throws:
java.net.URISyntaxException
-
buildString
private java.lang.String buildString()
-
digestURI
private void digestURI(java.net.URI uri, java.nio.charset.Charset charset)
-
setScheme
public URIBuilder setScheme(java.lang.String scheme)
Sets URI scheme.- Returns:
- this instance.
-
setSchemeSpecificPart
public URIBuilder setSchemeSpecificPart(java.lang.String schemeSpecificPart)
Sets the URI scheme specific part.- Parameters:
schemeSpecificPart-- Returns:
- this instance.
- Since:
- 5.1
-
setSchemeSpecificPart
public URIBuilder setSchemeSpecificPart(java.lang.String schemeSpecificPart, NameValuePair... nvps)
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
public URIBuilder setSchemeSpecificPart(java.lang.String schemeSpecificPart, java.util.List<NameValuePair> nvps)
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
public URIBuilder setUserInfo(java.lang.String userInfo)
Sets URI user info. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
setUserInfo
@Deprecated public URIBuilder setUserInfo(java.lang.String username, java.lang.String password)
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
public URIBuilder setHost(java.net.InetAddress host)
Sets URI host.- Returns:
- this instance.
-
setHost
public URIBuilder setHost(java.lang.String host)
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
public URIBuilder setHttpHost(HttpHost httpHost)
Sets the scheme, host name, and port.- Parameters:
httpHost- the scheme, host name, and port.- Returns:
- this instance.
-
setPort
public URIBuilder setPort(int port)
Sets URI port.- Returns:
- this instance.
-
setPath
public URIBuilder setPath(java.lang.String path)
Sets URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
appendPath
public URIBuilder appendPath(java.lang.String path)
Appends path to URI. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
setPathSegments
public URIBuilder setPathSegments(java.lang.String... pathSegments)
Sets URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
appendPathSegments
public URIBuilder appendPathSegments(java.lang.String... pathSegments)
Appends segments URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
setPathSegmentsRootless
public URIBuilder setPathSegmentsRootless(java.lang.String... pathSegments)
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
public URIBuilder setPathSegments(java.util.List<java.lang.String> pathSegments)
Sets URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
appendPathSegments
public URIBuilder appendPathSegments(java.util.List<java.lang.String> pathSegments)
Appends segments to URI path. The value is expected to be unescaped and may contain non ASCII characters.- Returns:
- this instance.
-
setPathSegmentsRootless
public URIBuilder setPathSegmentsRootless(java.util.List<java.lang.String> pathSegments)
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
public URIBuilder removeQuery()
Removes URI query.- Returns:
- this instance.
-
setParameters
public URIBuilder setParameters(java.util.List<NameValuePair> nameValuePairs)
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
public URIBuilder addParameters(java.util.List<NameValuePair> nameValuePairs)
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
public URIBuilder setParameters(NameValuePair... nameValuePairs)
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
public URIBuilder addParameter(java.lang.String param, java.lang.String value)
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
public URIBuilder addParameter(NameValuePair nameValuePair)
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
public URIBuilder removeParameter(java.lang.String param)
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
public URIBuilder setParameter(java.lang.String param, java.lang.String value)
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
public URIBuilder clearParameters()
Clears URI query parameters.- Returns:
- this instance.
-
setCustomQuery
public URIBuilder setCustomQuery(java.lang.String query)
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
public URIBuilder setFragment(java.lang.String fragment)
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
public java.lang.String getScheme()
Gets the scheme.- Returns:
- the scheme.
-
getSchemeSpecificPart
public java.lang.String getSchemeSpecificPart()
Gets the scheme specific part.- Returns:
- String
- Since:
- 5.1
-
getUserInfo
public java.lang.String getUserInfo()
Gets the user info.- Returns:
- the user info.
-
getHost
public java.lang.String 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
public java.util.List<java.lang.String> getPathSegments()
Gets the path segments.- Returns:
- the path segments.
-
getPath
public java.lang.String getPath()
Gets the path.- Returns:
- the path.
-
isQueryEmpty
public boolean isQueryEmpty()
Tests whether the query is empty.- Returns:
- whether the query is empty.
-
getQueryParams
public java.util.List<NameValuePair> getQueryParams()
Gets the query parameters as a List.- Returns:
- the query parameters as a List.
-
getFirstQueryParam
public NameValuePair getFirstQueryParam(java.lang.String name)
Gets the firstNameValuePairfor a given name.- Parameters:
name- the name- Returns:
- the first named
NameValuePairor null if not found. - Since:
- 5.2
-
getFragment
public java.lang.String getFragment()
Gets the fragments.- Returns:
- the fragments.
-
normalizeSyntax
@Deprecated public URIBuilder normalizeSyntax()
Deprecated.do not use this method.- See Also:
optimize()
-
optimize
public URIBuilder 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
public java.lang.String toString()
Converts this instance to a URI string.- Overrides:
toStringin classjava.lang.Object- Returns:
- this instance to a URI string.
-
-