Elvis Oliveira Elvis Oliveira - 3 months ago 15
Java Question

Get response header using Jax WS

I need to get a SOAP Header from a response using JAX-WS to use a WS. This is the response xml:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<sessaoIdHeader xmlns="http://www.cvm.gov.br/webservices/">
<Guid>9d63221b-8a3a-4fd6-84fe-100fa53a55d5</Guid>
<IdSessao>237196706</IdSessao>
</sessaoIdHeader>
</soap:Header>
<soap:Body>
<LoginResponse xmlns="http://www.cvm.gov.br/webservices/"/>
</soap:Body>
</soap:Envelope>


And this is the my Java Code:

WsDownloadInfs infs = new WsDownloadInfs();
WsDownloadInfsSoap proxy = infs.getWsDownloadInfsSoap();
proxy.login(1779, "5270");


in this operation I don't have a return (it's void).

I have tried using BindingProvider like this:

BindingProvider prov = (BindingProvider) proxy;
HeaderList list = (HeaderList) prov.getResponseContext().get("com.sun.xml.internal.ws.api.message.HeaderList");


But it doesn't have any properties.

Answer

I find a solution!

Here's what i have done:

I take the the binding provider and then get the response HeaderList from:

    BindingProvider prov = (BindingProvider) proxy;
    HeaderList list = (HeaderList) prov.getResponseContext().get("com.sun.xml.internal.ws.api.message.HeaderList");

So, from HeaderList i get the Header i want (in this case is "_SessaoIdHeader_QNAME"):

    Header h =list.get(ObjectFactory._SessaoIdHeader_QNAME, true);

Then i create a SOAPMessage and write the Header content to it, afeter that i take what i want from the SOAPHeader:

    SOAPHeader header = message.getSOAPHeader();
    NodeList idSessaoNode = header.getElementsByTagNameNS("*", "IdSessao");
    String idSess = idSessaoNode.item(0).getChildNodes().item(0).getNodeValue();
    String guid = header.getTextContent();

Here is the complete code:

    WsDownloadInfs infs = new WsDownloadInfs();     
    WsDownloadInfsSoap proxy = infs.getWsDownloadInfsSoap();
    proxy.login(1779, "5270");


    BindingProvider prov = (BindingProvider) proxy;
    HeaderList list = (HeaderList) prov.getResponseContext().get("com.sun.xml.internal.ws.api.message.HeaderList");
    Header h =list.get(ObjectFactory._SessaoIdHeader_QNAME, true);
    SOAPMessage message = MessageFactory.newInstance().createMessage();
    h.writeTo(message);
    SOAPHeader header = message.getSOAPHeader();
    NodeList idSessaoNode = header.getElementsByTagNameNS("*", "IdSessao");
    String idSess = idSessaoNode.item(0).getChildNodes().item(0).getNodeValue();
    String guid = header.getTextContent();

Thanks again!