Usage of a Respond Mediator in WSO2 ESB


In a situation like you want to send a message back to the client depending on some conditions, we can use Respond Mediator. This mediator stops the processing on the current message and sends the message back to the client as a response. So just before this mediator we can use mediator like Payload Factory Mediator to construct the response.

Following is a simple use case for this,

Continue reading

How to move to out sequence from in sequence in WSO2 ESB


I faced a situation, where I need to send a custom response after the request is being sent to the message queue. I couldn’t use FORCE_SC_ACCEPT property as I need to send a response. In such a case we can use loopback mediator which switch from in sequence to out sequence and in side the out sequence payload factory mediator can be used to construct the custom response.

Simple skeleton as follows,

Continue reading

JSON Support for WSO2 ESB Class Mediator


In WSO2 documentation you can find how the following mediators can be used with json payloads.

  • Log Mediator
  • Property Mediator
  • PayloadFactory Mediator
  • Switch Mediator
  • Filter Mediator

But there’s no any clue about how the class mediator can be used with json payloads. Following is the way you can do manipulation to json payload using class mediator.

In this post I just add a new tag to existing json payload

package com.wso2.test;

import org.apache.synapse.MessageContext;
import org.apache.synapse.commons.json.JsonUtil;
import org.json.JSONObject;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

/**
* Class mediator for JSON transformation.
*
* @author isuru
*
*/
public class JSONClassMediator extends AbstractMediator {

/**
* Holds the name.
*/
private String nameParam;

/**
* Mediate overridden method to set the token property.
*/
@Override
public boolean mediate(MessageContext context) {

try {

// Getting the json payload to string
String jsonPayloadToString = JsonUtil
.jsonPayloadToString(((Axis2MessageContext) context)
.getAxis2MessageContext());
// Make a json object
JSONObject jsonBody = new JSONObject(jsonPayloadToString);

// Adding the name:nameParam.
jsonBody.put("name", getNameParam());

String transformedJson = jsonBody.toString();

// Setting the new json payload.
JsonUtil.newJsonPayload(
((Axis2MessageContext) context).getAxis2MessageContext(),
transformedJson, true, true);

System.out.println("Transformed JSON body:\n" + transformedJson);

} catch (Exception e) {
System.err.println("Error occurred: " + e);
return false;
}

return true;
}

/**
* @return the nameParam
*/
public final String getNameParam() {
return nameParam;
}

/**
* @param nameParam
* the nameParam to set
*/
public final void setNameParam(String nameParam) {
this.nameParam = nameParam;
}
}

Continue reading

Usage of Fault mediator in WSO2 ESB


This post explains, an usage of Fault mediator in wso2 ESB.

The sample scenario, refuses and sends an exception back to the client based on the Content-Type header property.

This example is tested in ESB 4.7.0

First validate the content-type - using filter mediator

If true, send back an exception to the client - using fault mediator

If false, continue the flow

Here is the sample proxy,

Continue reading

WSO2 ESB Rule Mediator Example


This post is to demonstrate how to write Rule mediator in WSO2 ESB.
This example is tested in WSO2 ESB 4.7.0
The example, get the list of students with marks for their subjects and, inside the proxy it check whether the each student has passed the each subject and print the result. To define type of the input data POJO adapters are used in this example.

1.Write the following POJO classes.

Student class

package com.example.rulemediator;

public class Student {

	private String indexNumber;
	private String name;
	private Subject[] subjects;

	public String getIndexNumber() {
		return indexNumber;
	}
	public void setIndexNumber(String indexNumber) {
		this.indexNumber = indexNumber;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Subject[] getSubjects() {
		return subjects;
	}
	public void setSubjects(Subject[] subjects) {
		this.subjects = subjects;
	}
}

Subject class

package com.example.rulemediator;

public class Subject {

	private String subjectCode;
	private String subjectName;
	private int score;

	public String getSubjectCode() {
		return subjectCode;
	}
	public void setSubjectCode(String subjectCode) {
		this.subjectCode = subjectCode;
	}
	public String getSubjectName() {
		return subjectName;
	}
	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
}

2. Then create a Jar file containing above classes and copy it to ESB_HOME/repository/components/lib folder and start the ESB.

Continue reading

WSO2 ESB Spring Mediator Example


This post is to demonstrate how to initialize and use a SpringBean as a mediator.

We are going to print the HELLO ‘Argument’ in this example. And this ‘argument’ is passed using the spring configuration file. This example is tested in WSO2ESB 4.6.0

1. Build the jar file of the following class and place it in ESB_HOME/repository/components/lib folder.

package com.test.spring.mediators.springmediator;

import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class HelloWorld extends AbstractMediator{
   private String message;

   public void setMessage(String message){
      this.message = message;
   }

   public boolean mediate(MessageContext arg0) {
      System.out.println("HELLO "+message);
      return true;
   }
}

2. Start the WSO2ESB and add the following spring configuration file (springConfig.xml) under /_system/config/repository/springConfig.xml in the ESB Registry.

spring configuration file (springConfig.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="springtest" class="com.test.spring.mediator.springmediator.HelloWorld" singleton="false">
      <property name="message"><value>ISURU</value></property>
   </bean>
</beans>

Continue reading