SOAP sessionkey in header - java

I tried sending a SOAP message using saaj, but I can't add multiple headers element, how do I send a SOAP message like the following?
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"xmlns:ns1="urn://xparcws/ticket" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="urn://xparcws/session" xmlns:enc="http://www.w3.org/2003/05/soap-encoding">
<env:Header>
<ns2:Session><scalar>asd</scalar>
<sessionkey>67e61b5b36e3c989d91eba0d8621</sessionkey>
</ns2:Session>
</env:Header>
<env:Body>
<ns1:TicketService.requestTicket env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<ticketid xsi:type="xsd:long">1261534</ticketid>
</ns1:TicketService.requestTicket></env:Body>
</env:Envelope>

Related

org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected

I have the following file that I cURL:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ProcessMessage xmlns="http://www.user.com/test/">
<envelope>Encoded XML request message will go here</envelope>
</ProcessMessage>
</soap:Body>
</soap:Envelope>
The full error I get is this:
org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
When I use the file with a CDATA element then everything works as expected.
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ProcessMessage xmlns="http://www.user.com/test/">
<![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<envelope>Encoded XML request message will go here</envelope>
]]>
</ProcessMessage>
</soap:Body>
</soap:Envelope>
Any idea how I can accept an XML file without the CDATA part?
quote: "Only one XML declaration is permitted in well-formed XML, and it must be at the top if anywhere."
See: https://stackoverflow.com/a/20251895/3710053
So if you could get rid of that second <?xml version="1.0" encoding="UTF-8"?> your okay.

Can i send xml payload as a string to soap web service

Can i send xml payload as a string to soap web service ? Is it allowed to use any http client library (Apache HttpClient, RxNetty) for the same or should i only use SAAJ framework (http://stackoverflow.com/questions/15948927/working-soap-client-example) ?
E.g.,
curl --header "Content-Type: text/xml;charset=UTF-8"
--header "SOAPAction:http://www.webserviceX.NET/ConversionRate"
--data #request3.xml http://www.webservicex.net/CurrencyConvertor.asmx
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ConversionRate xmlns="http://www.webserviceX.NET/">
<FromCurrency>USD</FromCurrency>
<ToCurrency>GBP</ToCurrency>
</ConversionRate>
</soap:Body>
</soap:Envelope>
Yes, it is perfectly possible, in fact, under the hood, that's what happens when you call a SOAP based web service _action located at _url URI:
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml(#"<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ConversionRate xmlns="http://www.webserviceX.NET/">
<FromCurrency>USD</FromCurrency>
<ToCurrency>GBP</ToCurrency>
</ConversionRate>
</soap:Body>
</soap:Envelope>");
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
Now your request, loaded with your SOAP envelop is ready to be expedited!

Java: Customizing/Remove name space in Soap Request

I generated classes from the wsdl.
Request I got:
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:acr_JourneySearch xmlns:ns2="https://...../SoapApi/0.1/">
<access>...
Request client required:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="https://......./SoapApi/0.1/">
<S:Body>
<ns2:acr_JourneySearch>
<access>...
How to move ns2 from Body to Envelope?

how to Parse Soap Response in Java

The following is the Soap Response i am getting but i dont know
how to parse the Soap response.
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/plaza/api" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:requestResponse>
<return xsi:type="SOAP-ENC:Struct">
<last_dir_trans xsi:type="SOAP-ENC:Struct">
<p_liveid xsi:type="xsd:string">124</p_liveid>
<p_amount xsi:type="xsd:string">20</p_amount>
<p_dateadded xsi:type="xsd:string">2016-08-15 07:37:21</p_dateadded>
</last_dir_trans>
<last_knet_trans xsi:type="SOAP-ENC:Struct">
<p_knetsuc_id xsi:type="xsd:string">3</p_knetsuc_id>
<p_amount xsi:type="xsd:string">20</p_amount>
<p_dateadded xsi:type="xsd:string">2016-09-22 15:33:56</p_dateadded>
</last_knet_trans>
<last_user xsi:type="SOAP-ENC:Struct">
<s_subid xsi:type="xsd:string">23223</s_subid>
<s_name xsi:type="xsd:string">test1</s_name>
<s_address xsi:type="xsd:string"></s_address>
<s_phonenumber xsi:type="xsd:string">96566565656</s_phonenumber>
<s_civilid xsi:type="xsd:string">2016-08-15 07:3</s_civilid>
<s_total_payable xsi:type="xsd:string">240</s_total_payable>
<s_monthly xsi:type="xsd:string">20</s_monthly>
<s_email xsi:type="xsd:string"></s_email>
<s_dateadded xsi:type="xsd:string">0000-00-00 00:00:00</s_dateadded>
</last_user>
</return>
</ns1:requestResponse>
</SOAP-ENV:Body>
I want to parse the above code in java.thanks in advance

Override namespace with empty namespace CXF (JBoss EAP 6.2.4.GA)

I have the following issue with CXF version 2.7.7-redhat-1, that seems like a bug in the library.
With the following request:
<?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">
<soap:Body>
<authorizePayment xmlns="http://ejb.example.org">
<request xmlns="">
<role xmlns:q1="http://to.homepay.example.org" xsi:type="q1:CustomerTO">
<user>7C742</user>
</role>
<error>
<errorCode>1</errorCode>
<errorMessage>1</errorMessage>
</error>
<status>
<code>1</code>
</status>
<ordAccount>1</ordAccount>
<logId>374216011815223570893830967949</logId>
<signMethod>1</signMethod>
<lang>1</lang>
<terminalId>1</terminalId>
<terminalLocation>Earth</terminalLocation>
</request>
</authorizePayment>
</soap:Body>
</soap:Envelope>
I get the following error:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: unexpected element (uri:"http://ejb.example.org", local:"request"). Expected elements are <{}request></faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
From the request you can clearly see that the namespace of the request is "" overridden from its parent.
If I change the request to:
<?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">
<soap:Body>
<aa:authorizePayment xmlns:aa="http://ejb.example.org">
<request xmlns="">
<role xmlns:q1="http://to.homepay.example.org" xsi:type="q1:CustomerTO">
<user>7C742</user>
</role>
<error>
<errorCode>1</errorCode>
<errorMessage>1</errorMessage>
</error>
<status>
<code>1</code>
</status>
<ordAccount>1</ordAccount>
<logId>374216011815223570893830967949</logId>
<signMethod>1</signMethod>
<lang>1</lang>
<terminalId>1</terminalId>
<terminalLocation>Earth</terminalLocation>
</request>
</aa:authorizePayment>
</soap:Body>
</soap:Envelope>
The call works. I need a way to fix this issue server side, the client can't be modified to change the request.
Versions:
JBoss AS release: 7.3.4.Final-redhat-1 "Janus"
JBoss AS product: EAP 6.2.4.GA
java.version: 1.7.0_91
Set the elementFormDefault=qualified in the wsdl, generate the artifacts and redeploy the application.
Qualified means that local element declarations (xs:element within xs:complexType) refers to elements in the target namespace of the schema. Without it, they refer to elements in no namespace.

Categories