Package org.apache.sling.api.uri
Class SlingUriBuilder
java.lang.Object
org.apache.sling.api.uri.SlingUriBuilder
Builder for immutable
SlingUri
s.
Example:
SlingUri testUri = SlingUriBuilder.create() .setResourcePath("/test/to/path") .setSelectors(new String[] { "sel1", "sel2" }) .setExtension("html") .setSuffix("/suffix/path") .setQuery("par1=val1&par2=val2") .build();
- Since:
- 1.0.0 (Sling API Bundle 2.23.0)
-
Method Summary
Modifier and TypeMethodDescription@NotNull SlingUriBuilder
addQueryParameter
(@NotNull String parameterName, @NotNull String value) Add a query parameter to the query of the URI.@NotNull SlingUriBuilder
addSelector
(@NotNull String selector) Add a selector to the URI.@NotNull SlingUri
build()
Builds the immutable SlingUri from this builder.static @NotNull SlingUriBuilder
create()
Creates a builder without any URI parameters set.static @NotNull SlingUriBuilder
createFrom
(@NotNull URI uri, @Nullable ResourceResolver resourceResolver) Creates a builder from an arbitrary URI.static @NotNull SlingUriBuilder
createFrom
(@NotNull RequestPathInfo requestPathInfo) Creates a builder from a RequestPathInfo instance .static @NotNull SlingUriBuilder
createFrom
(@NotNull Resource resource) Creates a builder from a resource (only taking the resource path into account).static @NotNull SlingUriBuilder
createFrom
(@NotNull SlingHttpServletRequest request) Creates a builder from a request.static @NotNull SlingUriBuilder
createFrom
(@NotNull SlingUri slingUri) Creates a builder from another SlingUri (clone and modify use case).@Nullable String
Returns the extension.@Nullable String
Returns the fragment.@Nullable String
getHost()
Returns the host.@Nullable String
getPath()
Returns the joint path of resource path, selectors, extension and suffix.Returns the path parameters.int
getPort()
Returns the port.@Nullable String
getQuery()
Returns the query.@Nullable String
Returns the resource path.@Nullable String
Returns the scheme.@Nullable String
Returns the scheme-specific part of the URI, compare with Javadoc ofURI
.@NotNull String[]
Returns the selectors array.@Nullable String
Returns the selector string@Nullable String
Returns the suffix part of the URI@Nullable Resource
Returns the corresponding suffix resource or null if no resource resolver is available (depends on the create method used in SlingUriBuilder) the URI does not contain a suffix if the suffix resource could not be found@Nullable String
Returns the user info.boolean
Returns true the URI is an absolute URI.boolean
Returns true if the URI has an absolute path starting with a slash ('/').boolean
isOpaque()
Returns true for opaque URIs like e.g. mailto:jon@example.com.boolean
isPath()
Returns true the URI is either a relative or absolute path (this is the case if scheme and host is empty and the URI path is set)boolean
Returns true if the URI is a relative path (no scheme and path does not start with '/').static @NotNull SlingUriBuilder
parse
(@NotNull String uriStr, @Nullable ResourceResolver resourceResolver) Creates a builder from an arbitrary URI string.@NotNull SlingUriBuilder
Will rebase the URI based on the underlying resource structure.@NotNull SlingUriBuilder
Will remove scheme and authority (that is user info, host and port).@NotNull SlingUriBuilder
removeSelector
(@NotNull String selector) Remove a selector from the URI.@NotNull SlingUriBuilder
setExtension
(@Nullable String extension) Set the extension of the URI.@NotNull SlingUriBuilder
setFragment
(@Nullable String fragment) Set the fragment of the URI.@NotNull SlingUriBuilder
Set the host of the URI.@NotNull SlingUriBuilder
Set the path of the URI that contains a resource path and optionally path parameters, selectors, an extension and a suffix.@NotNull SlingUriBuilder
setPathParameter
(@NotNull String key, @NotNull String value) Set a path parameter to the URI.@NotNull SlingUriBuilder
setPathParameters
(@NotNull Map<String, String> pathParameters) Replaces all path parameters in the URI.@NotNull SlingUriBuilder
setPort
(int port) Set the port of the URI.@NotNull SlingUriBuilder
Set the query of the URI.@NotNull SlingUriBuilder
setQueryParameters
(@NotNull Map<String, String> queryParameters) Replace all query parameters of the URL.@NotNull SlingUriBuilder
setResourcePath
(@Nullable String resourcePath) Set the resource path of the URI.@NotNull SlingUriBuilder
setResourceResolver
(ResourceResolver resourceResolver) Sets the resource resolver (required forRequestPathInfo.getSuffixResource()
).@NotNull SlingUriBuilder
Set the scheme of the URI.@NotNull SlingUriBuilder
setSchemeSpecificPart
(@Nullable String schemeSpecificPart) Set the scheme specific part of the URI.@NotNull SlingUriBuilder
setSelectors
(@Nullable String[] selectors) Set the selectors of the URI.@NotNull SlingUriBuilder
Set the suffix of the URI.@NotNull SlingUriBuilder
setUserInfo
(@Nullable String userInfo) Set the user info of the URI.toString()
Builds the corresponding string URI for this builder.@NotNull SlingUriBuilder
useSchemeAndAuthority
(@NotNull URI uri) Will take over scheme and authority (user info, host and port) from provided URI.@NotNull SlingUriBuilder
useSchemeAndAuthority
(@NotNull SlingUri slingUri) Will take over scheme and authority (user info, host and port) from provided slingUri.
-
Method Details
-
create
Creates a builder without any URI parameters set.- Returns:
- a SlingUriBuilder
-
createFrom
Creates a builder from another SlingUri (clone and modify use case).- Parameters:
slingUri
- the Sling URI to clone- Returns:
- a SlingUriBuilder
-
createFrom
Creates a builder from a resource (only taking the resource path into account).- Parameters:
resource
- the resource to take the resource path from- Returns:
- a SlingUriBuilder
-
createFrom
@NotNull public static @NotNull SlingUriBuilder createFrom(@NotNull @NotNull RequestPathInfo requestPathInfo) Creates a builder from a RequestPathInfo instance .- Parameters:
requestPathInfo
- the request path info to take resource path, selectors, extension and suffix from.- Returns:
- a SlingUriBuilder
-
createFrom
@NotNull public static @NotNull SlingUriBuilder createFrom(@NotNull @NotNull SlingHttpServletRequest request) Creates a builder from a request.- Parameters:
request
- request to take the URI information from- Returns:
- a SlingUriBuilder
-
createFrom
@NotNull public static @NotNull SlingUriBuilder createFrom(@NotNull @NotNull URI uri, @Nullable @Nullable ResourceResolver resourceResolver) Creates a builder from an arbitrary URI.- Parameters:
uri
- the uri to transform to a SlingUriresourceResolver
- a resource resolver is needed to decide up to what part the path is the resource path (that decision is only possible by checking against the underlying repository). If null is passed in, the shortest viable resource path is used.- Returns:
- a SlingUriBuilder
-
parse
@NotNull public static @NotNull SlingUriBuilder parse(@NotNull @NotNull String uriStr, @Nullable @Nullable ResourceResolver resourceResolver) Creates a builder from an arbitrary URI string.- Parameters:
uriStr
- to uri string to parseresourceResolver
- a resource resolver is needed to decide up to what part the path is the resource path (that decision is only possible by checking against the underlying repository). If null is passed in, the shortest viable resource path is used.- Returns:
- a SlingUriBuilder
-
setUserInfo
Set the user info of the URI.- Parameters:
userInfo
- the user info- Returns:
- the builder for method chaining
-
setHost
Set the host of the URI.- Parameters:
host
- the host- Returns:
- the builder for method chaining
-
setPort
Set the port of the URI.- Parameters:
port
- the port- Returns:
- the builder for method chaining
-
setPath
Set the path of the URI that contains a resource path and optionally path parameters, selectors, an extension and a suffix. To remove an existing path set path tonull
.- Parameters:
path
- the path- Returns:
- the builder for method chaining
-
rebaseResourcePath
Will rebase the URI based on the underlying resource structure. Rebasing will potentially adjust theresourcePath
,selectors
,extension
andsuffix
in a way that the path resolves to an existing resource.A resource resolver is necessary for this operation, hence
setResourceResolver(ResourceResolver)
needs to be called beforerebaseResourcePath()
or a create method that implicitly sets this has to be used.- Returns:
- the builder for method chaining
- Throws:
IllegalStateException
- if no resource resolver is available
-
setResourcePath
Set the resource path of the URI.- Parameters:
resourcePath
- the resource path- Returns:
- the builder for method chaining
-
setSelectors
Set the selectors of the URI. Passing innull
has the same effect as passing in an empty array.- Parameters:
selectors
- the selectors- Returns:
- the builder for method chaining
-
addSelector
Add a selector to the URI.- Parameters:
selector
- the selector to add- Returns:
- the builder for method chaining
-
removeSelector
Remove a selector from the URI.- Parameters:
selector
- the selector to remove- Returns:
- the builder for method chaining
- Since:
- 1.3 (Sling API Bundle 2.25.0)
-
setExtension
Set the extension of the URI.- Parameters:
extension
- the extension- Returns:
- the builder for method chaining
-
setPathParameter
@NotNull public @NotNull SlingUriBuilder setPathParameter(@NotNull @NotNull String key, @NotNull @NotNull String value) Set a path parameter to the URI.- Parameters:
key
- the path parameter keyvalue
- the path parameter value- Returns:
- the builder for method chaining
-
setPathParameters
@NotNull public @NotNull SlingUriBuilder setPathParameters(@NotNull @NotNull Map<String, String> pathParameters) Replaces all path parameters in the URI.- Parameters:
pathParameters
- the path parameters- Returns:
- the builder for method chaining
-
setSuffix
Set the suffix of the URI.- Parameters:
suffix
- the suffix- Returns:
- the builder for method chaining
-
setQuery
Set the query of the URI.- Parameters:
query
- the query- Returns:
- the builder for method chaining
-
addQueryParameter
@NotNull public @NotNull SlingUriBuilder addQueryParameter(@NotNull @NotNull String parameterName, @NotNull @NotNull String value) Add a query parameter to the query of the URI. Key and value are URL-encoded before adding the parameter to the query string.- Parameters:
parameterName
- the parameter namevalue
- the parameter value- Returns:
- the builder for method chaining
-
setQueryParameters
@NotNull public @NotNull SlingUriBuilder setQueryParameters(@NotNull @NotNull Map<String, String> queryParameters) Replace all query parameters of the URL. Both keys and values are URL-encoded before adding them to the query string.
For adding multiple query parameters with the same name prefer to use
addQueryParameter(String, String)
.- Parameters:
queryParameters
- the map with the query parameters- Returns:
- the builder for method chaining
-
setFragment
Set the fragment of the URI.- Parameters:
fragment
- the fragment- Returns:
- the builder for method chaining
-
setScheme
Set the scheme of the URI.- Parameters:
scheme
- the scheme- Returns:
- the builder for method chaining
-
setSchemeSpecificPart
@NotNull public @NotNull SlingUriBuilder setSchemeSpecificPart(@Nullable @Nullable String schemeSpecificPart) Set the scheme specific part of the URI. Use this for e.g. mail:jon@example.com URIs.- Parameters:
schemeSpecificPart
- the scheme specific part- Returns:
- the builder for method chaining
-
removeSchemeAndAuthority
Will remove scheme and authority (that is user info, host and port).- Returns:
- the builder for method chaining
-
useSchemeAndAuthority
Will take over scheme and authority (user info, host and port) from provided slingUri.- Parameters:
slingUri
- the Sling URI- Returns:
- the builder for method chaining
-
getResourcePath
Returns the resource path.- Returns:
- returns the resource path or null if the URI does not contain a path.
-
getSelectorString
Returns the selector string- Returns:
- returns the selector string or null if the URI does not contain selector(s) (in line with
RequestPathInfo
)
-
getSelectors
Returns the selectors array.- Returns:
- the selectors array (empty if the URI does not contain selector(s), never null)
-
getExtension
Returns the extension.- Returns:
- the extension or null if the URI does not contain an extension
-
getPathParameters
Returns the path parameters.- Returns:
- the path parameters or an empty Map if the URI does not contain any
-
getSuffix
Returns the suffix part of the URI- Returns:
- the suffix string or null if the URI does not contain a suffix
-
getSuffixResource
Returns the corresponding suffix resource or null if- no resource resolver is available (depends on the create method used in SlingUriBuilder)
- the URI does not contain a suffix
- if the suffix resource could not be found
- Returns:
- the suffix resource if available or null
-
getPath
Returns the joint path of resource path, selectors, extension and suffix.- Returns:
- the path or null if no path is set
-
getSchemeSpecificPart
Returns the scheme-specific part of the URI, compare with Javadoc ofURI
.- Returns:
- scheme specific part of the URI
-
getQuery
Returns the query.- Returns:
- the query part of the URI or null if the URI does not contain a query
-
getFragment
Returns the fragment.- Returns:
- the fragment or null if the URI does not contain a fragment
-
getScheme
Returns the scheme.- Returns:
- the scheme or null if not set
-
getHost
Returns the host.- Returns:
- returns the host of the SlingUri or null if not set
-
getPort
public int getPort()Returns the port.- Returns:
- returns the port of the SlingUri or -1 if not set
-
getUserInfo
Returns the user info.- Returns:
- the user info of the SlingUri or null if not set
-
useSchemeAndAuthority
Will take over scheme and authority (user info, host and port) from provided URI.- Parameters:
uri
- the URI- Returns:
- the builder for method chaining
-
setResourceResolver
Sets the resource resolver (required forRequestPathInfo.getSuffixResource()
).- Parameters:
resourceResolver
- the resource resolver- Returns:
- the builder for method chaining
-
build
Builds the immutable SlingUri from this builder.- Returns:
- the builder for method chaining
-
toString
Builds the corresponding string URI for this builder. -
isPath
public boolean isPath()Returns true the URI is either a relative or absolute path (this is the case if scheme and host is empty and the URI path is set)- Returns:
- returns true for path URIs
-
isAbsolutePath
public boolean isAbsolutePath()Returns true if the URI has an absolute path starting with a slash ('/').- Returns:
- true if the URI is an absolute path
-
isRelativePath
public boolean isRelativePath()Returns true if the URI is a relative path (no scheme and path does not start with '/').- Returns:
- true if URI is a relative path
-
isAbsolute
public boolean isAbsolute()Returns true the URI is an absolute URI.- Returns:
- true if the URI is an absolute URI containing a scheme.
-
isOpaque
public boolean isOpaque()Returns true for opaque URIs like e.g. mailto:jon@example.com.- Returns:
- true if the URI is an opaque URI
-