session object
GatewayScript can access transaction information through session objects.
A session object represents a transaction flow in a GatewayScript application to access input, output, and other contexts that are visible to the service. The contexts in the session contain all the information that is associated with a transaction. The session object is attached to the Global object before the GatewayScript action begins execution. Because a session object is a property of the Global object, you can use session objects, through the session variable, without requiring a particular module (like service metadata).
A session object allows access to associated contexts through operations with
an invocation like
session.contextName.operation()
.
- contextName
- One of the predefined contexts or a named context.
- operation
- A method that is used with contexts; such as
readAsBuffer()
andwrite()
.
Use the session object to create and to access context objects and to access action or service parameters. The session object is unique for each individual GatewayScript session. The session object is also used to indicate that the processing action is in error at the completion of the script.
The session object is automatically available. You do not need to declare the
require('session')
statement. The transport headers are accessed through the
headers structure, which users access with a require('header-metadata')
statement.
You can use the session object to access contexts. In all method
specifications that use the context, when not specified, the context is assumed to be initialized.
For example, the context can be initialized by the createContext()
method or
created by a previous action in the processing rule. The variable context in these models can be
input
, INPUT
, or a named context, such as
name('myCtx')
. The exception is the write()
method, where the
context can be input
, output
, or a named context.
session.accept()
Overrides a previous reject decision.
- Syntax
session.accept()
- Guidelines
- The session.accept() API is used to override a previous reject decision. It does not take any argument.
session.accepting
Specifies whether a message is rejected or not.
- Syntax
session.accepting
- Guidelines
- The session.accepting property is read only and specifies whether a message is rejected or not. If the value is true, the message is not rejected. Otherwise, the message is rejected after the action completes.
session.createContext()
Creates a named context.
- Syntax
session.createContext(contextName)
- Parameters
-
- contextName
- The name of the context to create.
- Guidelines
- The session.createContext() method creates a named context and returns an object reference to the new context. An exception is raised if a context with the specified name exists.
session.INPUT - keyword
Returns the context that holds the body of the message as it was initially received.
- Syntax
session.INPUT
session.input - property
Returns the context that is specified for the Input property of the
GatewayScript processing action. In the GatewayScript assembly action,
session.input is set to null
.
- Syntax
session.input
session.name()
Returns a named context that accesses a context that is explicitly created by a GatewayScript or
other previously processed action. To access the context, you specify the context by referring to
the session
object. To create a named context, see the
createContext()
method.
- Syntax
session.name(contextName)
- Parameters
-
- contextName
- The name of the context. This parameter is a string.
session.output
Returns the context that is specified by the Output property of the GatewayScript processing action. In an API used in the GatewayScript assembly action, session.output is set to a temporary context. If the Output context is not empty at the end of the session, session.output is set to the payload of the message.
- Syntax
session.output
session.parameters.parameter
Gets or updates a parameter.
- Syntax
session.parameters.parameter
- Parameters
-
- parameter
- The name of a parameter.
- Guidelines
-
You can define the parameter in one of the manners.
- The GatewayScript processing action.
- The service that uses this action.
Updates are valid for only the current processing of the script and have no effect on this parameter when it is used on other actions.
- Example
- Set the value of the
myparam
parameter to 0.// Assign a default value to the parameter if not configured. // This default value is only valid for this script invocation. if(!session.parameters.myparam) session.parameters.myparam = 0;
session.reject()
Causes the processing rule to be in error after the action completes but the script does not stop immediately.
- Syntax
session.reject([[options],message])
- Parameters
-
- options
- A JSON object with the
override
property that uses a Boolean value, which specifies whether the rejection decision supersedes previous rejection decisions or not. For GatewayScript, the default istrue
. - message
- A message to write to the system log and to set in an error service variable for use during the processing of the error rule.
- Example
- Fail an action if variables are not
defined.
// Cause the processing rule to fail if a variable is not defined. if (!myVar) { session.reject('Variable myVar has no value.'); } else { // continue processing } if (!myVar2) { // The message will override previous one session.reject({'override': true}, 'Variable myVar2 has no value.'); } else { // continue processing }