How to get SOAP headers
Asked Answered
E

5

5

Here is the request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soap="http://soap.ws.server.wst.fit.cvut.cz/">
    <soapenv:Header>
        <userId>someId</userId>
    </soapenv:Header>
    <soapenv:Body>
    ...
    </soapenv:Body>
</soapenv:Envelope>

and I want to get that userId.

I tried this

private List<Header> getHeaders() {
    MessageContext messageContext = context.getMessageContext();
    if (messageContext == null || !(messageContext instanceof WrappedMessageContext)) {
        return null;
    }
    Message message = ((WrappedMessageContext) messageContext).getWrappedMessage();
    return CastUtils.cast((List<?>) message.get(Header.HEADER_LIST));
}

private String getHeader(String name) {
    List<Header> headers = getHeaders();
    if (headers != null) {
        for (Header header : headers) {
            logger.debug(header.getObject());
            // return header by the given name                   
        }
    }
    return null;
}

And it logs [userId : null]. How can I get the value and why is null there?

Erasmoerasmus answered 23/4, 2012 at 16:30 Comment(0)
V
8

"[userId : null]" is generally the "toString" printout of a DOM element. Most likely if you do something like

logger.debug(header.getObject().getClass())

you will see that it is a DOM Element subclass of somesort. Thus, something like:

logger.debug(((Element)header.getObject()).getTextContent())

might print the text node.

Viborg answered 23/4, 2012 at 17:33 Comment(0)
K
7
    import javax.xml.soap.*;

    SOAPPart part = request.getSOAPPart();
    SOAPEnvelope env = part.getEnvelope();
    SOAPHeader header = env.getHeader();
    if (header == null) {
        // Throw an exception
     }

    NodeList userIdNode = header.getElementsByTagNameNS("*", "userId");
    String userId = userIdNode.item(0).getChildNodes().item(0).getNodeValue();
Klansman answered 16/1, 2014 at 19:39 Comment(4)
Dunno how efficient and reliable this code snippet is but worked well for me ;)Monetmoneta
I found this answer helpful because it (correctly) shows how to extract things out of the SOAP header.Pastoral
SOAPPart part = request.getSOAPPart(); this part give me error, as it can't identify request.Raffaello
How do you obtain the request here?Clinkstone
K
2

You can get soap headers without Interceptors and without JAXB.

In your service_impl class add :

public class YourFunctionNameImpl implements YourFunctionName{

@Resource
private WebServiceContext context;

private List<Header> getHeaders() {
    MessageContext messageContext = context.getMessageContext();
    if (messageContext == null || !(messageContext instanceof WrappedMessageContext)) {
        return null;
    }

    Message message = ((WrappedMessageContext) messageContext).getWrappedMessage();
    List<Header> headers = CastUtils.cast((List<?>) message.get(Header.HEADER_LIST));
    return headers;
}

...

Then in your function you can use:

List<Header> headers = getHeaders();
        for(Iterator<Header> i = headers.iterator(); i.hasNext();) { 
            Header h = i.next(); 
            Element n = (Element)h.getObject(); 

            System.out.println("header name="+n.getLocalName()); 
            System.out.println("header content="+n.getTextContent()); 
    }
Kyser answered 9/7, 2013 at 15:9 Comment(1)
this method was more convenient for me to access the SOAP headers from service layer. CXF FAQ: cxf.apache.org/faq.html#FAQ-HowcanIaddsoapheaderstotherequest/…?Schizothymia
C
1

We can get SOAP header in server side by adding following code in CXF interceptor.

Create a class like

public class ServerCustomHeaderInterceptor extends AbstractSoapInterceptor {

@Resource
private WebServiceContext context;

public ServerCustomHeaderInterceptor() {
    super(Phase.INVOKE);

}

@Override
public void handleMessage(SoapMessage message) throws Fault,JAXBException {
    System.out.println("ServerCustomHeaderInterceptor  handleMessage");
    JAXBContext jc=null;
    Unmarshaller unmarshaller=null;
    try {
    jc = JAXBContext.newInstance("org.example.hello_ws");
    unmarshaller = jc.createUnmarshaller();
    } catch (JAXBException e) {
    e.printStackTrace();
    }


    List<Header> list = message.getHeaders();
    for (Header header : list) {
            ElementNSImpl el = (ElementNSImpl) header.getObject();
        ParentNode pn= (ParentNode) el.getFirstChild();
        //Node n1= (Node) pn;
        //Node n1= (Node) el.getFirstChild();

        CustomHeader customHeader=(CustomHeader)  unmarshaller.unmarshal(el.getFirstChild());


    }

}

After this we need to inject this as a interceptor like

 <jaxws:inInterceptors>
        <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
    <bean class="org.example.hellows.soap12.ServerCustomHeaderInterceptor" />
   </jaxws:inInterceptors>

in your server context xml.

We may need to change few lines as per your requirements. Basic flow will work like this.

Candis answered 18/3, 2013 at 13:34 Comment(0)
H
0

Having a MessageContext messageContext, you can use this code:

HeaderList hl = (HeaderList) messageContext.get(JAXWSProperties.INBOUND_HEADER_LIST_PROPERTY);

which gives you access to all SOAP headers.

Headrace answered 23/4, 2012 at 17:15 Comment(2)
As I see it, HeaderList and JAXWSProperties are classes in com.sun.xml.internal.ws.* package. I really don't want to use this...Erasmoerasmus
MessageContext is part of the regular java packages, nowHeadrace

© 2022 - 2024 — McMap. All rights reserved.