Add unit test for vfc-nfvo-wfengine-activiti-ext
[vfc/nfvo/wfengine.git] / activiti-extension / src / main / java / org / onap / workflow / activitiext / restservicetask / HighLevelRestApi.java
1 /**
2  * Copyright 2016-2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.workflow.activitiext.restservicetask;
17
18 import org.activiti.engine.ActivitiException;
19 import org.apache.commons.httpclient.HttpMethodBase;
20 import org.apache.commons.httpclient.NameValuePair;
21 import org.apache.commons.httpclient.methods.DeleteMethod;
22 import org.apache.commons.httpclient.methods.GetMethod;
23 import org.apache.commons.httpclient.methods.PostMethod;
24 import org.apache.commons.httpclient.methods.PutMethod;
25 import org.apache.commons.lang3.StringUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * @author 10222158
31  *
32  */
33 public class HighLevelRestApi {
34
35         private static final Logger logger = LoggerFactory.getLogger(HighLevelRestApi.class);
36
37         /**
38          * invoke http service
39          * 
40          * @param methodValue
41          * @param uri
42          * @param requestPayload
43          * @param acceptValue
44          * @param contentTypeValue
45          * @return
46          */
47         public static HttpResponseMessage invoke(String methodValue, String uri, String requestPayload, String acceptValue,
48                         String contentTypeValue) throws Exception {
49
50                 // complete uri
51                 uri = completeUri(uri);
52
53                 logger.info("uri: " + uri);
54                 logger.info("method: " + methodValue);
55                 logger.info("requestbody: " + requestPayload);
56                 logger.info("accept: " + acceptValue);
57                 logger.info("content-type: " + contentTypeValue);
58
59                 HttpResponseMessage msg = new HttpResponseMessage();
60                 switch (methodValue.toUpperCase()) {
61                 case ConstString.HTTP_GET:
62                         msg = HighLevelRestApi.Get(uri, acceptValue, contentTypeValue);
63                         break;
64                 case ConstString.HTTP_POST:
65                         msg = HighLevelRestApi.Post(uri, requestPayload, acceptValue, contentTypeValue);
66                         break;
67                 case ConstString.HTTP_DELETE:
68                         msg = HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue);
69                         break;
70                 case ConstString.HTTP_PUT:
71                         msg = HighLevelRestApi.Put(uri, requestPayload, acceptValue, contentTypeValue);
72                         break;
73                 default:
74                         throw new ActivitiException("can not find the http method type '" + methodValue + "'!");
75                 }
76                 return msg;
77         }
78
79         /**
80          * This method implements the HTTP Put Method
81          * 
82          * @param uri
83          *            Resource URI
84          * @param requestPayload
85          *            Content which has to be put into the Resource
86          * @return ResponseCode of HTTP Interaction
87          */
88         @SuppressWarnings("deprecation")
89         public static HttpResponseMessage Put(String uri, String requestPayload, String acceptValue,
90                         String contentTypeValue) {
91
92                 PutMethod method = new PutMethod(uri);
93
94                 HighLevelRestApi.setAcceptHeader(method, acceptValue);
95                 HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
96                 method.setRequestBody(requestPayload);
97
98                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
99
100                 return responseMessage;
101         }
102
103         /**
104          * This method implements the HTTP Post Method
105          * 
106          * @param uri
107          *            Resource URI
108          * @param requestPayload
109          *            Content which has to be posted into the Resource
110          * @return ResponseCode of HTTP Interaction
111          */
112         @SuppressWarnings("deprecation")
113         public static HttpResponseMessage Post(String uri, String requestPayload, String acceptValue,
114                         String contentTypeValue) {
115
116                 PostMethod method = null;
117                 if (uri.contains("?")) {
118                         String[] split = uri.split("\\?");
119                         method = new PostMethod(split[0]);
120                         method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
121                 } else {
122                         method = new PostMethod(uri);
123                 }
124                 method.setRequestBody(requestPayload);
125                 HighLevelRestApi.setAcceptHeader(method, acceptValue);
126                 HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
127                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
128                 return responseMessage;
129         }
130
131         /**
132          * This method implements the HTTP Get Method
133          * 
134          * @param uri
135          *            Resource URI
136          * @return Content represented by the Resource URI
137          */
138         public static HttpResponseMessage Get(String uri, String acceptValue, String contentTypeValue) {
139                 GetMethod method = null;
140                 if (uri.contains("?")) {
141                         String[] split = uri.split("\\?");
142                         method = new GetMethod(split[0]);
143                         method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
144                 } else {
145                         method = new GetMethod(uri);
146                 }
147
148                 HighLevelRestApi.setAcceptHeader(method, acceptValue);
149                 HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
150                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
151                 return responseMessage;
152         }
153
154         private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
155                 String[] pairs = query.trim().split("&");
156                 NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
157                 int count = 0;
158                 for (String pair : pairs) {
159                         String[] keyValue = pair.split("=");
160                         NameValuePair nameValuePair = new NameValuePair();
161                         nameValuePair.setName(keyValue[0]);
162                         nameValuePair.setValue(keyValue[1]);
163                         nameValuePairArray[count] = nameValuePair;
164                         count++;
165                 }
166                 return nameValuePairArray;
167         }
168
169         /**
170          * This method implements the HTTP Delete Method
171          * 
172          * @param uri
173          *            Resource URI
174          * @return ResponseCode of HTTP Interaction
175          */
176         public static HttpResponseMessage Delete(String uri, String acceptValue, String contentTypeValue) {
177
178                 DeleteMethod method = new DeleteMethod(uri);
179                 HighLevelRestApi.setAcceptHeader(method, acceptValue);
180                 HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
181                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
182                 return responseMessage;
183         }
184
185         private static void setAcceptHeader(HttpMethodBase method, String value) {
186                 if (!StringUtils.isEmpty(value)) {
187                         method.setRequestHeader("Accept", value);
188                 } else {
189                         method.setRequestHeader("Accept", "application/xml");
190                 }
191         }
192
193         private static void setContentTypeHeader(HttpMethodBase method, String value) {
194                 if (!StringUtils.isEmpty(value)) {
195                         method.setRequestHeader("content-type", value);
196                 } else {
197                         method.setRequestHeader("content-type", "application/json");
198                 }
199         }
200
201         private static String completeUri(String uri) {
202
203                 String urlReg = "^http[\\s\\S]*";
204                 if (uri != null && !uri.matches(urlReg)) {
205                         uri = PropertyUtil.getBasePath() + uri;
206                 }
207
208                 return uri;
209         }
210 }