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.IOException;
19 import java.io.InputStream;
20 import org.apache.http.HttpResponse;
21 import org.dom4j.Document;
22 import org.dom4j.DocumentException;
23 import org.dom4j.Node;
24 import org.dom4j.io.SAXReader;
25 import org.rundeck.api.RundeckApiException;
26
27
28
29
30
31
32 public class ParserHelper {
33
34
35
36
37
38
39
40
41
42 public static Document loadDocument(HttpResponse httpResponse) throws RundeckApiException {
43 InputStream inputStream = null;
44
45 try {
46 inputStream = httpResponse.getEntity().getContent();
47 } catch (IllegalStateException e) {
48 throw new RundeckApiException("Failed to read RunDeck reponse", e);
49 } catch (IOException e) {
50 throw new RundeckApiException("Failed to read RunDeck reponse", e);
51 }
52
53 return loadDocument(inputStream);
54 }
55
56
57
58
59
60
61
62
63
64 public static Document loadDocument(InputStream inputStream) throws RundeckApiException {
65 SAXReader reader = new SAXReader();
66 reader.setEncoding("UTF-8");
67
68 Document document;
69 try {
70 document = reader.read(inputStream);
71 } catch (DocumentException e) {
72 throw new RundeckApiException("Failed to read RunDeck reponse", e);
73 }
74 document.setXMLEncoding("UTF-8");
75
76 Node result = document.selectSingleNode("result");
77 if (result != null) {
78 Boolean failure = Boolean.valueOf(result.valueOf("@error"));
79 if (failure) {
80 throw new RundeckApiException(result.valueOf("error/message"));
81 }
82 }
83
84 return document;
85 }
86
87 }