users@saaj.java.net

[SAAJ-USR] SOAP Header problem

From: Harshad <harshadoak_at_hotpop.com>
Date: Mon, 2 Feb 2004 20:16:00 +0530

Hello,

I am using SAAJ to send a SOAP message that needs to be exactly in a certain format. I have managed to get quite close yet there are a few things that I am unable to get working with SAAJ. Would be great if somebody on the list can help out. I have simplified my real life example so that others could try out the code and hopefully come up with a solution.

The SOAP message I need to send is in the form:

<SOAP:Envelope xmlns:SOAP="urn:schemas-xmlsoap-org:soap.v1">
     <SOAP:Header>
          <Head1>H1</Head1>
     </SOAP:Header>
     <SOAP:Body>
          <Body1>B1</Body1>
     </SOAP:Body>
</SOAP:Envelope>

However I tried many things and the best I could do with SAAJ is:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header>
        <DontWantThisPrefix:Head1 xmlns:DontWantThisPrefix="DontWantThis">
            H1
        </DontWantThisPrefix:Head1>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <Body1>
            B1
        </Body1>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

So the things that change between expected and actual are:
  a.. SOAP-ENV: The tag name is SOAP-ENV instead of SOAP
  b.. SAAJ forces me to have a prefix and namespace to all header tags. So I could not create the expected tag within the SOAP header.
The Java Code that generates the above output is as follows:

import java.io.*;

import javax.xml.soap.*;
import javax.xml.transform.stream.*;

public class SAAJTrial {

    public static void main(String[] args) throws Exception {
        SOAPConnection connection = SOAPConnectionFactory.newInstance()
                                                         .createConnection();
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPPart soapPart = message.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();

        SOAPHeader header = message.getSOAPHeader();

        SOAPHeaderElement headEle = header.addHeaderElement(envelope.createName(
                    "Head1", "DontWantThisPrefix", "DontWantThis"));
        headEle.addTextNode("H1");

        SOAPBody body = envelope.getBody();

        SOAPElement postAdvert = body.addChildElement("Body1").addTextNode("B1");

        message.saveChanges();
        message.writeTo(System.out);
        connection.close();
    }
}

Is there any way on by which I can send the expected SOAP message ???????
thanks,
harshad