}
Source source = message.getContent(Source.class);
Element element = DomUtil.parse(source).getDocumentElement();
if (!JbiConstants.WSDL11_WRAPPER_NAMESPACE.equals(element.getNamespaceURI()) ||
!JbiConstants.WSDL11_WRAPPER_MESSAGE_LOCALNAME.equals(element.getLocalName())) {
throw new Fault("Message wrapper element is '" + QNameUtil.toString(element)
+ "' but expected '{" + JbiConstants.WSDL11_WRAPPER_NAMESPACE + "}message'");
}
List<NodeList> partsContent = new ArrayList<NodeList>();
Element partWrapper = DomUtil.getFirstChildElement(element);
while (partWrapper != null) {
if (!JbiConstants.WSDL11_WRAPPER_NAMESPACE.equals(element.getNamespaceURI()) ||
!JbiConstants.WSDL11_WRAPPER_PART_LOCALNAME.equals(partWrapper.getLocalName())) {
throw new Fault("Unexpected part wrapper element '" + QNameUtil.toString(partWrapper)
+ "' expected '{" + JbiConstants.WSDL11_WRAPPER_NAMESPACE + "}part'");
}
NodeList nodes = partWrapper.getChildNodes();
partsContent.add(nodes);
partWrapper = DomUtil.getNextSiblingElement(partWrapper);
}
Wsdl1SoapOperation wsdlOperation = getOperation(message);
Wsdl1SoapMessage wsdlMessage = server ? wsdlOperation.getOutput() : wsdlOperation.getInput();
Collection orderedParts = wsdlMessage.getParts();
if (orderedParts.size() != partsContent.size()) {
throw new Fault("Message contains " + partsContent.size() + " part(s) but expected "
+ orderedParts.size() + " parts");
}
Document document = DomUtil.createDocument();
Node body = null;
if (wsdlOperation.getStyle() == Style.RPC) {
body = DomUtil.createElement(document, wsdlMessage.getElementName());
}
int idxPart = 0;
for (Wsdl1SoapPart part : wsdlMessage.getParts()) {
NodeList nodes = partsContent.get(idxPart++);
if (part.isBody()) {
if (wsdlOperation.getStyle() == Style.DOCUMENT) {
Element e = null;
for (int i = 0; i < nodes.getLength(); i++) {
Node n = nodes.item(i);
if (n instanceof Element) {
if (e != null) {
throw new Fault("Body part '" + part.getName() + "' contains more than one element; expected a single element.");
} else {
e = (Element) n;
}
}
}
if (e == null) {
throw new Fault("Body part '" + part.getName() + "' contains no element; expected a single element.");
}
if (!wsdlMessage.getElementName().equals(DomUtil.getQName(e))) {
throw new Fault("Body part '" + part.getName() + "' element '" + DomUtil.getQName(e) + " doesn't match expected element '" + QNameUtil.toString(wsdlMessage.getElementName()) + "'");
}
body = document.importNode(e, true);
document.appendChild(body);
} else /* rpc-style */ {
for (int j = 0; j < nodes.getLength(); j++) {
/* note: we don't do any validation on RPC-style part value types */
Element e = document.createElementNS(wsdlMessage.getElementName().getNamespaceURI(), part.getName());
body.appendChild(e);
e.appendChild(document.importNode(nodes.item(j), true));
}
}
} else {
DocumentFragment frag = document.createDocumentFragment();
Element e = null;
for (int i = 0; i < nodes.getLength(); i++) {
Node n = nodes.item(i);
if (n instanceof Element) {
if (e != null) {
throw new Fault("Header part '" + part.getName() + "' contains more than one element; expected a single element.");
} else {
e = (Element) n;
}
}
}
if (e == null) {
throw new Fault("Header part '" + part.getName() + "' contains no element; expected a single element.");
}
QName headerName = part.getElement();
if (!headerName.equals(DomUtil.getQName(e))) {
throw new Fault("Header part '" + part.getName() + "' element '" + DomUtil.getQName(e) + " doesn't match expected element '" + QNameUtil.toString(headerName) + "'");
}
for (int j=0; j<nodes.getLength(); j++) {
frag.appendChild(document.importNode(nodes.item(j), true));
}
message.getSoapHeaders().put(headerName, frag);