How to check whether a string contains a substring in Mule ESB?

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about how to check whether a string contains a substring in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

To check whether a string contains a substring, we will need to install Mule ESB testing framework called Munit. MUnit is a Mule testing framework that lets you easily automate testing Mule applications, using both unit and integration tests. MUnit also provides a set of tools, such as a message processor mocking framework that lets you test units of code in actual isolation.

Installing Munit:

To use MUnit in Studio, you have to install it from the website. To install MUnit, follow these steps:

  • Verify that your version of Studio is 5.2.0 (July 2015 release) or newer.
  • Add the MUnit update site :
  • Go to HelpInstall New Software.
  • Studio displays the Available Software In the Work with:field, paste the     MUnit update site: http://studio.mulesoft.org/r4/munit
  • Check Munit and Munit Tools for Mule, as shown below.
  • Click Next to complete the installation.

  • Step1Drag and drop an HTTP listener from the palette

  • Step2:In the HTTP Listener’s properties, create a new configuration element for it. Let the port stay as the default value 8081 and set the host to localhost.Step3:Drag and drop a set payload from the palette

  • Step4: The Munit FolderUsing MUnit automatically adds a new folder, src/test/munit, to your project.

  • Creating a New MUnit Test in Studio

    MUnit Studio integration is mainly built around XML-based tests. Most of the MUnit-Studio integration tools guide you in the creation of these tests.

    There are two basic ways to create a new MUnit test in Studio:

    • Choose a specific flow by right clicking the flow and selecting MUnit.
    • Use the wizard, which allows you to create a test for any flow or application in the workspace.

    Creating a Test for a Specific Flow

    To create a test for a specific flow, right-click the flow, then select MUnit > Create a new <flow_name> suite.

  • This action:
    • Creates a new test suite named after the XML file where the flow resides, in this case munit2.xml. Studio displays the test suite on a new tab, next to the tab for the original application:

  • Imports the XML file to the test suite
  • Creates a new MUnit test like the one shown below

Step5:

To add an MUnit message processor to the test shown above, drag it from the Palette to the Testarea before and after the flow-ref.set message and assert true

in assert true

Running a Test Suite

To run a test suite, right-click the empty canvas where the suite resides, then select Run MUnit suite.

Studio displays the output from the running suite in the console.

Running a Test

To run a test, right-click the name of the test, then select Run MUnit Test.

To check that the test is actually running, view the output in the console. In order not to overwhelm the user, the default output provides little information, but enough to verify that the test has run.

An alternative way to run a test is to use the MUnit tab (see below).

  1. Select the desired test in the MUnit tab.
  2. Right-click the test, then select Run.

The MUnit tab displays successful tests in green, failed tests in red.

The Coverage button in the image above allows you to see what flow the test covered, and the percentage of message processors in the flow that was covered by the test:

If you run more than one test, the MUnit tab displays a history of run tests. For failed tests, the Errorsbutton displays the stack trace, which you can copy to your clipboard. To copy the stack trace, right-click the name of the failed test, then select Copy Stack Trace.

As you can see in the image above, you can also use the MUnit tab to Running Your Test or debug your test, by selecting the appropriate menu option.

Clicking the debug button or the play button on the top right causes the last run or debug to be rerun. This rerun includes all tests that were run on the previous one. You can also select a single test from the previous run to rerun on its own.

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

How to manage dataweave xml namespace declarations in Mule ESB?

28

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about managing DataWeave XML namespace declarations in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

 

Assume we have the following scenario in which we have dataweave script that produces XML like

%dw 1.0

%output application/XML skipNullOn=”everywhere”

%namespace soap http://www.w3.org/2003/05/soap-envelope

%namespace ns http://www.mycompany/2015/07

 

{

soap#Envelope: {

soap#Header: {

},

soap#Body: {

ns#GetVehDetails: {

 

}

}

}

}

}

 

It produces something like this…

<soap:Envelope xmlns:soap=”http://www.w3.org/2003/05/soap-envelope”>

<soap:Header/>

<soap:Body>

<ns:GetVehDetails xmlns:ns=”http://www.mycompany/2015/07″>

The requirement is to change the dataweave script to output the xml namespace declarations at the root element mentioned below:

<soap:Envelope xmlns:soap=”http://www.w3.org/2003/05/soap-envelope”

xmlns:ns=”http://www.mycompany/2015/07″>

<soap:Header/>

<soap:Body>

<ns:GetVehDetails>

 

Our Solutions team took the following approach while working on this scenario real time.

STEP 1: Creating a new project.

In The Any point studio, top left clicks on the file => New => Mule Project. Click on the Mule Project enter your project name and click finish.

STEP 2:

Drag and drop the connectors required from the Mule Palette to the canvas as shown

STEP 3:

The requirement in the above scenario is a known issue and the only way we at Massil Technologies could solve it is listed below:

You need to add a dummy attribute @(ns#name:”) with the namespace that you want to be at top of the topmost root tag such as Envelope tag in this case

%dw 1.0

%output application/XML skipNullOn=”everywhere”

%namespace soap http://www.w3.org/2003/05/soap-envelope

%namespace ns http://www.mycompany/2015/07

 

{

soap#Envelope @(ns#name:””): {

soap#Header: {

},

soap#Body: {

ns#GetVehDetails: {

 

}

}

}

}

FLOW:

CONSOLE OUTPUT:

XML FILE: <?xml version=“1.0” encoding=“UTF-8”?>

XML FILE: <?xml version=“1.0” encoding=“UTF-8”?>

 

<mule xmlns:tracking=“http://www.mulesoft.org/schema/mule/ee/tracking” xmlns:file=“http://www.mulesoft.org/schema/mule/file” xmlns:dw=“http://www.mulesoft.org/schema/mule/ee/dw” xmlns=“http://www.mulesoft.org/schema/mule/core” xmlns:doc=“http://www.mulesoft.org/schema/mule/documentation”

xmlns:spring=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd

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

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

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

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

<file:connector name=“File” outputPattern=“C:\Users\MI\Desktop/out” autoDelete=“true” streaming=“true” validateConnections=“true” doc:name=“File”/>

<flow name=“datawave”>

<file:inbound-endpoint path=“src/main/resources/output” responseTimeout=“10000” doc:name=“File”/>

<byte-array-to-string-transformer doc:name=“Byte Array to String”/>

<logger message=“#[payload]” level=“INFO” doc:name=“Logger”/>

<dw:transform-message doc:name=“Transform Message” mode=“deferred”>

<dw:set-payload>

<![CDATA[

%dw 1.0

%output application/xml skipNullOn=”everywhere”

%namespace soap http://www.w3.org/2003/05/soap-envelope

%namespace ns http://www.mycompany/2015/07

 

{

soap#Envelope @(ns#name:””): {

soap#Header: {

},

soap#Body: {

ns#GetVehDetails: {

 

}

}

}

}

 

]]>

</dw:set-payload>

</dw:transform-message>

<byte-array-to-string-transformer doc:name=“Byte Array to String”/>

<logger message=“#[payload]” level=“INFO” doc:name=“Logger”/>

</flow>

</mule>

 

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed the scenario mentioned above. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

 

Removing duplicate record sets from Dataweave in Mule ESB

27

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about removing duplicate record sets from Dataweave in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

During one of the projects on Mule ESB, we had come across this unique requirement which mostly involves removing duplicate record set on one record set fields value. In the next part of this article, we cover in steps the best solution that worked for our technology team with the client on that day.

Step 1:

In Anypoint Studio click File > New > Mule Project to create a new project, give it whatever name you wish and click Finish.

Step 2:

Drag an HTTP Connector from the palette to your empty canvas, then a Transform Message     Component

 

Step 3:

Click on the HTTP Connector to open its properties editor, then click the green arrow icon to create a new configuration for it. Leave all the fields in default and click OK.

Step 4:

Note that if you open the Transform Message component, the input section should show the fields that will be present in the incoming payload

Step 5:

You write the output payload in

%dw 1.0

%output application/xml inlineCloseOn = “empty” ,skipNullOn=”attributes”

%namespace ns0 http://www.example.org/IUGetMasterOrganizationRecordByIdOrName

%namespace ns01 urn:siperian.api

{CustomerMasterDataDSUltimateResponse :   {((payload.ns01#searchQueryReturn.*ns01#record  map

DSUltimate:   {

(  $.*field map {

(ElectronicAddressTypeCd: $.ns01#stringValue ) when $.ns01#name == “EADDR_TYPE_CD” and  $.ns01#stringValue !=  null and $.ns01#stringValue != “”,

(ElectronicAddressTxt: $.ns01#stringValue ) when $.ns01#name == “EADDR_TXT” and   $.ns01#stringValue != null and $.ns01#stringValue != “”,

(DSUltimateId: trim $.ns01#stringValue) when $.ns01#name == “IU_ROWID”,

(DSUltimateNm : $.ns01#stringValue) when $.ns01#name == “IU_NM”,

(DSUltimateClassificationCd : $.ns01#stringValue) when $.ns01#name == “IU_CLASS_CODE”,

(ApprovalStatusDSUltimateRequesterId : $.ns01#stringValue) when $.ns01#name == “APRV_STS_IU_RQSTR_ID”,

(DSUltimateCreateReasonCd : $.ns01#stringValue) when $.ns01#name == “IU_CRE_RSN_CD”,

(DSUltimateApprovalStatusCd : $.ns01#stringValue) when $.ns01#name == “IU_APRV_STS_CD”,

(DSUltimateEffectiveStartDtm : $.ns01#dateValue) when $.ns01#name == “EFF_STRT_DTM”,

(DSUltimateEffectiveEndDtm : $.ns01#dateValue) when $.ns01#name == “EFF_END_DTM”  and  $.ns01#stringValue !=  null and $.ns01#stringValue != “”

}

)

}

) distinctBy  $.DSUltimate.DSUltimateId )}}

 

 

 

Step 6:

In logger write  #[payload]

 

Step 7:

Save & Run your Mule project.

Step 8:

Send xml file using postman to the project

 

Step 9:

The output is

 

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

How to determine the source of inbound endpoints in Mule ESB?

26

From the technical centre of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about determining the source of inbound endpoints in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

Imagine a situation where you were given a ton of xml files containing mule flows and you need to draw the process diagrams showing the message paths between the flows. While working on the same, you will see inbound endpoints and then outbound endpoints. As you were tasked to draw process diagrams, it is important for you to fine the source of inbound end points. You also make need to understand whether the inbound endpoints come as outbound endpoints from other flows or where do they come from and where do they go?

SOLUTION:

Based on our understanding at Massil Technologies, the below steps worked out great for us.

Run your project in debug mode in your Anypoint Studio. Ensure that you have put a breakpoint to it, then do a step-by-step run. You may need to do this if there are several scenarios for every inbound payload.

CREATING A NEW PROJECT:

In The Anypoint studio top left click on the file => New => Mule Project, Click on the Mule Project enter the your project name and click finish.

Drag and drop the connecters you required from the Mule Palette to the canvas. In this case we drag and drop the HTTP connector and setpayload and logger for simple debugging process.

 

 

 

 

 

 

Studio’s Visual Debugger allows you to run your application in Debug mode, stopping execution to check the contents of a message at previously-specified building blocks.

To do this, you set a breakpoint at any building block in your flow that you wish to check or test (see image below). When you run your application in Debug mode, the application stops immediately after executing the building block with the breakpoint. Using the Mule Debugger View, you can browse through the contents of the message as it exists at that point in the flow, and evaluate Mule Expressions against the message.

Studio Debugger only works with Enterprise Edition runtimes and does not connect to Comunity Edition runtimes.

Note that the Visual Debugger is completely distinct from the Java Debugger contained in Studio. In fact, you may run both debuggers concurrently on your application.

Prerequisites :

When debugging on your local machine, Studio Visual Debugger listens for incoming TCP connections on localhost port 6666. This port must be available on your machine, i.e. not blocked by a firewall or other security software.

If you plan to remotely debug an application running in an external Mule ESB server, you will need connectivity between your machine (where you run Studio Visual Debugger) and the ESB server’s debug port, by default 6666. Ensure that there are no routing issues or firewalls blocking access between your host and the ESB server’s debug port.

Setting Breakpoints :

Right-click a building block, then select Toggle breakpoint.

Studio applies a red dot to the building block’s icon on the canvas.

When you run your application in Debug mode, Studio stops the flow execution at the breakpoint you have set, allowing you to check the message contents in the Mule Debugger View.

Running in Debug Mode

In the Package Explorer pane, right-click your application, then select Debug As > Mule Application. Studio begins running the application in Debug mode, and displays the Confirm Perspective Switch window.

Click Yes to open the Debug perspective, from which you can access the full functionality of the Visual Debugger.

Viewing Message Data at a Breakpoint

When you begin running your application in Debug mode, Mule opens the Mule Debug perspective. Until a message arrives at the first breakpoint, the Mule Debugger View in the console displays a message that reads, “Connected with mule ESB. Waiting for a mule message to arrive!”

The image below illustrates the Mule Debug perspective

When a message arrives at the first breakpoint, the Mule Debugger View displays two panes, as shown below

In this case you check the payload by using X+Y=? symbol from the HTTP connector it will give the nullpayload press F6 to the next processor and F7 Run To Processor and F8 Resume press F6 to the check payload coming from setpayload shown below

From the setpayload we get the payload/hello because in the setpayload we write value like.

it will takes the value from browser http path like below…

So finally we get the payload in console and browser

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

How to handle JSON transformer java heap error in Mule ESB

25

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about handling JSON transformer java heap error in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

In one of our working scenarios, we have been using an object to JSON transformer. We have noticed the payload to the object on JSON transformer is huge which resulted in JAVA heap error. We had futile efforts of using data weave to convert the payload to JSON. It has been very challenging to find a fix. We are listing out the best fix that worked for us below out of many ways we tried.

Solution:

In DataWeave use, mode=”immediate” try to change to ‘mode=” deferred” this allows DataWeave output to remain outside of the heap.

Increase DataWeave memory by adding com.mulesoft.dw.buffersiz= ${Increase value as required by your case (bytes) } in system Property ( Default value 1572864)

If you are running 3.8.3v version or lower, DataWeave limitation to handle up to 2GB of data, but there is no limitation of data from 3.8.4v onwards

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed the above-mentioned scenario. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

Merging two JSON outputs into one with Dataweave in Mule ESB

24

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about merging two JSON outputs into one using dataweave in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

We now are going to see a step by step approach to merge two JSON outputs into one.

Step 1:

In Anypoint Studio click File > New > Mule Project to create a new project, give it whatever name you wish and click Finish.

Step 2:

Drag an HTTP Connector from the palette to your empty canvas, then a set payload

Step 3:

In set, payload take JSON file

Step 4:

Drag a json-to-object from the palette

Step 5:

Drag a variable from the palette

Step 6:

Drag an set payload from the palette

Step 7:

 

drag an Transform Message from the palette

 

filtering UserPayload

%dw 1.0

%output application/json

payload map (action, index) -> {

user: action.user,

action: action.action,

FileName: action.FileName,

userId: (flowVars.UserPayload filter $.objectId == action.user)[0].objectName

}

Step 8:

 

Run your Mule project

Step 9:

Send JSON file using postman to the project

The output is

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

 

How to invoke java method into the flow of Mule ESB?

23

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about invoking java method into the flow of Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

If Mule ESB needs to built on top of Java and Spring, the strong integration capabilities on Mule ESB helps to invoke Java method. There are many ways to invoke java method into Mule ESB flow. Based on our expertise at Massil Technologies, we are going to discuss the most effective and most simple method know as “Invoke Component”.

Invoke Component:

By using  Invoke component we can invokes a specified method of an object defined in a Spring bean. we can provide an array of argument expressions to map the message to the method arguments. We provide the method name and with that Mule determines which method to use, along with the number of argument expressions provided. Mule automatically transforms the results of the argument expressions to match the method argument type, where possible and if you have some advanced objects as your input argument you can always give the argument types along with the argument array.

Note: Mule does not support multiple methods with the same name and same number of arguments. Which most of us Java developer know as method overloading.

Configuring the Invoke Component:

Use the invoke component when you have an existing method defined in custom Java code that you wish to use in processing a message. Configuring an invoke message processor involves two steps:

  1. Include the object that contains the method in the application’s src/main/java
  2. Configure the invoke message processor to reference the method in that object.

Field Description Required? Example XML
Display Name Customize to display a unique name for the component in your application. doc:name=”Invoke”
Name The name of the message processor for logging purposes. name=”someName”
Object Ref Reference to the object containing the method to be invoked. X object-ref=”beanName”
Method The name of the method to be invoked. X method=”addTwoNumbers”
Method Arguments Comma-separated list of Mule expressions that, when evaluated, are the arguments for the method invocation. methodArguments=”#[1], #[2]”
Method Argument Types Comma-separated list of argument types for the method invocation. Use when you have more than one method with the same name in your class. `methodArgumentTypes=”java.lang.Float, java.lang.Float” `

Step1:

In Anypoint Studio click File > New > Mule Project to create a new project, give it whatever name you wish and click Finish.

Step2:

Drag an HTTP Connector from the palette to your empty canvas

Step3:

 

Click on the HTTP Connector to open its properties editor, then click the green arrow icon to create a new configuration for it. Leave all of the fields in default and click OK.

Example

The following example creates a “Hello World” flow with an invoke component to implement a specific method in a referenced class.

  1. Right-click src/main/javaand click New > Class.
  2. Paste the code that follows and save your project.
  3. If you have not done so already, click RunRun As > Mule Application.

Step4:

Ensure that the GreetingService Java class is included in your project structure:

Step5:

In our mule flow xml file lets define a spring bean by which we will be invoking the java class

 

<spring:beans>

<spring:bean name=”beanName” class=”invoke.ClassName”/>

</spring:beans>

 

<flow

 

<invoke object-ref=”beanName” method=”addTwoNumbers” methodArguments=”#[1], #[2]” methodArgumentTypes=”java.lang.Float, java.lang.Float” name=”someName” doc:name=”Invoke”/>

 

 

</flow>

 

As you see we have not defined anything special and above bean is just plain old spring bean.

Step6:

Drag the invoke component from the mule palette.

Step7:

 

Configure the invoke component to reference the Java class:

Complete Flow:

 

Step8:

Using postman make a call to your endpoint

All your components will be invoked and processed. Below is the screenshot of the log

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed the scenario mentioned above. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

All you need to know about DataWeave Flat File Schemas

22

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about all you need to know about DataWeave Flat File Schemas in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

According to Mulesoft website, DataWeave allows you to process several different types of data. For most of these types, you can import a schema that describes the input structure to have access to valuable metadata at design time. See To Define Input and Output Structure of a Transformation. DataWeave uses a YAML format called FFD (for Flat File Definition) to represent flat file schemas. The FFD format is very flexible to support a range of use cases but is based on the concepts of elements, composites, segments, groups, and structures.

Schemas must be written in Flat File Schema Language, with a .ffs extension. This language is very similar to EDI Schema Language (ESL), which is also accepted by Anypoint Studio. n DataWeave, you can bind your input or your output to a flat file schema through a property. See below for instructions on how to do this through the Studio UI.

Applying a Schema as Metadata

Here’s a quick step by step example of how – once you have built out your full schema – you can easily apply it to the input metadata of a Transform Message component. This example uses a flat file schema that describes a Cobol Copybook format.

Step1

Download the sample schema file here.

Step2

In Anypoint Studio click File > New > Mule Project to create a new project, give it whatever name you wish and click Finish.

Step3

Drag an HTTP Connector from the palette to your empty canvas, then a Transform Message Component.

Step4:

Click on the HTTP Connector to open its properties editor, then click the green arrow icon to create a new configuration for it. Leave all of the fields by default and click OK.

Step5

Select the Metadata tab and click the Set Metadata button.

Step6

Click the Edit icon that appears next to the Payload element

Step7

Click the green plus icon to add a new metadata type and name it ‘my-flat-file-type’

Step8

Select Flat File as the type, on the Schema field point it to the location of your schema file. Then click Select to use this Metadata type.

Step9

Note that if you open the Transform Message component, the input section should show the fields that will be present in the incoming payload.

Step10

in logger write  #[message.payload]

Step11

Run your Mule project.

For more information visit https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-flat-file-schemas

Step12

Send csv using postman to the project

The output will be grouped accordingly.

 

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

How to see 302 http status with header name location in Mule ESB?

21

From the technical centre of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog introduces how to see 302 http status with header name location in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

While working on Mule ESB certifications, our team has come across the following situation: When you see there is a location header and http status 302 present in the code, Mule ESB http listener internally calls the location URL and gets the response with http status 200 which looks like a normal behavior. The requirement is to see is a 302 http status and a header with name location in Mule ESB postman.

Massil Technologies has come up with the following solution:

Step1: Go to POSTMAN setting as shown in the image:

 

Step2: Turn off the follow redirect as below:

 

Step3: Go to the interceptor on the top right as shown below:

Step4: If you don’t have the necessary interceptor, install it from from the following link: https://chrome.google.com/webstore/detail/postman-interceptor/aicmkgpgakddgnaphhhpliifpcfhicfo

After installing restart the POSTMAN client.

Step5: Go again to the interceptor on top right and enable it as below:

Step6: Test your URL again as below, now you can track 302 in POSTMAN as below:

Step7: In the Mule ESB console you can see it has redirected although POSTMAN has tracked 302:

Mule Flow is as shown below:

XML CODE:

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

 

<mule 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:spring=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd

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

<http:request-config name=“HTTP_Request_Configuration2” host=“localhost” port=“8081” doc:name=“HTTP Request Configuration”/>

<http:listener-config name=“HTTP_Listener_Configuration” host=“0.0.0.0” port=“8081” doc:name=“HTTP Listener Configuration”/>

<flow name=“xpathforeachFlow”>

<http:listener config-ref=“HTTP_Listener_Configuration” path=“/xpath” doc:name=“HTTP”/>

<logger message=“inside another flow” level=“INFO” doc:name=“Logger”/>

<set-payload value=“this is a test flow” doc:name=“Set Payload”/>

</flow>

<flow name=“MainFlow”>

<http:listener config-ref=“HTTP_Listener_Configuration” path=“/location302” doc:name=“HTTP” >

</http:listener>

<logger message=“inside 302” level=“INFO” doc:name=“Logger”/>

<set-property propertyName=“http.status” value=“#[‘302’]” doc:name=“Property”/>

<set-property propertyName=“location” value=“http://localhost:8081/xpath” doc:name=“Property”/>

<logger message=“after property setting #[message]” level=“INFO” doc:name=“Logger”/>

</flow>

</mule>

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed the situation we have come across. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech

How to consume an exception from a Mule ESB synchronous flow invoked from an until-successful scope?

20

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog introduces how to consume an exception from a Mule ESB flow invoked from an until-successful scope. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

 

In one another noticeable situation, we have come across while using Mule ESB until-successful scope to perform a wait-and-retry on a call to a REST API that can return a “rate limiting” response along with an HTTP response header containing a retry-duration time limit. As it is needed to handle the rate-limiting exception (to get the retry duration), we made the scope invoke a synchronous flow containing its own exception strategy. If we do not use a separate, synchronous flow the until-successful scope takes over catching the exception as its default behavior.

Our external-to-scope handling of the exception (which currently includes setting an exception message and code) is enough to stop the until-successful loop. We were in a situation to know if we can “consume” exceptions in a mule exception strategy and have execution continue the flow from the point the exception was?

Another area of debate is that if is it enough to not set any exception message payloads to consume an exception? We tried setting the payload to null after the exception was caught but the scope still terminated. We were in a dilemma if we were doomed to let the until-successful scope always handle exceptions, meaning we can’t interject with any exception handling of our own.

To overcome this situation we tried many options and the solution below seemed to be the best fit.

The most important part to notice is the failureExpression=”#[message.payloadAs(java.lang.String).equals(“FAILED”)]” setting and the set payload .

These two settings need to be configured to get the behavior desired.

Mule ESB Flow:

The most important part to notice is the failureExpression=”#[message.payloadAs(java.lang.String).equals(“FAILED”)]” setting and the set payload

 

XML Code:

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

 

<mule 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:spring=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd

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

<http:request-config name=“HTTP_Request_Configuration” host=“www.google.co.nz” port=“80” doc:name=“HTTP Request Configuration”/>

<http:listener-config name=“HTTP_Listener_Configuration” host=“0.0.0.0” port=“8081” doc:name=“HTTP Listener Configuration”/>

<flow name=“privateFlowUntillSuccessful”>

<until-successful  maxRetries=“3” millisBetweenRetries=“5000” doc:name=“Until Successful” synchronous=“true” failureExpression=“#[message.payloadAs(java.lang.String).equals(&quot;FAILED&quot;)]”>

<flow-ref name=“retryFlowConstruct” doc:name=“retryFlowConstruct”/>

</until-successful>

<logger level=“INFO” doc:name=“Logger”/>

<catch-exception-strategy doc:name=“Catch Exception Strategy” enableNotifications=“false” logException=“false”>

<logger message=“privateFlowUntillSuccessful Exception encountered #[message]” level=“INFO” doc:name=“Logger”/>

</catch-exception-strategy>

</flow>

<flow name=“retryFlowConstruct”>

<http:listener config-ref=“HTTP_Listener_Configuration” path=“/untill” doc:name=“HTTP”/>

<http:request config-ref=“HTTP_Request_Configuration” path=“/search?q=java.util.logging.Level” method=“GET” doc:name=“Google Search”/>

<logger message=“This logger will never be called” level=“INFO” doc:name=“Logger”/>

<catch-exception-strategy enableNotifications=“false” logException=“false” doc:name=“Catch Exception Strategy”>

<logger message=“Retry Failure #[exception]” level=“INFO” doc:name=“Logger”/>

<logger message=“Retry Failure Exception Payload:  #[payload.getExceptionPayload().getException()]” level=“INFO” doc:name=“Logger”/>

<set-payload value=“#[&quot;FAILED&quot;]” doc:name=“Set Payload”/>

</catch-exception-strategy>

</flow>

</mule>

 

This helped us consume an exception from a Mule ESB synchronous flow invoked from an until-successful scope. Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed the weird situation. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech