Rajesh
Rajesh

Reputation: 21

Read SOAP request header with Spring

I am trying to read the SOAP request header from a endpoint in spring this way:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
@ResponsePayload
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request, MessageContext context) {
    GetCountryResponse response = new GetCountryResponse();
    response.setCountry(countryRepository.findCountry(request.getName()));

    return response;
}

As you can see I have the MessageContext as a parameter in the handle method of the endpoint and I do the following in order to try to read the SOAP header coming from te request:

SaajSoapMessage soapRequest = (SaajSoapMessage) messageContext.getRequest();
SoapHeader reqheader = soapRequest.getSoapHeader();
while (itr.hasNext()) {
    SoapHeaderElement ele = itr.next();
}

Apparently I am getting access to the SOAP header, but at this point I´m not really sure how to read the value of any SOAP header element, I´ve tried different approaches with no success.

For example, if the following SOAP request is coming from the soapUI I want to read the value 123456 from networkCode element:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Header>
        <ns1:RequestHeader
     soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next"
     soapenv:mustUnderstand="0"
     xmlns:ns1="https://www.google.com/apis/ads/publisher/v201508">
      <ns1:networkCode>123456</ns1:networkCode>
      <ns1:applicationName>DfpApi-Java-2.1.0-dfp_test</ns1:applicationName>
    </ns1:RequestHeader>
  </soapenv:Header>
  <soapenv:Body>
    <getAdUnitsByStatement xmlns="https://www.google.com/apis/ads/publisher/v201508">
      <filterStatement>
        <query>WHERE parentId IS NULL LIMIT 500</query>
      </filterStatement>
    </getAdUnitsByStatement>
  </soapenv:Body>
</soapenv:Envelope>

Thanks in advance and best reards.

Upvotes: 1

Views: 4553

Answers (3)

Yoy can you QName to extract data by using necessary tag

SaajSoapMessage soapRequest = (SaajSoapMessage) messageContext
                .getRequest();
        SoapHeader reqheader = soapRequest.getSoapHeader();
        Iterator<SoapHeaderElement> itr = reqheader.examineAllHeaderElements();
        while (itr.hasNext()) {
            SoapHeaderElement testedElement = itr.next();
            if (testedElement.getName()
                    .equals(new QName("https://www.google.com/apis/ads/publisher/v201508", "networkCode", "ns1"))) {
                this.messageId = testedElement.getText();
                break;
            }
 }

Upvotes: 2

TestHat
TestHat

Reputation: 185

please try to get all the Header element from the Your Request like:

SaajSoapMessage soapRequest = (SaajSoapMessage) messageContext.getRequest();

Iterator HeaderList = soapRequest.getEnvelope().getHeader().examineAllHeaderElements();

while (HeaderList.hasNext()) {

        SoapHeaderElement HeaderElements = HeaderList.next();
        println("\n"+HeaderElements.getName().getLocalPart()+ " - "+HeaderElements.getText());   
    }

}

Upvotes: 0

TestHat
TestHat

Reputation: 185

In SoapUI using Script assertion,we can do this:

As your request itself contains the Header Details, we can read any element of your Request xml using xpath.

Replace the TeststepName with your TestStepName.

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def holder = groovyUtils.getXmlHolder( "TeststepName#Request" )

holder.namespaces["ns1"] = "https://www.google.com/apis/ads/publisher/v201508"

def y = holder["(//ns1:networkCode)"]

log.info "Value of networkCode"+ y

or

assert holder["(//ns1:networkCode)"]=='123456'

Upvotes: 0

Related Questions