2 * This static-class eases HTTP-method execution by self-managed fault-handling
3 * and automated Response-information processing
5 package org.opentosca.bpel4restlight.rest;
7 import java.io.IOException;
11 import org.apache.commons.httpclient.Header;
12 import org.apache.commons.httpclient.HttpClient;
13 import org.apache.commons.httpclient.HttpMethod;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.json.simple.JSONArray;
17 import org.json.simple.JSONObject;
18 import org.json.simple.parser.JSONParser;
19 import org.json.simple.parser.ParseException;
22 public class LowLevelRestApi {
24 protected static final Log log = LogFactory.getLog(LowLevelRestApi.class);
25 // Local HttpClient used for every communication (Singleton implementation)
26 // private static HttpClient httpClient = new HttpClient();
29 * Executes a passed HttpMethod (Method type is either PUT, POST, GET or
30 * DELETE) and returns a HttpResponseMessage
32 * @param method Method to execute
33 * @return HttpResponseMessage which contains all information about the
36 public static HttpResponseMessage executeHttpMethod(HttpMethod method) {
38 HttpResponseMessage responseMessage = null;
41 log.debug("Method invocation on URI: \n");
42 log.debug(method.getURI().toString());
45 HttpClient httpClient = new HttpClient();
46 httpClient.executeMethod(method);
47 responseMessage = LowLevelRestApi.extractResponseInformation(method);
49 } catch (Exception e) {
50 log.error("call rest error:", e);
52 // Release Connection anyway
53 method.releaseConnection();
56 // Extract response information and return
57 return responseMessage;
61 * Extracts the response information from an executed HttpMethod
63 * @param method Executed Method
64 * @return Packaged response information
66 private static HttpResponseMessage extractResponseInformation(HttpMethod method) {
67 // Create and return HttpResponseMethod
68 HttpResponseMessage responseMessage = new HttpResponseMessage();
69 responseMessage.setStatusCode(method.getStatusCode());
71 responseMessage.setResponseBody(getResponseBody(method));
72 } catch (Exception e) {
75 return responseMessage;
82 * get response body info, if response body is a json object, then translate json object to xml
83 * if the rest request failed, i.e. the response body is a 404 error page, then response the body with header <root>
86 * @throws ParseException
88 private static String getResponseBody(HttpMethod method) throws ParseException
92 result = method.getResponseBodyAsString();
95 } catch (IOException e) {
99 Header header = method.getRequestHeader("Accept");
100 if ("application/json".equals(header.getValue())) {
101 StringBuilder sb = new StringBuilder();
102 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
104 if(result != null && !"".equals(result)) {
106 if(result.startsWith("<html>")) {
107 sb.append("<![CDATA[");
111 Object json = new JSONParser().parse(result);
112 json2Xml(sb, "obj", json);
117 Object json = new JSONParser().parse(result);
118 json2Xml(sb, "obj", json);
119 } catch (Exception e) {
121 sb.append("<![CDATA[");
126 sb.append("</root>");
128 log.debug("responseBody:");
129 log.debug(sb.toString());
130 return sb.toString();
136 @SuppressWarnings("unchecked")
137 public static void json2Xml(StringBuilder sb, String key, Object jsonObject) {
138 if(jsonObject == null) {
139 sb.append("<error>empty</error>");
143 if(jsonObject instanceof JSONArray) {
144 JSONArray array = (JSONArray) jsonObject;
145 sb.append("<").append(key).append("s").append(">");
146 for(int i=0, len=array.size(); i<len; i++) {
147 json2Xml(sb, key, array.get(i));
149 sb.append("</").append(key).append("s").append(">");
152 } else if(jsonObject instanceof JSONObject) {
153 sb.append("<").append(key).append(">");
154 JSONObject json = (JSONObject) jsonObject;
155 for(Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>)json.entrySet()) {
156 json2Xml(sb, entry.getKey(), entry.getValue());
158 sb.append("</").append(key).append(">");
161 sb.append("<").append(key).append(">");
162 sb.append("<![CDATA[");
163 sb.append(jsonObject.toString());
165 sb.append("</").append(key).append(">");