Sunday, March 6, 2011

SOAP Service using Basic J2EE

In the previous article we discussed about creating a REST service using Basic J2EE. Here we expand our discussion to SOAP services. ABC Inc is now expanding and adding clients by the day. To increase the facilities offered they have now decided to move from REST to SOAP form of Web Services. As a prototype we will try to create the same order service as discussed before in a SOAP service.

    The transition from REST to SOAP has a few features that need to be looked into. A few of them are:

  1. The service operation no longer is decided by the HTTP methods. We need a new addressing strategy.
  2. The data/order can no longer be identified through URL (resourceId), We need to pass the data through XML.
  3. The Body content would now be a standardized SOAP Envelope.
  4. The authentication/encryption and other metadata is sent through SOAP Headers.
There could be a few more features that need to be added. However for the purpose of discussion we shall limit ourselves to the above mentioned points. Thus our new Servlet (com.abc.orderservice.SOAPEndpoint) looks something like the following.


 public class SOAPEndpoint extends HttpServlet {  
      @Override  
      protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
                throws ServletException, IOException {  
                // process only POST requests   
           throw new IllegalArgumentException("GET NOT Supported");  
      }  
      @Override  
      protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
                throws ServletException, IOException {  
           processSOAPMessage(req,resp);  
      }  
      private void processSOAPMessage(HttpServletRequest req,  
                HttpServletResponse resp) throws IOException {  
           String operation = req.getHeader("soapAction");  
           InputStream inputStream = req.getInputStream();  
           String soapXML = convertStreamToString(inputStream);  
           String payload = extractPayloadFromSOAPXML(soapXML);  
           String soapHeader = extractHeaderBlockFromSOAPXML(soapXML);  
           String orderId = extractOrderIdFromHeader(soapHeader);  
           if("http://localhost:8080/GETORDER".equalsIgnoreCase(operation)){  
                // get order xml from DB using order id  
 // enclose Order XML in the SOAP:Body and send back a SOAP:Envelope XML.  
           }else if("http://localhost:8080/POSTORDER".equalsIgnoreCase(operation)){  
                // set/store order xml into DB using order id and payload.  
           }  
      }  
 }  



As evident from the above snippet, the main operational method in the endpoint is the processSOAPMessage(HttpServletRequest, HttpServletResponse). The salient features of the method are as given below:

  1. Extract the SOAP Action header from the HTTP Header. This stores the name of the operation to be performed. (This is called the addressing strategy. Different strategies will be discussed later).
  2. Convert the HTTP Body to SOAP XML.
  3. Extract the SOAP Body from SOAP XML which contains the payload/order XML
  4. Extract the SOAP Headers from SOAP XML which contains the order id, authentication data and any encryption data.
  5. Process the business logic.
As is evident from the above steps, process a. through d. would be almost common throughout all implementations of ABC Inc and also for any other implementation code for web services. This "boilerplate" code can be standardized so as to reduce the effort required for development of web services. Examples of such framework or toolkit are Axis, JAX-WS and Spring-WS.

No comments:

Post a Comment