View Javadoc

1   /*
2    * Copyright 2011 Vincent Behar
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Helper for parsing RunDeck responses
29   * 
30   * @author Vincent Behar
31   */
32  public class ParserHelper {
33  
34      /**
35       * Load an XML {@link Document} from the given RunDeck {@link HttpResponse}.
36       * 
37       * @param httpResponse from an API call to RunDeck
38       * @return an XML {@link Document}
39       * @throws RundeckApiException if we failed to read the response, or if the response is an error
40       * @see #loadDocument(InputStream)
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       * Load an XML {@link Document} from the given {@link InputStream}
58       * 
59       * @param inputStream from an API call to RunDeck
60       * @return an XML {@link Document}
61       * @throws RundeckApiException if we failed to read the response, or if the response is an error
62       * @see #loadDocument(HttpResponse)
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  }