Package org.apache.sling.api.scripting

# Class SlingBindings

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<String,Object>
org.apache.sling.api.scripting.LazyBindings
org.apache.sling.api.scripting.SlingBindings

All Implemented Interfaces:
  `Serializable`, `Cloneable`, `Map<String,Object>`, `Bindings`

public class SlingBindings extends LazyBindings
The `SlingBindings` class is used to prepare global variables for script execution. The constants in this class define names of variables which MUST or MAY be provided for the script execution. Other variables may be define as callers see fit.

See Also:
  - Serialized Form

## Nested Class Summary

## Nested classes/interfaces inherited from class org.apache.sling.api.scripting.LazyBindings

`LazyBindings.Supplier`

## Nested classes/interfaces inherited from class java.util.AbstractMap

`AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>`

## Nested classes/interfaces inherited from interface java.util.Map

`Map.Entry<K extends Object,V extends Object>`

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`FLUSH`
The name of the global scripting variable indicating whether the output used by the script should be flushed after the script evaluation ended normally (value is "flush").
`static final String`
`JAKARTA_REQUEST`
The name of the global scripting variable providing the `SlingJakartaHttpServletRequest` object (value is "request").
`static final String`
`JAKARTA_RESPONSE`
The name of the global scripting variable providing the `SlingJakartaHttpServletResponse` object (value is "response").
`static final String`
`LOG`
The name of the global scripting variable providing a logger which may be used for logging purposes (value is "log").
`static final String`
`OUT`
The name of the global scripting variable providing the `java.io.PrintWriter` object to return the response content (value is "out").
`static final String`
`READER`
The name of the global scripting variable providing the `Reader` object (value is "reader").
`static final String`
`REQUEST`
Deprecated.
Use `JAKARTA_REQUEST` instead.
`static final String`
`RESOLVER`
The name of the global scripting variable providing the `ResourceResolver` object (value is "resolver").
`static final String`
`RESOURCE`
The name of the global scripting variable providing the `Resource` object (value is "resource").
`static final String`
`RESPONSE`
Deprecated.
Use `JAKARTA_RESPONSE` instead.
`static final String`
`SLING`
The name of the global scripting variable providing the `SlingScriptHelper` for the request (value is "sling").

## Constructor Summary

Constructors
Constructor
Description
`SlingBindings()`

## Method Summary

Modifier and Type
Method
Description
`protected <ObjectType>
ObjectType`
`get(String key, Class<ObjectType> type)`
Helper method to get an object with a given type from this map.
`boolean`
`getFlush()`
Returns the `FLUSH` property if not `null` and a `boolean`.
`@Nullable SlingJakartaHttpServletRequest`
`getJakartaRequest()`
Returns the `JAKARTA_REQUEST` property if not `null` and a `SlingJakartaHttpServletRequest` instance.
`@Nullable SlingJakartaHttpServletResponse`
`getJakartaResponse()`
Returns the `JAKARTA_RESPONSE` property if not `null` and a `SlingJakartaHttpServletResponse` instance.
`@Nullable org.slf4j.Logger`
`getLog()`
Returns the `LOG` property if not `null` and a `org.slf4j.Logger` instance.
`@Nullable PrintWriter`
`getOut()`
Returns the `OUT` property if not `null` and a `PrintWriter` instance.
`@Nullable Reader`
`getReader()`
Returns the `READER` property if not `null` and a `Reader` instance.
`@Nullable SlingHttpServletRequest`
`getRequest()`
Deprecated.
Use `getJakartaRequest()` instead.
`@Nullable Resource`
`getResource()`
Returns the `RESOURCE` property if not `null` and a `Resource` instance.
`@Nullable ResourceResolver`
`getResourceResolver()`
Returns the `RESOLVER` property if not `null` and a `ResourceResolver` instance.
`@Nullable SlingHttpServletResponse`
`getResponse()`
Deprecated.
Use `getJakartaResponse()` instead.
`@Nullable SlingScriptHelper`
`getSling()`
Returns the `SLING` property if not `null` and a `SlingScriptHelper` instance.
`Object`
`put(String key, Object value)`
`Object`
`remove(Object key)`
`protected void`
`safePut(String key, Object value)`
Helper method which invokes `HashMap.put(Object, Object)` only if the value is not null.
`void`
`setFlush(boolean flush)`
Sets the `FLUSH` property to `flush`.
`void`
`setJakartaRequest(SlingJakartaHttpServletRequest request)`
Sets the `JAKARTA_REQUEST` property to `request` if not `null`.
`void`
`setJakartaResponse(SlingJakartaHttpServletResponse response)`
Sets the `JAKARTA_RESPONSE` property to `response` if not `null`.
`void`
`setLog(org.slf4j.Logger log)`
Sets the `LOG` property to `log` if not `null`.
`void`
`setOut(PrintWriter out)`
Sets the `OUT` property to `out` if not `null`.
`void`
`setReader(Reader reader)`
Sets the `READER` property to `reader` if not `null`.
`void`
`setRequest(SlingHttpServletRequest request)`
Deprecated.
Use `setJakartaRequest(SlingJakartaHttpServletRequest)` instead.
`void`
`setResource(Resource resource)`
Sets the `RESOURCE` property to `resource` if not `null`.
`void`
`setResourceResolver(ResourceResolver resourceResolver)`
Sets the `RESOLVER` property to the provided `resourceResolver` if not `null`.
`void`
`setResponse(SlingHttpServletResponse response)`
Deprecated.
Use `setJakartaResponse(SlingJakartaHttpServletResponse)` instead.
`void`
`setSling(SlingScriptHelper sling)`
Sets the `SLING` property to `sling` if not `null`.

### Methods inherited from class org.apache.sling.api.scripting.LazyBindings

`clear, containsKey, entrySet, equals, get, getOrDefault, hashCode, isEmpty, keySet, putAll, size, values`

### Methods inherited from class java.util.HashMap

`clone, compute, computeIfAbsent, computeIfPresent, containsValue, forEach, merge, putIfAbsent, remove, replace, replace, replaceAll`

### Methods inherited from class java.util.AbstractMap

`toString`

### Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface java.util.Map

`compute, computeIfAbsent, computeIfPresent, containsValue, forEach, merge, putIfAbsent, remove, replace, replace, replaceAll`

## Field Details

### JAKARTA_REQUEST

public static final String JAKARTA_REQUEST
The name of the global scripting variable providing the `SlingJakartaHttpServletRequest` object (value is "request"). The value of the scripting variable is the same as that returned by the `SlingScriptHelper.getJakartaRequest()` method.

This bound variable is required in the bindings given the script.

Since:
  2.6.0
See Also:
  - Constant Field Values

### JAKARTA_RESPONSE

public static final String JAKARTA_RESPONSE
The name of the global scripting variable providing the `SlingJakartaHttpServletResponse` object (value is "response"). The value of the scripting variable is the same as that returned by the `SlingScriptHelper.getJakartaResponse()` method.

This bound variable is required in the bindings given the script.

Since:
  2.6.0
See Also:
  - Constant Field Values

### REQUEST

@Deprecated public static final String REQUEST
Deprecated.
Use `JAKARTA_REQUEST` instead.
The name of the global scripting variable providing the `SlingHttpServletRequest` object (value is "request"). The value of the scripting variable is the same as that returned by the `SlingScriptHelper.getRequest()` method.

This bound variable is required in the bindings given the script.

See Also:
  - Constant Field Values

### RESPONSE

@Deprecated public static final String RESPONSE
Deprecated.
Use `JAKARTA_RESPONSE` instead.
The name of the global scripting variable providing the `SlingHttpServletResponse` object (value is "response"). The value of the scripting variable is the same as that returned by the `SlingScriptHelper.getResponse()` method.

This bound variable is required in the bindings given the script.

See Also:
  - Constant Field Values

### READER

public static final String READER
The name of the global scripting variable providing the `Reader` object (value is "reader").

This bound variable is required in the bindings given the script.

See Also:
  - Constant Field Values

### SLING

public static final String SLING
The name of the global scripting variable providing the `SlingScriptHelper` for the request (value is "sling").

This bound variable is optional. If existing, the script helper instance must be bound to the same request and response objects as bound with the `REQUEST` and `RESPONSE` variables. If this variable is not bound, the script implementation will create it before actually evaluating the script.

See Also:
  - Constant Field Values

### RESOURCE

public static final String RESOURCE
The name of the global scripting variable providing the `Resource` object (value is "resource"). The value of the scripting variable is the same as that returned by the `SlingScriptHelper.getRequest().getResource()` method.

This bound variable is optional. If existing, the resource must be bound to the same resource as returned by the `SlingHttpServletRequest.getResource()` method. If this variable is not bound, the script implementation will bind it before actually evaluating the script.

See Also:
  - Constant Field Values

### RESOLVER

public static final String RESOLVER

The name of the global scripting variable providing the `ResourceResolver` object (value is "resolver"). The value of the scripting variable is the same as that returned by the `SlingScriptHelper.getRequest().getResourceResolver()` method.

This bound variable is optional. If existing, the resource resolver must be bound to the same resolver as returned by the `SlingHttpServletRequest.getResource().getResourceResolver` method. If this variable is not bound, the script implementation will bind it before actually evaluating the script.

See Also:
  - Constant Field Values

### OUT

public static final String OUT
The name of the global scripting variable providing the `java.io.PrintWriter` object to return the response content (value is "out"). The value of the scripting variable is the same as that returned by the `SlingScriptHelper.getResponse().getWriter()` method.

Note, that it may be advisable to implement a lazy acquiring writer for the out variable to enable the script to write binary data to the response output stream instead of the writer.

This bound variable is optional. If existing, the resource must be bound to the same writer as returned by the `SlingHttpServletResponse.getWriter()` method of the response object bound to the `RESPONSE` variable. If this variable is not bound, the script implementation will bind it before actually evaluating the script.

See Also:
  - Constant Field Values

### FLUSH

public static final String FLUSH
The name of the global scripting variable indicating whether the output used by the script should be flushed after the script evaluation ended normally (value is "flush").

The type of this variable is `java.lang.Boolean` indicating whether to flush the output (value is `TRUE`) or not (value is `FALSE`). If the variable has a non-`null` value of another type, the output is not flush as if the value would be `FALSE`.

See Also:
  - Constant Field Values

### LOG

public static final String LOG
The name of the global scripting variable providing a logger which may be used for logging purposes (value is "log"). The logger provides the API defined by the SLF4J `org.slf4j.Logger` interface.

This bound variable is optional. If this variable is not bound, the script implementation will bind it before actually evaluating the script.

See Also:
  - Constant Field Values

## Constructor Details

### SlingBindings

public SlingBindings()

## Method Details

### get

protected <ObjectType> ObjectType get(String key, Class<ObjectType> type)
Helper method to get an object with a given type from this map.

Type Parameters:
  `ObjectType` - The object type
Parameters:
  `key` - The key for the object
  `type` - The object type
Returns:
  The searched object if it has the specified type, otherwise `null` is returned.

### put

public Object put(String key, Object value)

Specified by:
  `put` in interface `Bindings`
Specified by:
  `put` in interface `Map<String,Object>`
Overrides:
  `put` in class `LazyBindings`

### remove

public Object remove(Object key)

Specified by:
  `remove` in interface `Bindings`
Specified by:
  `remove` in interface `Map<String,Object>`
Overrides:
  `remove` in class `LazyBindings`

### safePut

protected void safePut(String key, Object value)
Helper method which invokes `HashMap.put(Object, Object)` only if the value is not null.

Parameters:
  `key` - The key of the object
  `value` - The value

### setFlush

public void setFlush(boolean flush)
Sets the `FLUSH` property to `flush`.

Parameters:
  `flush` - Whether to flush or not

### getFlush

public boolean getFlush()
Returns the `FLUSH` property if not `null` and a `boolean`. Otherwise `false` is returned.

Returns:
  `true` if flush

### setLog

public void setLog(org.slf4j.Logger log)
Sets the `LOG` property to `log` if not `null`.

Parameters:
  `log` - The logger

### getLog

@Nullable public @Nullable org.slf4j.Logger getLog()
Returns the `LOG` property if not `null` and a `org.slf4j.Logger` instance. Otherwise `null` is returned.

Returns:
  The logger or `null`

### setOut

public void setOut(PrintWriter out)
Sets the `OUT` property to `out` if not `null`.

Parameters:
  `out` - The print writer

### getOut

@Nullable public @Nullable PrintWriter getOut()
Returns the `OUT` property if not `null` and a `PrintWriter` instance. Otherwise `null` is returned.

Returns:
  The print writer or `null`

### setJakartaRequest

public void setJakartaRequest(SlingJakartaHttpServletRequest request)
Sets the `JAKARTA_REQUEST` property to `request` if not `null`.

Parameters:
  `request` - The request object.
Since:
  2.6.0

### getJakartaRequest

@Nullable public @Nullable SlingJakartaHttpServletRequest getJakartaRequest()
Returns the `JAKARTA_REQUEST` property if not `null` and a `SlingJakartaHttpServletRequest` instance. Otherwise `null` is returned.

Returns:
  The request object or `null`
Since:
  2.6.0

### setRequest

@Deprecated public void setRequest(SlingHttpServletRequest request)
Deprecated.
Use `setJakartaRequest(SlingJakartaHttpServletRequest)` instead.
Sets the `REQUEST` property to `request` if not `null`.

Parameters:
  `request` - The request object.

### getRequest

@Deprecated @Nullable public @Nullable SlingHttpServletRequest getRequest()
Deprecated.
Use `getJakartaRequest()` instead.
Returns the `REQUEST` property if not `null` and a `SlingHttpServletRequest` instance. Otherwise `null` is returned.

Returns:
  The request object or `null`

### setReader

public void setReader(Reader reader)
Sets the `READER` property to `reader` if not `null`.

Parameters:
  `reader` - The reader

### getReader

@Nullable public @Nullable Reader getReader()
Returns the `READER` property if not `null` and a `Reader` instance. Otherwise `null` is returned.

Returns:
  The reader or `null`.

### setResource

public void setResource(Resource resource)
Sets the `RESOURCE` property to `resource` if not `null`.

Parameters:
  `resource` - The resource

### getResource

@Nullable public @Nullable Resource getResource()
Returns the `RESOURCE` property if not `null` and a `Resource` instance. Otherwise `null` is returned.

Returns:
  The resource or `null`.

### setResourceResolver

public void setResourceResolver(ResourceResolver resourceResolver)
Sets the `RESOLVER` property to the provided `resourceResolver` if not `null`.

Parameters:
  `resourceResolver` - the Resource Resolver

### getResourceResolver

@Nullable public @Nullable ResourceResolver getResourceResolver()
Returns the `RESOLVER` property if not `null` and a `ResourceResolver` instance. Otherwise `null` is returned.

Returns:
  the bound `ResourceResolver` if one exists, `null` otherwise

### setJakartaResponse

public void setJakartaResponse(SlingJakartaHttpServletResponse response)
Sets the `JAKARTA_RESPONSE` property to `response` if not `null`.

Parameters:
  `response` - The response
Since:
  2.6.0

### getJakartaResponse

@Nullable public @Nullable SlingJakartaHttpServletResponse getJakartaResponse()
Returns the `JAKARTA_RESPONSE` property if not `null` and a `SlingJakartaHttpServletResponse` instance. Otherwise `null` is returned.

Returns:
  The response or `null`.
Since:
  2.6.0

### setResponse

@Deprecated public void setResponse(SlingHttpServletResponse response)
Deprecated.
Use `setJakartaResponse(SlingJakartaHttpServletResponse)` instead.
Sets the `RESPONSE` property to `response` if not `null`.

Parameters:
  `response` - The response

### getResponse

@Deprecated @Nullable public @Nullable SlingHttpServletResponse getResponse()
Deprecated.
Use `getJakartaResponse()` instead.
Returns the `RESPONSE` property if not `null` and a `SlingHttpServletResponse` instance. Otherwise `null` is returned.

Returns:
  The response or `null`.

### setSling

public void setSling(SlingScriptHelper sling)
Sets the `SLING` property to `sling` if not `null`.

Parameters:
  `sling` - The script helper

### getSling

@Nullable public @Nullable SlingScriptHelper getSling()
Returns the `SLING` property if not `null` and a `SlingScriptHelper` instance. Otherwise `null` is returned.

Returns:
  The script helper or `null`.