Wednesday, July 25, 2012

BeanFace Web Service - Simple Example

1.BeanFace Web Service Server Side
If you don't copy the example java files, you need to extract 'BeanFaceWebServiceServer.zip' file and copy them into BeanFaceWebServiceServer project which contains servlet program called 'BeanFaceWebServiceServer.java'.

Let's start Web Service Server Template program called 'BeanFaceWebServiceServer.java'.
1. Open com.wwlee.beanface.webservice.server.BeanFaceWebServiceServer.java file. 
2. Import the following two lines. 
    import com.wwlee.beanface.webservice.server.impl.example.*;
    import com.wwlee.beanface.exception.BeanFaceException;
3. Modify the codes as the followings.
    private BeanFaceWebServiceSimple beanFaceWebService;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        beanFaceWebService.processRequest(request, response);
    }
    public void init() throws ServletException {
        try {
            beanFaceWebService = new BeanFaceWebServiceSimple();
        } catch (BeanFaceException ex) {
            throw new ServletException(ex);
        }
    }
4. Review the BeanFaceWebServiceSimple.java code. 
In processRequest() method, it called handleRequest() method.

    private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws BeanFaceException, IOException {

        beanFaceWebServiceInfo = new BeanFaceWebServiceInfo(request, response);

        System.out.println("SOAP Action : " + beanFaceWebServiceInfo.getSOAPAction());
        String routing = beanFaceWebServiceInfo.getSOAPRoutingInfo();
        System.out.println("SOAP Routing Info : " + routing);
        transform(response);
    }
transform() method will create response message and reply back to Web Service Client.
Response SOAP message doesn't have Header. Body contents will be "Response Body Message for Simple".

    private void transform(HttpServletResponse response) throws BeanFaceException, IOException {
        System.out.println("Input Message : " + beanFaceWebServiceInfo.getReqestXMLString());
        int length = defaultSOAPEnvelope.createResponse(response.getOutputStream(), "Response Body Message for Simple");
        sendResponseCommon(response, HttpServletResponse.SC_OK, length);
    }

Build and Deploy the Server Program.

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

public byte[] invokeWebService(Object soap, String endpointURL, String soapAction, int timeoutSeconds) throws BeanFaceException {
        BeanFaceWebService webservice = new BeanFaceWebServiceJAXB();
        if (soap instanceof Envelope) {
            webservice.setRequestMessage((Envelope) soap, soapAction);
        } else if (soap instanceof File) {
            try {
                webservice.setRequestMessage(new FileInputStream((File) soap), soapAction);
            } catch (FileNotFoundException ex) {
                throw new BeanFaceException(ex);
            }
        }
        webservice.invoke(endpointURL, timeoutSeconds);
        byte responses[] = webservice.getResponseBytesXML();
        System.out.println("Invoking Time : " + webservice.getElapsedInvokingTime());
        return responses;
    }

    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;
    }

    public static void main(String[] argv) throws Exception {
        String headerMessage = "Simple Bean Face Web Service Message";
        String bodyMessage = "EmployeeName,DepartmentName,CompanyName,CountryCode";
        String endpointURL = "http://localhost:18001/BeanFaceWebService/";
        String soapAction = "http://bfws.wwlee.com/BeanFaceWebServiceSimple";
        int timeoutSeconds = 30;
        BeanFaceWebServiceClientSimple beanFaceWebServiceClientSimple = new BeanFaceWebServiceClientSimple();
        Envelope envelope = beanFaceWebServiceClientSimple.createSOAPEvelope(headerMessage, bodyMessage);
        byte responses[] = beanFaceWebServiceClientSimple.invokeWebService(envelope, endpointURL, soapAction, timeoutSeconds);
        System.out.println("Response : " + new String(responses));
    }

In general, soapAction information is used for 'Operation'. In BeanFace Web Service, it will be used for Routing. You will see this example in Dynamic Transformation Invocation.

Execute this program and you will get the following response from server side. The result is.

Invoking Time : 63
Response : <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Envelope xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/">
    <ns2:Body>
        <ns3:bfwsResponse xmlns:ns3="http://beanface.wwlee.com/bfws/bfwsResponse">Response Body Message for Simple</ns3:bfwsResponse>
    </ns2:Body>
</ns2:Envelope>

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