Class LinkUtil


  • public class LinkUtil
    extends Object
    • Constructor Detail

      • LinkUtil

        public LinkUtil()
    • Method Detail

      • getUrl

        public static String getUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                    String url)
        Builds a mapped link to a path (resource path) without selectors and a determined extension.
        Parameters:
        request - the request context for path mapping (the result is always mapped)
        url - the URL to use (complete) or the path to an addressed resource (without any extension)
        Returns:
        the probably mapped (depends on the configuration) url for the referenced resource
      • getMappedUrl

        public static String getMappedUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                          String url)
        Builds a mapped link to a path (resource path) without selectors and a determined extension.
        Parameters:
        request - the request context for path mapping (the result is always mapped)
        url - the URL to use (complete) or the path to an addressed resource (without any extension)
        Returns:
        the mapped url for the referenced resource
      • getUnmappedUrl

        public static String getUnmappedUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                            String url)
        Builds a unmapped link to a path (resource path) without selectors and a determined extension.
        Parameters:
        request - the request context for path mapping (the result is always mapped)
        url - the URL to use (complete) or the path to an addressed resource (without any extension)
        Returns:
        the unmapped url for the referenced resource
      • getUrl

        public static String getUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                    String url,
                                    String extension)
        Builds a (mapped) link to a path (resource path) without selectors and with the given extension.
        Parameters:
        request - the request context for path mapping (the result is always mapped)
        url - the URL to use (complete) or the path to an addressed resource (without any extension)
        extension - the extension (can be 'null'; should be 'html or '.html' by default)
        Returns:
        the mapped url for the referenced resource
      • getUrl

        public static String getUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                    String url,
                                    String selectors,
                                    String extension)
        Builds a mapped link to the path (resource path) with optional selectors and extension.
        Parameters:
        request - the request context for path mapping (the result is always mapped)
        url - the URL to use (complete) or the path to an addressed resource (without any extension)
        selectors - an optional selector string with all necessary selectors (can be 'null')
        extension - an optional extension (can be 'null' for extension determination)
        Returns:
        the mapped url for the referenced resource
      • getUrl

        public static String getUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                    String url,
                                    String selectors,
                                    String extension,
                                    LinkMapper mapper)
        Builds a mapped link to the path (resource path) with optional selectors and extension.
        Parameters:
        request - the request context for path mapping (the result is always mapped)
        url - the URL to use (complete) or the path to an addressed resource (without any extension)
        selectors - an optional selector string with all necessary selectors (can be 'null')
        extension - an optional extension (can be 'null' for extension determination)
        mapper - the mapping strategy for the final link mapping
        Returns:
        the mapped url for the referenced resource
      • getAbsoluteUrl

        public static String getAbsoluteUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                            String url)
        Makes a URL already built external; the url should be built by the 'getUrl' method.
        Parameters:
        request - the request as the externalization context
        url - the url value (the local URL)
      • getAuthority

        public static String getAuthority​(org.apache.sling.api.SlingHttpServletRequest request)
        Builds the 'authority' part (host:port) of an absolute URL.
        Parameters:
        request - the current request with the 'host' and 'port' values
      • getDefaultPort

        public static int getDefaultPort​(org.apache.sling.api.SlingHttpServletRequest request)
      • isForwaredSSL

        public static boolean isForwaredSSL​(javax.servlet.http.HttpServletRequest request)
      • adjustMappedUrl

        protected static String adjustMappedUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                                String url)
        in the case of a forwarded SSL request the resource resolver mapping rules must contain the false port (80) to ensure a proper resolving - but in the result this bad port is included in the mapped URL and must be removed - done here
      • isExternalUrl

        public static boolean isExternalUrl​(String url)
        Returns 'true' if the url is an 'external' url (starts with 'https?://' or is a special URL)
      • resolveUrl

        public static org.apache.sling.api.resource.Resource resolveUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                                                        String url)
        Returns the resource referenced by an URL.
      • getFinalTarget

        public static String getFinalTarget​(org.apache.sling.api.resource.Resource resource)
                                     throws LinkUtil.RedirectLoopException
        Retrieves the target for a resource if there are redirects declared.
        Returns:
        the target path or url (can be external); 'null' if no redirect detected
        Throws:
        LinkUtil.RedirectLoopException - if a 'loop' has been detected during redirect resolving
      • getFinalTarget

        protected static String getFinalTarget​(ResourceHandle resource,
                                               List<String> trace)
                                        throws LinkUtil.RedirectLoopException
        Determines the 'final URL' of a link to a resource by traversing along the 'redirect' properties.
        Parameters:
        resource - the addressed resource
        trace - the list of paths traversed before (to detect loops in redirects)
        Returns:
        a 'final' path or URL; null if no different target found
        Throws:
        LinkUtil.RedirectLoopException - if a redirect loop has been detected
      • getExtension

        public static String getExtension​(ResourceHandle resource,
                                          String extension)
        Returns the extension for a URL to a resource based on a predefined value (can be null or ''). The result is always not 'null' and can be added without check; it starts with a '.' if not blank.
        Parameters:
        resource - the referenced resource
        extension - the predefined extension (can be 'null' or blank for determination)
        Returns:
        the string which has to add to the resources path; '' if nothing should add
      • getExtension

        @NotNull
        public static @NotNull String getExtension​(ResourceHandle resource,
                                                   String extension,
                                                   boolean detectMimeTypeExtension)
        Returns the extension for a URL to a resource based on a predefined value (can be null or ''). The result is always not 'null' and can be added without check; it starts with a '.' if not blank.
        Parameters:
        resource - the referenced resource
        extension - the predefined extension (can be 'null' or blank for determination)
        detectMimeTypeExtension - if 'true' an extension according to the mime type will be detected
        Returns:
        the string which has to add to the resources path; '' if nothing should add
      • encodeUrl

        public static String encodeUrl​(org.apache.sling.api.SlingHttpServletRequest request,
                                       String url)
        URL encoding for URL constructed form repository entities. E.g. when URL from user-input was saved into the repository, this will fix it up if it contains unquoted characters.
        Parameters:
        url - the url to encode
        Returns:
        the encoded URL
      • encodePath

        public static String encodePath​(String path)
        URL encoding for a resource path (without the encoding for the '/' path delimiters).
        Parameters:
        path - the path to encode
        Returns:
        the URL encoded path
      • decodePath

        public static String decodePath​(String path)
        URL-decode a path (same as decode(String) but also fixes the external naming /_jcr_ to /jcr:).
      • namespacePrefixEscape

        public static String namespacePrefixEscape​(String path)
        For Filename escaping: replace namespace prefix with _ quoting, e.g. jcr:content with _jcr_content in path.
      • namespacePrefixUnescape

        public static String namespacePrefixUnescape​(String path)
        Undo name space prefix replacement for filename escaping: replace e.g. _jcr_content with jcr:content in path.
      • encode

        public static String encode​(String value)
        URL encoding for a resource path (without the encoding for the '/' path delimiters).
        Parameters:
        component - the value to encode
        Returns:
        the URL encoded value
      • decodeInQuery

        public static String decodeInQuery​(String value)
        Decodes percent encodings in name or value in a query, as well as turns '+' into ' '.