1

I'm trying to find a way to add header to soap fault with out using interceptor. Is there any alternative solution.

Basically I've my soap request as follows.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://kp.web.com/schema/" xmlns:sch1="http://kp.web.com/Shared/schema/">
   <soapenv:Header>
      <sch:clientHeader>
         <sch1:consumerId>12</sch1:consumerId>
      </sch:clientHeader>
   </soapenv:Header>
   <soapenv:Body>
      <sch:addRequest>
         <sch:field1>-3</sch:field1>
         <sch:field2>-1</sch:field2>
      </sch:addRequest>
   </soapenv:Body>
</soapenv:Envelope>

Success message.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
   <soap:Header>
      <serverHeader xmlns:ns2="http://kp.web.com/schema/" xmlns="http://kp.web.com/Shared/schema/">
         <ns2:consumerId>12</ns2:consumerId>
         <ns2:completionCode>100</ns2:completionCode>
      </serverHeader>
   </soap:Header>
   <soap:Body>
      <addResponse xmlns="http://kp.web.com/schema/" xmlns:ns2="http://kp.web.com/Shared/schema/">
         <result>-4</result>
      </addResponse>
   </soap:Body>
</soap:Envelope>

When fault occurs

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Faulted you sent -1 and -1</faultstring>
         <detail>
            <faultResponse xmlns:ns2="http://kp.web.com/schema/" xmlns="http://kp.web.com/Shared/schema/">400</faultResponse>
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

I would expect fault response something like this.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://kp.web.com/schema/" xmlns:sch1="http://kp.web.com/Shared/schema/">
    <soap:Header>
          <serverHeader>
             <ns2:consumerId>12</ns2:consumerId>
             <ns2:completionCode>100</ns2:completionCode>
          </serverHeader>
   </soap:Header>
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Faulted you sent -1 and -1</faultstring>
         <detail>
             <faultResponse xmlns:ns2="http://kp.web.com/schema/" xmlns="http://kp.web.com/Shared/schema/">400</faultResponse>
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

Can I use holder or is there any solution. I can't use interceptor because I need to send the consumer Id back. In interceptor I will not have request details.

2

2 Answers 2

1

You can use SOAP Message Handlers and Handler Chains. Another way do it use Filters. I use Handler Chains for modify SOAP header.

This link might be helpful.

5
  • Thanks for your quick response. But how do I pass on the soap request data to out bound handler? Dec 27, 2013 at 10:45
  • 1
    Probably fastest(thought looks like a hack) solution would be to embed your information in SOAP fault body and leave it to handler to modify headers and/or body. Dec 27, 2013 at 15:41
  • @ArunasJunevicius Thanks for your suggestion. I was thinking something of similar kind. Rather I was thinking to avoid fault response and in success message add a tag with completion status and error message tag if there is any error I would add errormessage tag in my header and send. Dec 28, 2013 at 14:49
  • 1
    @KaPra As a rule of thumb I would use SAOP fault where ever it's appropriate and avoid error messages in regular SOAP responses(ironically due to need to integrate with legacy systems we have company policy not to ever throw SOAP faults). Problem is that to keep WS contract consistent every error should be caught and returned as normal SOAP response and for starters that's impossible since JAX-WS runtime might throw SOAP fault. Anyways choose best solution that suites your situation. Dec 28, 2013 at 16:41
  • @ArunasJunevicius Yep well said. Thanks for input Dec 28, 2013 at 17:49
1

You can still use an interceptor. There are two ways you can get the data you need from the interceptor:

1) In you business logic, if you have a WebServiceContext injected in (@Resource), you can set properties on that that you should be able to pick up later from the message. (or from the message.getExchange().getInMessage())

2) From the message, you can get the Exchange and then get the inMessage. From that message you can get the contents that were unmarshalled (inMessage.getContent(List.class)) and passed into your class.

Hope that helps.

1
  • Oh Thanks........ I had used getExchange in inbound chain, but didn't expect it to be available for outbound chain. Thanks for figuring it out. I found that header or multiple part was supported earlier but was discontinued recently. from the link w3.org/TR/wsdl#_soap:fault it says only one part is allowed. Then how we to keep a track request(JMS way with message id). Was there any specific reason to make it one fault. From consumer code perspective I can understand the difficulty if we have multiple parts. Other than that I don't find any issue Jan 2, 2014 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.