APEssentials3.7 Student Manual
User Manual: Pdf
Open the PDF directly: View PDF .
Page Count: 267
Anypoint Platform
Essentials
Student Manual
Mule Runtime 3.7.1
Anypoint Studio 5.2.1
August 7, 2015
1
Table of Contents
SETUP INSTRUCTIONS ...............................................................................................................4
MODULE 2: BUILDING INTEGRATION APPLICATIONS WITH ANYPOINT STUDIO ...7
Walkthrough 2-1: Create your first Mule application ............................................................................. 8
Walkthrough 2-2: Run, test, and debug an application ....................................................................... 12
Walkthrough 2-3: Read and write message properties ....................................................................... 17
Walkthrough 2-4: Read and write variables ........................................................................................ 20
MODULE 3: CONSUMING WEB SERVICES .......................................................................... 23
Walkthrough 3-1: Consume a RESTful web service ........................................................................... 24
Walkthrough 3-2: Pass arguments to a RESTful web service ............................................................ 29
Walkthrough 3-3: Consume a RESTful web service that has a RAML definition ................................ 33
Walkthrough 3-4: Consume a SOAP web service .............................................................................. 40
MODULE 4: CONNECTING TO ADDITIONAL RESOURCES ............................................ 45
Walkthrough 4-1: Connect to a database (MySQL) ............................................................................ 46
Walkthrough 4-2: Connect to a file (CSV) ........................................................................................... 52
Walkthrough 4-3: Connect to a JMS queue (ActiveMQ) ..................................................................... 57
Walkthrough 4-4: Connect to a SaaS application (Salesforce) ........................................................... 64
Walkthrough 4-5: Find and install not-in-the-box connectors .............................................................. 73
MODULE 5: TRANSFORMING DATA .................................................................................... 76
Walkthrough 5-1: Load external content into a message .................................................................... 77
Walkthrough 5-2: Write your first DataWeave transformation ............................................................. 80
Walkthrough 5-3: Transform basic Java, JSON, and XML data structures ......................................... 85
Walkthrough 5-4: Transform complex data structures ........................................................................ 91
Walkthrough 5-5: Use DataWeave operators ................................................................................... 104
Walkthrough 5-6: Transform data sources that have associated metadata ...................................... 115
Walkthrough 5-7: Pass arguments to a SOAP web service .............................................................. 125
Walkthrough 5-8: Transform a data source to which you add custom metadata .............................. 129
MODULE 6: REFACTORING MULE APPLICATIONS ....................................................... 135
Walkthrough 6-1: Separate applications into multiple configuration files .......................................... 136
Walkthrough 6-2: Encapsulate global elements in a separate configuration file ............................... 141
Walkthrough 6-3: Create and run multiple applications .................................................................... 143
Walkthrough 6-4: Create and reference flows and subflows ............................................................. 150
2
MODULE 7: HANDLING ERRORS ........................................................................................ 156
Walkthrough 7-1: Handle a messaging exception ............................................................................. 157
Walkthrough 7-2: Handle multiple messaging exceptions ................................................................ 160
Walkthrough 7-3: Create and use global exception handlers ........................................................... 168
Walkthrough 7-4: Specify a global default exception strategy .......................................................... 172
MODULE 8: CONTROLLING MESSAGE FLOW ................................................................. 175
Walkthrough 8-1: Multicast a message ............................................................................................. 176
Walkthrough 8-2: Route messages based on conditions .................................................................. 182
Walkthrough 8-3: Filter messages .................................................................................................... 188
Walkthrough 8-4: Pass messages to an asynchronous flow ............................................................. 194
MODULE 9: PROCESSING RECORDS ................................................................................. 196
Walkthrough 9-1: Process items in a collection individually .............................................................. 197
Walkthrough 9-2: Create a batch job for records in a file .................................................................. 202
Walkthrough 9-3: Create a batch job for records in a database ........................................................ 207
Walkthrough 9-4: Restrict processing using a poll watermark .......................................................... 210
Walkthrough 9-5: Restrict processing using a message enricher and a batch step filter .................. 214
MODULE 10: BUILDING RESTFUL INTERFACES WITH RAML AND APIKIT ............ 222
Walkthrough 10-1: Use API Designer to define an API with RAML .................................................. 223
Walkthrough 10-2: Use API Designer to simulate an API ................................................................. 228
Walkthrough 10-3: Use Anypoint Studio to create a RESTful API interface from a RAML file ......... 234
Walkthrough 10-4: Use Anypoint Studio to implement a RESTful web service ................................ 239
MODULE 11: DEPLOYING APPLICATIONS ...................................................................... 244
Walkthrough 11-1: Use application properties .................................................................................. 245
Walkthrough 11-2: Dynamically specify property files ....................................................................... 250
Walkthrough 11-3: (Optional) Deploy an application to the cloud ..................................................... 252
Walkthrough 11-4: (Optional) Deploy an application on-prem .......................................................... 259
3
Setup instructions
To complete the exercises in this class, you need:
1. A computer with at least 3GB RAM, 2GHz CPU, and 4GB storage
https://developer.mulesoft.com/docs/display/current/Hardware+and+Software+Requirements
These are the requirements to run Anypoint Studio.
2. Internet access with access to the following ports and hosts:
Port
Host
80
mulesoft.com (and subdomains)
80
mulesoft-training.com (and subdomains)
80
cloudhub.io (and subdomains)
80
salesforce.com (and subdomains)
3306
iltdb.mulesoft-training.com
61616
54.69.115.37
Note: You can use telnet to test the availability of these ports. For example, in a
Terminal/Command window, enter the following command and see if it connects:
telnet iltdb.mulesoft-training.com 3306
If you are using Windows and need to install a Telnet Client, see
https://technet.microsoft.com/en-us/library/Cc771275(v=WS.10).aspx.
If you have restricted internet access and cannot access some of these ports and hosts, you
can instead use VirtualBox and a MuleSoft Training server image. Instructions for installing and
setting up the server are included below.
You also need to install and/or set up BEFORE class:
1. Java SE Development Kit 8 (JDK 8 NOT JDK 6 or JDK 7)
http://www.oracle.com/technetwork/java/javase/downloads/index.html
This is required for Anypoint Studio.
Note: Instructions for checking what version of Java you have can be found here:
https://www.java.com/en/download/help/version_manual.xml.
2. Anypoint Studio with embedded Mule 3.7 runtime
https://www.mulesoft.com/lp/dl/studio
Download, install, and start it to make sure it runs BEFORE class.
Note: If you have any issues, make sure the JDK and Anypoint Studio are BOTH 64-bit or
BOTH 32-bit and that you have enough memory (at least 3 GB) to run Anypoint Studio.
4
3. An Anypoint Platform account
http://anypoint.mulesoft.com
Note: You can sign up for a free, 30-day trial account or you can use your company account (if
you already have one).
4. A Salesforce Developer account (NOT a standard account)
https://developer.salesforce.com/en/signup
You CANNOT use a standard account, which will not give you API access.
You should receive an activation email within 5-10 minutes of creating the account. Click the link
it contains to activate your email and set a password and password question. You will then be
logged in to Salesforce. You can also log in at http://salesforce.com using your developer
credentials.
5. A Salesforce API Access token
http://salesforce.com
In Salesforce, click your name at the top of the screen and select My Settings. On the left side
of the page, select Personal > Reset My Security Token and click the Reset Security Token
button. A security token will be sent to your email in a few minutes. You will need this token to
make API calls to Salesforce from your Mule applications.
6. Course materials
http://training.mulesoft.com/login.html
The MuleSoft Learning Management System (LMS) hosts all of the resources, links, and files for
the class. Browse to http://training.mulesoft.com/login.html and then enter your email and
password (the password used when registering for the course). If you did not set a password or
forgot it, go to http://training.mulesoft.com/reset_password.html.
Once you are logged in, click the Anypoint Platform Essentials 3.7 Course link under My
Learning Plan. Near the bottom of the page, you will see the course resources. Click the links to
download the student manual (PDF), the student files (ZIP), and the course slides (ZIP). If you
have restricted internet access and plan to use VirtualBox and the training server image, also
download the MuleSoft Training 3.6 Open Virtualization Archive (OVA).
Note: Some students find it useful to put the student manual on a tablet and reference that
during class.
7. VirtualBox (only if you have restricted internet access)
https://www.virtualbox.org/wiki/Downloads
Download, install, and set it up to run the MuleSoft Training server image BEFORE class.
Follow the setup instructions below and/or watch the setup video: https://vimeo.com/123113815.
After installing VirtualBox, open it and select File > Import Appliance. Browse to the mulesofttraining3.6.ova file you downloaded in the previous step. After the image is imported, you should
see a mulesoft-training3.6 virtual machine listed in the VirtualBox Manager.
Double-click the mulesoft-training3.6 virtual machine to start it. The server needs to be running
to complete the course exercises. Verify the server starts. You do not need to log in, but if you
5
use SSH the username and password are vagrant. To stop the virtual machine, right-click it in
the VirtualBox Manager and select Close > Power Off.
The virtual machine uses ports 61111, 8112, 4406, 8111, and 2222. If you have any port
conflicts, you will need to change these values. First, power off the virtual machine and then
click the Settings button. Click the Network button and expand the Advanced section. Click the
Port Forwarding button and change any of the host port values as necessary.
8. Mule 3.7 runtime with Mule Management Console (optional)
http://mulesoft-training.com/mule-runtime-bundle-3.7
In public classes, you will deploy to the cloud. In private classes, you will deploy to the cloud or
an on-prem, standalone Mule runtime. Step-by-step instructions for both are included in the
student manual.
If you want to follow the instructions to deploy to an on-prem Mule runtime, download the bundle
from the above link before class. You will start the server during class.
6
Module 2: Building Integration
Applications with Anypoint Studio
In this module, you will learn:
•
About Mule applications, flows, messages, and message processors.
•
To use Anypoint Studio to create flows graphically using connectors, transformers, components,
scopes, and flow control elements.
•
To build, run, test, and debug Mule applications.
•
To read and write message properties.
•
To write expressions with Mule Expression Language (MEL).
•
To create variables.
7
Walkthrough 2-1: Create your first Mule application
In this walkthrough, you will build your first Mule application. You will:
•
Create a new Mule project with Anypoint Studio.
•
Add a connector to receive requests at an endpoint.
•
Display a message in the Anypoint Studio console.
•
Set the message payload.
Launch Anypoint Studio
1. Open Anypoint Studio.
2. In the Workspace Launcher dialog box, look at the location of the default workspace; change
the workspace location if you want.
3. Click OK to select the workspace; Anypoint Studio should open.
4. If you get a Welcome Page, click the X on the tab to close it.
5. If you get an Updates Available pop-up in the lower-right corner of the application, click it and
install the available updates.
8
Create a project
6. Select File > New > Mule Project.
7. Set the Project Name to apessentials.
8. Ensure the Runtime is set to the latest version of the Mule Server.
9. Click Finish.
Create an HTTP connector endpoint to receive requests
10. Drag an HTTP connector from the palette to the canvas.
9
11. Double-click the HTTP connector endpoint.
12. In the Mule Properties view, click the Add button next to connector configuration.
13. In the Global Element Properties dialog box, look at the default values and click OK.
14. Click the Apply Changes button; the errors in the Problems view should disappear.
10
Display data
15. Drag a Set Payload transformer from the palette into the process section of the flow.
16. Drag in a Logger component and drop it after the Set Payload transformer.
Configure the Set Payload transformer
17. Double-click the Set Payload transformer.
18. In the Properties view, set the value field to Hello World.
19. Click the Configuration XML tab at the bottom of the canvas and examine the corresponding
XML.
20. Click the Message Flow tab to return to the canvas.
21. Click the Save button or press Ctrl+S to save the file.
11
Walkthrough 2-2: Run, test, and debug an application
In this walkthrough, you will run, test, and debug your first Mule application. You will:
•
Run a Mule application using the embedded Mule ESB runtime.
•
Make an HTTP request to the endpoint via a web browser or a tool like cURL or Postman.
•
Receive a response with the text Hello World.
•
Redeploy an application.
•
Use the Mule Debugger to debug an application.
Run the application
1. From the main menu bar, select Run > Run As > Mule Application.
2. Watch the Console view; it should display information letting you know that both the Mule
runtime and the apessentials application started.
12
Test the application
3. Send an HTTP request to http://localhost:8081 through a browser or tool like cURL or Postman;
you should see Hello World displayed.
4. Return to the console in Anypoint Studio.
5. Examine the last entry.
6. Click the red Terminate button to stop the application and the Mule runtime.
7. Answer the following questions.
•
What triggered all the output you saw in the console?
•
What is the last thing displayed?
•
What Java class represents the payload?
•
What are the inbound and outbound properties on the message?
Rerun the application
8. Change the value of the Set Payload transformer to a different value.
9. Click in the canvas and then save the file.
13
10. Click the Run button and watch the console; you should the application is redeployed but the
Mule runtime is also restarted.
Note: You may want to modify your perspective so you always see the console. In Eclipse, a
perspective is a specific arrangement of views in specific locations. You can rearrange the
perspective by dragging and dropping tabs and views to different locations. Use the Window
menu in the main menu bar to save and reset perspectives.
Redeploy the application
11. Change the value of the Set Payload transformer again to a different value.
12. Click the Apply Changes button in the upper-right corner of the Properties view and watch the
console; you should see the application is redeployed but the Mule runtime is not restarted.
Debug the application
13. Right-click the Set Payload component and select Toggle breakpoint.
14. Select Run > Debug or click the Debug button in the main menu bar.
14
15. In the Mule ESB Runtime window, click Yes to stop the Mule runtime and restart it connected to
the Mule Debugger.
16. In you get a Confirm Perspective Switch dialog box, click Yes.
Note: If you do not want to get this dialog box again, check Remember my decision.
17. Make another request to http://localhost:8081 with a browser or other tool like cURL or
Postman.
18. Return to Anypoint Studio and look at the Mule Debugger view.
19. Drill-down and explore the properties and variables.
20. Click the Next processor button.
15
21. Look at the new value of the payload.
22. Step through the rest of the application.
23. Stop the application and the Mule runtime.
24. Click the Mule Design tab in the upper-right corner of the application to switch perspectives.
16
Walkthrough 2-3: Read and write message properties
In this walkthrough, you will manipulate message properties. You will:
•
Write MEL expressions.
•
Use the Debugger to read inbound and outbound message properties.
•
Use the Property transformer to set outbound message properties.
Use an expression to set the payload
1. Navigate to the Properties view for the Set Payload transformer.
2. Change the value to an expression.
#['Hello
World']
3. Click the Apply Changes button and run the application; you have to run it instead of
redeploying it because you stopped the Mule runtime when you stopped the Debugger.
4. Make a request to http://localhost:8081; the application should work as before.
5. Return to the Set Payload expression and use autocomplete to use the toUpperCase() method
to return the value in upper case.
#['Hello
World'.toUpperCase()]
Note: Press Ctrl+Space to trigger autocomplete if it does not appear.
6. Click the Apply Changes button to redeploy the application.
7. Make a request to the endpoint; the return string should now be in upper case.
17
Use an expression to display info to the console
8. Navigate to the Properties view for the Logger component.
9. Set the message to display the http.query.params property of the message inbound properties.
10. Apply the changes and debug the application.
11. Make a request to the endpoint with a couple of query parameters; for example,
http://localhost:8081?name=pliny&type=cat.
12. Return to the Mule Debugger view and locate your query parameters.
13. Step through the application and watch the property values.
14. Navigate to the console; you should see a ParameterMap object listed.
15. Modify the Logger to display one of your query parameters.
#[message.inboundProperties.'http.query.params'.name]
16. Click Apply Changes to redeploy the application and make a request to the endpoint with query
parameters again.
17. Click the Resume button in the Mule Debugger view.
18. Return to the console; you should now see the value of the parameter displayed.
18
Set an outbound property
19. Add a Property transformer between the Set Payload and Logger processors.
Note: The Message Properties transformer has been deprecated; it has been replaced by the
collection of Property, Variable, Session Variable, and Attachment transformers.
20. In the Properties view for the Property transformer, select Set Property.
21. Set the name to qpname (or some other value) and the value to an expression that evaluates
one of your query parameters.
22. Modify the Logger to display the value of this new outbound property.
#[message.outboundProperties.qpname]
23. Click the Apply Changes button to redeploy the application with a connection to the Mule
Debugger.
24. Make another request to the endpoint with query parameters.
25. Click the Outbound tab in the Mule Debugger view.
26. Step to the Logger; you should see your new outbound property.
27. Click the Resume button.
28. Look at the console; you should see the value of your variable displayed.
19
Walkthrough 2-4: Read and write variables
In this walkthrough, you will create flow and session variables. You will:
•
Use the Variable transformer to create flow variables.
•
Use the Session transformer to create session variables.
Create a flow variable
1. Add a Variable transformer between the Property and Logger processors.
2. In the Variable Properties view, select Set Variable.
3. Set the name to qptype (or some other value) and the value to your second query parameter.
4. Modify the Logger to also display the value of this new variable.
#['Name:
'
+
message.outboundProperties.qpname
+
'
Type:
'
+
flowVars.qptype]
Note: The flowVars is optional.
20
5. Save the file (or click Apply Changes) to redeploy the application in debug mode.
6. Make a request to the endpoint with query parameters again.
7. Click the Variables tab in the Mule Debugger view.
8. Step to the Logger; you should see your new flow variable.
9. Click the Resume button.
10. Look at the console; you should see the value of your outbound property and the value of your
new flow variable displayed.
Create a session variable
11. Add a Session Variable transformer between the Variable and Logger processors.
12. In the Session Variable Properties view, select Set Session Variable.
13. Set the name to color (or some other value) and give it any value, static or dynamic.
21
14. Modify the Logger to also display the session variable.
#['Name:
'
+
message.outboundProperties.qpname
+
'
Type:
'
+
flowVars.qptype
+
'
Color:
'
+
sessionVars.color]
15. Save and redeploy the application and make a request to the endpoint with query parameters
again.
16. Click the Session tab in the Mule Debugger view.
17. Step to the Logger; you should see your new session variable.
18. Click the Resume button.
19. Look at the console; you should see the value of your session variable displayed.
20. Stop the Mule runtime.
Note: You will explore the persistence of these variables in a later module, Refactoring Mule
Applications.
22
Module 3: Consuming Web
Services
In this module, you will learn:
•
About RESTful and SOAP based web services.
•
What RAML is and how it can be used.
•
To consume RESTful web services with and without RAML definitions.
•
To consume SOAP web services.
23
Walkthrough 3-1: Consume a RESTful web service
In this walkthrough and many others, you will work on building a Mule United Airline (MUA) application
that returns flights for Delta, United, and American airlines. In this walkthrough, you will consume a
RESTful web service that returns a list of all United flights as JSON. You will:
•
Create a second flow and rename flows.
•
Add an HTTP Listener connector endpoint to receive requests at http://localhost:8081/united.
•
Add an HTTP Request connector endpoint to consume a RESTful web service for United flight
data.
Make a request to the web service
1. Make a request to the United RESTful web service URL listed in the course snippets.txt file; you
should see JSON data for the United flights returned.
2. Look at the destination values; you should see SFO, LAX, CLE, PDX, and PDF.
24
Add a new flow with an HTTP Listener connector endpoint
3. Return to apessentials.xml.
4. Drag out another HTTP connector and drop it in the canvas above the existing
apessentialsFlow.
5. Double-click the name of the flow in the canvas and give it a new name of getUnitedFlightsFlow.
Note: You can set the name in the Properties view or directly in the blue banner.
Look at the global elements
6. Click the Global Element tabs at the bottom of the canvas.
7. Select the HTTP Listener Configuration and click Edit (or double-click it).
8. In the Global Element Properties dialog box, review the HTTP_Listener_Configuration and click
OK.
25
Configure the HTTP Listener connector endpoint
9. Click the Message Flow tab.
10. Double-click the new HTTP Listener connector endpoint.
11. Set the connector configuration to the existing HTTP_Listener_Configuration.
12. Set the path to /united.
Add an HTTP Request connector endpoint
13. Drag out another HTTP connector and drop it in process section of the flow.
14. In the Properties view, change the display name to United REST Request.
15. Click the Add button next to connector configuration.
26
16. In the Global Element Properties dialog box, set the following values and click OK.
•
Name: United_REST_Request_Configuration
•
Host: Use the value for the United web service host listed in the course snippets.txt file.
•
Port: Use the value for the United web service port listed in the course snippets.txt file.
•
Base Path: /essentials/united/flights
17. In the Properties view, set the path to / and the method to GET.
27
18. Click the Global Elements tab at the bottom of the canvas and see the new global configuration
element.
19. Return to the Message Flow view.
Test the application
20. Run the application.
21. Make a request to http://localhost:8081/united; you should see JSON flight data returned.
28
Walkthrough 3-2: Pass arguments to a RESTful web service
In this walkthrough, you will retrieve United flights for a specific destination by setting the destination as
a URI parameter. You will:
•
Modify the HTTP Request connector endpoint to use a URI parameter for the destination.
•
Set the destination to a static value.
•
Set the destination to a dynamic query parameter value.
•
Create a variable to set the destination.
Note: In a later module, you will add an HTML form to the application for destination selection.
Make a request to the web service in a browser or another tool
1. Make a request to the United RESTful web service URL for a destination listed in the course
snippets.txt file; you should see JSON data for only the flights to SFO.
2. Make additional requests for destinations of LAX, CLE, PDX, or PDF.
Add a URI parameter with a static value
3. Return to apessentials.xml.
4. Double-click the United REST Request endpoint.
5. In the Properties view, click the Add Parameter button.
29
6. Set the following parameter values.
•
Parameter type: uri-param
•
Name: destination
•
Value: SFO
7. Change the United REST Request endpoint path to /{destination}.
Test the application
8. Save the application to redeploy the application and make a request to
http://localhost:8081/united/; you should only get the SFO flights.
9. Modify the United REST Request endpoint and set the URI parameter value to LAX.
30
10. Save and redeploy the application and make another request to http://localhost:8081/united/;
you should now only get the LAX flights.
Add a URI parameter with a dynamic value
11. Return to the Properties view for the United REST Request endpoint.
12. Change the value of the uri-param from LAX to an expression for the value of a query
parameter called code.
#[message.inboundProperties.'http.query.params'.code]
Test the application
13. Save and redeploy the application and make a request to
http://localhost:8081/united/?code=CLE; you should only get the CLE flights.
Create a variable to set the destination
14. Add a Variable transformer before the United REST Request endpoint.
31
15. In the Variable Properties view, change the display name to Set Destination.
16. Set the operation to Set Variable and the name to destination.
17. Use a ternary expression to set the value to 'SFO' or the value of a query parameter called
code.
#[(message.inboundProperties.'http.query.params'.code
==
empty)
?
'SFO'
:
message.inboundProperties.'http.query.params'.code]
18. Navigate to the Properties view for the United REST connector endpoint.
19. Modify the URI parameter to use the new destination variable.
20. Save and redeploy the application and make a request to http://localhost:8081/united; you
should see only flights to SFO again.
21. Make another request to http://localhost:8081/united?code=PDX; you should now see flights to
PDX.
32
Walkthrough 3-3: Consume a RESTful web service that has a
RAML definition
In this walkthrough, you will consume a RESTful web service containing some simple bank account
data that has a RAML definition file. You will:
•
Add a third flow to the application.
•
Add an HTTP Listener connector endpoint to receive requests at http://localhost:8081/bank.
•
Add an HTTP Request connector endpoint to consume a RESTful web service defined with a
RAML file.
Add a new flow with an HTTP Listener connector endpoint
1. Return to apessentials.xml.
2. Drag out another HTTP connector and drop it in the canvas between the two existing flows.
3. Rename the flow to getBankAccountsFlow.
4. In the Properties view for the endpoint, set the connector configuration to the existing
HTTP_Listener_Configuration.
33
5. Set the path to /bank.
6. Set the allowed methods to GET.
Add an HTTP Request connector with a RAML location
7. Drag out another HTTP connector and drop it in the process section of the new flow.
8. In the Properties view, change the name to Bank REST Request.
9. Click the Add button next to connector configuration.
10. In the Global Element Properties dialog box, change the name to
Bank_REST_Request_Configuration.
11. Set the RAML location to the Banking RAML file URL listed in the course snippets file.
12. Wait for the RAML to be parsed and the host, port, and base path fields to be populated and
then click OK.
Note: If the fields did not populate, click the Reload RAML button next to the RAML location.
13. Click the Global Elements tab at the bottom of the canvas and see the new global configuration
element.
34
14. Return to the Message Flow view.
15. In the Bank REST Request Properties view, click the drop-down button for the path; you should
see the available resources (paths) for the RESTful web service defined by the RAML file.
16. Select the /customers path.
17. Set the method to GET.
35
Test the application
18. Save to redeploy the application and make a request to http://localhost:8081/bank; you should
see the customers returned as JSON.
Modify the request to use a path requiring a URI parameter
19. Return to the Properties view for the Bank REST Request endpoint.
20. Change the path to /customers/{customer_id}/accounts and the method to GET; a URI
parameter with the name customer_id should have been created automatically.
21. Set the customer_id parameter to a value of 2.
36
Test the application
22. Save to redeploy the application make a request to http://localhost:8081/bank; you should see
the account info for the customer with an ID of 2 returned as JSON.
23. Make a request and try to pass the customer ID as a URI parameter:
http://localhost:8081/bank/2; you should get a Resource not found response.
Get the value of an HTTP Listener endpoint URI parameter
24. Return to the Properties view for the HTTP Listener endpoint.
25. Change the path to use a wildcard to specify any path starting with /bank/.
37
26. Add a breakpoint to the Bank REST Request endpoint.
27. Save the file and debug the application.
28. Make a request to http://localhost:8081/bank/3.
29. In the Mule Debugger view, locate the http.request.uri and http.uri.params inbound properties.
30. Step through the rest of the application; you should still get the account info for the customer
with an ID of 2.
31. Return to the Properties view for the HTTP Listener endpoint.
32. Change the path to specify a URI parameter called ID: /bank/{ID}.
33. Save the file to redeploy the application in debug mode.
34. Make a request to http://localhost:8081/bank/4.
38
35. In the Mule Debugger view, locate the http.request.uri and http.uri.params inbound properties.
36. Step through the rest of the application; you should still get the account info for the customer
with an ID of 2
Set the HTTP Request endpoint URI parameter to a dynamic value
37. Return to the Properties view for the Bank REST Request endpoint.
38. Change the value of the customer_id uri-param from 2 to an expression for the value of an
HTTP Listener endpoint URI parameter called ID.
#[message.inboundProperties.'http.uri.params'.ID]
Test the application
39. Save the file and run the application.
40. Make another request to http://localhost:8081/bank/4; you should now see the account info for
the customer with an ID of 4.
39
Walkthrough 3-4: Consume a SOAP web service
In this walkthrough, you will consume a SOAP web service that returns a list of flights for Delta airlines.
You will:
•
Create a fourth flow with an endpoint to receive requests at http://localhost:8081/delta.
•
Add a Web Service Consumer connector to consume a SOAP web service for Delta flight data.
•
Use the DOM to XML transformer to display the SOAP response.
Browse the WSDL
1. Make a request to the Delta SOAP web service WSDL listed in the course snippets.txt file; you
should see the web service WSDL returned.
2. Browse the WSDL; you should find references to operations listAllFlights and findFlight.
40
Create a new flow with an HTTP Listener connector endpoint
3. Return to apessentials.xml.
4. Drag out another HTTP connector and drop it in the canvas above the existing flows.
5. Give the flow a new name of getDeltaFlightsFlow.
6. In the Properties view for the endpoint, set the connector configuration to the existing
HTTP_Listener_Configuration.
7. Set the path to /delta.
8. Set the allowed methods to GET.
Add a Web Service Consumer connector endpoint
9. Drag out a Web Service Consumer connector and drop it in the process section of the flow.
10. In the Properties view, set the display name to Delta SOAP Request.
11. Click the Add button next to connector configuration.
12. In the Global Element Properties dialog box, change the name to
Delta_Web_Service_Consumer.
13. Set the WSDL Location to the Delta SOAP web service WSDL listed in the course snippets.txt
file.
41
14. Wait for the WSDL to be parsed and the service, port, and address fields to be automatically
populated.
15. Click OK.
16. Switch to the Global Elements view and see the new global configuration element.
17. Return to the Message Flow view.
18. In the Delta SOAP Request Properties view, click the operation drop-down button; you should
see all of the web service operations listed.
19. Select the listAllFlights operation.
42
Debug the application
20. Add a Logger component after the Delta SOAP Request endpoint.
21. In the Logger Properties view, set the message to #[payload].
22. Add a breakpoint to the Logger.
23. Save the file and debug the application.
24. Make a request to http://localhost:8081/delta.
25. In the Mule Debugger view, drill-down into the payload variable.
Note: If you step to the end of the application, you will get an error.
26. Stop the Mule runtime.
Display the payload as a String
27. Add a DOM to XML transformer before the Logger.
43
Test the application
28. Save the file and run the application.
29. Make another request to http://localhost:8081/delta; you should now see the flight data returned
as a SOAP response.
30. Look at the console; you should also see the XML SOAP response displayed there.
44
Module 4: Connecting to
Additional Resources
In this module, you will learn:
•
To connect to databases.
•
To connect to files.
•
To connect to JMS queues.
•
To connect to SaaS applications.
•
To discover and install connectors not bundled with Anypoint Studio.
•
About developing your own custom connectors with Anypoint Connector DevKit.
45
Walkthrough 4-1: Connect to a database (MySQL)
In this walkthrough, you will connect to a MySQL database that contains information about American
flights. You will:
•
Create a new flow to receive requests at http://localhost:8081/american.
•
Add a Database connector endpoint that connects to a MySQL database.
•
Write a query to return all flights from the database for the static destination SFO.
•
Modify the query to use a dynamic destination from a query parameter.
•
Use the Object to String transformer to return the results as a string.
Note: You will get a destination dynamically from a form in a later module.
Create a new flow with an HTTP Listener connector endpoint
1. Return to apessentials.xml.
2. Create a new flow above the Delta flow and name it getAmericanFlightsFlow.
3. In the HTTP Properties view, set the connector configuration to the existing
HTTP_Listener_Configuration.
4. Set the path to /american and the allowed methods to GET.
Add and configure a Database connector endpoint
5. Add a Database connector to the process section of the flow.
46
6. In the Database Properties view, change the display name to American DB Request.
7. Click the Add button next to connector configuration.
8. In the Choose Global Type dialog box, select Connector Configuration > MySQL Configuration
and click OK.
9. In the Global Element Properties dialog box, set the name to Training_MySQL_Configuration.
10. Set the server, port, user, password, and database values to the values listed in the course
snippets file for the MySQL database.
11. Click the Add File button next to the MySQL driver required dependency.
12. Browse to the mysql-connector-java-{version}-bin.jar file located in the
APEssentials3.7_studentFiles_{date}/jars folder and click Open.
47
13. Back in the Global Element Properties dialog box, click the Test Connection button; you should
get a successful test dialog box.
Note: If you are not using the MuleSoft Training server image (the OVA) and your database
connectivity test fails, make sure you are not behind a firewall restricting access to port 3306. If
you cannot access port 3306, use the setup instructions to install and set up VirtualBox to use
the MuleSoft Training server image.
14. Click OK to close the dialog box.
15. Click OK to close the Global Element Properties dialog box.
Write a query to return all flights
16. In the Properties view for the database endpoint, select the Select operation.
17. In the parameterized query text box, write a query to select all records from the flights database.
SELECT
*
FROM
flights
Test the application
18. Save the file to redeploy the application.
19. Make a request to http://localhost:8081/american; you should get some garbled plain text
displayed or contained in a download file, which is the tool’s best representation of an ArrayList.
48
Debug the application
20. Add a Logger component after the Database connector endpoint and add a breakpoint to it.
21. Save the file, debug the application, and make a request to http://localhost:8081/american.
22. In the Mule Debugger view, drill-down and look at the data contained in the payload.
23. Find the name of the field for the flight destination.
24. Stop the Mule Debugger.
Display the return data
25. Set the Logger message to #[payload].
26. Add an Object to String transformer before the Logger component.
49
27. Save and run the application.
28. Make another request to http://localhost:8081/american; you should see the query results
displayed.
29. Look at the console; you should also see the query results there.
Note: To set the width of the console, select Anypoint Studio > Preferences > Run/Debug >
Console and select Fixed width console and set a maximum character width.
Set a flow variable to hold the value of a query parameter
30. Select the Set Destination transformer in the getUnitedFlightsFlow and from the main menu,
select Edit > Copy (or press Cmd+C).
50
31. Click in the process section of the getAmericanFlightsFlow and from the main menu, select Edit
> paste (or press Cmd+V); you should see a copy of the transformer added to the flow.
32. Move the transformer before the American DB Request endpoint.
33. In the Variable Properties view, change the display name to Set Destination and review the
expression.
Modify the query to use a dynamic destination
34. In the Properties view for the Database connector endpoint, modify the query to use the
destination flow variable.
SELECT
*
FROM
flights
WHERE
toAirport
=
#[flowVars.destination]
35. Save to redeploy the application and make another request to http://localhost:8081/american;
you should now only see flights to SFO.
36. Make another request to http://localhost:8081/american?code=CLE; you should now see flights
to CLE.
51
Walkthrough 4-2: Connect to a file (CSV)
In this walkthrough, you will load data from a local CSV file. You will:
•
Add and configure a File connector endpoint to watch an input directory, read the contents of
any added files, and then rename the files and move them to an output folder.
•
Add a CSV file to the input directory and watch it renamed and moved.
•
Restrict the type of file read.
•
Use the File to String transformer to return the results as a string.
Create new directories
1. Return to apessentials.xml.
2. Right-click the src/main/resources folder in the Package Explorer and select New > Folder.
3. Set the folder name to input and click Finish.
4. Create a second folder called output.
52
Add and configure a File connector endpoint
5. Drag a File connector from the palette and drop it in the canvas to create a new flow.
6. Rename the flow to getCSVAccountsFlow.
7. In the File Properties view, set the path to src/main/resources/input.
8. Set the move to directory to src/main/resources/output.
9. Look at the default polling information; the endpoint checks for incoming messages every 1000
milliseconds and sets a minimum of 500 milliseconds a file must wait before it is processed.
Display the file contents
10. Add a File to String transformer to the process section of the flow.
11. Add a Logger after the transformer.
53
Run the application
12. Save the file to redeploy the application.
13. Leave the application running.
Add a CSV file to the input directory
14. Right-click the apessentials project in the Package Explore and select Show In > System
Explorer.
15. Navigate to the src/main/resources folder.
16. In another window, navigate to the student files folder for the course:
APEssentials3.7_studentFiles_{date}.
17. Locate the resources/accounts.csv file.
18. Make a copy of this file and put it in the src/main/resources folder of the apessentials project.
Test the application
19. Drag the CSV into the input folder; you should see it almost immediately moved to the output
folder.
20. Drag it back into the input folder; it will be read again and then moved to the output folder.
21. Leave this folder open.
Debug the application
22. Return to apessentials.xml.
23. Add a breakpoint to the Logger.
24. Debug the application.
54
25. Move the accounts.csv file from the output folder back to the input folder.
Note: You can do this in your operating system window or in Anypoint Studio. If you use
Anypoint Studio, you will need to right-click on the project and select Refresh to see the file.
26. Wait until code execution stops at the Logger.
27. Drill-down and look at the payload.
28. Look at the inbound message properties and locate the original filename.
29. Click the Resume button in the Mule Debugger view.
30. Stop the debugger.
Restrict the type of file read
31. Open the Properties view for the File endpoint.
32. Click the Add button next to file name regex filter.
33. In the File Name Regex Filter dialog box, set the pattern to .*csv and uncheck case sensitive.
34. Click Finish.
55
Rename the file
35. Set the move to pattern to append .backup to the original filename.
#[message.inboundProperties.originalFilename].backup
Test the application
36. Save the file and run the application.
37. Move the accounts.csv file from the output folder back to the input folder; you should see it
appear in the output folder with its new name.
38. Look at the console; you should see the contents of the file displayed.
39. Move the accounts.csv.backup file from the output folder back to the input folder; it should not
be processed and moved to the output folder.
56
Walkthrough 4-3: Connect to a JMS queue (ActiveMQ)
In this walkthrough, you will read and write messages from a JMS topic. You will:
•
Create a flow accessible at http://localhost:8081/jms.
•
Add and configure an ActiveMQ connector.
•
Use a JMS endpoint to retrieve messages from a JMS topic.
•
Add messages to the topic using a web form.
•
Use a JMS endpoint to send messages to a JMS topic.
Create a JMS inbound-endpoint
1. Return to apessentials.xml.
2. Drag out a JMS connector and drop it at the bottom of the canvas to create a new flow.
3. Give the flow a new name of getTopicMessagesFlow.
4. In the JMS Properties view, select topic and set it to apessentials.
5. Click the Add button next to connector configuration.
57
6. In the Choose Global Type dialog box, select Connector Configuration > JMS > Active MQ and
click OK.
7. In the Global Element Properties dialog box, change the broker URL to the JMS ActiveMQ
Broker URL listed in the course snippets file.
8. Set the Specification to v1.1.
9. Click the Advanced tab and take a look at the various settings.
10. Click OK.
11. Click the Apply Changes button in the Properties view.
58
12. If you see An Attribute ‘action’ is required warning, ignore it.
Note: This is a bug in the Anypoint Studio January 2015 release.
Add the ActiveMQ library
13. In the Package Explorer, right-click apessentials and select New > Folder.
14. In the New Folder dialog box, set the folder name to lib and click Finish.
15. Locate activemq-all-{version}.jar file located in the APEssentials3.7_studentFiles_{date}/jars
folder.
16. Copy and paste or drag the JAR file into your lib folder.
17. Right-click the file in the Package Explorer and select Build Path > Add to Build Path; you
should now see it under Referenced Libraries.
Note: Adding activemq-all.jar can create conflicts with other dependencies in projects, so it is
recommended that you only add only the jar files you need in relation to what you need
ActiveMQ for. For more details, see the documentation at
http://www.mulesoft.org/documentation/display/current/ActiveMQ+Integration.
59
Test the application and receive messages
18. Add a Logger to the process section of the flow and set its message to #[payload].
19. Save the file and run the application.
20. Make a request to the JMS form URL listed in the course snippets file.
21. In the form, enter your name and a message and click Send.
22. Return to the console in Anypoint Studio; you should see your message along with those from
your classmates – but you don’t see the names of the people who sent the messages.
Debug the application
23. Add a breakpoint to the Logger.
24. Debug the application.
25. Make another request to the JMS form URL and submit another message.
26. In the Mule Debugger view, look at the payload and inbound message properties.
27. Stop the Mule Debugger.
60
Display names with the messages
28. In the Logger Properties view, change the message to use an expression to display the name
and message.
#[message.inboundProperties.name
+
":
"
+
payload]
Test the application
29. Save the file and run the application.
30. Return to the message form and submit a new message.
31. Look at the console; you should now see names along with messages.
Create a JMS outbound-endpoint
32. Drag out another HTTP connector and drop it in the canvas to create a new flow.
33. Give the flow a new name of postTopicMessageFlow.
34. In the HTTP Properties view, set the connector configuration to the existing
HTTP_Listener_Configuration.
35. Set the path to /jms and the allowed methods to GET.
36. Drag out another JMS connector and drop it into the process section of the flow.
37. In the JMS Properties view, select topic and set it to apessentials.
38. Set the connector configuration to the existing Active_MQ.
39. If you see An Attribute ‘action’ is required warning, ignore it.
61
Set a message
40. Add a Set Payload transformer between the HTTP and JMS connectors.
41. In the Set Payload Properties view, change the display name to Set Message and set the value
to a message query parameter.
42. Add a breakpoint to the Set Payload transformer.
43. Add a Property transformer after the Set Payload transformer.
44. In the Properties view, change the display name to Set Name.
62
45. Select Set Property and set the name to name and the value to your name.
Note: You can set this to a query parameter instead if you prefer.
Test the application and post messages
46. Save to redeploy the application and make a request to
http://localhost:8081/jms?message=Hello.
47. Look at the console; you should see your name and message displayed – along with those of
your classmates.
63
Walkthrough 4-4: Connect to a SaaS application (Salesforce)
In this walkthrough, you will retrieve account records from Salesforce. You will:
•
Browse Salesforce data on http://salesforce.com.
•
Create a flow accessible at http://localhost:8081/sfdc.
•
Add a Salesforce connector endpoint to retrieve accounts for a specific postal code.
•
Use the Query Builder to write a query.
•
Use the Object to JSON transformer to return the results as a string.
Note: To complete this walkthrough, you need a Salesforce Developer account. Instructions for
creating a Salesforce developer account and getting a security token are included in the Setup
instructions at the beginning of this student manual.
Look at existing Salesforce account data
1. In a browser, go to http://salesforce.com and log in with your Salesforce Developer account.
2. Click the Accounts link in the main menu bar.
3. In the view drop-down, select All Accounts and click the Go button.
64
4. Look at the existing account data; a Salesforce Developer account is populated with some
sample data.
5. Notice that countries and postal codes are not displayed by default.
6. Click the Create New View link next to the drop-down displaying All Accounts.
7. Set the view name to All Accounts with Postal Code.
8. Locate the Select Fields to Display section.
9. Select Billing Zip/Postal Code as the available field and click the Add button.
10. Add the Billing Country and Account Number fields.
11. Use the Up and Down buttons to order the fields as you prefer.
12. Click the Save button; you should now see all the accounts with postal codes and countries.
Create a new project and flow
13. Return to apessentials.xml in Anypoint Studio.
14. Drag out an HTTP connector and drop it above the getCSVAccountsFlow to create a new flow.
15. Give the flow a new name of getSFDCAccountsFlow.
16. In the HTTP Properties view, set the connector configuration to the existing
HTTP_Listener_Configuration.
17. Set the path to /sfdc and the allowed methods to GET.
65
Add a Salesforce connector endpoint
18. Drag out a Salesforce connector and drop it in the process section of the flow.
19. In the Select a version dialog box, click Use newest.
20. Examine the flow.
Configure the Salesforce connector
21. In the Salesforce Properties view, click the Add button next to Connector Configuration.
22. In the Choose Global Type dialog box, select Connector Configuration > Salesforce: Basic
authentication and click OK.
66
23. In the Global Element Properties dialog box, enter your email username, password, and security
token.
Note: Instructions for creating a Salesforce Developer account and getting a security token are
included in the Setup instructions at the beginning of this student manual.
24. Click the Test Connection button; you will get a Test Connection dialog box letting you know if
the connection succeeds or fails.
25. Click OK to close the Test connection dialog box.
26. If your test connection failed, fix it; do not proceed until it is working.
Note: If it failed, check to see if you have any extra whitespace in your entries.
27. Click OK to close the Global Elements Properties dialog box.
67
Write the query
28. In the Salesforce Properties view, select an operation of Query.
29. Click the Query Builder button.
30. In the Query Builder dialog box, select a type of Account.
31. Select fields BillingCity, BillingCountry, BillingPostalCode, BillingState, BillingStreet, and Name.
32. Click OK.
68
33. Examine the generated query.
Test the application
34. Save the file to redeploy the application.
Note: If you get a SAXParseException, stop the Mule runtime and run the application again. If
you still get an exception, close and reopen the project. If that does not work, restart Anypoint
Studio.
35. Make a request to http://localhost:8081/sfdc; you should get an error in the console stating that
the transformer needs a valid endpoint.
Debug the application
36. Add a Logger component after the Salesforce endpoint.
37. Set the Logger message to #[payload].
38. Add a breakpoint to the Logger.
39. Save the file and debug the application.
40. Make another request to http://localhost:8081/sfdc.
41. Step to the Logger and drill-down into the payload variable.
42. Click the Evaluate Mule Expression button in the Mule Debugger view.
43. Enter the following expression and press the Enter key.
#[payload.next()]
69
44. Expand the results; you should see the account data for the first account.
45. Click anywhere outside the expression window to close it.
46. Stop the Mule Debugger.
Display the return data
47. Add an Object to JSON transformer before the Logger component.
48. Save the file and run the application.
49. Make another request to http://localhost:8081/sfdc; you should see the Salesforce accounts
displayed.
70
Modify the query
50. In the Salesforce Properties view, click the Query Builder button.
51. In the Query Builder dialog box, click the Add Filter button.
52. Create a filter for BillingPostalCode equal to one of the postal codes (like 27215) in your
Salesforce account data.
53. Click OK.
54. Examine the generated query.
71
Test the application
55. Save the file to redeploy the application.
56. Make another request to http://localhost:8081/sfdc; you should only the accounts with the
specified postal code displayed.
72
Walkthrough 4-5: Find and install not-in-the-box connectors
In this walkthrough, you will learn how to add a new connector to Anypoint Studio. You will:
•
Browse the Anypoint Exchange.
•
Add a new connector to Anypoint Studio.
Browse the Anypoint Exchange from Anypoint Studio
1. In Anypoint Studio, click the Open Exchange button; the Anypoint Exchange should open in a
new window.
2. Click the Connectors button.
73
3. Browse the connectors, exploring the different levels and categories.
4. Locate the Google Contacts connector (or any other connector you are interested in).
5. Click the View details button and browse the connector’s documentation.
74
Install the connector
6. In the Anypoint Exchange, click the connector’s Install button.
7. Wait until an Install dialog box appears in Anypoint Studio.
Note: If you do not see the connector listed, resize the Install dialog box.
Note: You can also install connector’s directly from Anypoint Studio. From the main menu bar,
select Help > Install New Software. In the Install dialog box, click the Work with drop-down
button and select Anypoint Connectors Update Site. Drill-down into Community and select the
Google Contacts Connector (or some other connector).
8. On the Install Details page, click Next.
9. On the Review Licenses page, select the I accept the terms radio button.
10. Click Finish; the software will be installed and then you will get a message to restart Anypoint
Studio.
11. In the Software Updates dialog box, click Yes to restart Anypoint Studio.
12. After Anypoint Studio restarts, locate the new connector in the Connectors section of the
palette.
75
Module 5: Transforming Data
In this module, you will learn:
•
About the different types of transformers.
•
To use the DataWeave Transform Message component.
•
To write DataWeave expressions for basic and complex XML, JSON, and Java transformations.
•
To use DataWeave with data sources that have associated metadata.
•
To add custom metadata to data sources.
76
Walkthrough 5-1: Load external content into a message
In this walkthrough, you will add an HTML form to your application that will eventually serve as a front
end for the MUA integration application. You will:
•
Create a new flow that receives GET requests at http://localhost:8081/flights.
•
Use the Parse Template transformer to load the content of an HTML file into a flow.
•
Examine the HTML code and determine what data it sends where
Add an HTML file to the project
1. On your computer, navigate to the student files folder for the course,
APEssentials3.7_studentFiles_{date}.
2. Copy and paste (or drag) the resources/FlightFinder.html file into your project’s
src/main/resources folder.
Note: You will examine the HTML code soon – after you load the form and see what it looks like.
Create a flow
3. Return to apessentials.xml.
4. Drag out another HTTP connector to create a new flow at the top of the canvas and name it
getFlightFormFlow.
5. In the HTTP Properties view, set the connector configuration to the existing
HTTP_Listener_Configuration.
6. Set the path to /flights and set the allowed methods to GET.
77
Set the payload to HTML and JavaScript
7. Add a Parse Template transformer to the process section of the flow.
8. In the Properties view for the transformer, set the location to FlightFinder.html.
Debug the application
9. Add a breakpoint to the Parse Template transformer.
10. Add a Logger component after the transformer.
11. Save the file and debug the application.
12. In a browser, make a request to http://localhost:8081/flights.
13. Step through the flow and watch the value of the payload change.
78
14. Step through the rest of the flow and then return to the browser window; you should see the
HTML form.
15. Click the Find Flights button; what happens?
Review the HTML form code
16. Open FlightFinder.html in Anypoint Studio.
17. Locate the form at the bottom and find the names of the select boxes and their option values.
18. Locate to where the form data is posted.
19. Locate in what format the data is that is posted.
79
Walkthrough 5-2: Write your first DataWeave transformation
In this walkthrough, you will work with the data posted from the HTML form. You will:
•
Create a new flow that receives POST requests at http://localhost:8081/flights.
•
Use the DataWeave Transform Message component.
•
Add sample data and use live preview.
•
Transform the form data from JSON to a Java object.
Create a new flow
1. Return to apessentials.xml.
2. Drag out another HTTP connector to create a new flow and name it getFlightsFlow
3. In the HTTP Properties view, set the connector configuration to the existing
HTTP_Listener_Configuration.
4. Set the path to /flights (the same as the form flow) and set the allowed methods to POST.
Look at the data posted from the form
5. Add a Logger to the flow and add a breakpoint to it.
6. Save the file to redeploy the application in debug mode.
7. Make another request to http://localhost:8081/flights.
8. Step through or remove the breakpoints in the getFlightsFormFlow to get to the form in the
browser window.
9. Click the Find Flights button.
80
10. Look at the Mule Debugger view; you should see the return data is a BufferInputStream.
11. Look at the inbound properties; you should see the content-type is set to application/json.
Add a DataWeave Transform Message component
12. Add a DataWeave Transform Message component before the Logger.
13. In the Transform Message Properties view, look at the Input, Transform, and Output sections.
14. In the Transform section, locate the drop-down menu at the top of the view that sets the output
type; it should be set to Payload.
15. Beneath it, locate the directive that sets the output to application/java.
16. Delete the existing DataWeave expression (the curly braces) and set it to payload.
Debug the application
17. Add a breakpoint to the Transform Message component (if it does not have one).
18. Save the file to redeploy the application in debug mode.
19. Make a request to http://localhost:8081/flights and submit the form again.
81
20. Step to the Logger in the getFlightsFlow; you should see the form data is now a
LinkedHashMap.
21. Click the Resume button.
Add sample data and preview
22. In the Input section of the Transform Message Properties view, click Payload: Unknown.
23. Click the Sample Data button.
24. In the Sample data format dialog box, select JSON and click OK.
25. In the new payload window that is created in the Input section, replace the sample JSON data
with {"destination":"SFO","airline":"united"}.
Note: You can copy this value from the course snippets.txt file.
82
26. Click the Preview tab in the Output section; you should see sample output of type Java.
Change the output type
27. In the Transform section, change the output type from application/java to application/xml.
28. Look at the Preview tab; you should get an error.
29. Change the output type to application/json; in the Preview tab, you should see sample JSON
output.
Debug the application
13. Save the file to redeploy the application in debug mode.
14. Make a request to http://localhost:8081/flights and submit the form again.
83
15. Step to the Logger in the getFlightsFlow; you should see the form data is now of type
WeaveOutputHandler.
16. Click the Resume button.
17. Stop the Mule runtime.
Change the output type
18. In the Transform section, change the output type from application/json back to application/java.
19. Save the file.
Examine the code
20. In the Package Explorer, locate the sample data in src/test/resources.
21. Switch to the Configuration XML view.
22. Locate the DataWeave code.
23. Switch back to the Message Flow view.
84
Walkthrough 5-3: Transform basic Java, JSON, and XML data
structures
In this walkthrough, you will continue to work with the data posted from the HTML form. You will:
•
Write transformations to store data in multiple outputs as different types of data.
•
Create a second transformation to store the destination in a flow variable.
•
Create a third transformation to output data as JSON.
•
Create a fourth transformation to output data as XML.
Create a second transformation to store destination in a flow variable
1. Return to getFlightsFlow in apessentials.xml.
2. Return to the Properties view for the Transform Message component.
3. In the Transform section, click the Add New Transformation button in the lower-right corner.
4. In the new myVar tabbed window that appears in the Transform section, make sure the Output
is set to Flow Variable.
5. Set the name to destination.
6. Set the DataWeave expression to payload.destination.
85
7. Look at the preview in the Output section; you should the string SFO.
Debug the application
8. Save the file and debug the application.
9. Make a request to http://localhost:8081/flights, select LAX and Delta, and submit the form again.
10. Step to the Logger and click the Variables tab; you should see now see your flow variable in
addition to the transformed payload.
11. Click the Resume button.
Note: You will continue working with this flow in a later walkthrough. You will add functionality to
get the requested flight data from the other flows and return it back to the form.
Create a third transformation to output data as JSON
12. In the Transform Message Properties view, click the Add New Transformation button.
13. In the new window, leave the Output set to Flow Variable.
14. Set the name to json.
15. Set the DataWeave expression to payload.
16. Change the output type to application/json.
86
17. Look at the preview in the Output section.
18. Change the DataWeave expression to data: payload.
19. Change the DataWeave expression to data: {}.
20. Add a field called hub and set it to “MUA”.
87
21. Add a field called code and set it to the destination property of the payload.
22. Add a field called airline and set it to the airline property of the payload.
Create a fourth transformation to output data as XML
23. Click the Add New Transformation button.
24. In the new window, leave the Output set to Flow Variable.
25. Set the name to xml.
26. Set the DataWeave expression to payload.
27. Change the output type to application/xml; you should get an error message in the preview tab.
28. Change the DataWeave expression to data: payload.
29. Click the json tab and copy the DataWeave expression.
88
30. Click the xml tab and replace the DataWeave expression with the one you just copied.
31. Modify the expression so the code and airline properties are child elements of a new element
called flight.
32. Modify the expression so the airline is an attribute of the flight element.
33. Change the name of the component to Set payload and destination flowVar.
89
Debug the application
34. Save the file to redeploy the application in debug mode.
35. Make a request to http://localhost:8081/flights and submit the form again.
36. Step to the Logger and click the Variables tab; you should now see three flow variables.
37. Stop the Debugger.
Note: The json and xml flow variables were created for learning purposes. If you want, you can
delete them from the DataWeave transformer.
90
Walkthrough 5-4: Transform complex data structures
In this walkthrough, you will work with static flight data not retrieved using a connector. You will:
•
Create a new flow that receives GET requests at http://localhost:8081/static.
•
Transform a JSON array of objects to Java, JSON, and XML.
•
Explicitly set the MIME type of the data to be transformed.
•
Transform XML with repeated elements to XML and JSON.
Note: You will work with CSV data in the Processing Records module.
Create a new flow
1. Return to apessentials.xml.
2. Drag out another HTTP connector to create a new flow and name it transformStaticFlightsFlow
3. In the HTTP Properties view, set the connector configuration to the existing
HTTP_Listener_Configuration.
4. Set the path to /static and set the allowed methods to GET.
Add static JSON payload data
5. Add a Set Payload transformer to the flow.
6. Return to the course snippets.txt file and copy the value for the Example flights JSON.
91
7. In the Set Payload Properties view, paste the copied value into the value field.
Transform the payload to Java
8. Add a DataWeave Transform Message component and a Logger to the flow.
9. In the Transform Message Properties view, set the DataWeave expression to payload.
10. Look at the Preview tab in the Output section; you should not see any preview of the data.
Add sample data
11. In the Input section, click Payload: Unknown.
12. Click the Sample Data button.
13. In the Sample data format dialog box, select JSON and click OK.
14. In the new payload tab, replace the sample JSON with the JSON you used in the Set Payload
transformer.
92
15. Look at the Preview tab in the Output section; you should now see a preview of the data and
there should be three LinkedHashMap objects.
Debug the application
16. Add a breakpoint to the Transform Message component.
17. Save the file and debug the application.
18. Make a request to http://localhost:8081/static.
19. Step to the Transform Message component; you should see the payload is of type String.
20. Step to the Logger; you should see the payload is still of type String.
21. Click the Resume button.
93
Set the payload MIME type
22. In the Set Payload Properties view, set the MIME type to application/json.
23. Apply the changes and wait for the application to redeploy.
24. Make a request to http://localhost:8081/static.
25. Step to the Logger; the payload should now be of type ArrayList.
26. Click the Resume button; you should get a file download or garbled data depending upon if you
are using a browser or some other tool to make your request.
Return values for the first object in the collection
27. Change the DataWeave expression to payload[0]; in the Preview tab, you should see one
LinkedHashMap object.
94
28. Change the DataWeave expression to payload[0].price; in the Preview tab, you should get 400,
the first price value.
29. Change the DataWeave expression to payload[0].*price; in the Preview tab, you should see an
ArrayList with one integer value of 400 – the first price value.
Return collections of values
30. Change the DataWeave expression to return a collection of all the prices.
payload.*price
or
payload.price
31. Look at the Preview tab; you should see an ArrayList with three values.
32. Change the DataWeave expression to return a collection of prices and available seats.
[payload.price,
payload.availableSeats]
33. Look at the Preview tab; you should see an ArrayList with two ArrayLists.
95
Use the map operator to return object collections with different data structures
34. Change the DataWeave expression to payload map $, in the Preview tab, you should see three
Java objects again.
35. Change the DataWeave expression to set a property called flight for each object that is equal to
its index value in the collection.
36. Change the DataWeave expression to create a property called dest for each object that is equal
to the value of the destination field in the payload collection.
96
37. Change the DataWeave expression to dynamically add each of the properties present on the
payload objects.
38. Change the DataWeave expression so the name of each object is flight+index and you get
flight0, flight1, and flight2.
Change the output to JSON and test the application
39. Change the DataWeave expression output type from application/java to application/json.
40. Save the file and run the application.
97
41. Make a request to http://localhost:8081/static; you should see the correct JSON returned.
Change the expression to output valid XML and test the application
42. Change the DataWeave expression output type from application/json to application/xml; you
should get an error in the Preview tab.
43. Change the DataWeave expression to create a root node called flights; you should still get an
error in the Preview tab.
44. Change the expression to map each item in the input array to an XML element.
flights:
{(payload
map
{
'flight$$':$
}
)}
98
45. Save the file and run the application.
46. Make a request to http://localhost:8081/static; you should see the XML returned.
47. Save the file and run the application.
48. Make a request to http://localhost:8081/static; you should see the XML returned.
Add static XML payload data
49. Return to the course snippets.txt file and copy the value for the Example flights XML.
50. In the Set Payload Properties view, paste the copied value into the value field.
99
51. Change the MIME type to application/xml.
52. In the Input section of the Transform Message Properties view, click the x on the payload tab to
delete the src/test/resources/sample_data/json_1.json file.
53. Click Payload: Unknown and then the Sample Data button.
54. In the Sample data format dialog box, select XML and click OK; a new sample data file
empty.xml should be created.
55. In the Transform Message Properties view, replace the sample payload data with the Example
flights XML you copied.
Change the return structure of the XML
56. Look at the Preview tab; you should see all the flights are children of the flight0 element.
100
57. Change the DataWeave expression to loop over the payload.listAllFlightsResponse element;
you should see the flights are now correctly transformed.
58. Change the DataWeave expression to loop over the payload.listAllFlightsResponse.return
element.
59. Change the DataWeave expression use * to loop over the XML repeated return elements.
101
60. Change the DataWeave expression to return XML with repeated flight elements.
flights:
{(payload.listAllFlightsResponse.*return
map
{
flight:
$
}
)}
61. Change the DataWeave expression to return flight elements with dest and price elements that
map to the corresponding destination and price input values.
Change the output structure to JSON
62. Change the transform output type from XML to JSON.
Note: This is invalid JSON and should result in an error.
102
63. Remove the {( and )} around the payload map expression.
flights:
payload.listAllFlightsResponse.*return
map
{
flight:
{
dest:
$.destination,
price:
$.price
}
}
64. Change the DataWeave expression to return an array of objects without the flights and flight
properties.
payload.listAllFlightsResponse.*return
map
{
dest:
$.destination,
price:
$.price
}
65. Save the file and run the application.
66. Make a request to http://localhost:8081/static and look at the JSON structure returned.
103
Walkthrough 5-5: Use DataWeave operators
In this walkthrough, you will continue to work with the static flight data from the last exercise. You will:
•
Format strings, dates, and numbers.
•
Convert data types.
•
Replace data values using pattern matching.
•
Order data, filter data, and remove duplicate data.
•
Define and use custom data types.
•
Transform objects to POJOs.
Format a string
1. Return to transformStaticFlightsFlow in apessentials.xml.
2. Change the DataWeave expression to return objects with a property called plane equal to the
value of the input planeType values.
3. Use the upper operator to return the value in uppercase.
plane:
upper
$.planeType
4. Look at the Preview tab; you should see the uppercase plane fields.
5. Look at the data type of the prices; you should see that they are strings (surrounded by
quotation marks).
104
Convert data types
6. Change the DataWeave expression to use the as operator to return the prices as numbers (not
surrounded by quotation marks) instead of strings.
7. Add a departureDate field to the return object.
payload.listAllFlightsResponse.*return
map
{
dest:
$.destination,
price:
$.price
as
:number,
plane:
upper
$.planeType
departureDate:
$.departureDate
}
8. Change the transform output type from json to java.
%output
application/java
9. Look at the Preview tab; you should see the departureDate property is a String.
105
10. Change the DataWeave expression to use the as operator to convert departureDate to a date
object.
11. Look at the Preview tab; you should get an error message.
12. Look at the format of the dates in the sample data in the payload tab in the Input section.
13. Use the format operator to specify the pattern of the input date strings.
departureDate:
$.departureDate
as
:date
{format:
"yyyy/MM/dd"}
Note: If you are not a Java programmer, you may want to look at the Java documentation for the
DateTimeFormatter class to review documentation on pattern letters. See:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html.
14. Look at the Preview tab; you should see departureDate is now a Date object.
106
Format a date
15. Use the as operator to convert the dates to strings, which can then be formatted.
departureDate:
$.departureDate
as
:date
{format:
"yyyy/MM/dd"}
as
:string
16. Look at the Preview tab; the departureDate is again a String.
17. Use the format operator with any pattern letters and characters to format the date strings.
departureDate:
$.departureDate
as
:date
{format:
"yyyy/MM/dd"}
as
:string
{format:
"MMM
dd,
yyyy"}
18. Look at the Preview tab; the departureDate properties should now be formatted.
19. Change the transform output from java to json.
%output
application/json
20. Save the file and run the application.
21. Make a request to http://localhost:8081/static; you should see the formatted dates.
107
Format a number
22. Use the format operator in the DataWeave expression to format the prices with two decimal
places.
price:
$.price
as
:number
{format:
"###.##"},
23. Look at the Preview tab; the prices should be formatted.
Note: There is a bug in Anypoint Studio 5.2.1 (whose live preview uses Mule runtime 3.7.1) and
Mule runtime 3.7.1 and the numbers are not formatted correctly.
24. Save the file and run the application.
25. Make a request to http://localhost:8081/static; you should see the formatted prices (but may
not).
26. Change the DataWeave expression to format the prices to zero decimal places.
price:
$.price
as
:number
{format:
"###"},
27. Look at the Preview tab; you should (but may not) see the second price is now 200 instead of
199.99.
Define a custom data type
28. In the header section of the Transform section, define a custom data type called currency.
29. Select the data coercion and formatting expression for the price in the DataWeave expression
and move it to the currency data type definition.
%type
currency
=
:number
{format:
"###"}
108
30. In the DataWeave expression, set the price to be of type currency.
price:
$.price
as
:currency,
31. Look at the Preview tab; the prices should still be formatted.
Note: There is a bug in Mule runtime 3.7.1 and the numbers are not formatted correctly.
Replace data values
32. Use the replace operator in the DataWeave expression to replace the string Boing with Boeing.
plane:
upper
$.planeType
replace
/(Boing)/
with
"Boeing",
33. Look at the Preview tab; Boeing should be spelled correctly.
Order data
34. In the Preview tab, look at the flight prices; the flights should not be ordered by price.
35. Change the DataWeave expression to use the orderBy operator to order the objects by price.
payload.listAllFlightsResponse.*return
map
{
…
}
orderBy
$.price
109
36. Look at the Preview tab or run the application; the flights should now be ordered by price.
37. Look at the PDX flights; they should not be ordered by departureDate.
38. Change the DataWeave expression to first sort by departureDate and then by price.
payload.listAllFlightsResponse.*return
map
{
…
}
orderBy
$.departureDate
orderBy
$.price
39. Look at the Preview tab or run the application; the flights should now be ordered by price and
flights of the same price should be sorted by departureDate.
Remove duplicate data
40. Use the distinctBy operator in the DataWeave expression to remove any duplicate objects.
payload.listAllFlightsResponse.*return
map
{
…
}
orderBy
$.departureDate
orderBy
$.price
distinctBy
$
110
41. Look at the Preview tab; you should now see only four flights instead of five.
Note: If you still get five flights, remove the currency type coercion (or replace it with the
currency expression) from the price field. This is a bug in Mule runtime 3.7.1.
42. Add an availableSeats field that is equal to the emptySeats field and coerce it to a number.
availableSeats:
$.emptySeats
as
:number
43. Look at the Preview tab; you should see five flights again.
Add Java classes file to the project
44. On your computer, navigate to the java folder in the student files folder for the course:
APEssentials3.7_studentFiles_{date}/java.
45. Copy and paste (or drag) the com.mulesoft.training folder into your Anypoint Studio project’s
src/main/java folder.
46. Open the Flight.java class and look at the names of the properties.
111
Transform objects to POJOs
47. Return to transformStaticFlightsFlow in apessentials.xml.
48. Change the transformation output from json to java.
%output
application/java
49. Look at the Preview tab and see that LinkedHashMap objects are created.
50. In the header section of the Transform section, define a custom data type called flight that is a
com.mulesoft.training.Flight Java object.
%type
flight
=
:object
{class:
"com.mulesoft.training.Flight"}
51. In the DataWeave expression, set the map objects to be of type flight.
payload.listAllFlightsResponse.*return
map
{
…
}
as
:flight
orderBy
$.departureDate
orderBy
$.price
distinctBy
$
52. In the Transform section, look at the warning you get: Key ‘dest’ not found.
53. Change the name of the dest key to destination.
destination:
$.destination,
54. Change the name of the plane key to planeType.
planeType:
$.planeType,
112
55. Look at the Preview tab; the objects should now be of type Flight.
56. Save the file and debug the application.
57. Make a request to http://localhost:8081/static.
58. Step to the Logger and drill-down into the payload; the objects should be of type Flight.
59. Stop the debugger.
Filter data
60. Return to transformStaticFlightsFlow in apessentials.xml.
61. In the Preview tab, look at the values of the availableSeats properties.
62. Add a filter to the DataWeave expression that removes any objects that have availableSeats
equal to 0.
payload.listAllFlightsResponse.*return
map
{
…
113
}
as
:flight
orderBy
$.departureDate
orderBy
$.price
distinctBy
$
filter
($.availableSeats
!=0)
63. Look at the Preview tab; you should no longer see the flight that had no seats.
114
Walkthrough 5-6: Transform data sources that have associated
metadata
In this walkthrough, you will work with the American and Delta flight data. You will:
•
Use DataWeave to transform the American flight data from a collection of Java objects to one
with a different data structure.
•
Use DataWeave to transform the Delta flight data from XML to a collection of Java objects.
Add a DataWeave Transform Message component
1. Locate getAmericanFlightsFlow in apessentials.xml.
2. Delete the Object to String transformer.
3. Replace it with a Transform Message component.
4. Change the name of the Transform Message component to Java to Java.
115
5. Look at the Input section of the Transform Message Properties view; the payload should
automatically be set to a List
Source Exif Data:
File Type : PDF
File Type Extension : pdf
MIME Type : application/pdf
PDF Version : 1.7
Linearized : Yes
Create Date : 2015:08:07 21:24:46Z
Creator : Word
Modify Date : 2015:08:07 14:25:54-07:00
XMP Toolkit : Adobe XMP Core 5.6-c015 81.157285, 2014/12/12-00:43:15
Creator Tool : Word
Metadata Date : 2015:08:07 14:25:54-07:00
Keywords :
Producer : Mac OS X 10.10.2 Quartz PDFContext
Format : application/pdf
Title : Microsoft Word - APEssentials3.7_studentManual.docx
Document ID : uuid:51bd7ca3-c776-3145-b6d5-9da99038d502
Instance ID : uuid:e9506a9a-5783-ed4d-8910-e3b8b3e71409
Page Count : 267
EXIF Metadata provided by EXIF.tools