1

目前,我需要在我必须进行的每个 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")
        }

一等奖是在某种闭包/变量/映射中拥有信封属性、版本和标题。二等奖是只提取标题

例如

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

这可能吗?

4

1 回答 1

0

我最近做了类似的事情。为了使您的示例独立工作以进行测试,我只是将其实例client化为 a并作为参数MarkupBuilder传递。usernamepassword

重构非常简单。

  • 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')
            }
        }
    }
}
于 2013-10-29T10:19:05.903 回答