Wednesday, July 25, 2012

BeanFace SOAP Example

This blog will show you how to create SOAP messages through BeanFace API.

1.com.wwlee.beanface.soap.examples.BeanFaceSOAPSimple
In general, to create SOAP and marshal/unmarshal to SOAP, you will import the following classes.

import com.wwlee.beanface.jaxb.BeanFaceJAXB;
import com.wwlee.beanface.soap.envelope11.Envelope;
import com.wwlee.beanface.soap.messages.BeanFaceWSSOAP;
import com.wwlee.beanface.soap.messages.BeanFaceWSSOAPFactory;

Let's see the createSOAPEvelope() method.

public Envelope createSOAPEvelope(String headerMessage, String bodyMessage) throws BeanFaceException {
        Envelope envelope = beanFaceWSSOAPFactory.createEnvelope();
        beanFaceWSSOAP.setEnvelope(envelope);
        if (!StringUtil.isNullOrEmpty(headerMessage)) {
            beanFaceWSSOAP.setHeader(headerMessage);
        }
        beanFaceWSSOAP.setRequestBody(bodyMessage);
        return envelope;
    }

It expects headerMessage and bodyMessage and create SOAP object called Envelope.
Let's run main() method and see the result.
public static void main(String[] args) throws Exception {
        BeanFaceSOAPSimple beanFaceSOAPSimple = new BeanFaceSOAPSimple();
        Envelope envelope = beanFaceSOAPSimple.createSOAPEvelope("Request Header", "Request Body");
        String soapXML = beanFaceSOAPSimple.marshalToString(envelope);
        System.out.println(soapXML);
        envelope = (Envelope) beanFaceSOAPSimple.unmarshalFromString(envelope, soapXML);
        String bodyInformation = beanFaceSOAPSimple.getBodyInfo(envelope);
        System.out.println("+++++ Body Information +++++");
        System.out.println(bodyInformation);
    }
Result]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Envelope xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/">
    <ns2:Header>
        <ns3:bfwsHeader xmlns:ns3="http://beanface.wwlee.com/bfws/bfwsHeader">Request Header</ns3:bfwsHeader>
    </ns2:Header>
    <ns2:Body>
        <ns3:bfwsRequest xmlns:ns3="http://beanface.wwlee.com/bfws/bfwsRequest">Request Body</ns3:bfwsRequest>
    </ns2:Body>
</ns2:Envelope>

+++++ Body Information +++++
BodyLocalPart : bfwsRequest
BodyNamespaceURI : http://beanface.wwlee.com/bfws/bfwsRequest
BodyPrefix : ns3
BodyValueLength : 12
BodyValue : Request Body

2. com.wwlee.beanface.soap.examples.BeanFaceSOAPWithBeanFaceObject
My motivation for BeanFace is from adding legacy format messages as part of SOAP body as it is.
For example, if the file contains the following messages with ',' delimiter, I would like to add it as a soap body.
EMP1,IT Dept,Bean Face Company,123
EMP2,IT Dept,Bean Face Company,123
EMP3,IT Dept,Bean Face Company,123
EMP4,IT Dept,Bean Face Company,123
....
EMP98,IT Dept,Bean Face Company,123
EMP99,IT Dept,Bean Face Company,123
EMP100,IT Dept,Bean Face Company,123

This example will show you how to do this scenario without creating any additional xsd and xml stuffs.
Let's see createSOAPBodyContents() method. It will create SOAP message with the Legacy SOAP Body based on BeanFace API. 

    public String createSOAPBodyContents(int count) throws BeanFaceException {
        String employeeName = "EMP";
        String departmentName = "IT Dept";
        String companyName = "Bean Face Company";
        String countryCode = "123";
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i <= count; i++) {
            beanFaceSampleDelimited.setEmployeeName(employeeName + i);
            beanFaceSampleDelimited.setDepartmentName(departmentName);
            beanFaceSampleDelimited.setCompanyName(companyName);
            beanFaceSampleDelimited.setCountryCode(countryCode);
            stringBuffer.append(beanFaceSampleDelimited.marshalToString() + "\n");
        }
        return stringBuffer.toString();
    }
After running main() method, it will generate SOAP messages as the following.
Result]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Envelope xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/">
    <ns2:Body>
        <ns3:bfwsRequest xmlns:ns3="http://beanface.wwlee.com/bfws/bfwsRequest">EMP1,IT Dept,Bean Face Company,123
EMP2,IT Dept,Bean Face Company,123
EMP3,IT Dept,Bean Face Company,123
EMP4,IT Dept,Bean Face Company,123
EMP5,IT Dept,Bean Face Company,123
EMP6,IT Dept,Bean Face Company,123
EMP7,IT Dept,Bean Face Company,123
EMP8,IT Dept,Bean Face Company,123
EMP9,IT Dept,Bean Face Company,123
EMP10,IT Dept,Bean Face Company,123
</ns3:bfwsRequest>
    </ns2:Body>
</ns2:Envelope>

My client will be happy with this result. But he asked 'how I send this soap messages to server' and 'how web service server side can handle this BeanFace message?.'  'Do we need to create WSDL?'

To get these answers, the following blogs will cover the Web Service Client/Server side examples based on BeanFace Web Service. Through these example, you will learn 'true dynamic web service' including dynamic transformation. Let's see what I am talking about in the next couple of blogs.

To fully use BeanFace and its Web Service, you need to download the followings.
1. BeanFace.jar - BeanFace Core API
2. BeanFaceExample.zip - Include all of the example source codes including Web Service Clients
3. BeanFaceWebService.zip - Include all of the sample java source for Web Service Server sides.  

No comments:

Post a Comment