Intermediate Studio Tutorial
The Basic Studio Tutorial demonstrated how to build a simple spell checker application.
This intermediate tutorial demonstrates how to use the Studio drag-and-drop interface to build a more sophisticated Ajax spell checker application that incorporates the following:
- the Spell Checker Web service
- an HTML page
- a sequence of pre-packaged Mule building blocks.
This tutorial covers the following tasks:
- Creating a flow that features an Ajax endpoint and an Ajax connector
- Translating data from a web page into the Mule message processing flow
- Using XSL and Mule Transformers to pre-process user input into an XML format that the Spell Checker API can understand
- Creating a connection to the Spell Checker API so it can process Web page input
- Returning XML data to the originating web page via the Ajax transport channel
- Implementing a Jetty HTTP server within the flow
This tutorial requires approximately 30 minutes to complete. If you prefer a more guided approach to this tutorial, consider signing up for free Mule ESB Training, which uses this tutorial to demonstrate how to use Mule Studio.
Create a Studio Project
The Ajax Spell Checker flow contains five Mule components plus a connector.
This section repeats the Mule project creation procedure, which was previously covered in slightly greater detail by the Basic Studio Tutorial.
- In the Studio application menu, navigate to File, New, then click MuleProject.
- In the Name field, type
Ajax Spell Checker
or some other description to help you differentiate this application-building project from your other Mule application projects. This project name, which can contain spaces when you enter it, will appear as a single string — with underscore characters ( _ ), rather than spaces, between the words — in the Package Explorer pane. To commit the name you just typed, click Next. - After the New Mule Flow panel appears, type
ajax flow
or some other description for your flow into the Name field. Once again, any spaces in the string you type are converted to underscores. For example,ajax flow
is rendered asajax_flow.mflow
. Click Finish to commit the name.
Add Components to the Flow
If the Ajax Spell Checker flow you just created is not already open, double-click ajax_flow.mflow
, which resides under the flows node in the Package Explorer pane.
Drag the following components onto the Canvas, sequencing them as illustrated in the diagram below. These components are found in the Endpoint, Transformer, and Component categories in the Palette.
- Ajax Endpoint
- Object-to-XML Transformer
- XSLT Transformer
- HTTP Endpoint
- Echo Component
Add and Configure a Connector
Next, add and configure the Ajax connector, which is a Global Element (i.e., template) that provides server connection information to the Ajax endpoint:
- Click the Global Elements tab at the bottom of the canvas to open the Global Elements view.
- Click the Create button to open the Choose Global Type pane.
- Click the arrow to the left of Connectors to expand that node.
- Select Ajax, then click OK.
- Type
AjaxConnector
in the Name field. - Type
http://127.0.0.1:8090/Ajax
in the Server URL field. - Type
src/main/app/docroot
in the Resource Base field.
- Type
- Click OK to close the pane.
The Global Elements view now lists an Ajax connector.
Configure the Building Blocks
Now the Ajax Connector has been created, configure the resources needed by the Ajax endpoint to reference the Ajax Connector.
Configure the Ajax Endpoint
The Ajax endpoint serves as the Message Source (i.e., inbound endpoint) for the flow. Configure it by completing the following steps:
- Click the Message Flow tab to display your application flow on the canvas.
- Double-click the Ajax component to open its Properties panel.
- For Display Name on the General tab, enter
Ajax Channel
. - For Channel, enter
/services/echo
. This is the path used for communication with the web page.
-
Click the References tab and click the down arrow to the right of the Connector Ref field. Select
AjaxConnector
from the drop-down list. This represents the Ajax connector created earlier in the Global Elements view.
- Click the Documentation tab, enter a meaningful description for the connector in the Description field, then click OK to close the Endpoint Properties pane.
Configure the Object-to-XML Transformer
This building block, which translates incoming messages into XML code, requires no specific configuration settings; however, populating the Display Name and Description fields can provide helpful information during troubleshooting sessions.
- Double-click the Object-to-XML icon on the Message Flow canvas to open the Properties pane.
- On the General tab, type
Convert JS Object to XML
in the Display Name field.
- Click the Documentation tab and type
Convert JS Object to XML
in the Description field, then click OK to close the Properties pane.
Configure the XSLT Transformer
Mule Studio includes most of the files you need to complete this tutorial and run the application after you have built it. Follow these steps to add one more file – transform.xml
– to your local drive and then import this file into your Studio application. The transform.xsl
file provides your application with instructions for extracting data from incoming messages and translating that data into a form the application can digest.
Download and Import the XSLT Translation File
- To download the
transform.xsl
file which contains information that supports this tutorial, right click the link below, then select Download Linked File As... (or Save Linked File As...).
transform.xsl file
- In the dialogue pop-up that appears, save the file to your hard drive.
- In Studio's Package Explorer pane, click the arrow to the left of the
src/main/resources
node to expand the branch. - Navigate to the location on your hard drive where you saved the
transform.xsl
file, then drag it into Studio into thesrc/main/resources
folder. - In the dialog box that appears, click Copy.
- Verify that Mule successfully imported the file: the
transform.xsl
file should now be present inside thesrc/main/resources
folder.
Configure the XSLT Message Processor
This building block uses the instructions in transform.xsl
to perform the actual transformation of messages so they can be processed by the rest of the flow. Configure it by completing the following steps:
- Double-click the XSLT transformer component to open the Pattern Properties pane.
- On the General tab, replace the Display Name with:
Convert XML Structure
. - In the XSL File field, type:
src/main/resources/transform.xsl
- Click the Documentation tab, then type the following Description:
Produces compliant XML for the API
. - Click OK to close the Pattern Properties pane.
Configure the HTTP Endpoint
The HTTP endpoint sends XML requests to the Web Service and receives XML responses.
- Double-click the HTTP Endpoint component to open the Endpoint Properties pane.
- On the General tab, type
API
in the Display Name field. - In the Host field, type
spellcheck-service.cloudhub.io/spellcheck
- In the Port field, type
80
- In the Method field, type
POST
- Click the HTTP Settings tab, click the down arrow to the right of the Content type field, then select
text/xml
from the drop-down list. - Click the Documentation tab, then type
Sends XML requests to the Web service and receives XML responses
in theDescription field. -
Click OK to commit the choices you made in the Endpoint Properties pane.
Configure the Echo Building Block
The Echo component, which sends the HTTP response to the Mule console, requires no specific configuration settings, but you can enter an optional display name and description.
- On the Message Flow canvas, double-click the Echo icon to open the Properties pane.
- On the General tab, type
Echo to Console
in the Display Name field. - Click the Documentation tab, then type
Terminates the flow and logs results to the console
in the Description field. - Click OK to close the Properties pane and commit your entries.
Import an HTML Page
This application task requires an HTML page that includes JavaScript code and a call to a Mule class, thus allowing communication with the flow.
Create a docroot
directory for the HTML file
- In the Package Explorer create a new folder named
docroot
under the branchsrc/main/app
- Right-click the app folder, then select New > Folder.
- In the New Folder dialog box, type
docroot
in the Folder name field, then click Finish.
- Right-click the
docroot
node, which is now located under the branchsrc/main/app
, to open it. - Navigate to the folder on your hard drive where you installed MuleStudio, and find the
SpellChecker.html
file under MuleStudio > examples > AjaxSpellChecker > HTMLfile. - Drag the
SpellChecker.html
file into Studio into thedocroot
folder in the Package Explorer. - In the dialog box that appears, click Copy.
- Verify that
SpellChecker.html
resides within thesrc/main/app/docroot
node in the Package Explorer view. - Double-click the
docroot
node, then double-clickSpellChecker.html
to open the file in the Mule Studio editor and view its contents.
Congratulations, you have now built an advanced Mule ESB project with Mule Studio!
Run the Project!
Run the project from within Mule Studio to observe exactly what it does.
- Be sure to save your project by selecting File > Save on the Studio menu. (This saves all projects that are open in Studio, although for this tutorial,
ajax-spell-checker
should be your only open project). -
To run the project, right-click
ajax_flow.mflow
in the Package Explorer pane, then select Run as Mule Application. This causes the Console to display the following message:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Started app
'ajax_spell_checker'
+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Studio specified the following details when creating the project:
http://127.0.0.1:8090/Ajax
was entered in the Ajax connector properties as the Server URL. This created a Jetty HTTP server on your local machine, which is accessible via the specified URL.- A HTML page named
SpellChecker.html
was created and placed in thedocroot
folder. The relative linksrc/main/app/docroot
was specified as the resource base for the Jetty server in the Ajax connector. - Combining the two properties launches your application at http://127.0.0.1:8090/Ajax/SpellChecker.html.
Open a web browser and navigate to http://127.0.0.1:8090/Ajax/SpellChecker.html. A web page will appear with an empty form field. Type heeello, and the following results will appear:
Notice that as you type, the Response: data updates for each character you enter. The Web Service returns a series of spelling suggestions. Usually, the first suggestion contains the correct spelling.
- The Echo building block makes it possible for you to monitor the Studio Console view for output as you type. The Console should display something similar to the following:
********************************************************************************
* Message received in service: ajax_flow_Flow1. Content is: '<?xml *
* version=
"1.0"
encoding=
"UTF-8"
?><spellresult error=
"0"
clipped=
"0"
*
* charschecked=
"7"
suggestedl...[
100
of
175
]' *
********************************************************************************
|
Stopping the Ajax Spell Checker Application
To exit the Mule application you just created, click the red Terminate icon above the Console view.