Objective
To create a Rest Service that receives images, store it into a predefined location and then return the file location in JSON format.
Prerequisites
- Start Fiorano Enterprise Server (FES) and Fiorano Peer Server (FPS)
- Login to eStudio application
- Understand the basic menus and perspectives (panels) in eStudio application
Scenario
- Configure RESTStub component to receive binary data from RESTConsumer and forward the data to FileWriter component to be uploaded to a predefined location. Provide Content Type as "application/octet-stream" for RESTStub.
- Configure RESTConsumer component to send the binary data (received from FileReader) to RESTStub and receive the uploaded file path as JSON message. For response message, provide Content Type as "apllication/json"
How it Works
The Event Process, as per scenario, works in the following manner:
- Binary file is read by FileReader and content is forwarded to RESTConsumer
- RESTConsumer sends the binary content to RESTStub and then to FileWriter
- FileWriter uploads the binary content to the predefined location
- RESTConsumer receives the JSON output message with file path of uploaded file
Components used
Setting up the Example
Configuring RESTStub component
This component is used to expose an Event Process or a part of Event Process as a RESTful web service deployed in the Peer Server. To configure RESTStub component, perform the following actions:
- Add a new Event Process with name 'REST_Service_Example'.
- Drag the RESTStub component from Web category in the Micro Service Palette to the Fiorano Orchestrator.
Figure 1: Adding RESTStub component to REST_Service_Example orchestrator
- Double-click the RESTStub component to open Configuration Property Sheet (CPS).
- In the Configuration dialog box, perform the following actions:
- Add Service Name: 'restdemo', right-click resource under Resources and click Remove Resource to remove default resource.
Figure 2: Removing default resource from RESTStub
- Under Create/Edit WADL section, Right-click Resources and click Add Resource to add new resource.
Figure 3: Adding new resource
- Type 'upload' in ID and path fields that appear under Resources Configuration tab. New resource with name 'upload' gets added under Resources.
Figure 4: Providing resource name 'upload'
- Right-click upload, point to Add Method and click POST.
Figure 5: Adding Method 'POST'
- Click POST option that appears under upload resource and click Add icon under Representations section present in the Request Configuration tab.
- Under Representation tab, select 'application/octet-stream' from the Media Type drop-down.
Figure 6: Adding Representation with Media Type as application/octet-stream
- Right-click POST node, select Add Response and select 'application/json' from Media Type drop-down
Figure 7: Adding Representation for RESPONSE - 'application/json'
- Add Service Name: 'restdemo', right-click resource under Resources and click Remove Resource to remove default resource.
- Click Next twice to skip security-related configuration (SSl Configuration) and Connection Configuration, and click Finish to save RESTStub configuration.
Configuring FileWriter component
The FileWriter component writes the received data from its input port to the specified output file. To configure RESTStub component, perform the following actions in REST_Service_Example event process:
- Add the FileWriter component from File category in the Micro Service Palette to the Fiorano Orchestrator.
Figure 8: Adding FileWriter component in REST_Service_Example event process
- Double-click the FileWriter component to open Configuration Property Sheet (CPS).
- In the Interaction Configuration dialog box, click the Attributes tab to configure the attributes.
- Select the Is input Binary ? checkbox to write the data as bytes and click the FileWriter Configuration Ellipsis button.
Figure 9: Selecting Binary Input
- In the FileWriterConfig dialog box, provide the following values and click OK:
- File Name: The name of the file to which the input data has to be written. If the file with this name does not exist, a new file is created
- Target Directory: Provide the location to save the file.
- Working Directory: Location to save the file during intermediate processing.
- Error Directory: Location to save the file in case the processing does not get successful.
Figure 10: Providing File Name and storage locations
- Click Next, clear the Enable Scheduling option and click Finish to save the FileWriter configuration.
Connecting FileWriter and RESTStub
In the REST_Service_Example event process, perform the following actions:
- Connect the FileWriter with RESTStub in the following manner:
- RESTStub output port to FileWriter input port.
- FileWriter output port to RESTStub input port.
Figure 11: Connecting RESTStub with FileWriter
- Replace the names of 'route1' and 'route2' with 'UploadRequest' and 'UploadResponse' respectively to match the scenario. After clicking on the respective route, do any of the following to change the name:
- Press F2, change the name and click OK.
Figure 12: Renaming using Keyboard action 'F2'
- Go to Name property under General tab in Properties, change the name and press ENTER
Figure 13: Renaming using General Properties
- Press F2, change the name and click OK.
- The routes show up with the changed names
Figure 14: Connection with route names changed
Configuring Transformation between FileWriter and RESTStub
To manage Route Transformation between FileWriter output port and RESTStub input port, perform the following actions in the REST_Service_Example event process:
- Right-click UploadResponse, point to Configure Transformation and select Mapper Project to open Fiorano Mapper editor.
Figure 15: Navigating to Mapper Project for Route Transformation
- In the Output Structures panel, click Status under Response. 'Status' appears in Funclet View panel.
Figure 16: Working on Funclet View
- In the Funclet View panel, drag the function element Constant to the Funclet easel. Double-click it, edit the name as '200' and click OK.
Figure 17: Adding Constant from String Functions
- Connect the source node of the constant '200' to the destination node of Status.
Figure 18: Connecting Constant with new name '200' to the Status element
- Similarly, connect source node of the constant 'application/json' to the destination node of MediaType (select from Representation under Output Structures panel).
Figure 19: application/json constant connected to Media Type element - Add the following elements to the Funclet easel in the following manner:
- Click ANY present under Element in Output Structures panel
- Drag FullName present under FileInfo in Input Structures panel.
- Add two Constant functions from String Functions and change their name as below:
- { "FilePath" : "
- " }
- Add Concat from String Functions.
Figure 20: Adding Constant functions, FullName from FileInfo, Concat and ANY
- Connect the elements added in Step#6 in the following order:
- Source node of { "FilePath" : " to the destination node '1' of Concat.
- Source node of FullName to the destination node '2' of Concat. Destination node '3' appears after this conncetion.
- Source node of " } to the destination node '3' of Concat.
- Source node of Concat to the destination node of ANY.
{ "FilePath" : "<TARGET_FILE_PATH>" }:
Figure 21: Functions and elements connected to generate Display output
- Click Save icon (or File ??Save) to save the transformation configuration and close the Mapper Project editor.
Launching the Event Process
To launch the event process, perform the following actions:
- Click Check Resource and Connectivity icon (or press ALT+SHIFT+C) to check resource and connectivity.
- Click Run Event Process icon (ALT+SHIFT+R) to run the Event Process.
Figure 22: Running REST_Service_Example event process
Configuring RESTConsumer component
This component is used to expose an Event Process or a part of Event Process as a RESTful web service deployed in the Peer Server. To configure RESTConsumer component, perform the following actions:
- Add a new Event Process with name 'REST_Client_Example'.
- Add the RESTConsumer component from Web category in the Micro Service Palette to the Fiorano Orchestrator.
Figure 23: Adding RESTConsumer component to REST_Client_example orchestrator
- Switch to REST_Server_Example event process, right-click RESTStub component which is running, and click Copy WADL URL to copy the WADL URL.
Figure 24: Copying WADL URL
- Switch back to REST_Client_Example event process and double-click RESTConsumer component to open the CPS.
- In the Configuration dialog box, perform the following actions:
- Select the Load WADL checkbox
- Select the From URL option.
- Paste the copied WADL URL copied in Step#2.
- Click the Load WADL button.
Figure 25: Pasting and loading WADL URL
- In the Configuration tab under Create/Edit WADL section, values as configured in RESTStub component gets populated.
Figure 26: Resources populated after configuration
- Click Next twice to skip SSL and Connection configurations as done in RESTStub component, and click Finish to save RESTConsumer configuration.
Configuring FileReader component
The FileReader component reads files from the file system and sends their contents to the output port. To configure FileReader, perform the following actions in REST_Client_example event process:
- Add the FileReader component from File category in the Micro Service Palette to the Fiorano Orchestrator.
Figure 27: Adding FileReader component to REST_Client_Example event process
- Double-click the FileReader component to open Configuration Property Sheet (CPS).
- In the Interaction Configuration dialog box, select the 'Is input Binary ?' checkbox to write the data as bytes and click the FileReader Configuration Ellipsis button.
Figure 28: Selecting Binary File
- In the FileReaderConfig dialog box, provide the following values and click OK:
- File Name: The name of the file to be read. A pattern of file names can also be provided using wildcard character * (asterisk symbol).
Example: *.* includes all the files and *.jpg includes the files with .jpg extension. Source Directory: The directory which holds the file(s) to be read. All the files in this directory whose names match the pattern specified for the File name property will be processed.
Figure 29: Providing File Name and storage location
- File Name: The name of the file to be read. A pattern of file names can also be provided using wildcard character * (asterisk symbol).
- Click Next and clear Enable Scheduling check box to allow Feeder component to provide input.
Figure 30: Disabling Scheduler Configurations
- Click Finish to save the FileReader configuration.
Connecting FileReader and RESTConsumer
In the REST_Client_Example event process, perform the following actions:
- Connect the FileReader output port to RESTConsumer input port.
- Rename the ruote as 'PushFile'.
Figure 31: FileReader and RESTConsumer connected and route name changed
Configuring Transformation between FileReader and RESTConsumer
To manage Route Transformation between FileReader output port and RESTConsumer input port, perform the following actions in REST_Client_Example event process:
- Right-click PushFile, point to Configure Transformation and select Mapper Project to open Fiorano Mapper editor.
- Following the steps mentioned in "Configuring Transformation between FileWriter and RESTStub" section, connect application/octet-stream constant source node to Media Type destination node (refer Step#5).
Figure 32: MediaType with constant value 'application/octet-stream'
- In Output Structures panel, right-click upload and click User XSL to edit User XSL.
Figure 33: Adding User XSL to 'upload' resource
- In the User xsl for upload dialog box, insert the text "upload" within a tag, and then click OK. The text is being added for the component to identify the resource being invoked.
Figure 34: Adding XSL for 'upload' element
- In the Output Structures panel, click ANY, add a constant to Funclet View and rename it as 'upload', and then connect upload constant source node to ANY destination node.
Upload element needs to be present for component to identify the resource being invoked.
Figure 35: Element ANY with constant value 'upload'
- Click Save icon (or File ??Save) to save the transformation configuration and close the Mapper Project editor.
Compose the Event Process
Add more components and compose the REST_Client_Example event process as mentioned below:
- Add Feeder and Display components from Micro Service Palette under Util category to Fiorano Orchestrator.
- Connect the components in the following manner:
- Feeder output port to FileReader input port. Rename the route to 'ScanFiles'
- RESTConsumer output port to Display input port. Rename the route to 'ExtractJSON'.
Figure 36: Connecting Feeder and Display to FileReader and RESTConsumer respectively
Configuring Transformation between RESTConsumer and Display
To get the output in JSON format, Route Transformation between FileWriter output port and RESTStub input port is required.
To configure transformation, perform the following actions in REST_Client_Example event process:
- Right-click ExtractJSON route, point to Configure Transformation and select Mapper Project to open Fiorano Mapper editor.
- In the Mapper Project, perform the following actions in Output Structures panel:
- Click the Add icon on the upper-right part of the page and select XML option.
- In the Load Output XML Structure dialog box, add the following text in XML Content editor:<output>output</output>
- Click DTD icon to generate DTD element. The message appears in Generated DTD section. Click Finish.
Figure 37: Configuring to generate JSON message
- Connect ANY element present in Input Structures panel to output element that appears in Output Structures panel.
Figure 38: Connecting ANY element to output
- In the Menu, go to Tools and select XSLT Properties. In XSLT Properties dialog box, select 'text' from Output Method drop-down.
Figure 39: Configuring XSLT Properties - Click Save icon (or File >Save) to save the transformation configuration and close the Mapper Project editor
Running the Example
To run the REST_Attachments example, perform the following actions:
- Click Check Resource and Connectivity icon (or press ALT+SHIFT+C) to check resource and connectivity.
- Click Run Event Process icon (ALT+SHIFT+R) to run the event process. Two components: Feeder and Display appear.
- In the Feeder component, click the Send button.
Figure 40: Sending message from Feeder
- Confirmation message appears in the following format in Display1: { "FilePath" : "<TARGET_FILE_PATH>" }
Figure 41: Receiving message in Display
Reference
- Download the event process rest_service_example and rest_client_example, and Importing an Event Process in the Server Explorer in eStudio to understand the configuration used in this example and to execute the working sample.
- Please note that the component options used in this example are minimum, which helps you to get an overview of the application. To explore the other options present in the RESTStub, RESTConsumer, FileReader, and FileWriter components, refer: RESTStub, RESTConsumer, FileReader and FileWriter sections.