Consume SOAP web service (having security) in Java -


i want post following xml web service :-

<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">       <soapenv:header>             <security xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">                   <usernametoken>                         <username>xyzuser</username>                         <password>xyzpass</password>                   </usernametoken>             </security>       </soapenv:header>       <soapenv:body>             <v1:ordersearchrequest>                   <v1:retas400ordernumber>1</v1:retas400ordernumber>             </v1:ordersearchrequest>       </soapenv:body> </soapenv:envelope> 

i expecting following response xml :-

<?xml version="1.0" encoding="utf-8"?> <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">       <soapenv:header/>       <soapenv:body>             <v1:ordersearchresponse xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">                   <v1:error>                         <v1:errorcode>errodr01</v1:errorcode>                         <v1:errormessage>order number invalid</v1:errormessage>                   </v1:error>             </v1:ordersearchresponse>       </soapenv:body> </soapenv:envelope> 

but instead, getting following response xml indicating fault :-

<?xml version="1.0" encoding="utf-8"?> <env:envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">    <env:header>    </env:header>    <env:body>       <env:fault>          <faultcode>env:server          </faultcode>          <faultstring>          </faultstring>          <detail xmlns:fault="http://www.vordel.com/soapfaults" fault:type="faultdetails">          </detail>       </env:fault>    </env:body> </env:envelope> 

i using java 8. tried make post operation using apache httpclient (version 4.4.1) , saaj, in 2 different programs, unable fix one. can please ?

the saaj code follows :-

public class requestinitiation {      public static void main(string args[]) {          try {             // create soap connection             soapconnectionfactory soapconnectionfactory = soapconnectionfactory.newinstance();             soapconnection soapconnection = soapconnectionfactory.createconnection();              // send soap message soap server             string url = "<service_endpoint>";             soapmessage soapresponse = soapconnection.call(createsoaprequest(), url);              // process soap response             printsoapresponse(soapresponse);              soapconnection.close();         } catch (exception e) {             system.err.println("error occurred while sending soap request server");             e.printstacktrace();         }     }      private static soapmessage createsoaprequest() throws exception {          messagefactory messagefactory = messagefactory.newinstance();         soapmessage soapmessage = messagefactory.createmessage();         soappart soappart = soapmessage.getsoappart();          string serveruri = "http://<some_site>/retail/schema/inventory/orderlookupservice/v1";          soapenvelope envelope = soappart.getenvelope();         envelope.addnamespacedeclaration("v1", serveruri);          soapheader header = envelope.getheader();          soapelement security =                 header.addchildelement("security", "", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");         security.addattribute(new qname("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");          soapelement usernametoken =                 security.addchildelement("usernametoken", "");          soapelement username =                 usernametoken.addchildelement("username", "");         username.addtextnode("xyzuser");          soapelement password =                 usernametoken.addchildelement("password", "");         password.addtextnode("xyzpass");          soapbody soapbody = envelope.getbody();          soapelement soapbodyelem = soapbody.addchildelement("ordersearchrequest", "v1");         soapelement soapbodyelem1 = soapbodyelem.addchildelement("retas400ordernumber", "v1");         soapbodyelem1.addtextnode("1");          soapmessage.setproperty(soapmessage.character_set_encoding, "utf-8");          soapmessage.savechanges();          /* print request message */         system.out.print("request soap message = ");         soapmessage.writeto(system.out);         system.out.println();          return soapmessage;     }      private static void printsoapresponse(soapmessage soapresponse) throws exception {         transformerfactory transformerfactory = transformerfactory.newinstance();         transformer transformer = transformerfactory.newtransformer();         source sourcecontent = soapresponse.getsoappart().getcontent();         system.out.print("\nresponse soap message = ");         streamresult result = new streamresult(system.out);         transformer.transform(sourcecontent, result);     } } 

the httpclient code follows :-

public class postsoaprequest {      public static string post() throws exception {          httpclient client = httpclientbuilder.create().build();         httppost post = new httppost("<service_endpoint>");         string xml = "<soapenv:envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://<some_site>/retail/schema/inventory/orderlookupservice/v1\"><soapenv:header><security xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><usernametoken><username>xyzuser</username><password>xyzpass</password></usernametoken></security></soapenv:header><soapenv:body><v1:ordersearchrequest><v1:retas400ordernumber>1</v1:retas400ordernumber></v1:ordersearchrequest></soapenv:body></soapenv:envelope>";         httpentity entity = new bytearrayentity(xml.getbytes("utf-8"));         post.setentity(entity);         httpresponse response = client.execute(post);         string result = entityutils.tostring(response.getentity());         return result;     }      public static void main(string[] args) {         try {             system.out.println(post());          } catch (exception e) {             e.printstacktrace();         }     }  } 

note : site name, endpoint, username , password replaced dummy values in message.

there 2 levels of operations happen : 1. apply credentials 2. send message. 2 step mechanism available in saaj.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -