74edbbc7aa0b67f23e429e6f0b51e97fa377e7a6
[vfc/nfvo/wfengine.git] /
1 /**
2  * This class wraps HTTP-Method functionality and thereby abstracts from low
3  * level code to simplify the usage.
4  */
5 package org.opentosca.bpel4restlight.rest;
6
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;
16
17
18 public class HighLevelRestApi {
19   protected static final Log log = LogFactory.getLog(HighLevelRestApi.class);
20         /**
21          * This method implements the HTTP Put Method
22          * 
23          * @param uri
24          *            Resource URI
25          * @param requestPayload
26          *            Content which has to be put into the Resource
27          * @return ResponseCode of HTTP Interaction
28          */
29         @SuppressWarnings("deprecation")
30         public static HttpResponseMessage Put(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {
31
32                 PutMethod method = new PutMethod(uri);
33                 // requestPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
34                 // requestPayload;
35
36                 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
37                 method.setRequestBody(requestPayload);
38
39                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
40
41                 // kill <?xml... in front of response
42                 HighLevelRestApi.cleanResponseBody(responseMessage);
43
44                 return responseMessage;
45         }
46
47         /**
48          * This method implements the HTTP Post Method
49          * 
50          * @param uri
51          *            Resource URI
52          * @param requestPayload
53          *            Content which has to be posted into the Resource
54          * @return ResponseCode of HTTP Interaction
55          */
56         @SuppressWarnings("deprecation")
57         public static HttpResponseMessage Post(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {
58
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]));
67                 } else {
68                         method = new PostMethod(uri);
69                         ;
70                 }
71                 method.setRequestBody(requestPayload);
72                 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
73                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
74                 HighLevelRestApi.cleanResponseBody(responseMessage);
75                 return responseMessage;
76         }
77
78         /**
79          * This method implements the HTTP Get Method
80          * 
81          * @param uri
82          *            Resource URI
83          * @return Content represented by the Resource URI
84          */
85         public static HttpResponseMessage Get(String uri, String acceptHeaderValue, String contentTypeHeader) {
86                 log.debug("Setting URI to: \n");
87                 log.debug(uri);
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]);
94                         
95                         method = new GetMethod(split[0]);
96                         method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
97                 } else {
98                         method = new GetMethod(uri);
99                 }
100                 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
101                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
102                 HighLevelRestApi.cleanResponseBody(responseMessage);
103                 return responseMessage;
104         }
105
106         private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
107                 // example:
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];
112                 int count = 0;
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;
121                         count++;
122                 }
123                 return nameValuePairArray;
124         }
125
126         /**
127          * This method implements the HTTP Delete Method
128          * 
129          * @param uri
130          *            Resource URI
131          * @return ResponseCode of HTTP Interaction
132          */
133         public static HttpResponseMessage Delete(String uri, String acceptHeaderValue, String contentTypeHeader) {
134
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;
140         }
141
142         private static void setHeader(HttpMethodBase method, String accept, String contentType) {
143                 if (!"".equals(accept)) {
144                         method.setRequestHeader(HttpHeaders.ACCEPT, accept);
145                 } else {
146                         method.setRequestHeader(HttpHeaders.ACCEPT, "application/xml");
147                 }
148                 
149                 if (contentType != null && !"".equals(contentType)) {
150                   method.setRequestHeader(HttpHeaders.CONTENT_TYPE, contentType);
151         } else {
152 //          method.setRequestHeader("Accept", accept);
153         }
154                 
155         }
156
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);
164           }
165         }
166         
167 }