You are on page 1of 9

More vats.namit@gmail.

com New Post Desi

TCB
WSO2 ESB - How to use PayloadFactory Mediator thecodebuddy

How to use PayloadFactory Mediator

TCB

About Me

thecodebuddy
View my complete profile
As per the WSO2-ESB documentation of PayloadFactory Mediator, it is stated that this mediator can be
used to transform/ replace the contents of a message at the runtime.
FB Page
To experience how it works, lets have a working demo.
For the same, as discussed in earlier tutorials, have the local setup of WSO2-ESB up and running.

For reference, feel free to drop by @  Find me on Quora


http://thecodebuddy.blogspot.in/2016/12/wso2-esb-i-came-across-this-thing.html

Our plan of action will be something like this -


1. One working url that can return as something when invoked
Get code on Github
2. An API, that works in 2 parts -
a. Inbound: Invokes the above mentioned url Follow @namitsharma99
b. Outbound: Transforms the message received using PayloadFactory
So, the plan sounds simple, and the coding will sound simpler too. html-css-badge
Let’s begin -

1. We need a URL/ Restful API that provides us a json data.


Why not create one instead of depending on any 3rd party?
Here is the mechanism, and it will make you more comfortable with ESB -
Exam 480:…
Create a sequence, to use PayloadFactory, to create JSON data. Issuer: Microsoft…
Provided by
Note – As a good practice, use Log mediator, so that you can trace your code flow in the console. Acclaim
I generally prefer using values as shown below, in logging. It’s up to you what you prefer.
Moving on, once you create your sequence, it will look somewhat like this -

Contact form

Name

Email *

The XML view for the above is -

? l i "1 0" di "UTF 8"?


<?xml version="1.0" encoding="UTF-8"?> Message *
<sequence xmlns="http://ws.apache.org/ns/synapse" name="myPayloadSeq"
trace="enable">
<log category="DEBUG" level="full">
<property name="where?" value="inner sequence acting as API - START" />
</log>
<payloadFactory media-type="json">
<format>{"id": "201", "fname": "Ramlal", "lname": "Gabbar"}</format>
</payloadFactory> Send
<log category="DEBUG" level="full">
<property name="where?" value="inner sequence acting as API - END" />
</log>
<respond />
</sequence>
Behind the scenes -
What we have done here is using PayloadFactory to create a JSON data, that will be returned whenever
the enclosing API is invoked. We can use this in an API so that it can be treated as an endpoint URL that Blog archive
provides us data.
► 2020 (6)
The APIs once created should look like this - 'MyPayloadApi' ► 2018 (7)
▼ 2017 (38)
► November (1)
► October (1)
► July (2)
► June (1)
► May (3)
► April (4)
▼ March (23)
STRUTS 1.X + FILE
IMAGE UPLOAD
DOWNLOAD + DB
SERVLET + JSP +
DATABASE + IMAGE
UPLOAD AND
DOWNLOAD
Epic Hustle - another
one (a new
The XML for the above is as follows - compilation)

<?xml version="1.0" encoding="UTF-8"?> Write an excel file using


<api xmlns="http://ws.apache.org/ns/synapse" name="myPayloadApi" java code
context="/payloadApi"> Writing a text file using
<resource methods="DELETE POST PUT GET"> java code
<inSequence>
<sequence key="myPayloadSeq" /> JAVASCRIPT SAMPLES
<respond /> - A SMALL SERIES
</inSequence> FOR HANDS ON ...
</resource>
</api> JAVASCRIPT SAMPLES
- TIC TAC TOE GAM
Behind the scenes - & DYNAMIC TA...
We have invoked the sequence, and ended the invocation using Respond mediator. JAVASCRIPT SAMPLES
- HUNGRY SNAKE
JAVASCRIPT SAMPLES
2. Now the big task is here - - SHOOTING GAME

Coming to the main task, we have to implement one API, that will get the message from the so called JAVASCRIPT SAMPLES
remote url (created above), and will modify it using the PayloadFactory mediator. - RACING CARS -
SIMPLE GAME
If we succeed, our mission is accomplished.
Dreams
Let’s try this. Mesmerized...
Design the API, and it should look like - 'InvokePayloadApi' Reading a Text file using
java code
Reading Excel file using
java code
Binary Search
Implementation in
Java
Linear Search
Implementation in
Java
White Paper - The
Unseen Side of DTM
ECSTASY BY NAMIT
MAGMA BY NAMIT
WSO2 ESB - How to us
PayloadFactory
Mediator
JAVA based project, tha
can be used to hit DB
usi...
Javascript Debugging &
Mathematical
Operators
RESTFul CRUD with a
pinch of AJAX and
Hibernate

► February (3)

► 2016 (3)

The XML view -


<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="invokePayloadApi"
context="/invoke">
<resource methods="DELETE POST PUT GET">
<inSequence>
<log level="full" category="DEBUG">
<property name="where?" value="Inside In Sequence that invokes
API" />
</log>
<send>
<endpoint>
<http uri-template="http://192.168.122.1:8280/payloadApi" />
</endpoint>
</send>
<log level="full" category="DEBUG">
<property name="where?" value="Exiting In Sequence that invokes
API" />
</log>
</inSequence>
<outSequence>
<log level="full" category="DEBUG">
<property name="where?" value="Beginning payload transformation
in out sequence" />
</log>
<payloadFactory media-type="xml">
<format>
<hello xmlns="">
<hello1>$1</hello1>
<hello2>$2</hello2>
<hello3>$3</hello3>
</hello>
</format>
<args>
<arg evaluator="json" expression="$.id" />
<arg evaluator="json" expression="$.fname" />
<arg evaluator="json" expression="$.lname" />
</args>
</payloadFactory>
<log level="full" category="DEBUG">
<property name="where?" value="Ending payload transformation in
out sequence" />
</log>
<respond />
</outSequence>
Total Pageviews
</resource>
</api>
28,633
Behind the scenes -
Let’s take a moment to understand what we have done here. Popular Posts
a. Divided the API in 2 parts – Insequence and OutSequence WSO2 ESB - Ho
b. Insequence - use PayloadFac
Mediator
Using Send mediator, invoked the above url (using HTTP endpoint) How to use
PayloadFactory
Note – If you get your localhost changed, like at the restarting of local WSO2 ESB server, make sure Mediator As per the WSO2-E
you change the IP here too. We are talking about the highlighted part in the XML. documentation of PayloadFac
Mediator, it is stated that this
You are free to choose any other working option to invoke the URL, for now I selected this.
Reading Excel file using java
For other readers, you can share your experiences in the comments, so that we all can ponder on new How to use JAVA code to rea
stuffs. excel cells? Here it goes - Fu
available @ https://github.com
c. Outsequence - harma99/fileHandlingUs...
We are trying to convert the incoming json into xml, so we have mapped the values as $.X of ‘X’ key
from the json to the value of custom tags of XML here. Please not how <args> section is responsibly
d i hi k dh f i k f h f
doing this task, and how <format> section takes care of the output format. JAVASCRIPT
SAMPLES - TIC
d. And as usual we have Log mediators for easy tracing in logs. TOE GAME &
DYNAMIC TABL
CREATION IN J
Here are a few highlights of my local logs - Hello Buddies. It's time to play
toe, but this time, not with pen
paper. We will create our own
DEBUG - RESTRequestHandler Located specific API: invokePayloadApi for processing message that allows us to ...
DEBUG - LogMediator To: /invoke, MessageID: urn:uuid:91624339-3f31-466b-8985-8287cfa1bd2a, WSO2 - ESB |
Direction: request, where? = Inside In Sequence that invokes API, Envelope: FOREACH MED
<?xml version="1.0" encoding="UTF-8"?> FOREACH MED
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> In the previous n
<soapenv:Body /> saw how we can
</soapenv:Envelope> custom classes as Mediators.
on to cover other important m
DEBUG - HTTPEndpoint Sending message through endpoint : null resolving to address =
http://192.168.122.1:8280/payloadApi JAVASCRIPT
SAMPLES - SH
DEBUG - LogMediator To: http://192.168.122.1:8280/payloadApi, MessageID: urn:uuid:91624339-3f31- GAME
466b-8985-8287cfa1bd2a, Direction: request, where? = Exiting In Sequence that invokes API, Envelope: Hello Buddies. T
simple shooting
<?xml version="1.0" encoding="UTF-8"?> using mouse cursor and javas
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> Complete code is available he
<soapenv:Body /> https:/...
</soapenv:Envelope>
DEBUG - LogMediator To: /payloadApi, MessageID: urn:uuid:d59b1afb-951d-4b17-a648-a517356fb8df, JAVASCRIPT
Direction: request, where? = inner sequence acting as API - START, Envelope: SAMPLES - RA
CARS - SIMPLE
<?xml version="1.0" encoding="UTF-8"?> Hello Buddies. T
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> is a simple javas
<soapenv:Body /> code written to get look and fe
</soapenv:Envelope> very basic racing game. The
complete code is avai...
DEBUG - PayloadFactoryMediator #mediate. Transformed payload format>>>
{   RESTFul CRUD
   "id":"201", pinch of AJAX a
   "fname":"Ramlal", Hibernate
   "lname":"Gabbar"
} Hello Buddies...
something bigge
DEBUG - LogMediator To: /payloadApi, MessageID: urn:uuid:d59b1afb-951d-4b17-a648-a517356fb8df, time. A commonly known task
Direction: request, where? = inner sequence acting as API - END, Payload: developers generally come ac
{  
   "id":"201", WSO2 ESB - HE
   "fname":"Ramlal", WORLD - CLAS
   "lname":"Gabbar" MEDIATION
} WSO2 - ESB I c
across this thing
DEBUG - StatisticsReporter Created statistics log : StatisticsLog{id='myPayloadSeq', recently, and generally as we
componentType=SEQUENCE}
developers do, I did some hea
DEBUG - SynapseCallbackReceiver Body :  banging. Assuming that theor
w...
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> JAVA code to handle versions
<soapenv:Body /> comparisons.
</soapenv:Envelope> Hello Buddies. Sharing a sma
useful piece of code, that can
you in handling version string
DEBUG - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , comparisons in day-to-day co
SOAPAction: , MessageID: urn:uuid:1e15723a-514b-428e-81d8-7c0bb25fc1a3, Direction: response,
where? = Beginning payload transformation in out sequence, Envelope: O(n^2) vs O(n) techniques for
<?xml version="1.0" encoding="UTF-8"?> the max count of a digit in the
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> numerical String.
<soapenv:Body> Hello Buddies Came across t
<jsonObject> question related Java String h
<id>201</id> so thought of sharing. Questio
<fname>Ramlal</fname> Given a string made up of n n
<lname>Gabbar</lname>
</jsonObject>
</soapenv:Body>
</soapenv:Envelope> Search This Blog
DEBUG - LogMediator End : Log mediator
DEBUG - JsonUtil #transformElement. Transformed OMElement. Result:
<?xml version="1.0" encoding="UTF-8"?>
<jsonObject> Blog archive
<id>201</id>
<fname>Ramlal</fname> ► 2020 (6)
<lname>Gabbar</lname>
</jsonObject> ► 2018 (7)
DEBUG - SynapseJsonPath #stringValueOf. Evaluated JSON path  ▼ 2017 (38)
<$.id> : <201> ► November (1)
DEBUG - JsonUtil #transformElement. Transformed OMElement. Result: ► October (1)
 <?xml version="1.0" encoding="UTF-8"?> ► July (2)
► June (1)
<jsonObject> ► May (3)

<id>201</id> ► April (4)

<fname>Ramlal</fname> ▼ March (23)


<lname>Gabbar</lname> STRUTS 1.X + FILE
IMAGE UPLOAD
</jsonObject> DOWNLOAD + DB
SERVLET + JSP +
DEBUG - SynapseJsonPath #stringValueOf. Evaluated JSON path  DATABASE + IMAGE
<$.fname> : <Ramlal>
UPLOAD AND
DEBUG - JsonUtil #transformElement. Transformed OMElement. Result:  DOWNLOAD

<?xml version="1.0" encoding="UTF-8"?> Epic Hustle - another


<jsonObject> one (a new
<id>201</id> compilation)
<fname>Ramlal</fname> Write an excel file using
<lname>Gabbar</lname> java code
</jsonObject>
Writing a text file using
DEBUG - SynapseJsonPath #stringValueOf. Evaluated JSON path  java code
<$.lname> : <Gabbar>
JAVASCRIPT SAMPLES
- A SMALL SERIES
DEBUG - PayloadFactoryMediator #mediate. Transformed payload format>>> FOR HANDS ON ...
<?xml version="1.0" encoding="UTF-8"?> JAVASCRIPT SAMPLES
<pfPadding> - TIC TAC TOE GAM
<hello> & DYNAMIC TA...
<hello1>201</hello1>
<hello2>Ramlal</hello2> JAVASCRIPT SAMPLES
<hello3>Gabbar</hello3> - HUNGRY SNAKE
</hello>
</pfPadding> JAVASCRIPT SAMPLES
- SHOOTING GAME
DEBUG - LogMediator Start : Log mediator JAVASCRIPT SAMPLES
DEBUG - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , - RACING CARS -
SOAPAction: , MessageID: urn:uuid:1e15723a-514b-428e-81d8-7c0bb25fc1a3, Direction: response,
where? = Ending payload transformation in out sequence, Envelope: SIMPLE GAME
Dreams
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> Mesmerized...
<soapenv:Body>
<hello> Reading a Text file using
<hello1>201</hello1> java code
<hello2>Ramlal</hello2>
<hello3>Gabbar</hello3> Reading Excel file using
</hello> java code
</soapenv:Body>
</soapenv:Envelope> Binary Search
Implementation in
DEBUG - LogMediator End : Log mediator Java
DEBUG - RespondMediator Start : Respond Mediator Linear Search
DEBUG - RespondMediator End : Respond Mediator Implementation in
DEBUG - SequenceMediator End : Sequence <anonymous>
Java
White Paper - The
Unseen Side of DTM
Here is my output, checked using Mr. Postman :P
ECSTASY BY NAMIT
MAGMA BY NAMIT
WSO2 ESB - How to us
PayloadFactory
Mediator
JAVA based project, tha
can be used to hit DB
usi...
Javascript Debugging &
Mathematical
Operators
RESTFul CRUD with a
Hope you enjoyed this one. pinch of AJAX and
Hibernate
Happy Coding !
► February (3)

at March 15, 2017 ► 2016 (3)


Labels: ESB, Payload Mediator, WSO2, XML JSON transformation

Newsletter
1 comment:
Email address...
sheela rajesh 21 April 2019 at 23:59

Really nice blog and impressive information you gave us.Thank you and i will expect more in future.
JAVA Training in Chennai
Best JAVA Training institute in Chennai
Python Training in Chennai
Selenium Training in Chennai
Android Training in Chennai
Big data training in chennai
JAVA Training in Chennai
Java Training in Tambaram

Reply Delete

Enter your comment...

Comment as: thecodebuddy ( Sign out

Publish Preview Notify me

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

Featured post

How to take backup of MYSQL tables daily and remove the older backups

In day to day heavy transactions happening in our DB, we sometimes wish to retain the previous states of the tables. This not only prevents ...

Ads
WSO2 ESB - How to use PayloadFactory Mediator
How to use PayloadFactory Mediator As per the WSO2-ESB documentation of PayloadFactory Mediator, it is stated that this me...

Reading Excel file using java code


How to use JAVA code to read the excel cells? Here it goes - Full code is available @ https://github.com/namitsharma99/fileHandlingUs...

JAVASCRIPT SAMPLES - TIC TAC TOE GAME & DYNAMIC TABLE CREATION IN JSP
Head First Java:
Hello Buddies. It's time to play tic-tac-toe, but this time, not with pen and paper. We will create our own page that allows us to ...
A...
INR 870.00

Shop now
Labels

AJAX algo Amazon Web Services Android Angular 2 Angular Components Apache httpd apache poi API API GATEWAY APK Automate build AWS AWS and Gmail AWS Email AWS Lambda AWS Lambda Node JS AWS Notification
SES backup binary search binary tree Bootstrap browser issue BST chat Cheat sheet chrome class mediator cloning code code-snippet Complexity Analysis concurrency consumer core java css CSV curl custom mediator dail
structures Data Structures and Algorithms database management deep download DSA DTMF eclipse EDM Electronic Digital Music electronic music Electronics Enum epic violins ESB esb5 events EXample angular 2 ex

fedora file firefox foreach mediator game games Git Git Hub Git Lab Git Repo Go Go Tutorials GoLang hello world Hibernate hip hop html HTTP GET POST PUT DELETE J2EE Java JAVA. Core JAVA java
Jenkins jquery jQuery in Angular 2 JS JSP kafka KEYGEN LAMBDA linear search mediation messaging microservices mozilla MSTSC multi threading MySQL NG-2 NODEJS NPM Path Annotations Payload Mediator POJO produc
PUTTY quick reference read text file read write csv remote access REST S3 schedulers sequence Server shallow shooting game small programs snake game socket programming Speech to Text springboot SSH stored procedures
questions tables text file writer thecodebuddy tic-tac-toe game time complexity topic Trance TUNNEL unix upload version-check white paper WINDOWS Workbench WSO2 WSO2 ESB XML JSON transformation zookeeper

Awesome Inc. theme. Powered by Blogger.

You might also like