Monday, August 8, 2011

WebServices : Soap

Requirement :

1) How to prepare Soap XML Request
2) How to Generate Soap XML
3) How to parse Soap Request using Axis WebServices.
4) Response : transactionID | Status-Code | Etc.....



1)
public class RequestTypes {

     public static final String XML_SOAP_REQUEST = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
            "xmlns:ws=\"http://ws.mobitel.com/\">" +
            "<soapenv:Header/>" +
            "<soapenv:Body>" +
            "<ws:chargeFromMSISDN>" +
            "<transactionId>_tid_</transactionId>" +
            "<msisdn>_msisdn_</msisdn>" +
            "</ws:chargeFromMSISDN>" +
            "</soapenv:Body>" +
            "</soapenv:Envelope>";

   

}




2)  public String generateSoapRequest(String tid,String msisdn) {
       
        String soapReq = RequestTypes.XML_SOAP_REQUEST;
        soapReq = soapReq.replaceAll("_tid_", tid);
        soapReq = soapReq.replaceAll("_msisdn_", msisdn);
       
        System.out.println("soapReq.."+soapReq);
   
        return soapReq;
        }

3) public String executeRequest(String destUrl, String tid, String msisdn) {
        Node result = null;
        SOAPConnection connection = null;
        try {
            String soapReq = generateSoapRequest(tid, msisdn);
            InputStream inStream =
                    new ByteArrayInputStream(soapReq.getBytes());
            Message axisMessage = new Message(inStream);
            axisMessage.getMimeHeaders().addHeader("Content-type", "text/xml;charset=utf-8");
            axisMessage.saveChanges();
            connection = SOAPConnectionFactory.newInstance().createConnection();
            SOAPMessage reply = connection.call(axisMessage,
                    new java.net.URL(destUrl));
            SOAPPart soapPart = reply.getSOAPPart();
            SOAPEnvelope envelope = soapPart.getEnvelope();
            SOAPBody body = envelope.getBody();
            Iterator iter = body.getChildElements();
            Node resultOuter = ((Node) iter.next()).getFirstChild();
            result = resultOuter.getFirstChild();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ex.getMessage();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SOAPException ex) {
                    ex.printStackTrace();
                }
            }
        }
        if (result != null) {
            System.out.println("result node. value"+result.getNodeValue());
            return result.getNodeValue();
        } else {
            return "";
        }
    }


Note : In the above executeRequest Method () we have to provide tid,msidn,desturl...in order to parse the Soap XML........

  The response would be in your customized format.......

Happy Learning ::::


Note :

Jars required :



axis.jar
axis-ant.jar
commons-discovery.jar
commons-logging.jar
jaxrpc.jar
log4j-1.2.8.jar
saaj.jar
wsdl4j.jar
wss4j-1.5.4.jar




No comments:

Post a Comment