Rules as Pre and Post Processors
Quantum Fabric provides the ability to write custom code as pre and post processors to modify request and response parameters to service calls. Historically custom code can be implemented either in Java or JavaScript. To simplify implementing custom logic in pre and post processors, a new Rules option is introduced from Quantum Fabric V8 SP4. You can use the Rules option to define your custom logic as a set of rules. This option makes defining pre and post processor custom logic closer to human language and is built using MVEL.
NOTE: The Rules functionality is supported only for the pre and post processor of integration services.
- Why Use Rules:
- Quick and easy to write custom logic.
- Easy to understand and change as per your business logic.
- No maintenance overhead. For example, No need to maintain separate jars or library and export them to various environments while publishing the updated app.
- What is the Structure of rules: Rules have a structure in the form of statements, as shown in the following table:
Sample Rules Structure name: "<Name of the rule>" description: "<Description of the rule>" priority: <Priority of the rule> condition: "<Condition to evaluate>" actions: - "<Set of actions to execute>"
Description of Rules Structure - Name: A unique name of the rule. This is a mandatory field.
- Description: A description for the rule.
- Priority: An integer value that represents the order to execute the rule. The bigger the value, the higher the priority.
- Condition: An expression that is evaluated by the Rules engine. When the condition evaluates to True, the engine executes a set of actions. This is a mandatory field.
For example,response != null
can be used to check whether the back-end response is empty. - Action: A set of statements that are executed when the condition evaluates to True. This is a mandatory field.
For example,statusCode = 200
sets status code to 200.
- How to Write rules:
- When you are configuring an operation, in the Advanced > Custom Code Invocation for pre-processor or post-processor, click the Rules option.
Built-in Objects
The following objects help you to write rules in Quantum Fabric.
Objects | Description |
---|---|
"configurationParameters" |
Used to access the Server and Client App parameters that are set by the developer in the App Services console. This is equivalent to using ConfigurableParameters in Java. |
"continueExecution" |
Used in the Pre-processor to terminate back-end calls. Set the value to false if you want to terminate the back-end call. By default, the value is set to true. |
"customMetrics" |
This is used to access custom metrics. For more details to create custom reports and Metrics, refer Custom Reporting – Metrics, Reports, and Dashboard Guide |
“deviceHeadersMap” |
Used to set headers that are passed to the client and is equivalent to using setDeviceHeaders in Java. |
"headerMap" |
Used to access the header map of a request. A client can directly access the header map or the individual key-value pairs of the header map. |
“identityHandler" |
Used to access the identity attributes when a service is protected by an identity service. |
"inputMap" |
Used to access the input map of a request. A client can directly access the input map or the individual key-value pairs of the input map. |
"logger" | Used to add a log statement with the appropriate level. |
"response" |
Used to modify the response body and is equivalent to using setResponse in Java. |
"results" |
Used to modify the results. A Result is an abstraction of a back-end response. The Result is a collection of Params, Data-sets, and Records. For more details, refer Result. |
"resultCache" |
Used to access the cache in pre-processor and post-processor. This is equivalent to using ResultCache in Java. |
"servicesManager" |
Used to invoke an integration service with the specified service id, operation id and version. |
"session" |
Used to modify the session that is associated with the request. For more details, refer Session A client can access values from the session and the individual attributes of the session. |
“statusCode” | Used to set the status code of the response and is equivalent to using setStatusCode in Java. |
"ua" |
Used to access the User Agent Header of the request. |
Built-in Functions
The following functions help you to write rules in Quantum Fabric.
Functions | Description |
---|---|
"Check.isWithin" |
Checks if an element is in a specified range. It will return true if the element present in the specified range, otherwise false.
|
"Check.isEmpty" |
Checks if a CharSequence is empty ("") or null.
|
"Check.isNotEmpty" |
Checks if a CharSequence is not empty ("") and not null.
|
"Check.isBlank" |
Checks if a CharSequence is empty (""), null or white-space only.
|
"Check.isNotBlank" |
Checks if a CharSequence is not empty (""), not null and not white-space only.
|
"Check.isEqualTo" |
Compares two CharSequences, returning true if they represent equal sequences of characters.
|
"Check.isEqualToIgnoringCase" |
Compares two CharSequences, returning true if they represent equal sequences of characters, ignoring case.
|
Sample Rules
Use Case |
Changing request input before sending it to the back end. For example, you can map the account type received from the request to an account code. |
Rule | name: "Convert account type to account code in pre-processor" description: "Rule to convert account type to account code" condition: "AccountType == \"Loan Account\"" actions: - "inputMap.AccountCode = 1" - "inputMap.Message = \"This is a loan account\"" The given sample rules above checks the account type, if the account type is Loan Account, then the associated account code is set to 1. The |
Use Case |
Modifying the result of an operation. For example, you can add the account type in the result depending upon the account code. |
Rule | name: "Add parameter in result" The given sample rule checks the account code, if the account code is equal to 1, then the account type parameter is set as Loan Account. The |
Use Case |
Changing the response body, status code, and headers that are sent to the device. For example, you can categorize the customer based on the quarterly average balance and send specific headers to the device to render appropriate UX of client application. |
Rule | name: "Modify response in rules." The given sample rule checks the quarterlyAvgBalance parameter. If the parameter is greater than or equal to 100000, then the response body, status code and headers sent to the device are changed. The The The |
Use case |
Aborting the call to back end in pre-processor. For example, you can allow requests only from mobile devices. |
Rule | name: "Abort call to back end" The given sample rule checks the user agent, if the user agent The |
Use case |
Accessing data from the cache. For example, you can populate a country code in the cache if it is not present. |
Rule | name: "Access cache in rules" The given sample rule checks the stored value in the cache. If the cache is empty, then the country code is added to the cache. The |
Use case |
Invoking an Integration service in pre-processor and post-processor. For example, you can invoke an SMS or email service based on a request parameter. |
Rule | --- name: "Invoke send email integration service" description: "Execute integration service to send email if sendEmail is true in input map." priority: 1 condition: "inputMap.get(\"sendEmail\") == \"true\"" actions: - servicesManager.invokeIntegration("RulesIntegrationService", "SendEmail") --- name: "Invoke send SMS integration service" description: "Execute integration service to send SMS if sendSms is true in input map" priority: 1 condition: "inputMap.get(\"sendSms\") == \"true\"" actions: - servicesManager.invokeIntegration("RulesIntegrationService", "SendSms") In the sample, based on the parameters sent from the device, we are invoking services either to send an SMS or email or both. The |
Use case |
Accessing the Identity data. For example, you can access the Identity data such as the app id and the user profile for the request. |
Rule | name: "Access Identity Info" The given sample rule accesses the identity information such as first name, last name, email id, and app id and sends it to the device. The |
Use case |
Accessing data from the session. For example, you can fetch the authorization token from the session and set it in the header of the back-end call. |
Rule | name: "Access session data" The given sample rule checks for an authorization token. If the authorization token is present in the session, then it is added to the header. The |
Use case |
Accessing the Configurable Parameters defined in App Services. |
Rule | name: "Access configuration properties" The given sample rule checks the encrypt property. If the encrypt property is enabled in the client properties, then the code fetches the encryption key from server properties and adds the key to the request. The |
Use case |
Accessing custom metrics in pre-processor and post-processor. |
Rule | name: "Access configuration properties" The given sample rule checks the encrypt property. If the encrypt property is enabled in the client properties, then the code fetches the encryption key from server properties and adds the key to the request. The |
Use case |
Writing multiple rules in the same pre-processor and post-processor. You can write multiple rules in the same pre/post processor by using NOTE: Please do not give separator after last rule. |
Rule | --- name: "Convert account code to type for loan account" description: "Rule for loan account to convert account type to code and add message in result" condition: "AccountCode == 1" actions: - "results.addParam(\"AccountType\", \"Loan Account\")" - "results.addParam(\"Message\", \"This is a Loan Account\")" --- name: "Convert account code to type for saving account" description: "Rule for saving account to convert account type to code and add message in result" condition: "AccountCode == 2" actions: - "results.addParam(\"AccountType\", \"Saving Account\")" - "results.addParam(\"Message\", \"This is a Saving Account\")" --- name: "Convert account code to type for current account" description: "Rule for current account to convert account type to code and add message in result" condition: "AccountCode == 3" actions: - "results.addParam(\"AccountType\", \"Current Account\")" - "results.addParam(\"Message\", \"This is a Current Account\")" The given sample rule invokes multiple rules. |
Use case |
Iterating over a set of values. |
Rules | name: "Iterate over records using for loop in rules." The given sample rule iterates a books dataset and adds discounted prices for each book in the dataset. |