2 * This class wraps HTTP-Method functionality and thereby abstracts from low
3 * level code to simplify the usage.
5 package org.opentosca.bpel4restlight.rest;
7 import org.apache.commons.httpclient.HttpMethodBase;
8 import org.apache.commons.httpclient.NameValuePair;
9 import org.apache.commons.httpclient.methods.DeleteMethod;
10 import org.apache.commons.httpclient.methods.GetMethod;
11 import org.apache.commons.httpclient.methods.PostMethod;
12 import org.apache.commons.httpclient.methods.PutMethod;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.http.HttpHeaders;
18 public class HighLevelRestApi {
19 protected static final Log log = LogFactory.getLog(HighLevelRestApi.class);
21 * This method implements the HTTP Put Method
25 * @param requestPayload
26 * Content which has to be put into the Resource
27 * @return ResponseCode of HTTP Interaction
29 @SuppressWarnings("deprecation")
30 public static HttpResponseMessage Put(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {
32 PutMethod method = new PutMethod(uri);
33 // requestPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
36 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
37 method.setRequestBody(requestPayload);
39 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
41 // kill <?xml... in front of response
42 HighLevelRestApi.cleanResponseBody(responseMessage);
44 return responseMessage;
48 * This method implements the HTTP Post Method
52 * @param requestPayload
53 * Content which has to be posted into the Resource
54 * @return ResponseCode of HTTP Interaction
56 @SuppressWarnings("deprecation")
57 public static HttpResponseMessage Post(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {
59 PostMethod method = null;
60 if (uri.contains("?")) {
61 log.debug("Found query trying to split");
62 String[] split = uri.split("\\?");
63 log.debug("Raw URI part: " + split[0]);
64 log.debug("Raw Query part: " + split[1]);
65 method = new PostMethod(split[0]);
66 method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
68 method = new PostMethod(uri);
71 method.setRequestBody(requestPayload);
72 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
73 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
74 HighLevelRestApi.cleanResponseBody(responseMessage);
75 return responseMessage;
79 * This method implements the HTTP Get Method
83 * @return Content represented by the Resource URI
85 public static HttpResponseMessage Get(String uri, String acceptHeaderValue, String contentTypeHeader) {
86 log.debug("Setting URI to: \n");
88 GetMethod method = null;
89 if (uri.contains("?")) {
90 log.debug("Found query trying to split");
91 String[] split = uri.split("\\?");
92 log.debug("Raw URI part: " + split[0]);
93 log.debug("Raw Query part: " + split[1]);
95 method = new GetMethod(split[0]);
96 method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
98 method = new GetMethod(uri);
100 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
101 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
102 HighLevelRestApi.cleanResponseBody(responseMessage);
103 return responseMessage;
106 private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
108 // csarID=Moodle.csar&serviceTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}Moodle&nodeTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}VmApache
109 log.debug("Splitting query: " + query);
110 String[] pairs = query.trim().split("&");
111 NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
113 for (String pair : pairs) {
114 log.debug("Splitting query pair: " + pair);
115 String[] keyValue = pair.split("=");
116 NameValuePair nameValuePair = new NameValuePair();
117 log.debug("Key: " + keyValue[0] + " Value: " + keyValue[1]);
118 nameValuePair.setName(keyValue[0]);
119 nameValuePair.setValue(keyValue[1]);
120 nameValuePairArray[count] = nameValuePair;
123 return nameValuePairArray;
127 * This method implements the HTTP Delete Method
131 * @return ResponseCode of HTTP Interaction
133 public static HttpResponseMessage Delete(String uri, String acceptHeaderValue, String contentTypeHeader) {
135 DeleteMethod method = new DeleteMethod(uri);
136 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
137 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
138 HighLevelRestApi.cleanResponseBody(responseMessage);
139 return responseMessage;
142 private static void setHeader(HttpMethodBase method, String accept, String contentType) {
143 if (!"".equals(accept)) {
144 method.setRequestHeader(HttpHeaders.ACCEPT, accept);
146 method.setRequestHeader(HttpHeaders.ACCEPT, "application/xml");
149 if (contentType != null && !"".equals(contentType)) {
150 method.setRequestHeader(HttpHeaders.CONTENT_TYPE, contentType);
152 // method.setRequestHeader("Accept", accept);
157 private static void cleanResponseBody(HttpResponseMessage responseMessage) {
158 log.debug("ResponseBody: \n");
159 if (responseMessage != null && responseMessage.getResponseBody() != null) {
160 log.debug(responseMessage.getResponseBody());
161 String temp = responseMessage.getResponseBody()
162 .replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
163 responseMessage.setResponseBody(temp);