How to read SOAP Header information from request and add it to response in spring web services

Vishal Agrahari picture Vishal Agrahari · Dec 15, 2011 · Viewed 28.1k times · Source

I am working on spring web services. I need to add some custom elements in the request and response message.which should look like this:

<soapenv:Envelope>
   <soapenv:Header>
      <tid:SplsTID>
         <tid:Trantype>123</tid:Trantype>
         <tid:Tranver>234</tid:Tranver>
      </tid:SplsTID>
   </soapenv:Header>
   <soapenv:Body>
      <get:GetOrderNumberRequest LoggingLevel="REGULAR" MonitorFlag="Y">
         <get:Header>
            <get:TransactionId>111</get:TransactionId>
            <get:SourceSystemId>SOMS</get:SourceSystemId>
            <get:DateTime>2011-11-11T11:11:11</get:DateTime>
         </get:Header>
         <get:Body>
            <get:StaplesOrderNumber RangeFlag="N" ReleaseFlag="N">
               <get:OrderNumber Count="1" End="11" Start="9"/>
            </get:StaplesOrderNumber>
         </get:Body>
      </get:GetOrderNumberRequest>
   </soapenv:Body>
</soapenv:Envelope>

i am able to append <tid:SplsTID> under <soapenv:Header> in request by modifying the WSDL file. which looks like this:

<wsdl:message name="GetOrderNumberRequest">
        <wsdl:part element="tns:GetOrderNumberRequest" name="GetOrderNumberRequest">
        </wsdl:part>
        <wsdl:part element="sch1:SplsTID" name="SplsTID">
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="GetOrderNumberResponse">
        <wsdl:part element="tns:GetOrderNumberResponse" name="GetOrderNumberResponse">
        </wsdl:part>
        <wsdl:part element="sch1:SplsTID" name="SplsTID">
        </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="ONAS">
        <wsdl:operation name="GetOrderNumber">
            <wsdl:input message="tns:GetOrderNumberRequest" name="GetOrderNumberRequest">
            </wsdl:input>
            <wsdl:output message="tns:GetOrderNumberResponse" name="GetOrderNumberResponse">
            </wsdl:output>
        </wsdl:operation>
    </wsdl:portType>

The problem is, i want to read <tid:SplsTID> part from the request and wanted to append it under soap header part of the response, which is not happening. i am using annotation based end point. what is the code which will read the soap header and will append the same in the response.

currently my end point class is:

@Endpoint
public class OrderNumberServiceEndPoint {
    public static final String NAMESPACE_URI = "http://schemas.staples.com/onas/getOrderNumber";

    /**
     * The local name of the expected request.
     */
    public static final String REQUEST_LOCAL_NAME = "GetOrderNumberRequest";

    /**
     * The local name of the created response.
     */
    public static final String RESPONSE_LOCAL_NAME = "GetOrderNumberResponse";

    private GetOrderNumberService getOrderNumberService;

    public void setGetOrderNumberService(
            GetOrderNumberService getOrderNumberService) {
        this.getOrderNumberService = getOrderNumberService;
    }

    @PayloadRoot(localPart = REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
    public GetOrderNumberResponse processOrderNumberRequest(
            GetOrderNumberRequest request) throws Exception {
        GetOrderNumberResponse response = null;
        try{
        response = getOrderNumberService.executeRequest(request);
        }catch(CannotCreateTransactionException e){
            logger.error(ErrorConstants.ERROR_E17);
            throw new ServiceException(ErrorConstants.ERROR_E17);
        }
        return response;
    }

}

Let me know if more details are required. Any help would be appreciated.

Answer

Vishal Agrahari picture Vishal Agrahari · Dec 22, 2011

Finally i succeeded in reading the soap header from request and append into response. This is how my end point method looks like now:

 @PayloadRoot(localPart = REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
    @ResponsePayload
    public GetOrderNumberResponse processOrderNumberRequest(
            @RequestPayload GetOrderNumberRequest request,
            MessageContext messageContext) throws Exception {
        logger.info("Request Received");
        // read SOAP Header from request and append in response
        SaajSoapMessage soapRequest = (SaajSoapMessage) messageContext
                .getRequest();
        SoapHeader reqheader = soapRequest.getSoapHeader();
        SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext
                .getResponse();
        SoapHeader respheader = soapResponse.getSoapHeader();
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Iterator<SoapHeaderElement> itr = reqheader.examineAllHeaderElements();
        while (itr.hasNext()) {
            SoapHeaderElement ele = itr.next();
            transformer.transform(ele.getSource(), respheader.getResult());
        }
        // process the request PayLoad
        GetOrderNumberResponse response = null;
        try {
            response = getOrderNumberService.executeRequest(request);
        } catch (CannotCreateTransactionException e) {
            logger.error(ErrorConstants.ERROR_E17);
            throw new ServiceException(ErrorConstants.ERROR_E17);
        }
        logger.info("Response Sent");
        return response;
    }