Announcement Announcement Module
Collapse
No announcement yet.
Reading and writing nested header Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading and writing nested header

    I have an Iterceptor where I am handilg both the request and response. My header has nested elemnts. The datastructure is like this

    <xsd:complexType name="soapRequestHeader">
    <xsd:all>
    <xsd:element name="requestID" type="xsd:string" />
    <xsd:element name="requestPriority" type="xsd:string" />
    <xsd:element name="isTraceEnabled" type="xsd:boolean"/>
    <xsd:element name="clientIPAddress" type="xsd:string"/>
    <xsd:element name="customerProfile" type="tns:customerProfile"/>
    <xsd:element name="userEntitlement" type="tns:userEntitlement"/>
    <xsd:element name="serviceMetadataKeys" type="tns:serviceMetadataKeys"/>
    </xsd:all>




    In the handleRequest when I try to get the header elements by iterating I am only able to get the root elemnt i.e the soapRequestHeader and not the nested elemnts.

    I tried like this

    SoapHeader header = (SoapHeader) requestHeader;
    SoapHeaderElement headerElement = null;
    Iterator iter = header.examineAllHeaderElements();

    while (iter.hasNext()) {
    Object headerElementObject = iter.next();

    if (headerElementObject instanceof SoapHeaderElement) {
    headerElement = (SoapHeaderElement) headerElementObject;
    }
    }

    How can I get the nested elemnts?

    It can be done by getting headerElement.getSource(); and then unmarshalling the source. But is there any other way.

    Also in case of handleResponse I am constructing the hesder with nested elements.

    How can it be done?

  • #2
    Once you obtain the Source for an element you can transform it to any kind of Result you see fit (DOMResult for instance) or pass it to an unmarshaller.

    As for the adding SOAP headers to the response, it also works with the Source/Result abstraction. Here's a snippet:
    Code:
        public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
            SoapMessage message = (SoapMessage) messageContext.getResponse();
            SoapHeader header = message.getSoapHeader();
            Result result = header.getResult();
            MyPojo pojo = new MyPojo();
            pojo.setText("abc");
            pojo.setX(123);
            marshaller.marshal(pojo, result);
            return true;
        }
    MyPojo being a class marshalled by JAXB2.

    Comment


    • #3
      Thanks
      I was wondering instead of transforming and then using a marshaller does the spring-ws provide any API to directly read the nested header?
      Something like iterating through all the headerelemnts?

      Comment


      • #4
        Well, you don't have to use marshalling. You can obtain a DOM document or a Stax stream and use it to process your headers.
        If you are worried about performance, you can use a technique similar to PayloadRootUtils.getPayloadRootQName to avoid unnecessary transformations.

        Comment


        • #5
          The spring-ws api does allow you to gain access to the nested headers, but you have to end up using the Axiom API (or SAAJ I assume if not using Axiom) directly once you get ahold of the underlying Axiom objects in a message.

          See my post in http://forum.springframework.org/showthread.php?t=47876 for a few pointers on writing nested hdrs. This seems to work ok as long as you don't mind being Axiom specific.

          If you just want to read the nested hdrs you can do something like:
          AxiomSoapMessage msg = ...
          msg.getEnvelope(); // make sure there is an envelope on the spring-ws side
          SOAPMessage axiomMsg = msg.getAxiomMessage(); // now axiom specific
          SOAPEnvelope axiomEnv = axiomMsg.getSOAPEnvelope();
          SOAPHeader axiomHeader = axiomEnv.getHeader();

          // you can use axiomHeader.getHeaderBlocksWithNSURI() to search for a hdr block of interest

          Comment

          Working...
          X