2013-10-29 32 views
1

目前我需要重复下面的头在每家每户的SOAP调用我必须做出传递的SOAP Header关闭

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,版本和标题/地图。二等奖只是提取标题

例如

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

这可能吗?

回答

0

我最近做了类似的事情。为了让您的示例单独进行测试,我刚刚将client例示为MarkupBuilder,并将usernamepassword作为参数传递。

重构非常简单。

  • envelopeAttributesMapenvelopeAttributesMap是一个简单的地图,可以在任何地方作为参数传递。
  • 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') 
      } 
     } 
    } 
}