Wednesday, July 25, 2012

BeanFace WebService - Transformation Example

1.BeanFace Web Service Server Side
In this example, we will use BeanFace Object for the Web Service called BeanFaceWebServiceTransform.java
1. Open com.wwlee.beanface.webservice.server.BeanFaceWebServiceServer.java file. 
Modify the codes to point BeanFaceWebServiceTransform java.

2. Review the  BeanFaceWebServiceTransform .java code. 
This program will use BeanFaceSampleDelimited which has ',' delimiter. You already created this java file in the previous blogs. I will use the same file. Let's see the transform() method.

private void transform(HttpServletResponse response) throws BeanFaceException, IOException {
        Envelope requestEnvelope = beanFaceWebServiceInfo.getRequestEnvelope();
        beanFaceWSSOAP.setEnvelope(requestEnvelope);
        int bodyLength = beanFaceWSSOAP.getBodyValueLength();
        ReadLineUtil readLineUtil = new ReadLineUtil();
        readLineUtil.setMessage(beanFaceWSSOAP.getBodyValue());
        String record = "";
        int count = 0;
        BeanFaceSampleDelimited beanFace = new BeanFaceSampleDelimited();
        while ((record = readLineUtil.readLine()) != null) {
            beanFace.unmarshalFromString(record);
            count++;
        }
        readLineUtil.close();
        /* Prepare Response SOAP Message */
        String responseHeaderMessage = "Response Header";
        String responseBodyMessage = "ReceivedTotalCount[" + count + "],BodySize[" + bodyLength + " bytes], Process Time[" + TimeUtil.getElapsedTime(inTime, TimeUtil.getDateTime()) + "], LastEmployeeName[" + beanFace.getEmployeeName() + "]";
        int length = defaultSOAPEnvelope.createResponse(response.getOutputStream(), responseHeaderMessage, responseBodyMessage);
        sendResponseCommon(response, HttpServletResponse.SC_OK, length);
    }

Like BufferedReader Class, ReadLineUtil Class provide readLine() method. You can do unmarshallFromString() and do actual transformation based on your requirement.

Build and Deploy the Server Program.

2.BeanFace Web Client Server Side called com.wwlee.beanface.webservice.examples.BeanFaceWebServiceClientTransformation

public String transformForBody(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();
    }
This method will create actual transformed messages based on ',' delimiter and this message will be used in the soap body message. Let's execute the main() method with the count 100 and the response will be.

SOAPAction,http://bfws.wwlee.com/BeanFaceWebServiceTransformation
Invoking Time : 140
Response : <?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">Response Header</ns3:bfwsHeader>
    </ns2:Header>
    <ns2:Body>
        <ns3:bfwsResponse xmlns:ns3="http://beanface.wwlee.com/bfws/bfwsResponse">ReceivedTotalCount[100],BodySize[3592 bytes], Process Time[89], LastEmployeeName[Emp100]</ns3:bfwsResponse>
    </ns2:Body>
</ns2:Envelope>

Through the response SOAP message, you can see how many messages were processed and how long it took to process.

Next example will be the highlight of BeanFace Web Service.

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