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 class
LinkUtil.RedirectLoopException
-
Field Summary
Fields Modifier and Type Field Description protected static Pattern
ESCAPED_PATHSEGMENT
static String
EXT_HTML
static String
FORWARDED_PROTO
static String
FORWARDED_PROTO_HTTPS
static String
FORWARDED_SSL_HEADER
static String
FORWARDED_SSL_ON
static String
PROP_REDIRECT
static String
PROP_TARGET
static Pattern
SPECIAL_URL_PATTERN
static String
SPECIAL_URL_STRING
protected static Pattern
UNESCAPED_PATHSEGMENT
static Pattern
URL_PATTERN
static String
URL_PATTERN_STRING
-
Constructor Summary
Constructors Constructor Description LinkUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 herestatic String
decode(String value)
Decodes percent encodings in a value.static String
decodeInQuery(String value)
Decodes percent encodings in name or value in a query, as well as turns '+' into ' '.static String
decodePath(String path)
URL-decode a path (same asdecode(String)
but also fixes the external naming /_jcr_ to /jcr:).static String
encode(String value)
URL encoding for a resource path (without the encoding for the '/' path delimiters).static String
encodePath(String path)
URL encoding for a resource path (without the encoding for the '/' path delimiters).static String
encodeUrl(org.apache.sling.api.SlingHttpServletRequest request, String url)
URL encoding for URL constructed form repository entities.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.static String
getAuthority(org.apache.sling.api.SlingHttpServletRequest request)
Builds the 'authority' part (host:port) of an absolute URL.static int
getDefaultPort(org.apache.sling.api.SlingHttpServletRequest request)
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 '').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 '').protected static String
getFinalTarget(ResourceHandle resource, List<String> trace)
Determines the 'final URL' of a link to a resource by traversing along the 'redirect' properties.static String
getFinalTarget(org.apache.sling.api.resource.Resource resource)
Retrieves the target for a resource if there are redirects declared.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.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.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.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.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.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.static boolean
isExternalUrl(String url)
Returns 'true' if the url is an 'external' url (starts with 'https?://' or is a special URL)static boolean
isForwaredSSL(javax.servlet.http.HttpServletRequest request)
static String
namespacePrefixEscape(String path)
For Filename escaping: replace namespace prefix with _ quoting, e.g.static String
namespacePrefixUnescape(String path)
Undo name space prefix replacement for filename escaping: replace e.g.static org.apache.sling.api.resource.Resource
resolveUrl(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;
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 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)
-
-