2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.workflow.activitiext.restservicetask;
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;
35 public class HighLevelRestApi {
37 private static final Logger logger = LoggerFactory.getLogger(HighLevelRestApi.class);
49 public static HttpResponseMessage invoke(RestInfo restInfo) throws Exception {
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();
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);
63 HttpResponseMessage msg = new HttpResponseMessage();
64 switch (method.toUpperCase()) {
65 case ConstString.HTTP_GET:
66 msg = HighLevelRestApi.Get(realUri, accept, contentType);
68 case ConstString.HTTP_POST:
69 msg = HighLevelRestApi.Post(realUri, requestBody, accept, contentType);
71 case ConstString.HTTP_DELETE:
72 msg = HighLevelRestApi.Delete(realUri, accept, contentType);
74 case ConstString.HTTP_PUT:
75 msg = HighLevelRestApi.Put(realUri, requestBody, accept, contentType);
78 throw new ActivitiException("can not find the http method type '" + method + "'!");
84 * This method implements the HTTP Put Method
89 * Content which has to be put into the Resource
90 * @return ResponseCode of HTTP Interaction
92 @SuppressWarnings("deprecation")
93 public static HttpResponseMessage Put(String uri, String requestBody, String accept,
94 String contentType) throws Exception {
96 PutMethod method = new PutMethod(uri);
98 HighLevelRestApi.setAcceptHeader(method, accept);
99 HighLevelRestApi.setContentTypeHeader(method, contentType);
100 method.setRequestBody(requestBody);
102 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
104 return responseMessage;
108 * This method implements the HTTP Post Method
113 * Content which has to be posted into the Resource
114 * @return ResponseCode of HTTP Interaction
116 @SuppressWarnings("deprecation")
117 public static HttpResponseMessage Post(String uri, String requestBody, String accept,
118 String contentType) throws Exception {
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]));
126 method = new PostMethod(uri);
128 method.setRequestBody(requestBody);
129 HighLevelRestApi.setAcceptHeader(method, accept);
130 HighLevelRestApi.setContentTypeHeader(method, contentType);
131 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
132 return responseMessage;
136 * This method implements the HTTP Get Method
140 * @return Content represented by the Resource URI
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]));
149 method = new GetMethod(uri);
152 HighLevelRestApi.setAcceptHeader(method, accept);
153 HighLevelRestApi.setContentTypeHeader(method, contentType);
154 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
155 return responseMessage;
158 static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
159 String[] pairs = query.trim().split("&");
160 NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
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;
170 return nameValuePairArray;
174 * This method implements the HTTP Delete Method
178 * @return ResponseCode of HTTP Interaction
180 public static HttpResponseMessage Delete(String uri, String accept, String contentType) throws Exception {
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;
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);
195 method.setRequestHeader("Accept", value);
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);
205 method.setRequestHeader("content-type", value);