WSO2 ESB provides the facility to create your own
custom mediator and use it. There might be possibility in your requirement that
in some cases you have no option except to create custom mediator.
In Custom mediator you can write your own logic and use
in ESB.
To write a custom mediator I am using Developer studio,
you can write it in normal eclipse or net beans only thing is that you need to
have supporting library files.
Go to the File ---à
New ---à Other and choose Mediator Project under WSO2 then click Next to open below screen.
Now Create New Mediator, this is default radio button
selected, click on Next button.
Give the Project Name, Package Name and Class name as
required in above screen shot. Click Next button.
Click Finish button.
This is the class you have created now you can write
your logic which needs to be used in ESB.
In next blog (http://shriwithjava.blogspot.co.nz/2015/08/how-to-use-custom-mediator-in-wso2-esb.html) we will use this mediator to fetch multiple
rows from database and show them in ESB without using DSS or other mediator
like DBlookup or DBReport.
Java code to call multiple rows from database is here:
package java.is.easy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.synapse.ManagedLifecycle;
import org.apache.synapse.MessageContext;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.mediators.AbstractMediator;
public class MyCustomMediator extends AbstractMediator implements
ManagedLifecycle {
private String email = null;
private String pass = null;
private Connection conn = null;
public boolean mediate(MessageContext msgCtx) {
System.out.println("++++++++++++++++++++++++ Start mediate function++++++++++++++++++++++++++++++++++++");
boolean traceOn = isTraceOn(msgCtx);
boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
if (traceOrDebugOn) {
traceOrDebug(traceOn, "Start : MyCustomMediator mediator");
}
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3308/emp", "root", "root");
Statement stmt = conn.createStatement();
//getting variable through message context
email=(String)msgCtx.getProperty("email");
pass=(String)msgCtx.getProperty("pass");
String query = "select * from User_Customer where email_id='"+email+"' and password='"+pass+"'";
System.out.println(query);
//execute query
ResultSet rs = stmt.executeQuery(query);
System.out.println("arg 1 is : " + email);
System.out.println("arg 2 is :" + pass);
//generating custom body
org.apache.axiom.soap.SOAPBody body = msgCtx.getEnvelope().getBody();
System.out.println("Step 1");
//body.getFirstElement().detach();
System.out.println("Step 2");
OMFactory factory = OMAbstractFactory.getOMFactory();
System.out.println("Step 3");
OMNamespace ns1 = factory.createOMNamespace("http://java.is.easy/custommediator", "p");
OMElement root = factory.createOMElement("EmployeeDetails", ns1);
OMText email_txt = null;
OMElement email_El = null;
System.out.println("Step 4");
while (rs.next()) {
email_El = factory.createOMElement("email_id", ns1);
email_txt = factory.createOMText(email_El,rs.getString("email_id"));
System.out.println("adding child");
root.addChild(email_El);
email_El = factory.createOMElement("pass", ns1);
email_txt = factory.createOMText(email_El,rs.getString("password"));
System.out.println("adding child");
root.addChild(email_El);
email_El = factory.createOMElement("first_name", ns1);
email_txt = factory.createOMText(email_El,rs.getString("first_name"));
System.out.println("adding child");
root.addChild(email_El);
email_El = null;
email_txt = null;
}
System.out.println("adding body");
body.addChild(root);
rs.close();
stmt.close();
conn.close();
System.out.println("adding body");
} catch (Exception e) {
e.printStackTrace();
return false;
}
System.out.println("++++++++++++++++++++++++ end mediate function++++++++++++++++++++++++++++++++++++");
return true;
}
public void destroy() {
}
@Override
public void init(SynapseEnvironment arg0) {
// TODO Auto-generated method stub
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.synapse.ManagedLifecycle;
import org.apache.synapse.MessageContext;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.mediators.AbstractMediator;
public class MyCustomMediator extends AbstractMediator implements
ManagedLifecycle {
private String email = null;
private String pass = null;
private Connection conn = null;
public boolean mediate(MessageContext msgCtx) {
System.out.println("++++++++++++++++++++++++ Start mediate function++++++++++++++++++++++++++++++++++++");
boolean traceOn = isTraceOn(msgCtx);
boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
if (traceOrDebugOn) {
traceOrDebug(traceOn, "Start : MyCustomMediator mediator");
}
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3308/emp", "root", "root");
Statement stmt = conn.createStatement();
//getting variable through message context
email=(String)msgCtx.getProperty("email");
pass=(String)msgCtx.getProperty("pass");
String query = "select * from User_Customer where email_id='"+email+"' and password='"+pass+"'";
System.out.println(query);
//execute query
ResultSet rs = stmt.executeQuery(query);
System.out.println("arg 1 is : " + email);
System.out.println("arg 2 is :" + pass);
//generating custom body
org.apache.axiom.soap.SOAPBody body = msgCtx.getEnvelope().getBody();
System.out.println("Step 1");
//body.getFirstElement().detach();
System.out.println("Step 2");
OMFactory factory = OMAbstractFactory.getOMFactory();
System.out.println("Step 3");
OMNamespace ns1 = factory.createOMNamespace("http://java.is.easy/custommediator", "p");
OMElement root = factory.createOMElement("EmployeeDetails", ns1);
OMText email_txt = null;
OMElement email_El = null;
System.out.println("Step 4");
while (rs.next()) {
email_El = factory.createOMElement("email_id", ns1);
email_txt = factory.createOMText(email_El,rs.getString("email_id"));
System.out.println("adding child");
root.addChild(email_El);
email_El = factory.createOMElement("pass", ns1);
email_txt = factory.createOMText(email_El,rs.getString("password"));
System.out.println("adding child");
root.addChild(email_El);
email_El = factory.createOMElement("first_name", ns1);
email_txt = factory.createOMText(email_El,rs.getString("first_name"));
System.out.println("adding child");
root.addChild(email_El);
email_El = null;
email_txt = null;
}
System.out.println("adding body");
body.addChild(root);
rs.close();
stmt.close();
conn.close();
System.out.println("adding body");
} catch (Exception e) {
e.printStackTrace();
return false;
}
System.out.println("++++++++++++++++++++++++ end mediate function++++++++++++++++++++++++++++++++++++");
return true;
}
public void destroy() {
}
@Override
public void init(SynapseEnvironment arg0) {
// TODO Auto-generated method stub
}
}
No comments:
Post a Comment