How to process CSV with WSO2 Class Mediator

Dilan Tharaka
2 min readJun 3, 2020

When using WSO2 Enterprise Integrator, the Class Mediator is a useful mediator. When there’s a scenario where we can’t use the existing mediators to resolve, we have to write a custom mediator using the Class Mediator. Class Mediator is a custom Java code written by the user, especially for a relevant task.

Purpose of this article is not to provide details on how to get started with Class mediators. If you’re not familiar with Class Mediator, then I found this article helpful to get started from scratch.

JSON, XML and CSV are the most common payload types that are used today in web services. In this article, I’m discussing how to process a CSV payload using Class Mediator. Following is a ClassMediator I developed to read CSV payload and make the second column of the CSV to upper case. Then set the modified CSV to the payload.

We can send a CSV payload via a web endpoint or using a file connector to read a CSV file. Either way, CSV payload is transferred as a text payload in the message context.

So, processing a CSV file in the Class Mediator means, we’re processing the text payload in the message context.

We can use org.apache.synapse.util.PayloadHelper( which is provided in default mediator project) to get and set text payload from the message context.

To process the payload, we can use any third party library ( in this code I used OpenCSV). If the library we use is not provided at the runtime of MI, then we would get a ClassNotFoundException. If so, we have to download the Jar file for the library and copy it to the MI_HOME/dropins (MI_HOME is the directory you installed Micro Integrator) directory and restart MI.

If you want to try out this example, clone following GitHub repository and open it in the Integration Studio.

--

--

Dilan Tharaka

Software engineer curious about how things actually work