From f94ae366b9b4920c27fedb995fe5d730cba0a8a4 Mon Sep 17 00:00:00 2001 From: Tobias Tschech Date: Wed, 9 Jul 2014 10:58:21 +0200 Subject: [PATCH] CustomerUserDataGet was not working The CustomerUserDataGet method in the CustomerUserObject returns a not valid SOAP Message. The SOAP:Body contains not only the DispatchResponse and for this request the extractContentAsDocument() fails --> Cannot extract Document from body This is the workaround to get this mehtod working --- .../otrsclient/OtrsSoapMessageParser.java | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tudan/otrsclient/OtrsSoapMessageParser.java b/src/main/java/de/tudan/otrsclient/OtrsSoapMessageParser.java index c962cd9..14167d6 100644 --- a/src/main/java/de/tudan/otrsclient/OtrsSoapMessageParser.java +++ b/src/main/java/de/tudan/otrsclient/OtrsSoapMessageParser.java @@ -6,10 +6,13 @@ import org.w3c.dom.NodeList; import javax.xml.XMLConstants; +import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; + import java.util.Arrays; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -29,8 +32,7 @@ private Object nodeToObject(Node node) { } public Object[] nodesToArray(SOAPMessage msg) throws SOAPException { - Document doc = msg.getSOAPPart().getEnvelope().getBody().extractContentAsDocument(); - Element el = doc.getDocumentElement(); + Element el = getDispatchResponse(msg.getSOAPPart().getEnvelope().getBody()); NodeList nl = el.getChildNodes(); Object[] results = new Object[nl.getLength()]; @@ -48,8 +50,7 @@ public List nodesToList(SOAPMessage msg) throws SOAPException { public Map nodesToMap(SOAPMessage msg) throws SOAPException { Map map = new HashMap<>(); - Document doc = msg.getSOAPPart().getEnvelope().getBody().extractContentAsDocument(); - Element el = doc.getDocumentElement(); + Element el = getDispatchResponse(msg.getSOAPPart().getEnvelope().getBody()); NodeList nl = el.getChildNodes(); for (int i = 0; i < (nl.getLength() / 2); i++) { @@ -63,4 +64,28 @@ public Map nodesToMap(SOAPMessage msg) throws SOAPException { return map; } + + private Element getDispatchResponse(SOAPBody body) throws SOAPException{ + Element dispatchResponse = null; + try { + Document doc = body.extractContentAsDocument(); + dispatchResponse = doc.getDocumentElement(); + } catch (SOAPException e){ + //Workaround for CustomerUserDataGet because it returns more child elements in SOAP:Body + @SuppressWarnings("unchecked") + Iterator it = body.getChildElements(); + while(it.hasNext() && dispatchResponse == null){ + Element tmp = it.next(); + if(tmp.getNodeName().equals("DispatchResponse")){ + dispatchResponse = tmp; + } + } + //If the DispatchResponse can still not been found throw the SOAPException + if(dispatchResponse == null){ + throw e; + } + } + + return dispatchResponse; + } }