I try to change the body (the namespaceURI of a part the request) of a soap message with a SOAPHandler, but this seems not to work.
The reason for the change is, that I have to work with different clients, and there is one old one which can not be changend, so I want to change this clients message to get it work with my Server.
Here is my code:
@Override
public boolean handleMessage(SOAPMessageContext context) {
boolean isResponse = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
try {
if (!isResponse) {
SOAPMessage msg = context.getMessage();
SOAPBody body = msg.getSOAPBody();
Iterator<?> iter = body.getChildElements();
boolean implChanged = false;
while (iter.hasNext()) {
Object next = iter.next();
if (next instanceof SOAPElement) {
SOAPElement se = (SOAPElement) next;
String uri = se.getNamespaceURI();
if (uri.equalsIgnoreCase("http://service.plr.mycomp.com/BruttoNettoRechner/1.0")) {
if (!se.removeNamespaceDeclaration("impl")) {
System.out.println("FEHLER beim entfernen");
}
if (se.addNamespaceDeclaration("impl", "http://service.plr.mycomp.com") == null) {
System.out.println("Fehler beim hinzufügen");
}
System.out.println(se.getNamespaceURI());
System.out.println(se.lookupNamespaceURI("impl"));
implChanged = true;
}
}
}
if (msg.saveRequired()) {
msg.saveChanges();
}
msg.writeTo(System.out);
}
} catch (SOAPException e) {
System.out.println("SOAP FEHLER");
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
Here is the input:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<impl:calculateBruttoNetto xmlns:impl="http://service.plr.mycomp.com/BruttoNettoRechner/1.0">
<bruttoNettoIn>
<ns1:aktBruttoEink xmlns:ns1="http://model.plr.mycomp.com">4000.0</ns1:aktBruttoEink>
<ns1:aktNettoEink ns2:nil="true" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://model.plr.mycomp.com"/>
<ns1:alleinverdienerJN xmlns:ns1="http://model.plr.mycomp.com">false</ns1:alleinverdienerJN>
<ns1:anzGehaelter xmlns:ns1="http://model.plr.mycomp.com">2</ns1:anzGehaelter>
<ns1:berufsgruppe xmlns:ns1="http://model.plr.mycomp.com">2</ns1:berufsgruppe>
<ns1:bruttoNetto xmlns:ns1="http://model.plr.mycomp.com">1</ns1:bruttoNetto>
</bruttoNettoIn>
</impl:calculateBruttoNetto>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The console output says that the URI has not changed, but when I ask for the whole message it seems to work. But the WARNING after shows that nothing has changed:
11:08:52,801 INFO [stdout] (http-/0.0.0.0:8080-1) http://service.plr.mycomp.com/BruttoNettoRechner/1.0
11:08:52,801 INFO [stdout] (http-/0.0.0.0:8080-1) http://service.plr.mycomp.com/BruttoNettoRechner/1.0
11:08:52,803 INFO [stdout] (http-/0.0.0.0:8080-1) <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="http://service.plr.mycomp.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header/><SOAP-ENV:Body>
11:08:52,803 INFO [stdout] (http-/0.0.0.0:8080-1) <impl:calculateBruttoNetto xmlns:impl="http://service.plr.mycomp.com">
11:08:52,803 INFO [stdout] (http-/0.0.0.0:8080-1) <bruttoNettoIn>
11:08:52,803 INFO [stdout] (http-/0.0.0.0:8080-1) <ns1:aktBruttoEink xmlns:ns1="http://model.plr.mycomp.com">4000.0</ns1:aktBruttoEink>
11:08:52,803 INFO [stdout] (http-/0.0.0.0:8080-1) <ns1:aktNettoEink xmlns:ns1="http://model.plr.mycomp.com" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" ns2:nil="true"/>
11:08:52,803 INFO [stdout] (http-/0.0.0.0:8080-1) <ns1:alleinverdienerJN xmlns:ns1="http://model.plr.mycomp.com">false</ns1:alleinverdienerJN>
11:08:52,804 INFO [stdout] (http-/0.0.0.0:8080-1) <ns1:anzGehaelter xmlns:ns1="http://model.plr.mycomp.com">2</ns1:anzGehaelter>
11:08:52,804 INFO [stdout] (http-/0.0.0.0:8080-1) <ns1:berufsgruppe xmlns:ns1="http://model.plr.mycomp.com">2</ns1:berufsgruppe>
11:08:52,804 INFO [stdout] (http-/0.0.0.0:8080-1) <ns1:bruttoNetto xmlns:ns1="http://model.plr.mycomp.com">1</ns1:bruttoNetto>
11:08:52,804 INFO [stdout] (http-/0.0.0.0:8080-1) </bruttoNettoIn>
11:08:52,804 INFO [stdout] (http-/0.0.0.0:8080-1) </impl:calculateBruttoNetto>
11:08:52,804 INFO [stdout] (http-/0.0.0.0:8080-1) </SOAP-ENV:Body>
11:08:52,804 INFO [stdout] (http-/0.0.0.0:8080-1)
11:08:52,805 WARNING [org.apache.cxf.phase.PhaseInterceptorChain] (http-/0.0.0.0:8080-1) Interceptor for {http://service.plr.mycomp.com/}BruttoNettoRechnerService#{http://service.plr.mycomp.com}calculateBruttoNetto has thrown exception, unwinding now: org.apache.cxf.interceptor.Fault: Unexpected wrapper element {http://service.plr.mycomp.com/BruttoNettoRechner/1.0}calculateBruttoNetto found. Expected {http://service.plr.mycomp.com}calculateBruttoNetto.
at org.apache.cxf.interceptor.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:106)
So I dont understand why my change for the namespace is not working. (I dont expect that the message is working, there are other tasks to do).
thx