Question

Currently I need to repeat the following header in every every SOAP call I have to make

 client.send(SOAPAction: 'http://TEST/developments/2013/01/IP24DevelopmentService1/PingSecured') {
        envelopeAttributes "xmlns:test": 'http://test.cxf.grails.org/', "xmlns:soapenv":"soapenv"
        version SOAPVersion.V1_1
        header {
            'wsse:Security'('soapenv:mustUnderstand': "1", 'xmlns:wsse': 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'xmlns:wsu': 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd') {
                'wsse:UsernameToken'('wsu:Id':"UsernameToken-13") {
                    'wsse:Username'(username)
                    'wsse:Password'('Type':'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText',password)
                    'wsse:Nonce'('EncodingType':'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary',new String(password.bytes.encodeBase64().toString()))
                    'wsu:Created'('2013-01-18T16:19:17.950Z')
                }
            }
        }
        body {
            PingSecured(xmlns:"http://TEST/developments/2013/01")
        }

First prize is to have the envelopeAttributes, version and header in some sort of closure/variable/map. Second prize is to just have the header extracted

e.g.

client.send(SOAPAction: 'http://TEST/developments/2013/01/IP24DevelopmentService1/PingSecured') {
    header
    body {
        PingSecured(xmlns:"http://TEST/developments/2013/01")
    }

Is this possible?

No correct solution

OTHER TIPS

I recently did something similar. To make your example work standalone for testing, I just instantiated client as a MarkupBuilder and passed username and password as parameters.

The refactoring is quite simple.

  • envelopeAttributesMap is a simple map that could be passed as a parameter wherever used.
  • soapVersion is simply a variable holding the original constant.
  • header() is a method that continues the output on the client whenever it is called and inserts the header elements.

Heres the refactored code, which produced the same output as the original:

def refactored(username, password) {
    MarkupBuilder client = new MarkupBuilder()
    def envelopeAttributesMap = ["xmlns:test": 'http://test.cxf.grails.org/', "xmlns:soapenv":"soapenv"]
    def soapVersion = SOAPVersion.V1_1
    client.send(SOAPAction: 'http://TEST/developments/2013/01/IP24DevelopmentService1/PingSecured') {
        header(username, password, envelopeAttributesMap, soapVersion, client)
        body {
            PingSecured(xmlns:"http://TEST/developments/2013/01")
        }
    }
}

def header(username, password, envelopeAttributesMap, soapVersion, client) {
    client.envelopeAttributes(envelopeAttributesMap)
    client.version(soapVersion)
    client.header {
        'wsse:Security'('soapenv:mustUnderstand': "1", 'xmlns:wsse': 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'xmlns:wsu': 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd') {
            'wsse:UsernameToken'('wsu:Id':"UsernameToken-13") {
                'wsse:Username'(username)
                'wsse:Password'('Type':'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText',password)
                'wsse:Nonce'('EncodingType':'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary',password.bytes.encodeBase64())
                'wsu:Created'('2013-01-18T16:19:17.950Z')
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top