How to create soap client for ofbiz service or ofbiz service invocation from another application.
Sometimes ofbiz developer need to integrate ofbiz with 3rd party or another web application. This is very simple to create soap service in ofbiz.In ofbiz here soap engine to mail soap service.
To create soap service or make service exportable follow the bellow steps:
1. Set export="true" in service definition for example
<service name="createColleague" engine="java" auth="true"
location="org.ofbiz.crm.colleague.ColleagueService" invoke="createColleague" export="true" >
</service>
You find all exportable service or soap service wsdl under webtools component.
Open a your browser new tab and below url,
http://localhost:8080/webtools/control/SOAPService?wsdl
Here you find all soap or exportable service.
2. Create java client to all this as below,
/**
*
*/
import java.util.Map;
import java.util.logging.Logger;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
/**
* @author tariq
*
*/
public class Client {
public static final OMFactory fac;
public static final OMNamespace omNs;
public static final Logger log = Logger.getLogger(Client.class.getName());
public static final String serviceEndPointAddress="http://localhost:8080/webtools/control/SOAPService";
private static Client client=null;
/**
* @author tariq
*/
private Client() {
// TODO Auto-generated constructor stub
}
/**
* @author tariq
* @return
*/
public static synchronized Client getInstance(){
if(client==null){
client=new Client();
}
return client;
}
/**
*
*/
static {
fac = OMAbstractFactory.getOMFactory();
omNs = fac.createOMNamespace("http://ofbiz.apache.org/service/", "ns1");
}
/**
* @author tariq
* @param serviceName
* @param resultClassName
* @param resultName
* @param serviceInputMap
* @return
* @throws AxisFault
*/
public OMElement callService(String serviceName,Map<String, String> serviceInputMap) throws AxisFault{
ServiceClient sc = new ServiceClient();
Options opts = new Options();
log.info("==========:Set Service Enpoint Address:=============");
opts.setTo(new EndpointReference(serviceEndPointAddress));
opts.setAction(serviceName);
sc.setOptions(opts);
log.info("==========:Create Payload For Service Calling:=============");
OMElement payload=createPayLoad(serviceName,serviceInputMap);
log.info("==========:Start Calling Service:=============");
OMElement result = sc.sendReceive(payload);
return result;
}
/**
* @author tariq
* @param key
* @param val
* @return
*/
public OMElement createMapEntry(String key, String val) {
OMElement mapEntry = fac.createOMElement("map-Entry", omNs);
// create the key
OMElement mapKey = fac.createOMElement("map-Key", omNs);
OMElement keyElement = fac.createOMElement("std-String", omNs);
OMAttribute keyAttribute = fac.createOMAttribute("value", null, key);
mapKey.addChild(keyElement);
keyElement.addAttribute(keyAttribute);
// create the value
OMElement mapValue = fac.createOMElement("map-Value", omNs);
OMElement valElement = fac.createOMElement("std-String", omNs);
OMAttribute valAttribute = fac.createOMAttribute("value", null, val);
mapValue.addChild(valElement);
valElement.addAttribute(valAttribute);
// attach to map-Entry
mapEntry.addChild(mapKey);
mapEntry.addChild(mapValue);
return mapEntry;
}
/**
* @author tariq
* @param serviceName
* @param map
* @return
*/
public OMElement createPayLoad(String serviceName, Map<String, String> map) {
// Create root of payload/xml with service name
OMElement oOMElement = fac.createOMElement(serviceName, omNs);
// Create map for service input parameters
OMElement mapMap = fac.createOMElement("map-Map", omNs);
// Attach service input map to payload/xml
oOMElement.addChild(mapMap);
// Fill service input map
for (Map.Entry<String, String> entry : map.entrySet())
{
mapMap.addChild(createMapEntry(entry.getKey(), entry.getValue()));
}
// put service calling credentials
mapMap.addChild(createMapEntry("login.username","admin"));
mapMap.addChild(createMapEntry("login.password", "ofbiz"));
return oOMElement;
}
}
And then call this clinet as below,
import org.apache.axis2.AxisFault;
/**
* @author tariq
*
*/
public class Test {
/**
* @param args
* @throws IllegalAccessException
* @throws InstantiationException
* @throws AxisFault
*/
public static void main(String[] args) throws AxisFault, InstantiationException, IllegalAccessException {
// TODO Auto-generated method stub
Client client=Client.getInstance();
Map<String, String> serviceInputMap = new HashMap<String, String>();
// Here put input parameter as required
// such as
// serviceInputMap.put("key",''value");
//
//
//
OMElement response=client.callService("createColleague", serviceInputMap);
}
}
Here you get response as OMElement(xml)
Now process your result as required.
You need to add axis 2 bundles jar in your class path. From here
http://axis.apache.org/axis2/java/core/download.cgi
To create soap service or make service exportable follow the bellow steps:
1. Set export="true" in service definition for example
<service name="createColleague" engine="java" auth="true"
location="org.ofbiz.crm.colleague.ColleagueService" invoke="createColleague" export="true" >
</service>
You find all exportable service or soap service wsdl under webtools component.
Open a your browser new tab and below url,
http://localhost:8080/webtools/control/SOAPService?wsdl
Here you find all soap or exportable service.
2. Create java client to all this as below,
/**
*
*/
import java.util.Map;
import java.util.logging.Logger;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
/**
* @author tariq
*
*/
public class Client {
public static final OMFactory fac;
public static final OMNamespace omNs;
public static final Logger log = Logger.getLogger(Client.class.getName());
public static final String serviceEndPointAddress="http://localhost:8080/webtools/control/SOAPService";
private static Client client=null;
/**
* @author tariq
*/
private Client() {
// TODO Auto-generated constructor stub
}
/**
* @author tariq
* @return
*/
public static synchronized Client getInstance(){
if(client==null){
client=new Client();
}
return client;
}
/**
*
*/
static {
fac = OMAbstractFactory.getOMFactory();
omNs = fac.createOMNamespace("http://ofbiz.apache.org/service/", "ns1");
}
/**
* @author tariq
* @param serviceName
* @param resultClassName
* @param resultName
* @param serviceInputMap
* @return
* @throws AxisFault
*/
public OMElement callService(String serviceName,Map<String, String> serviceInputMap) throws AxisFault{
ServiceClient sc = new ServiceClient();
Options opts = new Options();
log.info("==========:Set Service Enpoint Address:=============");
opts.setTo(new EndpointReference(serviceEndPointAddress));
opts.setAction(serviceName);
sc.setOptions(opts);
log.info("==========:Create Payload For Service Calling:=============");
OMElement payload=createPayLoad(serviceName,serviceInputMap);
log.info("==========:Start Calling Service:=============");
OMElement result = sc.sendReceive(payload);
return result;
}
/**
* @author tariq
* @param key
* @param val
* @return
*/
public OMElement createMapEntry(String key, String val) {
OMElement mapEntry = fac.createOMElement("map-Entry", omNs);
// create the key
OMElement mapKey = fac.createOMElement("map-Key", omNs);
OMElement keyElement = fac.createOMElement("std-String", omNs);
OMAttribute keyAttribute = fac.createOMAttribute("value", null, key);
mapKey.addChild(keyElement);
keyElement.addAttribute(keyAttribute);
// create the value
OMElement mapValue = fac.createOMElement("map-Value", omNs);
OMElement valElement = fac.createOMElement("std-String", omNs);
OMAttribute valAttribute = fac.createOMAttribute("value", null, val);
mapValue.addChild(valElement);
valElement.addAttribute(valAttribute);
// attach to map-Entry
mapEntry.addChild(mapKey);
mapEntry.addChild(mapValue);
return mapEntry;
}
/**
* @author tariq
* @param serviceName
* @param map
* @return
*/
public OMElement createPayLoad(String serviceName, Map<String, String> map) {
// Create root of payload/xml with service name
OMElement oOMElement = fac.createOMElement(serviceName, omNs);
// Create map for service input parameters
OMElement mapMap = fac.createOMElement("map-Map", omNs);
// Attach service input map to payload/xml
oOMElement.addChild(mapMap);
// Fill service input map
for (Map.Entry<String, String> entry : map.entrySet())
{
mapMap.addChild(createMapEntry(entry.getKey(), entry.getValue()));
}
// put service calling credentials
mapMap.addChild(createMapEntry("login.username","admin"));
mapMap.addChild(createMapEntry("login.password", "ofbiz"));
return oOMElement;
}
}
And then call this clinet as below,
import org.apache.axis2.AxisFault;
/**
* @author tariq
*
*/
public class Test {
/**
* @param args
* @throws IllegalAccessException
* @throws InstantiationException
* @throws AxisFault
*/
public static void main(String[] args) throws AxisFault, InstantiationException, IllegalAccessException {
// TODO Auto-generated method stub
Client client=Client.getInstance();
Map<String, String> serviceInputMap = new HashMap<String, String>();
// Here put input parameter as required
// such as
// serviceInputMap.put("key",''value");
//
//
//
OMElement response=client.callService("createColleague", serviceInputMap);
}
}
Here you get response as OMElement(xml)
Now process your result as required.
You need to add axis 2 bundles jar in your class path. From here
http://axis.apache.org/axis2/java/core/download.cgi
Thanks for sharing code, it works for me.
ReplyDeleteThank you so much for sharing about OFBiz Development. NOI technology follows a consumer-centric process to deliver industry-standard solutions to clients.
ReplyDelete