The OpenIDM router service provides the uniform interface to all objects in OpenIDM: managed objects, system objects, configuration objects, and so on.
The router object as shown in conf/router.json
defines an array of filter objects.
{
"filters": [ filter object, ... ]
}The required filters array defines a list of filters to be processed on each router request. Filters are processed in the order in which they are specified in this array.
Filter objects are defined as follows.
{
"pattern": string,
"methods": [ string, ... ],
"condition": script object,
"onRequest": script object,
"onResponse": script object,
"onFailure": script object
}string, optional
Specifies a regular expression pattern matching the JSON pointer of
the object to trigger scripts. If not specified, all identifiers
(including null) match.
array of strings, optional
One or more methods for which the script(s) should be triggered.
Supported methods are: "create",
"read", "update",
"delete", "patch",
"query", "action". If not specified,
all methods are matched.
script object, optional
Specifies a script that is called first to determine if the script
should be triggered. If the condition yields "true",
the other script(s) are executed. If no condition is specified, the
script(s) are called unconditionally.
script object, optional
Specifies a script to execute before the request is dispatched to the resource. If the script throws an exception, the method is not performed, and a client error response is provided.
script object, optional
Specifies a script to execute after the request is successfully dispatched to the resource and a response is returned. Throwing an exception from this script does not undo the method already performed.
script object, optional
Specifies a script to execute if the request resulted in an exception being thrown. Throwing an exception from this script does not undo the method already performed.
All "onRequest" and "onResponse" scripts are executed in sequence. First, the "onRequest" scripts are executed from the top down, then the "onResponse" scripts are executed from the bottom up.
client -> filter 1 onRequest -> filter 2 onRequest -> resource
client <- filter 1 onResponse <- filter 2 onResponse <- resource
The following sample router.json file shows the
order in which the scripts would be executed:
{
"filters" : [
{
"onRequest" : {
"type" : "text/javascript",
"file" : "script/router-authz.js"
}
},
{
"pattern" : "^managed/user/.*",
"methods" : [
"read"
],
"onRequest" : {
"type" : "text/javascript",
"source" : "java.lang.System.out.println('requestFilter 1');"
}
},
{
"pattern" : "^managed/user/.*",
"methods" : [
"read"
],
"onResponse" : {
"type" : "text/javascript",
"source" : "java.lang.System.out.println('responseFilter 1');"
}
},
{
"pattern" : "^managed/user/.*",
"methods" : [
"read"
],
"onRequest" : {
"type" : "text/javascript",
"source" : "java.lang.System.out.println('requestFilter 2');"
}
},
{
"pattern" : "^managed/user/.*",
"methods" : [
"read"
],
"onResponse" : {
"type" : "text/javascript",
"source" : "java.lang.System.out.println('responseFilter 2');"
}
}
]
}
Will produce a log like:
requestFilter 1
requestFilter 2
responseFilter 2
responseFilter 1
Scripts are provided with the following scope.
{
"openidm": openidm-functions object,
"request": resource-request object,
"response": resource-response object,
"exception": exception object
}openidm-functions object
Provides access to OpenIDM resources.
resource-request object
The resource-request context, which has one or more parent contexts.
Provided in the scope of "condition",
"onRequest", "onResponse" and
"onFailure" scripts.
openidm-functions object
The response to the resource-request. Only provided in the scope of
the "onResponse" script.
exception object
The exception value that was thrown as a result of processing the
request. Only provided in the scope of the
"onFailure" script.
An exception object is defined as follows.
{
"error": integer,
"reason": string,
"message": string,
"detail": string
}integer
The numeric code of the exception.
string
The short reason phrase of the exception.
string
A brief message describing the exception.
(optional), string
A detailed description of the exception, in structured JSON format, suitable for programmatic evaluation.