Class SlingUriBuilder


  • @ProviderType
    public class SlingUriBuilder
    extends Object
    Builder for SlingUris.

    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 Detail

      • create

        @NotNull
        public static @NotNull SlingUriBuilder create()
        Creates a builder without any URI parameters set.
        Returns:
        a SlingUriBuilder
      • createFrom

        @NotNull
        public static @NotNull SlingUriBuilder createFrom​(@NotNull
                                                          @NotNull SlingUri slingUri)
        Creates a builder from another SlingUri (clone and modify use case).
        Parameters:
        slingUri - the Sling URI to clone
        Returns:
        a SlingUriBuilder
      • createFrom

        @NotNull
        public static @NotNull SlingUriBuilder createFrom​(@NotNull
                                                          @NotNull Resource resource)
        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 SlingUri
        resourceResolver - 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 parse
        resourceResolver - 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

        @NotNull
        public @NotNull SlingUriBuilder setUserInfo​(@Nullable
                                                    @Nullable String userInfo)
        Set the user info of the URI.
        Parameters:
        userInfo - the user info
        Returns:
        the builder for method chaining
      • setHost

        @NotNull
        public @NotNull SlingUriBuilder setHost​(@Nullable
                                                @Nullable String host)
        Set the host of the URI.
        Parameters:
        host - the host
        Returns:
        the builder for method chaining
      • setPort

        @NotNull
        public @NotNull SlingUriBuilder setPort​(int port)
        Set the port of the URI.
        Parameters:
        port - the port
        Returns:
        the builder for method chaining
      • setPath

        @NotNull
        public @NotNull SlingUriBuilder setPath​(@Nullable
                                                @Nullable String path)
        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 to null.
        Parameters:
        path - the path
        Returns:
        the builder for method chaining
      • rebaseResourcePath

        @NotNull
        public @NotNull SlingUriBuilder rebaseResourcePath()
        Will rebase the uri based on the underlying resource structure. A resource resolver is necessary for this operation, hence setResourceResolver() needs to be called before balanceResourcePath() 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

        @NotNull
        public @NotNull SlingUriBuilder setResourcePath​(@Nullable
                                                        @Nullable String resourcePath)
        Set the resource path of the URI.
        Parameters:
        resourcePath - the resource path
        Returns:
        the builder for method chaining
      • setSelectors

        @NotNull
        public @NotNull SlingUriBuilder setSelectors​(@NotNull
                                                     @NotNull String[] selectors)
        Set the selectors of the URI.
        Parameters:
        selectors - the selectors
        Returns:
        the builder for method chaining
      • addSelector

        @NotNull
        public @NotNull SlingUriBuilder addSelector​(@NotNull
                                                    @NotNull String selector)
        Add a selector to the URI.
        Parameters:
        selector - the selector to add
        Returns:
        the builder for method chaining
      • setExtension

        @NotNull
        public @NotNull SlingUriBuilder setExtension​(@Nullable
                                                     @Nullable String extension)
        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 key
        value - 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

        @NotNull
        public @NotNull SlingUriBuilder setSuffix​(@Nullable
                                                  @Nullable String suffix)
        Set the suffix of the URI.
        Parameters:
        suffix - the suffix
        Returns:
        the builder for method chaining
      • setQuery

        @NotNull
        public @NotNull SlingUriBuilder setQuery​(@Nullable
                                                 @Nullable String query)
        Set the query of the URI.
        Parameters:
        query - the query
        Returns:
        the builder for method chaining
      • setFragment

        @NotNull
        public @NotNull SlingUriBuilder setFragment​(@Nullable
                                                    @Nullable String fragment)
        Set the fragment of the URI.
        Parameters:
        fragment - the fragment
        Returns:
        the builder for method chaining
      • setScheme

        @NotNull
        public @NotNull SlingUriBuilder setScheme​(@Nullable
                                                  @Nullable String scheme)
        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

        @NotNull
        public @NotNull SlingUriBuilder removeSchemeAndAuthority()
        Will remove scheme and authority (that is user info, host and port).
        Returns:
        the builder for method chaining
      • useSchemeAndAuthority

        @NotNull
        public @NotNull SlingUriBuilder useSchemeAndAuthority​(@NotNull
                                                              @NotNull SlingUri slingUri)
        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
      • useSchemeAndAuthority

        @NotNull
        public @NotNull SlingUriBuilder useSchemeAndAuthority​(@NotNull
                                                              @NotNull URI uri)
        Will take over scheme and authority (user info, host and port) from provided URI.
        Parameters:
        uri - the URI
        Returns:
        the builder for method chaining
      • build

        @NotNull
        public @NotNull SlingUri build()
        Builds the immutable SlingUri from this builder.
        Returns:
        the builder for method chaining
      • toString

        public String toString()
        Builds the corresponding string URI for this builder.
        Overrides:
        toString in class Object
        Returns:
        string representation of 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