REST API sample details
This sample demonstrates the use of the REST API in Java™ in different scenarios. To run the sample, you use the Samples Console.
Running this sample
First, open the Samples Console as explained in Setting up the REST API sample.
Important: You must
run the scenarios in the following order to make them work.
- RuleApp scenario: Lists, adds, and modifies RuleApps.
- XOM scenario: Lists, adds, and modifies execution object models (XOM).
- Ruleset scenario: Lists, adds, and modifies rulesets.
- Execution scenario: Takes the parameters of a ruleset in the XML format and runs the ruleset.
- Execution with the JSON request scenario: Takes the parameters of a ruleset in the JSON object format and runs the ruleset.
- Clean scenario: Removes all the new artifacts.
The output trace is formatted as follows:
- ** Announces the action.
- >> URL: Reports the requested URL.
- << RESPONSE - Status code: Reports the returned status.
- The JSON response.
RuleApp scenario
- In the Samples Commands view, double-click the
add.ruleapp command. The command compiles the code and processes as follows:
- Checks whether the scenario can run.
- Lists all the RuleApps.
- Adds a RuleApp.
- Lists all the RuleApps again to show the new one.
- Modifies the RuleApp.
- Shows the modified RuleApp.
- To see the results, open a web browser and enter the following URL to start the Rule Execution Server console: http://localhost:<PORT>/res
- Log on to the Rule Execution Server console:
- User name: resAdmin
- Password: resAdmin
- Click the Explorer tab. In the RuleApps View, you can see the added RuleApp myRuleApp.
- Click the new RuleApp to see its description: My REST modified RuleApp. It contains no ruleset.
XOM scenario
- In the Samples Commands view, double-click the add.xom
command. The command compiles the code and processes as follows:
- Checks whether the scenario can run.
- Lists all the execution object models (XOM).
- Adds a XOM.
- Lists all the XOMs again to show the new one.
- To see the results, open the Rule Execution Server console as shown in the RuleApp scenario.
- Click the Explorer tab. In the Resources View, you see the added resource myxom.zip.
- Click the new resource to see its properties. You can download this resource. No ruleset references it.
Ruleset scenario
- In the Samples Commands view, double-click the
add.ruleset command. The command compiles the code and processes as follows:
- Checks whether the scenario can run.
- Adds a ruleset to the previously added RuleApp.
- Shows the created ruleset.
- Modifies the ruleset description.
- Adds a property to the XOM URI.
- Shows the modified ruleset.
- To see the results, open the Rule Execution Server console as shown in the RuleApp scenario.
- Click the Explorer tab. In the RuleApps View, you can see that myRuleApp now contains one ruleset.
- Click myRuleApp, and then click myRuleset to open its Ruleset View. You can see its description: My REST ruleset.
- Open the Managed URIs section. You can see one managed URI.
- Click the URI. You can see that the myxom.zip resource is now referenced by one ruleset.
Execution scenario
- In the Samples Commands view, double-click the execute
command. The command compiles the code and processes as follows:
- Checks whether the scenario can run.
- Passes the ruleset parameters in XML format, and runs the ruleset to apply for a loan in the amount of 5,000.
- Shows the XML response: In the output parameter loan, the approved field is set to true.
- In the Samples Commands view, double-click the executeNotApproved command to run the ruleset. The amount of the requested loan is 500,000. This time, the loan is not approved.
Execution with the JSON request scenario
- In the Samples Commands view, double-click the
execute.json command. The command compiles the code and processes as follows:
- Checks whether the scenario can run.
- Passes the ruleset parameters in JSONObject format, and runs the ruleset to apply for a loan in the amount of 5,000.
- Shows the JSON response: In the output parameter loan, the approved field is set to true.
- In the Samples Commands view, double-click the executeNotApproved.json command to run the ruleset. The amount of the requested loan is 500,000. This time, the loan is not approved. The reject message is also displayed.
Clean scenario
- In the Samples Commands view, double-click the clean
command. The command compiles the code and processes as follows:
- Checks whether the scenario can run.
- Removes the myRuleApp application.
- Lists all the RuleApps to show that myRuleApp does not exist any more.
- Lists all the resources to show that myxom.zip is not referenced any more.
- Removes the myxomp.zip resource.
- To see the results, open the Rule Execution Server console as shown in the RuleApp scenario.
- Click the Explorer tab. The RuleApps View does not list myRuleApp and the Resources View does not list myxom.zip any more.
Rebuilding this sample
To rebuild this sample:
- Make sure that you are in the Samples Console perspective.
- In the Samples Commands view, double-click the build command to build the classes.
Source files
You can find this sample in <InstallDir>/executionserver/samples/restapi.
The JavaClient.java file is the class that calls
the REST API.
- The constructor creates the HttpClient instance and sets the preemptive authentication. All further requests use the apiauth URL where the credentials are already given.
- The class defines one runXXScenario method for each scenario. The method first calls a canRunXXScenario method to check whether the scenario runs.
- The canRunXXScenario method runs a GET request that returns a JSON result to check whether the artifact to be added is already there.
- The runXXscenario method makes several requests of type GET, POST, UPDATE, or DELETE, according to the scenario.
- The getXOMURI method makes a GET request that returns a JSON result to access the URI from the server.
You can also view and modify the sample source files in Rule Designer. To import the sample into your workspace, click Import projects in the Samples and Tutorials view and switch to the Java perspective.