java - SOAPHandler: How to remove automatically added namespace/attribute from child element -
i'm trying setup soaphandler
on server convert incoming request
<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:body> <getmachine xmlns="http://machine.soap.webservices.product.company.at/"> <machineid>92623-15853588</machineid> </getmachine> </s:body> </s:envelope>
to request.
<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:body> <enns1:getmachine xmlns:enns1="http://machine.soap.webservices.product.company.at/"> <machineid>92623-15853588</machineid> </enns1:getmachine> </s:body> </s:envelope>
my soaphandler looks this.
package at.company.product.webservices.soap; import java.io.ioexception; import java.util.iterator; import java.util.set; import javax.xml.namespace.qname; import javax.xml.soap.soapbody; import javax.xml.soap.soapelement; import javax.xml.soap.soapexception; import javax.xml.soap.soapmessage; import javax.xml.ws.handler.messagecontext; import javax.xml.ws.handler.soap.soaphandler; import javax.xml.ws.handler.soap.soapmessagecontext; public class soapvalidationhandler implements soaphandler<soapmessagecontext> { private static final string prefix = "enns1"; @override public boolean handlemessage(soapmessagecontext context) { system.out.println("server : handlemessage()......"); boolean isrequest = (boolean) context .get(messagecontext.message_outbound_property); // inbound if (!isrequest) { try { soapmessage soapmsg = context.getmessage(); soapbody body = soapmsg.getsoapbody(); iterator<soapelement> = body.getchildelements(); // body elements have "xmlns" attribute , // add namespace prefix while (it.hasnext()) { soapelement elem = it.next(); addnamespaceprefix(elem); iterator itchildren = elem.getchildelements(); while (itchildren.hasnext()) { object child = itchildren.next(); if (child instanceof soapelement) { soapelement celem = ((soapelement) child); // todo: remove namespace child // celem.removenamespacedeclaration(""); => not // work } } } // tracking soapmsg.writeto(system.out); } catch (soapexception e) { system.err.println(e); } catch (ioexception e) { system.err.println(e); } } return true; } private void addnamespaceprefix(soapelement elem) throws soapexception { iterator<object> = elem.getallattributes(); qname name = new qname("xmlns"); string value = elem.getattributevalue(name); if (value != null) { elem.addnamespacedeclaration(prefix, elem.getnamespaceuri()); elem.removenamespacedeclaration(""); elem.setprefix(prefix); } } @override public boolean handlefault(soapmessagecontext context) { system.out.println("server : handlefault()......"); return true; } @override public void close(messagecontext context) { system.out.println("server : close()......"); } @override public set<qname> getheaders() { system.out.println("server : getheaders()......"); return null; } }
after processing of request soaphandler request looks this:
<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:body> <enns1:getmachine xmlns:enns1="http://machine.soap.webservices.product.company.at/"> <machineid xmlns="http://machine.soap.webservices.product.company.at/">92623-15853588</machineid> </enns1:getmachine> </s:body> </s:envelope>
as can see i'm able add namespace prefix <getmachine>
tag automatically adds xmlns
attribute child element <machineid>
. how can avoid or fix this?
after toying around api came solution. solves described case.
package at.company.product.webservices.soap; import java.io.ioexception; import java.util.iterator; import java.util.set; import javax.xml.namespace.qname; import javax.xml.soap.soapbody; import javax.xml.soap.soapelement; import javax.xml.soap.soapenvelope; import javax.xml.soap.soapexception; import javax.xml.soap.soapfault; import javax.xml.soap.soapheader; import javax.xml.soap.soapmessage; import javax.xml.ws.handler.messagecontext; import javax.xml.ws.handler.soap.soaphandler; import javax.xml.ws.handler.soap.soapmessagecontext; import javax.xml.ws.soap.soapfaultexception; public class soapvalidationhandler implements soaphandler<soapmessagecontext> { private static final string prefix = "enns1"; @override public boolean handlemessage(soapmessagecontext context) { boolean isrequest = (boolean) context .get(messagecontext.message_outbound_property); // response message only, true outbound messages, false // inbound if (!isrequest) { try { soapmessage soapmsg = context.getmessage(); soapenvelope soapenv = soapmsg.getsoappart().getenvelope(); soapheader soapheader = soapenv.getheader(); // if no header, add 1 if (soapheader == null) { soapheader = soapenv.addheader(); // throw exception generatesoaperrmessage(soapmsg, "no soap header."); } soapbody body = soapmsg.getsoapbody(); iterator<soapelement> = body.getchildelements(); while (it.hasnext()) { soapelement elem = it.next(); addnamespaceprefix(elem); iterator itchildchildren = elem.getchildelements(); while (itchildchildren.hasnext()) { object obj = itchildchildren.next(); if ((obj instanceof soapelement)) { soapelement soapelem = (soapelement) obj; string name = soapelem.getelementname().getlocalname(); qname qname = new qname(name); ((soapelement) obj).setelementqname(qname); } } } // tracking soapmsg.writeto(system.out); } catch (soapexception e) { system.err.println(e); } catch (ioexception e) { system.err.println(e); } } // continue other handler chain return true; } private void addnamespaceprefix(soapelement elem) throws soapexception { iterator<object> = elem.getallattributes(); qname name = new qname("xmlns"); string value = elem.getattributevalue(name); if (value != null) { elem.addnamespacedeclaration(prefix, elem.getnamespaceuri()); elem.removenamespacedeclaration(""); elem.setprefix(prefix); } } @override public boolean handlefault(soapmessagecontext context) { return true; } @override public void close(messagecontext context) { } @override public set<qname> getheaders() { return null; } private void generatesoaperrmessage(soapmessage msg, string reason) { try { soapbody soapbody = msg.getsoappart().getenvelope().getbody(); soapfault soapfault = soapbody.addfault(); soapfault.setfaultstring(reason); throw new soapfaultexception(soapfault); } catch (soapexception e) { } } }
Comments
Post a Comment