2 * Copyright 2016-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.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
33 public class HighLevelRestApi {
35 private static final Logger logger = LoggerFactory.getLogger(HighLevelRestApi.class);
42 * @param requestPayload
44 * @param contentTypeValue
47 public static HttpResponseMessage invoke(String methodValue, String uri, String requestPayload, String acceptValue,
48 String contentTypeValue) throws Exception {
51 uri = completeUri(uri);
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);
59 HttpResponseMessage msg = new HttpResponseMessage();
60 switch (methodValue.toUpperCase()) {
61 case ConstString.HTTP_GET:
62 msg = HighLevelRestApi.Get(uri, acceptValue, contentTypeValue);
64 case ConstString.HTTP_POST:
65 msg = HighLevelRestApi.Post(uri, requestPayload, acceptValue, contentTypeValue);
67 case ConstString.HTTP_DELETE:
68 msg = HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue);
70 case ConstString.HTTP_PUT:
71 msg = HighLevelRestApi.Put(uri, requestPayload, acceptValue, contentTypeValue);
74 throw new ActivitiException("can not find the http method type '" + methodValue + "'!");
80 * This method implements the HTTP Put Method
84 * @param requestPayload
85 * Content which has to be put into the Resource
86 * @return ResponseCode of HTTP Interaction
88 @SuppressWarnings("deprecation")
89 public static HttpResponseMessage Put(String uri, String requestPayload, String acceptValue,
90 String contentTypeValue) {
92 PutMethod method = new PutMethod(uri);
94 HighLevelRestApi.setAcceptHeader(method, acceptValue);
95 HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
96 method.setRequestBody(requestPayload);
98 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
100 return responseMessage;
104 * This method implements the HTTP Post Method
108 * @param requestPayload
109 * Content which has to be posted into the Resource
110 * @return ResponseCode of HTTP Interaction
112 @SuppressWarnings("deprecation")
113 public static HttpResponseMessage Post(String uri, String requestPayload, String acceptValue,
114 String contentTypeValue) {
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]));
122 method = new PostMethod(uri);
124 method.setRequestBody(requestPayload);
125 HighLevelRestApi.setAcceptHeader(method, acceptValue);
126 HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
127 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
128 return responseMessage;
132 * This method implements the HTTP Get Method
136 * @return Content represented by the Resource URI
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]));
145 method = new GetMethod(uri);
148 HighLevelRestApi.setAcceptHeader(method, acceptValue);
149 HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
150 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
151 return responseMessage;
154 private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
155 String[] pairs = query.trim().split("&");
156 NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
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;
166 return nameValuePairArray;
170 * This method implements the HTTP Delete Method
174 * @return ResponseCode of HTTP Interaction
176 public static HttpResponseMessage Delete(String uri, String acceptValue, String contentTypeValue) {
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;
185 private static void setAcceptHeader(HttpMethodBase method, String value) {
186 if (!StringUtils.isEmpty(value)) {
187 method.setRequestHeader("Accept", value);
189 method.setRequestHeader("Accept", "application/xml");
193 private static void setContentTypeHeader(HttpMethodBase method, String value) {
194 if (!StringUtils.isEmpty(value)) {
195 method.setRequestHeader("content-type", value);
197 method.setRequestHeader("content-type", "application/json");
201 private static String completeUri(String uri) {
203 String urlReg = "^http[\\s\\S]*";
204 if (uri != null && !uri.matches(urlReg)) {
205 uri = PropertyUtil.getBasePath() + uri;