Portal library
Contents
API to access functions related to rendering.
Usage
Add the following to your build.gradle file:
dependencies {
include xplibs.portal
}
Add the import statement to your code:
import portalLib from '/lib/xp/portal';
You are now ready to use the API.
Functions
apiUrl
Generates a URL pointing to a Universal API. Picks the right URL form based on the calling context — a service-point under the active service when called from a site, webapp, or admin tool, and /api/ otherwise.
Parameters
apiUrl() takes a single ApiUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
api |
string |
Descriptor key in |
|
type |
string |
Optional. URL type. Either |
|
path |
string | string[] |
Optional. Path appended after the API segment. Accepts a string or a string array. |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
|
baseUrl |
string |
Optional. Override the resolved base URL. |
Returns
string : The generated URL.
Example
import {apiUrl} from '/lib/xp/portal';
const url = apiUrl({
api: 'com.example.myapp:myapi',
path: '/items',
params: {
id: '42'
}
});
assetUrl
assetUrl is deprecated and will be removed in future versions. lib-asset and lib-static are introduced as a replacement.
Generates URL to a static file.
Parameters
assetUrl() takes a single AssetUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
path |
string |
Path to the asset. |
|
application |
string |
Optional. Other application to reference to. Defaults to current application. |
|
type |
string |
Optional. URL type. Either |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
Example
import {assetUrl} from '/lib/xp/portal';
const url = assetUrl({
path: 'styles/main.css'
});
attachmentUrl
Generates URL to an attachment.
If name is provided, XP will try to find an attachment by the unique name and give an exception if it’s not found. If name is empty but label is provided, XP will try to find an attachment with the provided label (will return the first one if several were found, or give an exception if none were found). If both name and label are empty, XP will generate attachment url only for a binary content with an attachment.
Parameters
attachmentUrl() takes a single AttachmentUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
id |
string |
Optional. Id of the content with the attachment. |
|
path |
string |
Optional. Path to the content with the attachment. |
|
name |
string |
Optional. Unique name of the attachment. |
|
label |
string |
Optional. Label of the attachment. Default is |
|
download |
boolean |
Optional. Set to |
|
type |
string |
Optional. URL type. Either |
|
project |
string |
Optional. Name of the project. |
|
branch |
string |
Optional. Name of the branch. |
|
baseUrl |
string |
Optional. Custom baseUrl. |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
Example
import {attachmentUrl} from '/lib/xp/portal';
const url = attachmentUrl({
id: '1234',
download: true
});
baseUrl
Generates a base URL.
Parameters
baseUrl() takes a single BaseUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
type |
string |
Optional. URL type. Either |
|
id |
string |
Optional. ID of the content. |
|
path |
string |
Optional. Path to the content. |
|
project |
string |
Optional. Name of the project to use for resolving the URL. |
|
branch |
string |
Optional. Name of the branch to use for resolving the URL. |
Returns
string : The generated URL.
Example
import {baseUrl} from '/lib/xp/portal';
const url = baseUrl({
type: 'server',
path: '/path',
project: 'explicit-project',
branch: 'explicit-branch'
});
componentUrl
Generates URL to a page component.
Parameters
componentUrl() takes a single ComponentUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
id |
string |
Optional. Id to the page. |
|
path |
string |
Optional. Path to the page. |
|
component |
string |
Optional. Path to the component. If not set, the current path is set. |
|
type |
string |
Optional. URL type. Either |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
Example
import {componentUrl} from '/lib/xp/portal';
const url = componentUrl({
component: 'main/0'
});
csp
Returns the request-scoped enforced Content Security Policy builder for the current portal request. Its directives are composed into the Content-Security-Policy header emitted on the response. For the report-only companion, use cspReportOnly(). Takes no arguments.
One policy is shared for the whole request, so a page with its layouts, parts and any response processors all contribute to the same header. It is composed and emitted automatically when the response is flushed, so late contributions still land — you never set the header yourself.
A Content-Security-Policy header set directly on the response is folded into the policy (as if passed to resetTo()) rather than sent verbatim, so later contributions still apply on top; a Content-Security-Policy-Report-Only header is folded into the report-only policy the same way.
Setting the Content-Security-Policy header directly is a legacy path, kept only for compatibility with existing apps. New code should declare the policy through csp() (and cspReportOnly() for report-only) instead. |
Returns
object : (Csp) The request-scoped Content Security Policy; its directives are emitted as the response’s Content-Security-Policy header.
Example
import {csp, CspSource} from '/lib/xp/portal';
const policy = csp();
// Deny-all baseline: default-src, base-uri and frame-ancestors all 'none'
policy.strict();
// Open up only the directives this page needs (calls chain)
policy
.scriptSrc(CspSource.SELF, 'https://cdn.example.com')
.styleSrc(CspSource.SELF)
.imgSrc(CspSource.SELF, CspSource.DATA);
import {csp} from '/lib/xp/portal';
// Wires 'nonce-<value>' into script-src and returns the value to stamp on the tag
const nonce = csp().nonceScriptSrc();
const view = `<script nonce="${nonce}">window.app = {};</script>`;
cspReportOnly
Returns the request-scoped report-only Content Security Policy builder, emitted as the Content-Security-Policy-Report-Only header. Takes no arguments.
It is an independent companion to the enforced policy from csp() — the same Csp API, sharing the request nonce — and the two headers can coexist on one response, so you can enforce a settled policy while trialling a stricter one. While the report-only policy is left empty, no header is emitted.
Returns
object : (Csp) The request-scoped report-only Content Security Policy; its directives are emitted as the response’s Content-Security-Policy-Report-Only header.
Example
import {csp, cspReportOnly, CspSource} from '/lib/xp/portal';
// Enforced: still allow inline scripts for now
csp().scriptSrc(CspSource.SELF, CspSource.UNSAFE_INLINE);
// Report-only: trial a strict policy — violations are reported, not blocked
cspReportOnly().scriptSrc(CspSource.SELF);
getComponent
Returns the component in the current execution context. Meant to be called from a layout or part. Takes no arguments.
Returns
object : (Component) The current component, or null if there is no component in the current context.
Example
import {getComponent} from '/lib/xp/portal';
const result = getComponent();
log.info('Current component name = %s', result.name);
const expected = {
path: "/main/0",
type: "layout",
descriptor: "myapplication:mylayout",
config: {
a: "1"
},
regions: {
bottom: {
components: [
{
path: "/main/0/bottom/0",
type: "part",
descriptor: "myapplication:mypart",
config: {
a: "1"
}
}
],
name: "bottom"
}
}
};
getContent
Returns the content in the current execution context. Meant to be called from a page, layout, or part. Takes no arguments.
Returns
object : (Content) The current content, or null if there is no content in the current context.
Example
import {getContent} from '/lib/xp/portal';
const result = getContent();
log.info('Current content path = %s', result._path);
const expected = {
_id: "123456",
_name: "mycontent",
_path: "/a/b/mycontent",
creator: "user:system:admin",
modifier: "user:system:admin",
createdTime: "1970-01-01T00:00:00Z",
modifiedTime: "1970-01-01T00:00:00Z",
type: "base:unstructured",
displayName: "My Content",
hasChildren: false,
language: "en",
valid: false,
data: {
a: "1"
},
x: {},
page: {},
attachments: {},
publish: {}
};
getIdProviderKey
Returns the key of ID provider in the current execution context. Takes no arguments.
Returns
string|null : The current ID provider key, or null if no ID provider is bound to the current context.
Example
import {getIdProviderKey} from '/lib/xp/portal';
const idProviderKey = getIdProviderKey();
if (idProviderKey) {
log.info('Id provider key: %s', idProviderKey);
}
const expected = "myidprovider";
getMultipartForm
Returns a JSON containing multipart items. If the request is not multipart, an empty object is returned. Takes no arguments.
Returns
object : (MultipartForm) The multipart form items.
Example
import {getMultipartForm} from '/lib/xp/portal';
const result = getMultipartForm();
log.info('Multipart form %s', result);
const expected = {
item1: {
name: "item1",
fileName: "item1.jpg",
contentType: "image/png",
size: 10
},
item2: [
{
name: "item2",
fileName: "image1.png",
contentType: "image/png",
size: 123
},
{
name: "item2",
fileName: "image2.jpg",
contentType: "image/jpeg",
size: 456
}
]
};
getMultipartItem
Returns a JSON containing a named multipart item. If the item does not exist, it returns null.
Parameters
getMultipartItem() takes the name of the multipart item (string) and an optional zero-based index (number) — specify the index when several items share the same name.
Returns
object : (MultipartItem) The named multipart form item, or null if it does not exist.
Example
import {getMultipartItem} from '/lib/xp/portal';
const result = getMultipartItem('item1');
log.info('Multipart item %s', result);
const expected = {
name: "item1",
fileName: "item1.jpg",
contentType: "image/png",
size: 10
};
getMultipartStream
Returns a data stream for a named multipart item. If the item does not exist, it returns null.
Parameters
getMultipartStream() takes the name of the multipart item (string) and an optional zero-based index (number) — specify the index when several items share the same name.
Returns
object : (ByteSource) Stream of multipart item data, or null if the item does not exist.
Example
import {getMultipartStream} from '/lib/xp/portal';
const stream1 = getMultipartStream('item2');
const stream2 = getMultipartStream('item2', 1);
getMultipartText
Returns the multipart item data as text. If the item does not exist, it returns null.
Parameters
getMultipartText() takes the name of the multipart item (string) and an optional zero-based index (number) — specify the index when several items share the same name.
Returns
string|null : Text for multipart item data, or null if the item does not exist.
Example
import {getMultipartText} from '/lib/xp/portal';
const text = getMultipartText('item1');
getSite
Returns the parent site of the content in the current execution context. Meant to be called from a page, layout, or part. Takes no arguments.
Returns
object : (Site) The current site, or null if the content has no parent site.
Example
import {getSite} from '/lib/xp/portal';
const result = getSite();
log.info('Current site name = %s', result._name);
const expected = {
_id: "100123",
_name: "my-content",
_path: "/my-content",
type: "base:unstructured",
hasChildren: false,
valid: false,
data: {
siteConfig: {
applicationKey: "myapplication",
config: {
Field: 42
}
}
},
x: {},
page: {},
attachments: {},
publish: {}
};
getSiteConfig
Returns the configuration of the parent site for the content in the current execution context. Meant to be called from a page, layout, or part. Takes no arguments.
Returns
object|null : The site configuration for the current application, or null if the content has no parent site.
Example
import {getSiteConfig} from '/lib/xp/portal';
const result = getSiteConfig();
log.info('Field value for the current site config = %s', result.Field);
const expected = {
Field: 42
};
idProviderUrl
Generates URL to an ID provider.
Parameters
idProviderUrl() takes a single, optional IdProviderUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
idProvider |
string |
Optional. Key of an ID provider. If idProvider is not set, then the id provider corresponding to the current execution context will be used. |
|
type |
string |
Optional. URL type. Either |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
imagePlaceholder
Generates URL of an image placeholder with a specified size.
Parameters
imagePlaceholder() takes a single ImagePlaceholderParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
width |
number |
Optional. Width of the image in pixels. |
|
height |
number |
Optional. Height of the image in pixels. |
Returns
string : Placeholder image URL.
Example
import {imagePlaceholder} from '/lib/xp/portal';
const url = imagePlaceholder({
width: 32,
height: 24
});
const expected = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAYCAYAAACbU/80AAAAGUlEQVR42u3BAQEAAACCIP+vbkhAAQAA7wYMGAAB93LuRQAAAABJRU5ErkJggg==';
imageUrl
Generates URL to an image.
Parameters
imageUrl() takes a single ImageUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
id |
string |
Optional. ID of the image content. Either |
|
path |
string |
Optional. Path to the image. If |
|
scale |
string |
Options are |
|
quality |
number |
Optional. Quality for JPEG images, ranges from 0 (max compression) to 100 (min compression). Default is |
|
background |
string |
Optional. Background color. |
|
format |
string |
Optional. Format of the image. |
|
filter |
string |
Optional. A number of filters are available to alter the image appearance, for example, blur(3), grayscale(), rounded(5), etc. |
|
type |
string |
Optional. URL type. Either |
|
project |
string |
Optional. Name of the project. |
|
branch |
string |
Optional. Name of the branch. |
|
baseUrl |
string |
Optional. Custom baseUrl. |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
Example
import {imageUrl} from '/lib/xp/portal';
const url = imageUrl({
id: '1234',
scale: 'block(1024,768)',
filter: 'rounded(5);sharpen()'
});
loginUrl
Generates URL to the login endpoint of an ID provider.
Parameters
loginUrl() takes a single, optional LoginUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
idProvider |
string |
Optional. Id provider key. If idProvider is not set, then the id provider corresponding to the current execution context will be used. |
|
redirect |
string |
Optional. The URL to redirect to after the login. |
|
type |
string |
Optional. URL type. Either |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
logoutUrl
Generates URL to the logout endpoint of ID provider in the current context.
Parameters
logoutUrl() takes a single, optional LogoutUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
redirect |
string |
Optional. The URL to redirect to after the logout. |
|
type |
string |
Optional. URL type. Either |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
pageUrl
Generates URL to a content page.
Parameters
pageUrl() takes a single PageUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
id |
string |
Optional. Id of a content. If id is set, then path is not used. |
|
path |
string |
Optional. Path of a content. Relative paths are resolved based on the current context. |
|
type |
string |
Optional. URL type. Either |
|
project |
string |
Optional. Project of the context. |
|
branch |
string |
Optional. Branch of the project for context. |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
Example
import {pageUrl} from '/lib/xp/portal';
const url = pageUrl({
path: '/my/page',
params: {
a: 1,
b: [1, 2]
}
});
processHtml
Resolves internal links to images and internal content items contained in an HTML text and replaces them with correct URLs. It will also process embedded macros.
When outputting processed HTML in Thymeleaf, use attribute data-th-utext="${processedHtml}". |
Parameters
processHtml() takes a single ProcessHtmlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
value |
string |
Html value string to process. |
|
type |
string |
Optional. URL type. Either |
|
imageWidths |
number[] |
Optional. A comma-separated list of image widths. If this parameter is provided, all |
|
imageSizes |
string |
Optional. Specifies the width for an image depending on browser dimensions. The value has the following format: |
Returns
string : The processed HTML.
Example
import {processHtml} from '/lib/xp/portal';
const html = processHtml({
value: '<a href="content://123" target="">Content</a>' +
'<a href="media://inline/123" target="">Inline</a>' +
'<a href="media://download/123" target="">Download</a>' +
'<img src="image://123"/>',
imageWidths: [32, 480, 800]
});
sanitizeHtml
Sanitizes an HTML string by stripping all potentially unsafe tags and attributes.
| HTML sanitization can be used to protect against cross-site scripting (XSS) attacks by sanitizing any HTML code submitted by a user. |
Parameters
sanitizeHtml() takes the html string value to process.
Returns
string : The sanitized HTML.
Example
import {sanitizeHtml} from '/lib/xp/portal';
const unsafeHtml = '<p><a href="https://example.com/" onclick="stealCookies()">Link</a></p>' +
'<iframe src="javascript:alert(\'XSS\');"></iframe>';
const sanitizedHtml = sanitizeHtml(unsafeHtml);
const expected = '<p><a href="https://example.com/">Link</a></p>';
serviceUrl
HTTP services are deprecated in XP 8 in favor of Universal APIs. Use apiUrl for new code; serviceUrl() is retained for backward compatibility with legacy services in src/main/resources/services/. |
Generates URL to a service.
Parameters
serviceUrl() takes a single ServiceUrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
service |
string |
Name of the service. |
|
application |
string |
Optional. Other application to reference to. Default is current application. |
|
type |
string |
Optional. URL type. Either |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
Example
import {serviceUrl} from '/lib/xp/portal';
const url = serviceUrl({
service: 'myservice',
params: {
a: 1,
b: 2
}
});
url
Generates URL to a resource.
Parameters
url() takes a single UrlParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
path |
string | string[] |
Path to the resource. If a string is provided, it is treated as a full path. If an array of strings is provided, each element is treated as a path segment and will be joined using |
|
type |
string |
Optional. URL type. Either |
|
params |
object |
Optional. Custom query parameters to append to the URL. |
Returns
string : The generated URL.
Example
import {url as buildUrl} from '/lib/xp/portal';
const url = buildUrl({
path: '/site/master/mysite',
params: {
a: 1,
b: 2
}
});
Type Definitions
Site
A site is a specialized Content of type portal:site. In addition to the standard content properties, its data carries the site’s app configurations.
| Name | Type | Description |
|---|---|---|
|
description |
string |
Optional. Site description. |
|
siteConfig |
SiteConfig | SiteConfig[] |
Configuration for one or more applications enabled on the site. A single object when one application is configured, otherwise an array. |
SiteConfig
Configuration entry for a single application enabled on a site.
| Name | Type | Description |
|---|---|---|
|
applicationKey |
string |
Key of the application this configuration applies to. |
|
config |
object |
The application’s site configuration, shaped by its site descriptor form. |
MultipartForm
A JSON object keyed by item name, where each value is a MultipartItem or an array of them when several items share the same name.
MultipartItem
| Name | Type | Description |
|---|---|---|
|
name |
string |
Name of the multipart item. |
|
fileName |
string |
Original file name of the uploaded item. |
|
contentType |
string |
MIME content type of the item. |
|
size |
number |
Size of the item in bytes. |
Csp
A request-scoped Content Security Policy builder, obtained from csp() (enforced) or cspReportOnly() (report-only). Within each, repeated calls to the accessor hand back a fresh handle onto the same underlying policy, so every page, layout, part and processor contributes to the one emitted header. Method calls on a handle, by contrast, all return the same Csp instance (except the nonce getters and directive()), so calls chain.
Contributions merge by union: a directive’s source list is the union of every contributor’s sources, deduped and emitted in the order added. The builder does not arbitrate between sources that interact in the browser — for example 'unsafe-inline' alongside a 'nonce-…', a hash, or 'strict-dynamic', which the browser then ignores. It emits exactly what you declare.
Sources are validated: a value containing ; or whitespace (a policy-injection attempt) throws, and a hand-supplied 'nonce-…' source is rejected — only the nonce* methods generate the request nonce.
Source-list directives
Each unions the given sources — CspSource values or raw strings (hosts, schemes, paths, URLs) — into the named directive.
| Method | Returns | Description |
|---|---|---|
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
Document and navigation directives
| Method | Returns | Description |
|---|---|---|
|
|
|
Unions sources into |
|
|
|
Unions sources into |
|
|
|
Unions sources into |
Baseline and low-level building blocks
Prefer the typed methods above; reach for these for directives the typed API does not cover, to inspect the policy, or for whole-policy operations.
| Method | Returns | Description |
|---|---|---|
|
|
|
Seeds a deny-all baseline — |
|
|
|
Unions sources into any directive by name (deduped). With no sources, registers a valueless directive such as |
|
|
|
Parses a |
|
|
|
The sources currently declared for |
|
|
|
Replaces a directive’s source list with exactly these sources, overriding what other contributors set. Not frozen — a later |
|
|
|
Removes the named directives (for example to unset a valueless directive). With no argument, removes nothing. |
|
|
|
Replaces the whole policy with the directives parsed from a raw header string; later contributions still apply on top. Parsing is lenient, mirroring the browser — invalid tokens are skipped, |
Nonces and hashes
| Method | Returns | Description |
|---|---|---|
|
|
|
Wires the request nonce into |
|
|
|
Wires the same nonce into |
|
|
|
Wires the same nonce into |
|
|
|
Wires the same nonce into |
|
|
|
Unions a hash source into |
|
|
|
Unions a hash source into |
Sandbox and Trusted Types
| Method | Returns | Description |
|---|---|---|
|
|
|
Unions |
|
|
|
Registers |
|
|
|
Adds policy names and/or |
Reporting
| Method | Returns | Description |
|---|---|---|
|
|
|
Adds the |
CspSource
Common source-list keywords and schemes for a CSP source expression, per W3C CSP3. Keyword values are single-quoted; scheme values end in a colon. Pass these to any source-list directive method, alongside raw strings for hosts, paths and URLs.
| Name | Value | Description |
|---|---|---|
|
|
|
The page’s own origin. |
|
|
|
The empty set — matches nothing. |
|
|
|
Allows inline scripts and styles. The browser ignores it when a nonce or hash is also present. |
|
|
|
Allows |
|
|
|
Propagates trust from a nonced/hashed script to the scripts it loads. |
|
|
|
Allows specific inline event handlers and |
|
|
|
Allows WebAssembly compilation without allowing |
|
|
|
Asks the browser to include a sample of the offending content in violation reports. |
|
|
|
The |
|
|
|
The |
|
|
|
Matches any URL, except the |
SandboxFlag
Flags for the sandbox directive, passed to sandbox(). Tokens are emitted unquoted, as the CSP spec defines them. Each flag lifts one of the sandbox’s default restrictions.
| Name | Value | Description |
|---|---|---|
|
|
|
Lets the content run scripts. |
|
|
|
Lets the content keep its origin instead of being forced into an opaque one. |
|
|
|
Lets the content submit forms. |
|
|
|
Lets the content open popups. |
|
|
|
Lets the content open modal dialogs. |
|
|
|
Lets the content navigate the top-level browsing context. |
|
|
|
Lets the content start downloads. |
|
|
|
Lets the content use the Pointer Lock API. |
|
|
|
Lets the content start a presentation session. |
|
|
|
Lets the content lock the screen orientation. |
TrustedTypesKeyword
Special keywords for the trusted-types directive, passed to trustedTypes() alongside user-defined policy names.
| Name | Value | Description |
|---|---|---|
|
|
|
Allows creating more than one Trusted Types policy with the same name. |
|
|
|
Disallows creating any Trusted Types policy. |
|
|
|
Allows any policy name. |
CspHashSource
A hash source passed to shaScriptSrc() / shaStyleSrc(). Provide either inline content to digest, or a precomputed hash — the two are mutually exclusive.
| Name | Type | Description |
|---|---|---|
|
content |
string |
Optional. Inline script or style text to digest. The computed |
|
hash |
string |
Optional. Precomputed base64 digest, emitted verbatim as |
|
algo |
string |
Optional. Hash algorithm — one of |