modify license header
[vfc/nfvo/wfengine.git] / wso2bpel-ext / wso2bpel-core / BPEL4RESTLight / src / main / java / org / opentosca / bpel4restlight / rest / HighLevelRestApi.java
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  *******************************************************************************/
9 /*
10  * Modifications Copyright 2016-2017 ZTE Corporation.
11  */
12 /**
13  * This class wraps HTTP-Method functionality and thereby abstracts from low
14  * level code to simplify the usage.
15  */
16 package org.opentosca.bpel4restlight.rest;
17
18 import org.apache.commons.httpclient.HttpMethodBase;
19 import org.apache.commons.httpclient.NameValuePair;
20 import org.apache.commons.httpclient.methods.DeleteMethod;
21 import org.apache.commons.httpclient.methods.GetMethod;
22 import org.apache.commons.httpclient.methods.PostMethod;
23 import org.apache.commons.httpclient.methods.PutMethod;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.http.HttpHeaders;
27
28
29 public class HighLevelRestApi {
30   protected static final Log log = LogFactory.getLog(HighLevelRestApi.class);
31         /**
32          * This method implements the HTTP Put Method
33          * 
34          * @param uri
35          *            Resource URI
36          * @param requestPayload
37          *            Content which has to be put into the Resource
38          * @return ResponseCode of HTTP Interaction
39          */
40         @SuppressWarnings("deprecation")
41         public static HttpResponseMessage Put(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {
42
43                 PutMethod method = new PutMethod(uri);
44                 // requestPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
45                 // requestPayload;
46
47                 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
48                 method.setRequestBody(requestPayload);
49
50                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
51
52                 // kill <?xml... in front of response
53                 HighLevelRestApi.cleanResponseBody(responseMessage);
54
55                 return responseMessage;
56         }
57
58         /**
59          * This method implements the HTTP Post Method
60          * 
61          * @param uri
62          *            Resource URI
63          * @param requestPayload
64          *            Content which has to be posted into the Resource
65          * @return ResponseCode of HTTP Interaction
66          */
67         @SuppressWarnings("deprecation")
68         public static HttpResponseMessage Post(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {
69
70                 PostMethod method = null;
71                 if (uri.contains("?")) {
72                         log.debug("Found query trying to split");
73                         String[] split = uri.split("\\?");
74                         log.debug("Raw URI part: " + split[0]);
75                         log.debug("Raw Query part: " + split[1]);
76                         method = new PostMethod(split[0]);
77                         method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
78                 } else {
79                         method = new PostMethod(uri);
80                         ;
81                 }
82                 method.setRequestBody(requestPayload);
83                 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
84                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
85                 HighLevelRestApi.cleanResponseBody(responseMessage);
86                 return responseMessage;
87         }
88
89         /**
90          * This method implements the HTTP Get Method
91          * 
92          * @param uri
93          *            Resource URI
94          * @return Content represented by the Resource URI
95          */
96         public static HttpResponseMessage Get(String uri, String acceptHeaderValue, String contentTypeHeader) {
97                 log.debug("Setting URI to: \n");
98                 log.debug(uri);
99                 GetMethod method = null;
100                 if (uri.contains("?")) {
101                   log.debug("Found query trying to split");
102                         String[] split = uri.split("\\?");
103                         log.debug("Raw URI part: " + split[0]);
104                         log.debug("Raw Query part: " + split[1]);
105                         
106                         method = new GetMethod(split[0]);
107                         method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
108                 } else {
109                         method = new GetMethod(uri);
110                 }
111                 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
112                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
113                 HighLevelRestApi.cleanResponseBody(responseMessage);
114                 return responseMessage;
115         }
116
117         private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
118                 // example:
119                 // csarID=Moodle.csar&serviceTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}Moodle&nodeTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}VmApache
120                 log.debug("Splitting query: " + query);
121                 String[] pairs = query.trim().split("&");
122                 NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
123                 int count = 0;
124                 for (String pair : pairs) {
125                   log.debug("Splitting query pair: " + pair);
126                         String[] keyValue = pair.split("=");
127                         NameValuePair nameValuePair = new NameValuePair();
128                         log.debug("Key: " + keyValue[0] + " Value: " + keyValue[1]);
129                         nameValuePair.setName(keyValue[0]);
130                         nameValuePair.setValue(keyValue[1]);
131                         nameValuePairArray[count] = nameValuePair;
132                         count++;
133                 }
134                 return nameValuePairArray;
135         }
136
137         /**
138          * This method implements the HTTP Delete Method
139          * 
140          * @param uri
141          *            Resource URI
142          * @return ResponseCode of HTTP Interaction
143          */
144         public static HttpResponseMessage Delete(String uri, String acceptHeaderValue, String contentTypeHeader) {
145
146                 DeleteMethod method = new DeleteMethod(uri);
147                 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
148                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
149                 HighLevelRestApi.cleanResponseBody(responseMessage);
150                 return responseMessage;
151         }
152
153         private static void setHeader(HttpMethodBase method, String accept, String contentType) {
154                 if (!"".equals(accept)) {
155                         method.setRequestHeader(HttpHeaders.ACCEPT, accept);
156                 } else {
157                         method.setRequestHeader(HttpHeaders.ACCEPT, "application/xml");
158                 }
159                 
160                 if (contentType != null && !"".equals(contentType)) {
161                   method.setRequestHeader(HttpHeaders.CONTENT_TYPE, contentType);
162         } else {
163 //          method.setRequestHeader("Accept", accept);
164         }
165                 
166         }
167
168         private static void cleanResponseBody(HttpResponseMessage responseMessage) {
169           log.debug("ResponseBody: \n");
170           if (responseMessage != null && responseMessage.getResponseBody() != null) {  
171             log.debug(responseMessage.getResponseBody());
172             String temp = responseMessage.getResponseBody()
173                 .replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
174             responseMessage.setResponseBody(temp);
175           }
176         }
177         
178 }