目前,我需要在每个必须进行的每次SOAP调用中重复以下标题
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")
}
一等奖是将envelopeAttributes,version和header放在某种闭包/变量/ map中。二等奖是提取标题
e.g。
client.send(SOAPAction: 'http://TEST/developments/2013/01/IP24DevelopmentService1/PingSecured') {
header
body {
PingSecured(xmlns:"http://TEST/developments/2013/01")
}
这可能吗?
答案 0 :(得分:0)
我最近做了类似的事情。为了让您的示例独立进行测试,我只是将client
实例化为MarkupBuilder
并将username
和password
作为参数传递。
重构非常简单。
envelopeAttributesMap
是一个简单的地图,可以在任何地方作为参数传递。soapVersion
只是一个保持原始常量的变量。header()
是一种在client
调用时继续输出的方法,并插入标题元素。 这是重构的代码,它产生与原始代码相同的输出:
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')
}
}
}
}