Issue accessing SOAP Header in JBoss AS7
arulprashanth May 5, 2013 6:52 PMI have to access the Soap Header in the WebService Request and perform a functionality based on the information in header. I am running into the below issues
1. I have a SOAPHandler written but when I access the header attributes all values are null but I can see any new attributes added as part of the request being available in SOAPHeader expect that the value is null.
2. How can I access the header value in my WebMethod
CustomSOAPHandler
public class CustomSOAPHandler implements SOAPHandler<SOAPMessageContext>{
@Override
public boolean handleMessage(SOAPMessageContext context) {
System.out.println("Server : handleMessage()......");
Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
//for response message only, true for outbound messages, false for inbound
if(!isRequest){
try{
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
System.out.println("Server : handleMessage()......"+isRequest);
if (soapHeader == null){
soapHeader = soapEnv.addHeader();
throw new IllegalArgumentException("No SOAP header.");
}
Iterator it = soapHeader.extractAllHeaderElements();
System.out.println(" >> message_type_id >> "+soapHeader.getAttribute("message_type_id"));
while(it.hasNext()){
System.out.println(it.next().toString());
}
//tracking
soapMsg.writeTo(System.out);
}catch(SOAPException e){
System.err.println(e);
}catch(IOException e){
System.err.println(e);
}
}
//continue other handler chain
return true;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
System.out.println("Server : handleFault()......");
return true;
}
@Override
public void close(MessageContext context) {
System.out.println("Server : close()......");
}
@Override
public Set<QName> getHeaders() {
System.out.println("Inside SOAP handler of get Headers");
QName messageTypeId = new QName("http://www.test.com","message_type_id");
HashSet<QName> headers = new HashSet<QName>();
headers.add(messageTypeId);
System.out.println("got Headers: " + headers);
return headers;
}
}
SOAP Request :
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rule="http://ws.test.com/rule" xmlns:ws="http://ws.test.com/" xmlns:ebus="http://schemas.datacontract.org/2004/07/test.e.Components.Messaging">
<soapenv:Header>
<message_type_id mustUnderstand="1" xmlns="http://www.test.com">102</message_type_id>
</soapenv:Header>
<soapenv:Body>
<ws:Respond>
</ws:Respond>
</soapenv:Body>
</soapenv:Envelope>
Server Output:
stdout - Server : handleMessage()......
stdout - Server : handleMessage()......false
stdout - >> message_type_id >>
stdout - [message_type_id: null]
stdout - <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing">
stdout - <soapenv:Header>
stdout -
stdout - </soapenv:Header>
stdout - <soapenv:Body>
stdout - <ws:Respond>
stdout - </ws:Respond>