Generation of complex SOAP Envelope in accordance with OASIS

Put your problem here if it does not fit any of the other categories.

Generation of complex SOAP Envelope in accordance with OASIS

Postby jeets23 » Fri Mar 09, 2012 1:48 pm

Hi Guys,

I am new to Android Development. I need to consume a .NET web service which is designed in accordance with [OASIS Web Services Security specification][1].

I am using KSOAP2 latest API to generate SOAP envelope. I need to send SOAP request strictly in below format

Required SOAP Envelope
----------------------------
Code: Select all
     <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">   
   
    <soap:Header>
       <wsa:Action>http://myXYZ_Action</wsa:Action>
       <wsa:MessageID>XYZ</wsa:MessageID>
       <wsa:ReplyTo>
          <wsa:Address>http://something.com</wsa:Address>
       </wsa:ReplyTo>
       <wsa:To>http://something.asmx</wsa:To>
       <wsse:Security soap:mustUnderstand="1">
          <wsu:Timestamp wsu:Id="XYZ">
             <wsu:Created>2012-03-02T14:03:24Z</wsu:Created>
             <wsu:Expires>2012-03-02T14:08:24Z</wsu:Expires>
          </wsu:Timestamp>
          <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-XYZ">
             <wsse:Username>XYZ</wsse:Username>
             <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XYZ</wsse:Password>
             <wsse:Nonce>XYZ==</wsse:Nonce>
             <wsu:Created>2012-03-02T14:04:24Z</wsu:Created>
          </wsse:UsernameToken>
       </wsse:Security>
    </soap:Header>
    <soap:Body>
       <DoSomeThing xmlns="http://something"/></soap:Body>
    </soap:Envelope>



After doing loads of searches and referring to online help, i have been able to generate SOAP request using KSOAP2 like given below

KSOAP2 Generated Request dump
-----------------------------
Code: Select all
    <?xml version="1.0" encoding="utf-8"?>
    <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
   
    <v:Header>
       <Action>http://myXYZ_Action</Action>
       <MessageID>XYZ</MessageID>
       <ReplyTo>
          <Address>http://something.com</Address>
       </ReplyTo>
       <To>http://something.asmx</To>
       <Security mustUnderstand="1">
          <Timestamp Id="XYZ">
             <Created>2012-03-02T14:03:24Z</Created>
             <Expires>2012-03-02T14:08:24Z</Expires>
          </Timestamp>
          <n0:UsernameToken xmlns:n0="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
             <Username>XYZ</Username>
             <Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XYZ</Password>
             <Nonce>XYZ==</Nonce>
             <Created>2012-03-06T00:04:24Z</Created>
          </n0:UsernameToken>
       </Security>
    </v:Header>
    <v:Body>
       <GetActualDateTime xmlns="http://something/" />
    </v:Body>
    </v:Envelope>


My Code (This is just a POC code hence no coding standards/best practices followed)
------------------------------------------------------------------------

Code: Select all
public class TestActivity extends Activity {
        /** Called when the activity is first created. */
       private static final String SOAP_ACTION = "http://myXYZ_Action";
        private static final String METHOD_NAME = "DoSomeThing";
        private static final String NAMESPACE = "http://something/";
        private static final String URL = "http://xyz.asmx/wse";
        TextView tv;
       
       @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           
           
            try
            {
               SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
               SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
               
               envelope.xsd = SoapSerializationEnvelope.XSD;
               envelope.xsi= SoapSerializationEnvelope.XSI;
               envelope.env= SoapSerializationEnvelope.ENV;
               
               //Prepare header
               envelope.headerOut = new Element[5];
               envelope.headerOut[0] = buildActionHeader();
               envelope.headerOut[1] = buildMessageIDHeader();
               envelope.headerOut[2] = buildReplyToHeader();
               envelope.headerOut[3] = buildToHeader();
               envelope.headerOut[4] = buildSecurityHeader();
               
               
                envelope.dotNet=true;
                envelope.implicitTypes=true;
                envelope.setAddAdornments(false);
                envelope.setOutputSoapObject(request);
               
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
               
                androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                androidHttpTransport.debug=true;
                androidHttpTransport.call(SOAP_ACTION, envelope);
               
               //To be removed. This is just to check the request and response.
                @SuppressWarnings("unused")
             String req = androidHttpTransport.requestDump;
                @SuppressWarnings("unused")
             String res = androidHttpTransport.responseDump;
                //End
               
                Object result = (Object)envelope.getResponse();
     
                @SuppressWarnings("unused")
             String[] results = (String[])  result;
               
            }
           
            catch (Exception e)
            {
     
               e.printStackTrace();
            }
        }
       
   
       private Element buildActionHeader() {
          // TODO Auto-generated method stub
          //<wsa:Action> Element
          Element actionElement = new Element().createElement("", "Action");
          actionElement.addChild(Node.TEXT, "http://myXYZ_Action");
          return actionElement;
       }
       
       private Element buildMessageIDHeader() {
          // TODO Auto-generated method stub
          //<wsa:MessageID> Element
          Element messageIDElement = new Element().createElement("", "MessageID");
          messageIDElement.addChild(Node.TEXT, "XYZ");      
          return messageIDElement;
       }
       
       private Element buildReplyToHeader() {
          // TODO Auto-generated method stub
          //<wsa:Address> Element
          Element addressElement = new Element().createElement("", "Address");
          addressElement.addChild(Node.TEXT, "http://something.com");
          
          //<wsa:ReplyTo> Element
          Element replyToElement = new Element().createElement("", "ReplyTo");
          replyToElement.addChild(Node.ELEMENT, addressElement);
          return replyToElement;
       }
   
       private Element buildToHeader() {
          Element toElement = new Element().createElement("", "To");
          toElement.addChild(Node.TEXT, "http://something.asmx");
          
          return toElement;
       }
   
       private Element buildSecurityHeader() {
          Element securityElement = new Element().createElement(null, "Security");
          securityElement.setAttribute(null, "mustUnderstand", "1");
          
          //<wsu:Timestamp> Element
          Element timestampElement = new Element().createElement(null, "Timestamp");
          timestampElement.setAttribute(null, "Id", "XYZ");
          //<wsu:Created> Element
          Element createdElement = new Element().createElement(null, "Created");
          createdElement.addChild(Node.TEXT, "2012-03-06T00:01:24Z");
          
          //<wsu:Expires> Element
          Element expiresElement = new Element().createElement(null, "Expires");
          expiresElement.addChild(Node.TEXT, "2012-03-06T00:05:24Z");
          
          timestampElement.addChild(Node.ELEMENT, createdElement);
          timestampElement.addChild(Node.ELEMENT,expiresElement);
          
          Element usernameTokenElement = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken");
          
          Element usernameElement = new Element().createElement(null, "Username");
          usernameElement.addChild(Node.TEXT, "XYZ");
          
          Element passwordElement = new Element().createElement(null, "Password");
          passwordElement.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
          passwordElement.addChild(Node.TEXT, "XYZ");
          
          Element nonceElement = new Element().createElement(null, "Nonce");
          nonceElement.addChild(Node.TEXT, "XYZ====");
          
          Element created2Element = new Element().createElement(null, "Created");
          created2Element.addChild(Node.TEXT, "2012-03-06T00:04:24Z");
          
          usernameTokenElement.addChild(Node.ELEMENT, usernameElement);
          usernameTokenElement.addChild(Node.ELEMENT, passwordElement);
          usernameTokenElement.addChild(Node.ELEMENT, nonceElement);
          usernameTokenElement.addChild(Node.ELEMENT, created2Element);
          
          securityElement.addChild(Node.ELEMENT, timestampElement);
          securityElement.addChild(Node.ELEMENT, usernameTokenElement);
          
          return securityElement;
       }
       
    }



I did not find any help on my below queries. I will appreciate your expert advice on help me generate correct SOAP request.
1.How to add additional namespaces on envelope? i.e adding below attributes to envelope.
---------------------------------------------------------------------------------------------------------

xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"

2. How to change/map correct namespaces across envelope i.e. adding xmlns:wsse, xmlns:wsu, xmlns:was namespaces in header and mapping them like as mentioned below
------------------------------------------------------------------------------------------------------------------------------



xmlns:i="http://www.w3.org/2001/XMLSchema-instance"

to
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

OR

xmlns:d="http://www.w3.org/2001/XMLSchema"
to
xmlns:xsd="http://www.w3.org/2001/XMLSchema"

AND

<v:Envelope> to <soap:Envelope> ; <Action> to <wsa:Action> ;

<n0:UsernameToken xmlns:n0="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
TO
<wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-XYZ">
jeets23
Freshman
Freshman
 
Posts: 2
Joined: Mon Oct 10, 2011 11:46 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 15 guests