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 theclient
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')
}
}
}
}