Package com.composum.sling.core.util
Class LinkUtil
- java.lang.Object
-
- com.composum.sling.core.util.LinkUtil
-
public class LinkUtil extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLinkUtil.RedirectLoopException
-
Field Summary
Fields Modifier and Type Field Description protected static PatternESCAPED_PATHSEGMENTstatic StringEXT_HTMLstatic StringFORWARDED_PROTOstatic StringFORWARDED_PROTO_HTTPSstatic StringFORWARDED_SSL_HEADERstatic StringFORWARDED_SSL_ONstatic StringPROP_REDIRECTstatic StringPROP_TARGETstatic PatternSPECIAL_URL_PATTERNstatic StringSPECIAL_URL_STRINGprotected static PatternUNESCAPED_PATHSEGMENTstatic PatternURL_PATTERNstatic StringURL_PATTERN_STRING
-
Constructor Summary
Constructors Constructor Description LinkUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static StringadjustMappedUrl(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 herestatic Stringdecode(String value)Decodes percent encodings in a value.static StringdecodeInQuery(String value)Decodes percent encodings in name or value in a query, as well as turns '+' into ' '.static StringdecodePath(String path)URL-decode a path (same asdecode(String)but also fixes the external naming /_jcr_ to /jcr:).static Stringencode(String value)URL encoding for a resource path (without the encoding for the '/' path delimiters).static StringencodePath(String path)URL encoding for a resource path (without the encoding for the '/' path delimiters).static StringencodeUrl(org.apache.sling.api.SlingHttpServletRequest request, String url)URL encoding for URL constructed form repository entities.static StringgetAbsoluteUrl(org.apache.sling.api.SlingHttpServletRequest request, String url)Makes a URL already built external; the url should be built by the 'getUrl' method.static StringgetAuthority(org.apache.sling.api.SlingHttpServletRequest request)Builds the 'authority' part (host:port) of an absolute URL.static intgetDefaultPort(org.apache.sling.api.SlingHttpServletRequest request)static StringgetExtension(ResourceHandle resource, String extension)Returns the extension for a URL to a resource based on a predefined value (can be null or '').static @NotNull StringgetExtension(ResourceHandle resource, String extension, boolean detectMimeTypeExtension)Returns the extension for a URL to a resource based on a predefined value (can be null or '').protected static StringgetFinalTarget(ResourceHandle resource, List<String> trace)Determines the 'final URL' of a link to a resource by traversing along the 'redirect' properties.static StringgetFinalTarget(org.apache.sling.api.resource.Resource resource)Retrieves the target for a resource if there are redirects declared.static StringgetMappedUrl(org.apache.sling.api.SlingHttpServletRequest request, String url)Builds a mapped link to a path (resource path) without selectors and a determined extension.static StringgetUnmappedUrl(org.apache.sling.api.SlingHttpServletRequest request, String url)Builds a unmapped link to a path (resource path) without selectors and a determined extension.static StringgetUrl(org.apache.sling.api.SlingHttpServletRequest request, String url)Builds a mapped link to a path (resource path) without selectors and a determined extension.static StringgetUrl(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.static StringgetUrl(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.static StringgetUrl(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.static booleanisExternalUrl(String url)Returns 'true' if the url is an 'external' url (starts with 'https?://' or is a special URL)static booleanisForwaredSSL(javax.servlet.http.HttpServletRequest request)static StringnamespacePrefixEscape(String path)For Filename escaping: replace namespace prefix with _ quoting, e.g.static StringnamespacePrefixUnescape(String path)Undo name space prefix replacement for filename escaping: replace e.g.static org.apache.sling.api.resource.ResourceresolveUrl(org.apache.sling.api.SlingHttpServletRequest request, String url)Returns the resource referenced by an URL.
-
-
-
Field Detail
-
EXT_HTML
public static final String EXT_HTML
- See Also:
- Constant Field Values
-
PROP_TARGET
public static final String PROP_TARGET
- See Also:
- Constant Field Values
-
PROP_REDIRECT
public static final String PROP_REDIRECT
- See Also:
- Constant Field Values
-
FORWARDED_PROTO
public static final String FORWARDED_PROTO
- See Also:
- Constant Field Values
-
FORWARDED_PROTO_HTTPS
public static final String FORWARDED_PROTO_HTTPS
- See Also:
- Constant Field Values
-
FORWARDED_SSL_HEADER
public static final String FORWARDED_SSL_HEADER
- See Also:
- Constant Field Values
-
FORWARDED_SSL_ON
public static final String FORWARDED_SSL_ON
- See Also:
- Constant Field Values
-
URL_PATTERN_STRING
public static final String URL_PATTERN_STRING
- See Also:
- Constant Field Values
-
URL_PATTERN
public static final Pattern URL_PATTERN
-
SPECIAL_URL_STRING
public static final String SPECIAL_URL_STRING
- See Also:
- Constant Field Values
-
SPECIAL_URL_PATTERN
public static final Pattern SPECIAL_URL_PATTERN
-
UNESCAPED_PATHSEGMENT
protected static final Pattern UNESCAPED_PATHSEGMENT
-
ESCAPED_PATHSEGMENT
protected static final Pattern ESCAPED_PATHSEGMENT
-
-
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 contexturl- 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 resourcetrace- the list of paths traversed before (to detect loops in redirects)- Returns:
- a 'final' path or URL;
nullif 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 resourceextension- 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 resourceextension- 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 asdecode(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
-
decode
public static String decode(String value)
Decodes percent encodings in a value. Caution: for parameter values usedecodeInQuery(String)
-
-