Tuesday, June 5, 2007

JAX-WS and how to send SOAP Header

When i do my project, i met a problems. How can i add soap header to soap messages. The webservice is developed with JAX-WS library.

I really don't know why jax-ws does not implements such a method to add soap header from the implementation of web-service. We have to use another way to do that: Soap Handler. A Soap Handler is called before the implementation of webservice called. So we used them to send soap header.

for example:
we have a webservice with one function: sayHello(). They function required that its response and request come along with a soap header. It should contains the value of session.

[soap:header]
[session] 12424512[/session]
[/soap:header]
[soap:body]
[sayhelloresponse] .... [/sayhelloresponse]
[/soap:body]

How we add soap:header??

Step to do:
in the class implement the function sayHello()

@Resource
WebServiceContext wsContext

void sayHello(){
MessageContext message = wsContext.getMessage();
message.put("session", 245235); //this will put the value to the context of the //webservice
}

In the soap handler, we get the value we store in the context of webservice. and put to soap handler. Only in soap handler, we have a chance to create soap header. So that why we have to do in this way.

public class SOAPLoggingHandler implements
SOAPHandler {
...

public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean)
smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

if (outboundProperty) {
MessageContext context = smc.getMessageContext();;
String session = context.get("Session");
SOAPMessage soapMessage = smc.getMessage();
//Add header to soapmessage. Using function of soap message like
//GetElementHeader, GetHeader ... I am not remember exactly. I will update this soon.
}
return true;
}

public boolean handleFault(SOAPMessageContext smc) {
logToSystemOut(smc);
return true;
}

public void close(MessageContext messageContext) {
}
}

No comments:

Google