cd8517b4566cf5372de39c6faa13a02398b13a80
[vfc/nfvo/wfengine.git] / activiti-extension / src / main / java / org / onap / workflow / activitiext / restservicetask / HighLevelRestApi.java
1 /**
2  * Copyright 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.onap.workflow.activitiext.common.ConstString;
27 import org.onap.workflow.activitiext.common.RestInfo;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * @author 10222158
33  *
34  */
35 public class HighLevelRestApi {
36
37         private static final Logger logger = LoggerFactory.getLogger(HighLevelRestApi.class);
38
39         /**
40          * invoke http service
41          * 
42          * @param method
43          * @param uri
44          * @param requestBody
45          * @param accept
46          * @param contentType
47          * @return
48          */
49         public static HttpResponseMessage invoke(RestInfo restInfo) throws Exception {
50
51                 String realUri = restInfo.getRealUri();
52                 String method = restInfo.getMethod();
53                 String requestBody = restInfo.getRequestBody();
54                 String accept = restInfo.getAccept();
55                 String contentType = restInfo.getContentType();
56
57                 logger.info("uri: " + realUri);
58                 logger.info("method: " + method);
59                 logger.info("requestbody: " + requestBody);
60                 logger.info("accept: " + accept);
61                 logger.info("content-type: " + contentType);
62                 
63                 HttpResponseMessage msg = new HttpResponseMessage();
64                 switch (method.toUpperCase()) {
65                 case ConstString.HTTP_GET:
66                         msg = HighLevelRestApi.Get(realUri, accept, contentType);
67                         break;
68                 case ConstString.HTTP_POST:
69                         msg = HighLevelRestApi.Post(realUri, requestBody, accept, contentType);
70                         break;
71                 case ConstString.HTTP_DELETE:
72                         msg = HighLevelRestApi.Delete(realUri, accept, contentType);
73                         break;
74                 case ConstString.HTTP_PUT:
75                         msg = HighLevelRestApi.Put(realUri, requestBody, accept, contentType);
76                         break;
77                 default:
78                         throw new ActivitiException("can not find the http method type '" + method + "'!");
79                 }
80                 return msg;
81         }
82
83         /**
84          * This method implements the HTTP Put Method
85          * 
86          * @param uri
87          *            Resource URI
88          * @param requestBody
89          *            Content which has to be put into the Resource
90          * @return ResponseCode of HTTP Interaction
91          */
92         @SuppressWarnings("deprecation")
93         public static HttpResponseMessage Put(String uri, String requestBody, String accept,
94                         String contentType) throws Exception {
95
96                 PutMethod method = new PutMethod(uri);
97
98                 HighLevelRestApi.setAcceptHeader(method, accept);
99                 HighLevelRestApi.setContentTypeHeader(method, contentType);
100                 method.setRequestBody(requestBody);
101
102                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
103
104                 return responseMessage;
105         }
106
107         /**
108          * This method implements the HTTP Post Method
109          * 
110          * @param uri
111          *            Resource URI
112          * @param requestBody
113          *            Content which has to be posted into the Resource
114          * @return ResponseCode of HTTP Interaction
115          */
116         @SuppressWarnings("deprecation")
117         public static HttpResponseMessage Post(String uri, String requestBody, String accept,
118                         String contentType) throws Exception {
119
120                 PostMethod method = null;
121                 if (uri.contains("?")) {
122                         String[] split = uri.split("\\?");
123                         method = new PostMethod(split[0]);
124                         method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
125                 } else {
126                         method = new PostMethod(uri);
127                 }
128                 method.setRequestBody(requestBody);
129                 HighLevelRestApi.setAcceptHeader(method, accept);
130                 HighLevelRestApi.setContentTypeHeader(method, contentType);
131                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
132                 return responseMessage;
133         }
134
135         /**
136          * This method implements the HTTP Get Method
137          * 
138          * @param uri
139          *            Resource URI
140          * @return Content represented by the Resource URI
141          */
142         public static HttpResponseMessage Get(String uri, String accept, String contentType) throws Exception {
143                 GetMethod method = null;
144                 if (uri.contains("?")) {
145                         String[] split = uri.split("\\?");
146                         method = new GetMethod(split[0]);
147                         method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
148                 } else {
149                         method = new GetMethod(uri);
150                 }
151
152                 HighLevelRestApi.setAcceptHeader(method, accept);
153                 HighLevelRestApi.setContentTypeHeader(method, contentType);
154                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
155                 return responseMessage;
156         }
157
158         static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
159                 String[] pairs = query.trim().split("&");
160                 NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
161                 int count = 0;
162                 for (String pair : pairs) {
163                         String[] keyValue = pair.split("=");
164                         NameValuePair nameValuePair = new NameValuePair();
165                         nameValuePair.setName(keyValue[0]);
166                         nameValuePair.setValue(keyValue[1]);
167                         nameValuePairArray[count] = nameValuePair;
168                         count++;
169                 }
170                 return nameValuePairArray;
171         }
172
173         /**
174          * This method implements the HTTP Delete Method
175          * 
176          * @param uri
177          *            Resource URI
178          * @return ResponseCode of HTTP Interaction
179          */
180         public static HttpResponseMessage Delete(String uri, String accept, String contentType)  throws Exception {
181
182                 DeleteMethod method = new DeleteMethod(uri);
183                 HighLevelRestApi.setAcceptHeader(method, accept);
184                 HighLevelRestApi.setContentTypeHeader(method, contentType);
185                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
186                 return responseMessage;
187         }
188
189         private static void setAcceptHeader(HttpMethodBase method, String value) {
190                 if (StringUtils.isNotEmpty(value)) {
191                         if(value.startsWith("[")&&value.endsWith("]")){
192                                 value = value.substring(1, value.length()-1);
193                         }
194                         
195                         method.setRequestHeader("Accept", value);
196                 }
197         }
198
199         private static void setContentTypeHeader(HttpMethodBase method, String value) {
200                 if (StringUtils.isNotEmpty(value)) {
201                         if(value.startsWith("[")&&value.endsWith("]")){
202                                 value = value.substring(1, value.length()-1);
203                         }
204                         
205                         method.setRequestHeader("content-type", value);
206                 }
207         }
208 }