Interface UrlHandler


  • @ProviderType
    public interface UrlHandler
    Rewrites and builds URLs for links to content pages and resources.

    The interface is implemented by a Sling Model. You can adapt from SlingHttpServletRequest or Resource to get a context-specific handler instance.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static @NotNull java.lang.String SELECTOR_SUFFIX
      Selector that is always added if a Sling-URL contains a suffix (to avoid files and directories with same name in dispatcher cache)
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.lang.String applySiteUrlAutoDetection​(@Nullable java.lang.String siteUrl)
      Applies auto-detection of Site URL (author or publish instance) for given Site URL that is configured in SiteConfig.
      @NotNull UrlBuilder get​(@NotNull com.day.cq.wcm.api.Page page)
      Builds and optionally externalizes an URL using a builder pattern.
      @NotNull UrlBuilder get​(@NotNull java.lang.String path)
      Builds and optionally externalizes an URL using a builder pattern.
      @NotNull UrlBuilder get​(@NotNull org.apache.sling.api.resource.Resource resource)
      Builds and optionally externalizes an URL using a builder pattern.
      boolean isExternalized​(@NotNull java.lang.String url)
      Checks if the given URL is externalized.
      @Nullable java.lang.String rewritePathToContext​(@NotNull org.apache.sling.api.resource.Resource resource)
      Rewrites given path to current site or context.
      @Nullable java.lang.String rewritePathToContext​(@NotNull org.apache.sling.api.resource.Resource resource, @NotNull org.apache.sling.api.resource.Resource contextResource)
      Rewrites given path to given site or context.
    • Field Detail

      • SELECTOR_SUFFIX

        @NotNull
        static final @NotNull java.lang.String SELECTOR_SUFFIX
        Selector that is always added if a Sling-URL contains a suffix (to avoid files and directories with same name in dispatcher cache)
        See Also:
        Constant Field Values
    • Method Detail

      • get

        @NotNull
        @NotNull UrlBuilder get​(@NotNull
                                @NotNull java.lang.String path)
        Builds and optionally externalizes an URL using a builder pattern.
        Parameters:
        path - Path to start URL building with
        Returns:
        URL builder which allows to chain further optional parameters before building the URL string.
      • get

        @NotNull
        @NotNull UrlBuilder get​(@NotNull
                                @NotNull org.apache.sling.api.resource.Resource resource)
        Builds and optionally externalizes an URL using a builder pattern.
        Parameters:
        resource - Resource, URL building is started with its path
        Returns:
        URL builder which allows to chain further optional parameters before building the URL string.
      • get

        @NotNull
        @NotNull UrlBuilder get​(@NotNull
                                @NotNull com.day.cq.wcm.api.Page page)
        Builds and optionally externalizes an URL using a builder pattern.
        Parameters:
        page - Page Page, URL building is started with its path
        Returns:
        URL builder which allows to chain further optional parameters before building the URL string.
      • rewritePathToContext

        @Nullable
        @Nullable java.lang.String rewritePathToContext​(@NotNull
                                                        @NotNull org.apache.sling.api.resource.Resource resource)
        Rewrites given path to current site or context. The site root path is replaced with the one from current site This is useful if a link to an internal page points to a page outside the site (e.g. because the page containing the link was copied from the other site or inherited). When the AEM built-in rewrite logic was not applied the link would be invalid. This methods rewrites the link path to the current site to try to resolve it there.
        Parameters:
        resource - Resource to rewrite path from
        Returns:
        Rewritten path or null if resource invalid
      • rewritePathToContext

        @Nullable
        @Nullable java.lang.String rewritePathToContext​(@NotNull
                                                        @NotNull org.apache.sling.api.resource.Resource resource,
                                                        @NotNull
                                                        @NotNull org.apache.sling.api.resource.Resource contextResource)
        Rewrites given path to given site or context. The site root path is replaced with the one from current site. This is useful if a link to an internal page points to a page outside the site (e.g. because the page containing the link was copied from the other site or inherited). When the AEM built-in rewrite logic was not applied the link would be invalid. This methods rewrites the link path to the current site to try to resolve it there.
        Parameters:
        resource - Resource to rewrite path from
        contextResource - Context resource to which the path should be rewritten to
        Returns:
        Rewritten path or null if resource or context resource is invalid
      • isExternalized

        boolean isExternalized​(@NotNull
                               @NotNull java.lang.String url)
        Checks if the given URL is externalized.

        An URL is treated as externalized if:

        • It starts with a protocol and a colon (e.g. http:, tel:, mailto:, javascript:)
        • It starts with // or #
        Parameters:
        url - URL
        Returns:
        true if the URL is externalized.
      • applySiteUrlAutoDetection

        java.lang.String applySiteUrlAutoDetection​(@Nullable
                                                   @Nullable java.lang.String siteUrl)
        Applies auto-detection of Site URL (author or publish instance) for given Site URL that is configured in SiteConfig.

        If this Site URL contains an <auto> placeholder the Site URL detection is enabled and the Site URL is replaced with the current hostname (if possible). Otherwise the remaining part of the Site URL string is returned as fallback.

        Site URL auto-detection does only work in context of a request - outside request context the placeholders is removed and the remaining string returned as fallback.

        Parameters:
        siteUrl - Site URL (author or publish) SiteConfig.
        Returns:
        Automatic detected Site URL or fallback