Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot change soap URI in envelope using java SoapMessage

Tags:

java

uri

I'm trying to create a simple SOAP message to send from a client, but I am (seemingly) unable to change the URI of the "soap" namespace in the envelope.

This is what the soap header SHOULD look like:

<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope/"  soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding/"> ... </soap:Envelope>

So I have the following code:

    final SOAPMessage sm = MessageFactory.newInstance().createMessage();

    final SOAPPart sp = sm.getSOAPPart();
    final SOAPEnvelope se = sp.getEnvelope();
    final SOAPHeader sh = se.getHeader();
    final SOAPBody sb = se.getBody();

    se.removeNamespaceDeclaration(se.getPrefix());
    se.addNamespaceDeclaration("soap", "http://www.w3.org/2001/12/soap-envelope");
    se.setPrefix("soap");
    sb.setPrefix("soap");
    sh.setPrefix("soap");
    se.setEncodingStyle("http://www.w3.org/2001/12/soap-encoding/");

However, when I print the message before sending, the following is my envelope:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding/">

Notice the differences in the URIs of xmlns:soap in the "should-be" section and the actual.

If I change the first argument of the addNamespaceDeclaration call to "soapy" instead of "soap", this is the following envelope I get:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapy="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding/">

I'm guessing it may have something to do with the fact that the call is addNamespaceDeclaration rather than something like changeNamespaceDeclaration, and it is ignored considering the namespace is already present, but I cannot find something that works (I've already tried setAttributeNS).

EDIT: I just realized that setAttributeNS is silly because that's changing the namespace, not the URI. EDIT AGAIN: I'm a little confused, as I continue to search I see sometimes that the naming goes soap:"Namespace", so in that sense I do want to change the namespace... but I thought the namespace was the "soap" part. Any clarifications?

This is my first post so I apologize if I'm asking something that has already been solved, but I've searched around and most of what I've found is related to changing the namespace (like from SOAP-ENV, which is the default namespace, to soap) rather than the URI itself. Thanks in advance.

-M

like image 603
MandM Avatar asked Apr 23 '12 18:04

MandM


1 Answers

In general you should not need to manually modify the SOAP namespace. What you probably want to achieve is to create a SOAP 1.2 message (which has a different namespace than SOAP 1.1). Try removing all namespace altering lines from your code and change the first line to

final SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();

In case you REALLY need to specify which prefix that should be used, this code seems to work:

SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
sm.getSOAPPart().getEnvelope().setPrefix("soap");
sm.getSOAPPart().getEnvelope().removeNamespaceDeclaration("env");
sm.getSOAPHeader().setPrefix("soap");
sm.getSOAPBody().setPrefix("soap");
like image 116
erikxiv Avatar answered Nov 14 '22 21:11

erikxiv