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
-
public class SlingBindings extends LazyBindings
TheSlingBindingsclass 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>
-
-
Field Summary
Fields Modifier and Type Field Description static StringFLUSHThe 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 StringLOGThe name of the global scripting variable providing a logger which may be used for logging purposes (value is "log").static StringOUTThe name of the global scripting variable providing thejava.io.PrintWriterobject to return the response content (value is "out").static StringREADERThe name of the global scripting variable providing theReaderobject (value is "reader").static StringREQUESTThe name of the global scripting variable providing theSlingHttpServletRequestobject (value is "request").static StringRESOLVERThe name of the global scripting variable providing theResourceResolverobject (value is "resolver").static StringRESOURCEThe name of the global scripting variable providing theResourceobject (value is "resource").static StringRESPONSEThe name of the global scripting variable providing theSlingHttpServletResponseobject (value is "response").static StringSLINGThe name of the global scripting variable providing theSlingScriptHelperfor the request (value is "sling").
-
Constructor Summary
Constructors Constructor Description SlingBindings()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <ObjectType>
ObjectTypeget(String key, Class<ObjectType> type)Helper method to get an object with a given type from this map.booleangetFlush()@Nullable org.slf4j.LoggergetLog()@Nullable PrintWritergetOut()@Nullable ReadergetReader()@Nullable SlingHttpServletRequestgetRequest()@Nullable ResourcegetResource()@Nullable ResourceResolvergetResourceResolver()@Nullable SlingHttpServletResponsegetResponse()@Nullable SlingScriptHelpergetSling()protected voidsafePut(String key, Object value)Helper method which invokesHashMap.put(Object, Object)only if the value is not null.voidsetFlush(boolean flush)Sets theFLUSHproperty toflush.voidsetLog(org.slf4j.Logger log)voidsetOut(PrintWriter out)voidsetReader(Reader reader)voidsetRequest(SlingHttpServletRequest request)voidsetResource(Resource resource)voidsetResourceResolver(ResourceResolver resourceResolver)voidsetResponse(SlingHttpServletResponse response)voidsetSling(SlingScriptHelper sling)-
Methods inherited from class org.apache.sling.api.scripting.LazyBindings
clear, containsKey, entrySet, equals, get, getOrDefault, hashCode, isEmpty, keySet, put, putAll, remove, 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 Detail
-
REQUEST
public static final String REQUEST
The name of the global scripting variable providing theSlingHttpServletRequestobject (value is "request"). The value of the scripting variable is the same as that returned by theSlingScriptHelper.getRequest()method.This bound variable is required in the bindings given the script.
- See Also:
- Constant Field Values
-
RESPONSE
public static final String RESPONSE
The name of the global scripting variable providing theSlingHttpServletResponseobject (value is "response"). The value of the scripting variable is the same as that returned by theSlingScriptHelper.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 theReaderobject (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 theSlingScriptHelperfor 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
REQUESTandRESPONSEvariables. 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 theResourceobject (value is "resource"). The value of the scripting variable is the same as that returned by theSlingScriptHelper.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
ResourceResolverobject (value is "resolver"). The value of the scripting variable is the same as that returned by theSlingScriptHelper.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().getResourceResolvermethod. 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 thejava.io.PrintWriterobject to return the response content (value is "out"). The value of the scripting variable is the same as that returned by theSlingScriptHelper.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 theRESPONSEvariable. 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.Booleanindicating whether to flush the output (value isTRUE) or not (value isFALSE). If the variable has a non-nullvalue of another type, the output is not flush as if the value would beFALSE.- 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 SLF4Jorg.slf4j.Loggerinterface.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
-
-
Method Detail
-
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 objecttype- The object type- Returns:
- The searched object if it has the specified type, otherwise
nullis returned.
-
safePut
protected void safePut(String key, Object value)
Helper method which invokesHashMap.put(Object, Object)only if the value is not null.- Parameters:
key- The key of the objectvalue- The value
-
setFlush
public void setFlush(boolean flush)
Sets theFLUSHproperty toflush.- Parameters:
flush- Whether to flush or not
-
getFlush
public boolean getFlush()
- Returns:
trueif flush
-
setLog
public void setLog(org.slf4j.Logger log)
- Parameters:
log- The logger
-
getLog
@Nullable public @Nullable org.slf4j.Logger getLog()
- Returns:
- The logger or
null
-
setOut
public void setOut(PrintWriter out)
- Parameters:
out- The print writer
-
getOut
@Nullable public @Nullable PrintWriter getOut()
- Returns:
- The print writer or
null
-
setRequest
public void setRequest(SlingHttpServletRequest request)
- Parameters:
request- The request object.
-
getRequest
@Nullable public @Nullable SlingHttpServletRequest getRequest()
Returns theREQUESTproperty if notnulland aSlingHttpServletRequestinstance. Otherwisenullis returned.- Returns:
- The request object or
null
-
setReader
public void setReader(Reader reader)
- Parameters:
reader- The reader
-
getReader
@Nullable public @Nullable Reader getReader()
- Returns:
- The reader or
null.
-
setResource
public void setResource(Resource resource)
- Parameters:
resource- The resource
-
getResource
@Nullable public @Nullable Resource getResource()
- Returns:
- The resource or
null.
-
setResourceResolver
public void setResourceResolver(ResourceResolver resourceResolver)
- Parameters:
resourceResolver- the Resource Resolver
-
getResourceResolver
@Nullable public @Nullable ResourceResolver getResourceResolver()
Returns theRESOLVERproperty if notnulland aResourceResolverinstance. Otherwisenullis returned.- Returns:
- the bound
ResourceResolverif one exists,nullotherwise
-
setResponse
public void setResponse(SlingHttpServletResponse response)
- Parameters:
response- The response
-
getResponse
@Nullable public @Nullable SlingHttpServletResponse getResponse()
Returns theRESPONSEproperty if notnulland aSlingHttpServletResponseinstance. Otherwisenullis returned.- Returns:
- The response or
null.
-
setSling
public void setSling(SlingScriptHelper sling)
- Parameters:
sling- The script helper
-
getSling
@Nullable public @Nullable SlingScriptHelper getSling()
Returns theSLINGproperty if notnulland aSlingScriptHelperinstance. Otherwisenullis returned.- Returns:
- The script helper or
null.
-
-