diff --git a/src/main/java/org/rundeck/api/RundeckClient.java b/src/main/java/org/rundeck/api/RundeckClient.java index 101bf6d..a610219 100644 --- a/src/main/java/org/rundeck/api/RundeckClient.java +++ b/src/main/java/org/rundeck/api/RundeckClient.java @@ -1587,26 +1587,19 @@ public class RundeckClient implements Serializable { .param(new ExecutionQueryParameters(query)) .param("max", max) .param("offset", offset), - patchApiV11Response( + APIV11Helper.unwrapIfNeeded( new PagedResultParser( - new ListParser(new ExecutionParser(), "execution"), + new ListParser( + new ExecutionParser(), + "execution" + ), rootXpath() + "/executions" - ) + ), + rootXpath() + "/executions" ) ); } - /** - * Fix potential buggy response from Rundeck, where <result> wrapper exists - * even if it should not. - * @param parser - * @return - */ - private XmlNodeParser> patchApiV11Response - (final PagedResultParser parser) { - return new PagedResultParser_BugPatchV11(parser); - } - /** * Get a single execution, identified by the given ID * diff --git a/src/main/java/org/rundeck/api/parser/APIV11Helper.java b/src/main/java/org/rundeck/api/parser/APIV11Helper.java new file mode 100644 index 0000000..a9711b2 --- /dev/null +++ b/src/main/java/org/rundeck/api/parser/APIV11Helper.java @@ -0,0 +1,78 @@ +package org.rundeck.api.parser; + +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.Node; + +/** + * Utility to handle API v11 responses with <result> wrapper element. + * + * @author Greg Schueler + * @since 2014-11-10 + */ +public class APIV11Helper { + + /** + * Detect and remove extra <result> wrapper around xml response. + * @param parser + * @param xpath + * @param + * @return + */ + public static XmlNodeParser unwrapIfNeeded( + final XmlNodeParser parser, + final String xpath + ) { + return new NodeParser_unwrap(parser, xpath); + } + + static class NodeParser_unwrap implements XmlNodeParser { + XmlNodeParser parser; + String xpath; + + public NodeParser_unwrap(final XmlNodeParser parser, final String xpath) { + this.parser = parser; + this.xpath = xpath; + } + + @Override public T parseXmlNode(final Node node) { + + Node sourceNode = unwrapResultElement(node, xpath); + return parser.parseXmlNode(sourceNode); + } + } + + /** + * Test the node for matching the xpath string, if it doesnt match, attempt to prefix it with + * "result" and match that. If that matches, return the first child of the 'result' element. + * + * @param node + * @param xpath + * + * @return + */ + public static Node unwrapResultElement(final Node node, final String xpath) { + Node sourceNode = node; + final Node tested = sourceNode.selectSingleNode(xpath); + if (null == tested && !xpath.startsWith("result")) { + //prepend /result + if (null != sourceNode.selectSingleNode("result" + xpath)) { + Node resultNode = sourceNode.selectSingleNode("result"); + if (resultNode instanceof Element) { + Element result = (Element) resultNode; + if (result.elements().size() == 1) { + + Node node1 = (Node) result.elements().get(0); + if (node1 instanceof Element) { + sourceNode = node1; + + sourceNode.getParent().remove(sourceNode); + DocumentHelper.createDocument((Element) sourceNode); + } + } + } + } + } + return sourceNode; + } +} diff --git a/src/main/java/org/rundeck/api/parser/PagedResultParser_BugPatchV11.java b/src/main/java/org/rundeck/api/parser/PagedResultParser_BugPatchV11.java deleted file mode 100644 index 083b3d1..0000000 --- a/src/main/java/org/rundeck/api/parser/PagedResultParser_BugPatchV11.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.rundeck.api.parser; - -import org.dom4j.DocumentHelper; -import org.dom4j.Element; -import org.dom4j.Node; -import org.rundeck.api.util.PagedResults; - -/** - * WRaps a {@link PagedResultParser} to detect whether the result XML incorrectly is wrapped with a - * <result> element. - * - * @author Greg Schueler - * @since 2014-11-07 - */ -public class PagedResultParser_BugPatchV11 implements XmlNodeParser> { - PagedResultParser parser; - - public PagedResultParser_BugPatchV11(final PagedResultParser parser) { - this.parser = parser; - } - - @Override public PagedResults parseXmlNode(final Node node) { - Node sourceNode = node; - final Node tested = sourceNode.selectSingleNode(parser.getXpath()); - if (null == tested && !parser.getXpath().startsWith("result")) { - //prepend /result - if (null != sourceNode.selectSingleNode("result" + parser.getXpath())) { - sourceNode = sourceNode.selectSingleNode("result" + parser.getXpath()); - sourceNode.getParent().remove(sourceNode); - DocumentHelper.createDocument((Element) sourceNode); - } - } - return parser.parseXmlNode(sourceNode); - } -}