String nsPrefix = null;
String oldNSPrefix = null;
boolean introduceNewNS = false;
String operationName = node.getOperationName();
WsdlMessage newInputMessage = null;
Iterable<WsdlPortType> servicePortTypes = serviceWSDL.portTypes();
WsdlPortTypeOperation serviceOperation = null;
WsdlPortType servicePortType = null;
for (WsdlPortType wsdlPortType : servicePortTypes) {
serviceOperation = wsdlPortType.getOperation(operationName);
if(serviceOperation != null){
QName inputMessageName = serviceOperation.getInput().getMessage();
WsdlMessagePart part = WSDLUtil.getfirst(serviceWSDL.getMessage(inputMessageName.getLocalPart()).parts());
nsPrefix = part.getElement().getPrefix();
oldNSPrefix = nsPrefix;
introduceNewNS = false;
while( null != wsdl.xml().lookupNamespaceByPrefix(nsPrefix)){
//this namespace is already there so keep adding control at the end and check for
//its existance
nsPrefix += "_control";
introduceNewNS = true;
}
wsdl.xml().declareNamespace(BUILDER.newNamespace(nsPrefix, serviceTns));
WsdlMessage inputMessage = serviceWSDL.getMessage(inputMessageName.getLocalPart());
newInputMessage = wsdl.addMessage(inputMessageName.getLocalPart());
Iterable<WsdlMessagePart> parts = inputMessage.parts();
for (WsdlMessagePart wsdlMessagePart : parts) {
XmlElement newPart = null;
if(introduceNewNS){
XmlElement clone = wsdlMessagePart.xml().clone();
String xmlAsString = BUILDER.serializeToString(clone).replaceAll(oldNSPrefix+":", nsPrefix+":");
newPart= BUILDER.parseFragmentFromString(xmlAsString);
}else{
newPart = wsdlMessagePart.xml().clone();
}
newInputMessage.xml().addElement(newPart);
}
//add lead header part to the new input messag because this message is copied from the service wsdl.
XmlElement newLeadHeaderPart = newInputMessage.xml().addElement(newInputMessage.xml().getNamespace(), "part");
newLeadHeaderPart.setAttributeValue("name", "leadHeader");
newLeadHeaderPart.setAttributeValue("element", "lc:context");
WsdlMessage newOutputMessage = null;
if(null != serviceOperation.getOutput()){
QName outputMessageName = serviceOperation.getOutput().getMessage();
WsdlMessage outputMessage = serviceWSDL.getMessage(outputMessageName.getLocalPart());
newOutputMessage = wsdl.addMessage(outputMessageName.getLocalPart());
Iterable<WsdlMessagePart> parts2 = outputMessage.parts();
for (WsdlMessagePart wsdlMessagePart : parts2) {
XmlElement newPart = null;
if(introduceNewNS){
XmlElement clone = wsdlMessagePart.xml().clone();
String xmlAsString = BUILDER.serializeToString(clone).replaceAll(oldNSPrefix+":", nsPrefix+":");