GeeCON Prague 2014*
The moose likes Web Services and the fly likes SOAP Header problem SAAJ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "SOAP Header problem SAAJ" Watch "SOAP Header problem SAAJ" New topic
Author

SOAP Header problem SAAJ

Harshad Oak
Author
Ranch Hand

Joined: Oct 21, 2002
Posts: 45
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 ontWantThisPrefix="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:
1) SOAP-ENV: The tag name is SOAP-ENV instead of SOAP
2) 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. Wish to get rid of DontWantThisPrefix and DontWantThis in the message.
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 by which I can send the expected SOAP message ???
thanks,
harshad


------------------------<br />Harshad Oak<br /> <br />- Oracle ACE Director, Sun Java Champion<br /> <br />Author:<br />- Pro Jakarta Commons<br />- Oracle JDeveloper 10g: Empowering J2EE development<br />- Java 2 Enterprise Edition 1.4 Bible<br /> <br />- SCJP, SCWCD<br />------------------------
Harshad Oak
Author
Ranch Hand

Joined: Oct 21, 2002
Posts: 45
I was hoping for some help from folks at Javaranch. Have got no reply as yet even on the SAAJ users forum. Strange? Doesn't anybody use SAAJ?
James Zhang
Ranch Hand

Joined: Sep 06, 2002
Posts: 30
Hi, harshad:
I tried your sample code on my machine and here is the result:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><DontWantThisPrefix:Head1 xmlns ontWantThisPrefix="DontWantThis">H1</DontWantThisPrefix:Head1></SOAP-ENV:Header><SOAP-ENV:Body><Body1>B1</Body1></SOAP-ENV:Body></SOAP-ENV:Envelope>
I guess probably you have some out-of-date JAR files in your classpath which caused the problem.
Good luck
--Jian
 
GeeCON Prague 2014
 
subject: SOAP Header problem SAAJ