public interface Externalizer
Allows creation of absolute URLs (i.e. including scheme and host) used
outside the context of HTML (where all URLs should be relative). A
typical example would be an RSS feed link:
http://server.com/feed.xml
. Since instances itself cannot know
their externally visible URL if they are running behind a web layer, and
sometimes those links have to be created outside of the request scope, this
service provides a central place to configure those external URLs and build
them.
There are the following options:
externalLink
,
publishLink
and
authorLink
methods; the
domain can be one of these:
absoluteLink
(not recommended for most use cases)relativeLink
(provided
for convenience, same as
ResourceResolver#map
)These elements are considered:
ResourceResolver
; also
includes namespace mangling (jcr:content
becomes
_jcr_content)
Can be retrieved as a normal OSGi service:
@Reference
Externalizer externalizer;
It can also be adapted from a ResourceResolver
:
Externalizer externalizer = resourceResolver.adaptTo(Externalizer.class);
Always pass a raw resource path. It might contain an extension, URL query and/or fragment part already, but it is best to add those after the call:
// public facing website externalizer.publishLink(resolver, "/my/page") + ".html"; // => "http://www.website.com/contextpath/my/page.html" externalizer.publishLink(resolver, "webcal", "/my/cal") + ".ics"; // => "webcal://www.website.com/contextpath/my/cal.ics" externalizer.publishLink(resolver, "/my/path?query=part#fragment"); // => "http://www.website.com/contextpath/my/path?query=part#fragment" // link to author externalizer.authorLink(resolver, "/my/page") + ".html"; // => "http://author.website.com/contextpath/my/page.html" // direct link to instance itself externalizer.externalLink(resolver, Externalizer.LOCAL, "/my/page") + ".html"; // => "http://publish-3.internal/contextpath/my/page.html" // custom configured domain externalizer.externalLink(resolver, "mydomain", "/my/page") + ".html"; // => "http://mydomain.com/my/page.html" // absolute link based on the request externalizer.absoluteLink(slingRequest, "http", "/my/path"); // => "http://host.com/contextpath/my/path" // relative links always require the request object externalizer.relativeLink(slingRequest, "/my/path"); // => "/contextpath/my/path"
For basic <a>
and <area>
links in HTML,
the CQ link checker will automatically run
ResourceResolver#map(request, path)
to handle mappings, the context path and
namespace mangling. The same applies to href
, src
and action
attributes on any HTML element. For those HTML
cases this utility should not be used, as no absolute links should be
created in the context of HTML. If relative URLs need to be written in places
that are not covered by the link checker, such as generated Javascript or
CSS, use
ResourceResolver#map(request, path)
manually (or
relativeLink
, which is
the same).
However, any link that was already sent through this utility should also go through untouched by the link checker an additional time (but only seen as valid if the resource exists).
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
AUTHOR
Name for domain configuration that contains the author DNS address.
|
static java.lang.String |
LOCAL
Name for domain configuration that contains the instance's local address.
|
static java.lang.String |
PUBLISH
Name for domain configuration that contains the public website DNS
address.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
absoluteLink(ResourceResolver resolver,
java.lang.String scheme,
java.lang.String path)
Deprecated.
since 5.5, use
externalLink(resolver, Externalizer.LOCAL, scheme, path)
instead |
java.lang.String |
absoluteLink(SlingHttpServletRequest request,
java.lang.String scheme,
java.lang.String path)
Externalizes the given resource path as an absolute URL based on the
request.
|
java.lang.String |
absoluteLink(java.lang.String scheme,
java.lang.String path)
Deprecated.
since 5.5, use
externalLink(null, Externalizer.LOCAL, scheme, path) instead |
java.lang.String |
authorLink(ResourceResolver resolver,
java.lang.String path)
Creates an absolute URL for the authoring system.
|
java.lang.String |
authorLink(ResourceResolver resolver,
java.lang.String scheme,
java.lang.String path)
Creates an absolute URL for the authoring system.
|
java.lang.String |
externalLink(ResourceResolver resolver,
java.lang.String domain,
java.lang.String path)
Creates an absolute URL for a named domain.
|
java.lang.String |
externalLink(ResourceResolver resolver,
java.lang.String domain,
java.lang.String scheme,
java.lang.String path)
Creates an absolute URL for a named domain.
|
java.lang.String |
publishLink(ResourceResolver resolver,
java.lang.String path)
Creates an absolute URL for the public website.
|
java.lang.String |
publishLink(ResourceResolver resolver,
java.lang.String scheme,
java.lang.String path)
Creates an absolute URL for the public website.
|
java.lang.String |
relativeLink(SlingHttpServletRequest request,
java.lang.String path)
Externalizes the given resource path relative to the URL of the
request.
|
static final java.lang.String LOCAL
http://author-1.internal:4502
or
http://publish-3.internal:4503
.static final java.lang.String AUTHOR
http://author.website.com
.static final java.lang.String PUBLISH
http://www.website.com
.java.lang.String externalLink(ResourceResolver resolver, java.lang.String domain, java.lang.String path)
Use the standard LOCAL
, PUBLISH
or AUTHOR
domains. Custom ones are also possible.
resolver
- a resource resolver for handling the sling mappings and
namespace mangling; can be null
domain
- name of the domain configuration to usepath
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
pathpublishLink(ResourceResolver, String)
,
authorLink(ResourceResolver, String)
java.lang.String externalLink(ResourceResolver resolver, java.lang.String domain, java.lang.String scheme, java.lang.String path)
Use the standard LOCAL
, PUBLISH
or AUTHOR
domains. Custom ones are also possible.
resolver
- a resource resolver for handling the sling mappings and
namespace mangling; can be null
domain
- name of the domain configuration to usescheme
- a protocol scheme such as "http", that will be part of the URLpath
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
pathpublishLink(ResourceResolver, String, String)
,
authorLink(ResourceResolver, String, String)
java.lang.String publishLink(ResourceResolver resolver, java.lang.String path)
Shortcut for externalLink(resolver, Externalizer.PUBLISH, path)
.
resolver
- a resource resolver for handling the sling mappings and
namespace mangling; can be null
path
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
pathexternalLink(ResourceResolver, String, String)
java.lang.String publishLink(ResourceResolver resolver, java.lang.String scheme, java.lang.String path)
Shortcut for externalLink(resolver, Externalizer.PUBLISH, scheme, path)
.
resolver
- a resource resolver for handling the sling mappings and
namespace mangling; can be null
scheme
- a protocol scheme such as "http", that will be part of the URLpath
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
pathexternalLink(ResourceResolver, String, String, String)
java.lang.String authorLink(ResourceResolver resolver, java.lang.String path)
Shortcut for externalLink(resolver, Externalizer.AUTHOR, path)
.
resolver
- a resource resolver for handling the sling mappings and
namespace mangling; can be null
path
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
pathexternalLink(ResourceResolver, String, String)
java.lang.String authorLink(ResourceResolver resolver, java.lang.String scheme, java.lang.String path)
Shortcut for externalLink(resolver, Externalizer.AUTHOR, scheme, path)
.
resolver
- a resource resolver for handling the sling mappings and
namespace mangling; can be null
scheme
- a protocol scheme such as "http", that will be part of the URLpath
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
pathexternalLink(ResourceResolver, String, String, String)
java.lang.String relativeLink(SlingHttpServletRequest request, java.lang.String path)
Note: This is exactly the same as
request.getResourceResolver().map(request, path)
.
Note that the result might be an absolute URL if the sling mappings define an explicit hostname and the current request's hostname is different.
request
- a sling http request object (required for context path and
sling resource resolver mapping)path
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
pathjava.lang.String absoluteLink(SlingHttpServletRequest request, java.lang.String scheme, java.lang.String path)
ServletRequest.getServerName()
and
ServletRequest.getServerPort()
, while the scheme is given as
argument.
Use with care, as this is request dependent: the host header might not be what is expected.
request
- a sling http request object (required for host, port, context
path and sling resource resolver mapping)scheme
- a protocol scheme such as "http", that will be part of the URLpath
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
path@Deprecated java.lang.String absoluteLink(ResourceResolver resolver, java.lang.String scheme, java.lang.String path)
externalLink(resolver, Externalizer.LOCAL, scheme, path)
insteadresolver
- a resource resolver for retrieving the sling mapping
configuration; can be null
to rely solely on this
service's configuration of host and context pathscheme
- a protocol scheme such as "http", that will be part of the URLpath
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
path@Deprecated java.lang.String absoluteLink(java.lang.String scheme, java.lang.String path)
externalLink(null, Externalizer.LOCAL, scheme, path)
insteadscheme
- a protocol scheme such as "http", that will be part of the URLpath
- a resource path; might contain extension, query or fragment,
but plain paths are recommended; has to be without context
pathCopyright © 2010 - 2020 Adobe. All Rights Reserved