1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.rundeck.api.parser;
17
18 import java.io.InputStream;
19 import org.dom4j.Document;
20 import org.dom4j.DocumentException;
21 import org.dom4j.Node;
22 import org.dom4j.io.SAXReader;
23 import org.rundeck.api.RundeckApiException;
24
25
26
27
28
29
30 public class ParserHelper {
31
32
33
34
35
36
37
38
39 public static Document loadDocument(InputStream inputStream) throws RundeckApiException {
40 SAXReader reader = new SAXReader();
41 reader.setEncoding("UTF-8");
42
43 Document document;
44 try {
45 document = reader.read(inputStream);
46 } catch (DocumentException e) {
47 throw new RundeckApiException("Failed to read Rundeck response", e);
48 }
49 document.setXMLEncoding("UTF-8");
50
51 Node result = document.selectSingleNode("result");
52 if (result != null) {
53 Boolean failure = Boolean.valueOf(result.valueOf("@error"));
54 if (failure) {
55 throw new RundeckApiException(result.valueOf("error/message"));
56 }
57 }
58
59 return document;
60 }
61
62 }