onsdag 9 februari 2011

GroovyWS: setting endpoint and soap headers

I've seen questions on various forums regarding how to change the endpoint and add soap headers with GroovyWS.
Since GroovyWS is using CXF under its cover it is possible (albeit not intuitive) to do:


@Grab(
group='org.codehaus.groovy.modules',
module='groovyws',
version='0.5.2')
import groovyx.net.ws.WSClient
import org.apache.cxf.headers.Header
import javax.xml.soap.SOAPFactory
import javax.xml.namespace.QName
import org.apache.cxf.binding.soap.SoapHeader
import javax.xml.ws.BindingProvider
import org.apache.cxf.interceptor.LoggingOutInterceptor

proxy = new WSClient(
"http://www.w3schools.com/webservices/tempconvert.asmx?WSDL", this.class.classLoader)
proxy.initialize()

def cxfClient = proxy.client

def headers = []

// Add header
SOAPFactory sf = SOAPFactory.newInstance()
def authElement = sf.createElement(
new QName("a_namespace", "a_name"))

def productToken =
authElement.addChildElement("productToken")

def tokenHeader = new SoapHeader(
new QName("a_namespace", "a_name"), authElement);
headers.add(tokenHeader);

cxfClient.getRequestContext()
.put(Header.HEADER_LIST, headers)

// Set endpoint
cxfClient.getRequestContext()
.put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "another_url")

// Add a logging interceptor
cxfClient.outInterceptors.add(
new LoggingOutInterceptor())

result = proxy.CelsiusToFahrenheit(0)
println """You are probably freezing at
${result} degrees Farhenheit"""

Inga kommentarer:

Skicka en kommentar