1 /*******************************************************************************
2 * Copyright (c) 2012-2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
10 * Modifications Copyright 2016-2017 ZTE Corporation.
13 * This static-class eases HTTP-method execution by self-managed fault-handling
14 * and automated Response-information processing
16 package org.opentosca.bpel4restlight.rest;
18 import java.io.IOException;
22 import org.apache.commons.httpclient.Header;
23 import org.apache.commons.httpclient.HttpClient;
24 import org.apache.commons.httpclient.HttpMethod;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.json.simple.JSONArray;
28 import org.json.simple.JSONObject;
29 import org.json.simple.parser.JSONParser;
30 import org.json.simple.parser.ParseException;
33 public class LowLevelRestApi {
35 protected static final Log log = LogFactory.getLog(LowLevelRestApi.class);
36 // Local HttpClient used for every communication (Singleton implementation)
37 // private static HttpClient httpClient = new HttpClient();
40 * Executes a passed HttpMethod (Method type is either PUT, POST, GET or
41 * DELETE) and returns a HttpResponseMessage
43 * @param method Method to execute
44 * @return HttpResponseMessage which contains all information about the
47 public static HttpResponseMessage executeHttpMethod(HttpMethod method) {
49 HttpResponseMessage responseMessage = null;
52 log.debug("Method invocation on URI: \n");
53 log.debug(method.getURI().toString());
56 HttpClient httpClient = new HttpClient();
57 httpClient.executeMethod(method);
58 responseMessage = LowLevelRestApi.extractResponseInformation(method);
60 } catch (Exception e) {
61 log.error("call rest error:", e);
63 // Release Connection anyway
64 method.releaseConnection();
67 // Extract response information and return
68 return responseMessage;
72 * Extracts the response information from an executed HttpMethod
74 * @param method Executed Method
75 * @return Packaged response information
77 private static HttpResponseMessage extractResponseInformation(HttpMethod method) {
78 // Create and return HttpResponseMethod
79 HttpResponseMessage responseMessage = new HttpResponseMessage();
80 responseMessage.setStatusCode(method.getStatusCode());
82 responseMessage.setResponseBody(getResponseBody(method));
83 } catch (Exception e) {
86 return responseMessage;
93 * get response body info, if response body is a json object, then translate json object to xml
94 * if the rest request failed, i.e. the response body is a 404 error page, then response the body with header <root>
97 * @throws ParseException
99 private static String getResponseBody(HttpMethod method) throws ParseException
101 String result = null;
103 result = method.getResponseBodyAsString();
104 log.debug("result:");
106 } catch (IOException e) {
110 Header header = method.getRequestHeader("Accept");
111 if ("application/json".equals(header.getValue())) {
112 StringBuilder sb = new StringBuilder();
113 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
115 if(result != null && !"".equals(result)) {
117 if(result.startsWith("<html>")) {
118 sb.append("<![CDATA[");
122 Object json = new JSONParser().parse(result);
123 json2Xml(sb, "obj", json);
128 Object json = new JSONParser().parse(result);
129 json2Xml(sb, "obj", json);
130 } catch (Exception e) {
132 sb.append("<![CDATA[");
137 sb.append("</root>");
139 log.debug("responseBody:");
140 log.debug(sb.toString());
141 return sb.toString();
147 @SuppressWarnings("unchecked")
148 public static void json2Xml(StringBuilder sb, String key, Object jsonObject) {
149 if(jsonObject == null) {
150 sb.append("<error>empty</error>");
154 if(jsonObject instanceof JSONArray) {
155 JSONArray array = (JSONArray) jsonObject;
156 sb.append("<").append(key).append("s").append(">");
157 for(int i=0, len=array.size(); i<len; i++) {
158 json2Xml(sb, key, array.get(i));
160 sb.append("</").append(key).append("s").append(">");
163 } else if(jsonObject instanceof JSONObject) {
164 sb.append("<").append(key).append(">");
165 JSONObject json = (JSONObject) jsonObject;
166 for(Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>)json.entrySet()) {
167 json2Xml(sb, entry.getKey(), entry.getValue());
169 sb.append("</").append(key).append(">");
172 sb.append("<").append(key).append(">");
173 sb.append("<![CDATA[");
174 sb.append(jsonObject.toString());
176 sb.append("</").append(key).append(">");