Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SOAP Header problem SAAJ

 
Harshad Oak
Author
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harshad Oak
Author
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic