Load static resources and parse template in Mule 4

  • January 05, 2022

Introduction

First off, let’s touch on the topics of load static resource operation and the parse template Mule component as both lay the foundation for today’s use case.

Load static resource is the operation that serves the static content through a resource base path that helps find the static resource. It can be found under the HTTP module:

  • Resource base path is where you give the path of your static resource
  • Default file is where you give the default file name — for example, index.html
  • Attributes is the default that’s already present when you drag-and-drop in Canvas

Parse template is the Mule component under the core module that helps process the template. It also evaluates the embedded DataWeave expression and renders the result. You can write the DataWeave expressions inside your template and it, accordingly, is evaluated and replaced with the result.

It has five basic fields:

  • Display Name — is just for naming a thing
  • Content — instead of defining an external file, you can write your content here, where it contains embedded expressions
  • Location — you can use this field to give the location of your template if it’s not given in content
  • Target Variable — stores the result of evaluating the expression defined in Target Value
  • Target Value — it is just a variable value

Today’s use case serves the static resource page to the user, and the user should fill in their credentials — for example, name and employee code. Now, we’ll grab those details and insert them into the database. After that, it redirects users to a new page that dynamically fetches their name.

Flow

Structure

Step 1. In the first flow, drag-and-drop the HTTP Listener and click plus (+) to configure the connection. After that, from the Mule Palette drag-and-drop the Load static resource.

Step 2. Create a package called “web” inside src/main/resources and inside that, create two HTML files:

  • home.html
  • thankyou.html

Important points:

  • In home.html, while submitting the form, make sure you use the correct URL in the form action to initiate the second flow — that is, details-insertion-in-database

    formaction=”http://localhost:8081/thankyou.html”

  • In thankyou.html, make sure you use the correct expressions in the template

    #[vars.emp_name] to dynamically fetch the data.

Step 3. Make a second flow, and then drag-and-drop and configure the HTTP Listener.

Step 4. In the Transform Message, grab the query parameters used for logging the payload.

Step 5. In the set variable, grab the employee name and employee code used to insert the data inside the database in the next component. Also, this variable will be used for writing the DataWeave expressions inside the template.

Step 6. Drag-and-drop the insert connector in the Canvas and click plus (+) to configure the database connection — in this case, the local database.

             Database – apisero

              Table – employee

   Host – localhost

   User – root

Step 7. Drag-and-drop the parse template, under the core module. Here, you’ll need to specify the thankyou.html path.

Step 8. Click Save all and run the application.

Code for XML file

saveempdetails.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<mule xmlns:db=”http://www.mulesoft.org/schema/mule/db” xmlns:ee=”http://www.mulesoft.org/schema/mule/ee/core”

xmlns:http=”http://www.mulesoft.org/schema/mule/http”

xmlns=”http://www.mulesoft.org/schema/mule/core” xmlns:doc=”http://www.mulesoft.org/schema/mule/documentation” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd

http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd

http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd

http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd”>

<flow name=”post-employee-details” doc:id=”e8233598-d53d-4c55-8a38-633c028318a3″ >

<http:listener doc:name=”/employee_details” doc:id=”2a569519-023b-4eb1-ac60-b6ea5d04b385″ config-ref=”HTTP_Listener_config” path=”/employee_details” allowedMethods=”GET”/>

<http:load-static-resource doc:name=”Load static resource” doc:id=”16601260-a17a-496f-b9fb-ffbf1cc54dc1″ resourceBasePath=”C:\Users\princekumar\AnypointStudio\studio-workspace\work\saveempdetails\src\main\resources\web” defaultFile=”home.html” />

</flow>

<flow name=”details-insertion-in-database” doc:id=”2d62a846-7d52-4283-81a4-a35091a5ef58″ >

<http:listener doc:name=”/thankyou.html” doc:id=”86b7ba1f-783f-4d78-b190-6e76d183711b” config-ref=”HTTP_Listener_config” path=”/thankyou.html”/>

<ee:transform doc:name=”Transform Message” doc:id=”c0ec55b0-8743-415e-ba77-3a6dbb6fa61f” >

<ee:message >

<ee:set-payload ><![CDATA[%dw 2.0

output application/json

{

“Name”: attributes.queryParams.yourname,

“Employee Code”: attributes.queryParams.employeecode

}]]></ee:set-payload>

</ee:message>

</ee:transform>

<set-variable value=”#[attributes.queryParams.yourname]” doc:name=”emp_name” doc:id=”46ea1a5e-83f5-48ac-bd8e-9ee6709c993f” variableName=”emp_name”/>

<set-variable value=”#[attributes.queryParams.employeecode]” doc:name=”emp_code” doc:id=”4c440bcf-cb4a-4dfd-b6c3-887c89d0271d” variableName=”emp_code”/>

<logger level=”INFO” doc:name=”payload” doc:id=”2fb1257f-1c01-4a34-ac7a-9106bb043c6f” message=”#[payload]”/>

<db:insert doc:name=”Insert” doc:id=”e2a81a48-9951-46db-b188-54cc5fc5e5b6″ config-ref=”Database_Config”>

<db:sql ><![CDATA[INSERT INTO `employee` (`Name`, `Employee_Code`) VALUES (:Emp_Name, :Emp_Code)]]></db:sql>

<db:input-parameters ><![CDATA[#[{

Emp_Name : vars.emp_name,

Emp_Code : vars.emp_code

}]]]></db:input-parameters>

</db:insert>

<parse-template doc:name=”Parse Template” doc:id=”d57783e3-674a-4787-9868-0b7b34ee7036″ location=”C:\Users\princekumar\AnypointStudio\studio-workspace\work\saveempdetails\src\main\resources\web\thankyou.html”/>

</flow>

</mule>

How to call API

  1. Go to http://localhost:8081/employee_details
  2. Submit the details — for example, add James Gosling as the name and AMA1782 as the employee code
  3. Click Submit; it will redirect you to another page where you’ll see that a new record has been created inside the table

— By Prince Kumar