diff options
Diffstat (limited to 'wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest')
4 files changed, 389 insertions, 0 deletions
diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HighLevelRestApi.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HighLevelRestApi.java new file mode 100644 index 0000000..74edbbc --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HighLevelRestApi.java @@ -0,0 +1,167 @@ +/** + * This class wraps HTTP-Method functionality and thereby abstracts from low + * level code to simplify the usage. + */ +package org.opentosca.bpel4restlight.rest; + +import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.methods.DeleteMethod; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.HttpHeaders; + + +public class HighLevelRestApi { + protected static final Log log = LogFactory.getLog(HighLevelRestApi.class); + /** + * This method implements the HTTP Put Method + * + * @param uri + * Resource URI + * @param requestPayload + * Content which has to be put into the Resource + * @return ResponseCode of HTTP Interaction + */ + @SuppressWarnings("deprecation") + public static HttpResponseMessage Put(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) { + + PutMethod method = new PutMethod(uri); + // requestPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + // requestPayload; + + HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader); + method.setRequestBody(requestPayload); + + HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); + + // kill <?xml... in front of response + HighLevelRestApi.cleanResponseBody(responseMessage); + + return responseMessage; + } + + /** + * This method implements the HTTP Post Method + * + * @param uri + * Resource URI + * @param requestPayload + * Content which has to be posted into the Resource + * @return ResponseCode of HTTP Interaction + */ + @SuppressWarnings("deprecation") + public static HttpResponseMessage Post(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) { + + PostMethod method = null; + if (uri.contains("?")) { + log.debug("Found query trying to split"); + String[] split = uri.split("\\?"); + log.debug("Raw URI part: " + split[0]); + log.debug("Raw Query part: " + split[1]); + method = new PostMethod(split[0]); + method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1])); + } else { + method = new PostMethod(uri); + ; + } + method.setRequestBody(requestPayload); + HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader); + HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); + HighLevelRestApi.cleanResponseBody(responseMessage); + return responseMessage; + } + + /** + * This method implements the HTTP Get Method + * + * @param uri + * Resource URI + * @return Content represented by the Resource URI + */ + public static HttpResponseMessage Get(String uri, String acceptHeaderValue, String contentTypeHeader) { + log.debug("Setting URI to: \n"); + log.debug(uri); + GetMethod method = null; + if (uri.contains("?")) { + log.debug("Found query trying to split"); + String[] split = uri.split("\\?"); + log.debug("Raw URI part: " + split[0]); + log.debug("Raw Query part: " + split[1]); + + method = new GetMethod(split[0]); + method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1])); + } else { + method = new GetMethod(uri); + } + HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader); + HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); + HighLevelRestApi.cleanResponseBody(responseMessage); + return responseMessage; + } + + private static NameValuePair[] createNameValuePairArrayFromQuery(String query) { + // example: + // csarID=Moodle.csar&serviceTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}Moodle&nodeTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}VmApache + log.debug("Splitting query: " + query); + String[] pairs = query.trim().split("&"); + NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length]; + int count = 0; + for (String pair : pairs) { + log.debug("Splitting query pair: " + pair); + String[] keyValue = pair.split("="); + NameValuePair nameValuePair = new NameValuePair(); + log.debug("Key: " + keyValue[0] + " Value: " + keyValue[1]); + nameValuePair.setName(keyValue[0]); + nameValuePair.setValue(keyValue[1]); + nameValuePairArray[count] = nameValuePair; + count++; + } + return nameValuePairArray; + } + + /** + * This method implements the HTTP Delete Method + * + * @param uri + * Resource URI + * @return ResponseCode of HTTP Interaction + */ + public static HttpResponseMessage Delete(String uri, String acceptHeaderValue, String contentTypeHeader) { + + DeleteMethod method = new DeleteMethod(uri); + HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader); + HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); + HighLevelRestApi.cleanResponseBody(responseMessage); + return responseMessage; + } + + private static void setHeader(HttpMethodBase method, String accept, String contentType) { + if (!"".equals(accept)) { + method.setRequestHeader(HttpHeaders.ACCEPT, accept); + } else { + method.setRequestHeader(HttpHeaders.ACCEPT, "application/xml"); + } + + if (contentType != null && !"".equals(contentType)) { + method.setRequestHeader(HttpHeaders.CONTENT_TYPE, contentType); + } else { +// method.setRequestHeader("Accept", accept); + } + + } + + private static void cleanResponseBody(HttpResponseMessage responseMessage) { + log.debug("ResponseBody: \n"); + if (responseMessage != null && responseMessage.getResponseBody() != null) { + log.debug(responseMessage.getResponseBody()); + String temp = responseMessage.getResponseBody() + .replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", ""); + responseMessage.setResponseBody(temp); + } + } + +} diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HttpMethod.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HttpMethod.java new file mode 100644 index 0000000..ace91c9 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HttpMethod.java @@ -0,0 +1,9 @@ +/** + * This enum is intended to simplify identifying different HTTP-methods + */ +package org.opentosca.bpel4restlight.rest; + + +public enum HttpMethod { + PUT, POST, GET, DELETE +}
\ No newline at end of file diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HttpResponseMessage.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HttpResponseMessage.java new file mode 100644 index 0000000..0836918 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HttpResponseMessage.java @@ -0,0 +1,42 @@ +/** + * This enum is intended to simplify identifying different HTTP-methods + */ +package org.opentosca.bpel4restlight.rest; + +/** + */ +public class HttpResponseMessage { + + private int statusCode; + private String responseBody; + + + /** + * @return the statusCode + */ + public int getStatusCode() { + return this.statusCode; + } + + /** + * @param statusCode the statusCode to set + */ + protected void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } + + /** + * @return the responseBody + */ + public String getResponseBody() { + return this.responseBody; + } + + /** + * @param responseBody the responseBody to set + */ + protected void setResponseBody(String responseBody) { + this.responseBody = responseBody; + } + +} diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/LowLevelRestApi.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/LowLevelRestApi.java new file mode 100644 index 0000000..2c6bc94 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/LowLevelRestApi.java @@ -0,0 +1,171 @@ +/** + * This static-class eases HTTP-method execution by self-managed fault-handling + * and automated Response-information processing + */ +package org.opentosca.bpel4restlight.rest; + +import java.io.IOException; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + + +public class LowLevelRestApi { + + protected static final Log log = LogFactory.getLog(LowLevelRestApi.class); + // Local HttpClient used for every communication (Singleton implementation) +// private static HttpClient httpClient = new HttpClient(); + + /** + * Executes a passed HttpMethod (Method type is either PUT, POST, GET or + * DELETE) and returns a HttpResponseMessage + * + * @param method Method to execute + * @return HttpResponseMessage which contains all information about the + * execution + */ + public static HttpResponseMessage executeHttpMethod(HttpMethod method) { + + HttpResponseMessage responseMessage = null; + + try { + log.debug("Method invocation on URI: \n"); + log.debug(method.getURI().toString()); + + // Execute Request + HttpClient httpClient = new HttpClient(); + httpClient.executeMethod(method); + responseMessage = LowLevelRestApi.extractResponseInformation(method); + + } catch (Exception e) { + log.error("call rest error:", e); + } finally { + // Release Connection anyway + method.releaseConnection(); + } + + // Extract response information and return + return responseMessage; + } + + /** + * Extracts the response information from an executed HttpMethod + * + * @param method Executed Method + * @return Packaged response information + */ + private static HttpResponseMessage extractResponseInformation(HttpMethod method) { + // Create and return HttpResponseMethod + HttpResponseMessage responseMessage = new HttpResponseMessage(); + responseMessage.setStatusCode(method.getStatusCode()); + try { + responseMessage.setResponseBody(getResponseBody(method)); + } catch (Exception e) { + log.error(e); + } + return responseMessage; + + } + + /** + * getResponseBody + * + * get response body info, if response body is a json object, then translate json object to xml + * if the rest request failed, i.e. the response body is a 404 error page, then response the body with header <root> + * @param method + * @return + * @throws ParseException + */ + private static String getResponseBody(HttpMethod method) throws ParseException + { + String result = null; + try { + result = method.getResponseBodyAsString(); + log.debug("result:"); + log.debug(result); + } catch (IOException e) { + log.error(e); + } + + Header header = method.getRequestHeader("Accept"); + if ("application/json".equals(header.getValue())) { + StringBuilder sb = new StringBuilder(); + sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"); + sb.append("<root>"); + if(result != null && !"".equals(result)) { + /** + if(result.startsWith("<html>")) { + sb.append("<![CDATA["); + sb.append(result); + sb.append("]]>"); + } else { + Object json = new JSONParser().parse(result); + json2Xml(sb, "obj", json); + } + */ + + try { + Object json = new JSONParser().parse(result); + json2Xml(sb, "obj", json); + } catch (Exception e) { + log.error(e); + sb.append("<![CDATA["); + sb.append(result); + sb.append("]]>"); + } + } + sb.append("</root>"); + + log.debug("responseBody:"); + log.debug(sb.toString()); + return sb.toString(); + } + return result; + } + + + @SuppressWarnings("unchecked") + public static void json2Xml(StringBuilder sb, String key, Object jsonObject) { + if(jsonObject == null) { + sb.append("<error>empty</error>"); + return; + } + + if(jsonObject instanceof JSONArray) { + JSONArray array = (JSONArray) jsonObject; + sb.append("<").append(key).append("s").append(">"); + for(int i=0, len=array.size(); i<len; i++) { + json2Xml(sb, key, array.get(i)); + } + sb.append("</").append(key).append("s").append(">"); + + return; + } else if(jsonObject instanceof JSONObject) { + sb.append("<").append(key).append(">"); + JSONObject json = (JSONObject) jsonObject; + for(Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>)json.entrySet()) { + json2Xml(sb, entry.getKey(), entry.getValue()); + } + sb.append("</").append(key).append(">"); + return; + } else { + sb.append("<").append(key).append(">"); + sb.append("<![CDATA["); + sb.append(jsonObject.toString()); + sb.append("]]>"); + sb.append("</").append(key).append(">"); + + return; + } + } + +} |