Wednesday, July 25, 2012

BeanFace Web Service Server Template

To test BeanFace Web Service, you need Web Service client and server.
In this blog, I will mainly cover how to create BeanFace Web Service Server Templates.
Later on, you will see how this simple single template can extend Web Service Server to dynamically transform any interface messages through "Dynamic Transformation Invocation".

I use Netbean and glassfish for Servlet container. If you use any other Java UI and container, you need to slightly change the configuration.

Create Project call 'BeanFaceWebServiceServer'
1. Create New Project --> Java Web --> Web Application
2. Provide Project Name and its Location. As a project name, use 'BeanFaceWebServiceServer'
3. Choose J2EE container and provide name for 'Context Path'
    I select 'GlassFish' and as a context path, use '/BeanFaceWebService'
4. Finish

Create Servlet call 'BeanFaceWebServiceServer'
In Source Package, right click and choose 'Servlet'. Class Name will be 'BeanFaceWebServiceServer' and  package name is 'com.wwlee.beanface.webservice.server'. It will create BeanFaceWebServiceServer.java file.

Configure web.xml file. 
Instead of index.jsp in welcome-file element, use BeanFaceWebServiceServer.
Now your web.xml file will be similar with the following.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>BeanFaceWebServiceServer</servlet-name>
        <servlet-class>com.wwlee.beanface.webservice.server.BeanFaceWebServiceServer</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>BeanFaceWebServiceServer</servlet-name>
        <url-pattern>/BeanFaceWebServiceServer</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>1</session-timeout>
    </session-config>
    <welcome-file-list>
        <!--<welcome-file>index.jsp</welcome-file>-->
        <welcome-file>BeanFaceWebServiceServer</welcome-file>
    </welcome-file-list>
</web-app>

Now you are ready to have the template for BeanFace Web Service Server. 
Let's test the servlet program after adding the following lines in processRequest() method. 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String defaultDirectory = ".";
            File file = new File(defaultDirectory);
            System.out.println("+++++ Absolute File Path for Default Directory : " + file.getAbsoluteFile());
        } finally { 
            out.close();
        }
    }

After deploying it, you can simply test through your browser. 
http://localhost:18001/BeanFaceWebService/
And check your log file to see the following message "++++++ Absolute File Path for Default Directory .....". If you cannot see it, troubleshoot your self. 

Next couple of blogs, you will see how to use BeanFace Web Service in Server and Client modes.
In addition, you will see how to dynamically invoke transformation logic in Server side and how to handle the big messages. I will use the simple delimiter messages with about 60M bytes. Guess if you convert it to xml message, how its size will be.

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