Thursday, September 27, 2012

Testing webservices manually with raw soap request

This method is useful when you are required to generate a SOAP request manually without using any tools like axis2, jaxrpc etc. Here we manually create the request using java, no other jar files are required.

Pre-requisite: 


First we have to identify the request which we are going to create (you can identify your request using SOAP-UI here http://helptodeveloper.blogspot.com/2012/09/testing-webservice-or-wsdl-with-soap-ui.html). I am using the PalinServ webservice, so below is the request.

Request:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pal="http://palin.com">
   <soap:Header/>
   <soap:Body>
      <pal:isPalindrome>
         <pal:args0>123454321</pal:args0>
      </pal:isPalindrome>
   </soap:Body>
</soap:Envelope>


Java code for request creation:

package com.service.palinImpl;


import java.io.IOException;
import java.net.URL;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;



public class PalindromeServiceImpl {
public static final String PAL_NS = "http://palin.com";
public static final String PAL_PREFIX = "pal";
public static final String WEBSERVICE_URL = "http://localhost:8080/axis2/services/PalinServ?wsdl";

public static Object makeRequest(SOAPElement request) throws IOException, SOAPException, Exception{
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
     SOAPPart soapPart = soapMessage.getSOAPPart();
     SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
     SOAPBody soapBody = soapEnvelope.getBody();
     soapEnvelope.addNamespaceDeclaration(PAL_PREFIX, PAL_NS);
     SOAPHeader soapHeader = soapMessage.getSOAPHeader();
     if (soapHeader == null){
      soapHeader = soapEnvelope.addHeader();
     }
     soapBody.addChildElement(request);
     soapMessage.writeTo(System.out);
    return soapMessage;    
    }

public static void callService() throws IOException, Exception {
SOAPElement bodyElem = PalindromeServiceImpl.makePalindromeOperation(123454321);
SOAPMessage soapMessageRequest = (SOAPMessage) PalindromeServiceImpl.makeRequest(bodyElem);
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
URL endpoint = new URL(WEBSERVICE_URL);
   SOAPMessage soapMessageResponse = connection.call(soapMessageRequest, endpoint);
   soapMessageResponse.writeTo(System.out);
   connection.close();
}

/*<pal:isPalindrome>
         <pal:args0>123454321</pal:args0>
      </pal:isPalindrome> */
public static SOAPElement makePalindromeOperation(int number) throws SOAPException{
        SOAPFactory soapFactory = SOAPFactory.newInstance();
        SOAPElement isPalin = soapFactory.createElement("isPalindrome", PAL_PREFIX, PAL_NS);
        SOAPElement args = isPalin.addChildElement("args0", PAL_PREFIX, PAL_NS);
        args.addTextNode(String.valueOf(number));
return isPalin;
}

public static void main(String[] args) {
try {
PalindromeServiceImpl.callService();
} catch (Exception e) {
e.printStackTrace();
}
}
}



Below are the Request and Response after running the above program:

Request
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:pal="http://palin.com">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<pal:isPalindrome>
<pal:args0>123454321</pal:args0>
</pal:isPalindrome>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Response
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:isPalindromeResponse xmlns:ns="http://palin.com">
<ns:return>Palindrome</ns:return>
</ns:isPalindromeResponse>
</soapenv:Body>
</soapenv:Envelope>

Hope this helped :)


No comments:

Post a Comment