diff options
Diffstat (limited to 'activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java')
-rw-r--r-- | activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java new file mode 100644 index 0000000..31001b7 --- /dev/null +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java @@ -0,0 +1,210 @@ +/** + * Copyright 2016-2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.workflow.activitiext.restservicetask; + +import org.activiti.engine.ActivitiException; +import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.methods.DeleteMethod; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author 10222158 + * + */ +public class HighLevelRestApi { + + private static final Logger logger = LoggerFactory.getLogger(HighLevelRestApi.class); + + /** + * invoke http service + * + * @param methodValue + * @param uri + * @param requestPayload + * @param acceptValue + * @param contentTypeValue + * @return + */ + public static HttpResponseMessage invoke(String methodValue, String uri, String requestPayload, String acceptValue, + String contentTypeValue) throws ActivitiException { + + // complete uri + uri = completeUri(uri); + + logger.info("uri: " + uri); + logger.info("method: " + methodValue); + logger.info("requestbody: " + requestPayload); + logger.info("accept: " + acceptValue); + logger.info("content-type: " + contentTypeValue); + + HttpResponseMessage msg = new HttpResponseMessage(); + switch (methodValue.toUpperCase()) { + case ConstString.HTTP_GET: + msg = HighLevelRestApi.Get(uri, acceptValue, contentTypeValue); + break; + case ConstString.HTTP_POST: + msg = HighLevelRestApi.Post(uri, requestPayload, acceptValue, contentTypeValue); + break; + case ConstString.HTTP_DELETE: + msg = HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue); + break; + case ConstString.HTTP_PUT: + msg = HighLevelRestApi.Put(uri, requestPayload, acceptValue, contentTypeValue); + break; + default: + throw new ActivitiException("can not find the http method type '" + methodValue + "'!"); + } + return msg; + } + + /** + * This method implements the HTTP Put Method + * + * @param uri + * Resource URI + * @param requestPayload + * Content which has to be put into the Resource + * @return ResponseCode of HTTP Interaction + */ + @SuppressWarnings("deprecation") + public static HttpResponseMessage Put(String uri, String requestPayload, String acceptValue, + String contentTypeValue) { + + PutMethod method = new PutMethod(uri); + + HighLevelRestApi.setAcceptHeader(method, acceptValue); + HighLevelRestApi.setContentTypeHeader(method, contentTypeValue); + method.setRequestBody(requestPayload); + + HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); + + return responseMessage; + } + + /** + * This method implements the HTTP Post Method + * + * @param uri + * Resource URI + * @param requestPayload + * Content which has to be posted into the Resource + * @return ResponseCode of HTTP Interaction + */ + @SuppressWarnings("deprecation") + public static HttpResponseMessage Post(String uri, String requestPayload, String acceptValue, + String contentTypeValue) { + + PostMethod method = null; + if (uri.contains("?")) { + String[] split = uri.split("\\?"); + method = new PostMethod(split[0]); + method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1])); + } else { + method = new PostMethod(uri); + } + method.setRequestBody(requestPayload); + HighLevelRestApi.setAcceptHeader(method, acceptValue); + HighLevelRestApi.setContentTypeHeader(method, contentTypeValue); + HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); + return responseMessage; + } + + /** + * This method implements the HTTP Get Method + * + * @param uri + * Resource URI + * @return Content represented by the Resource URI + */ + public static HttpResponseMessage Get(String uri, String acceptValue, String contentTypeValue) { + GetMethod method = null; + if (uri.contains("?")) { + String[] split = uri.split("\\?"); + method = new GetMethod(split[0]); + method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1])); + } else { + method = new GetMethod(uri); + } + + HighLevelRestApi.setAcceptHeader(method, acceptValue); + HighLevelRestApi.setContentTypeHeader(method, contentTypeValue); + HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); + return responseMessage; + } + + private static NameValuePair[] createNameValuePairArrayFromQuery(String query) { + String[] pairs = query.trim().split("&"); + NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length]; + int count = 0; + for (String pair : pairs) { + String[] keyValue = pair.split("="); + NameValuePair nameValuePair = new NameValuePair(); + nameValuePair.setName(keyValue[0]); + nameValuePair.setValue(keyValue[1]); + nameValuePairArray[count] = nameValuePair; + count++; + } + return nameValuePairArray; + } + + /** + * This method implements the HTTP Delete Method + * + * @param uri + * Resource URI + * @return ResponseCode of HTTP Interaction + */ + public static HttpResponseMessage Delete(String uri, String acceptValue, String contentTypeValue) { + + DeleteMethod method = new DeleteMethod(uri); + HighLevelRestApi.setAcceptHeader(method, acceptValue); + HighLevelRestApi.setContentTypeHeader(method, contentTypeValue); + HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); + return responseMessage; + } + + private static void setAcceptHeader(HttpMethodBase method, String value) { + if (!StringUtils.isEmpty(value)) { + method.setRequestHeader("Accept", value); + } else { + method.setRequestHeader("Accept", "application/xml"); + } + } + + private static void setContentTypeHeader(HttpMethodBase method, String value) { + if (!StringUtils.isEmpty(value)) { + method.setRequestHeader("content-type", value); + } else { + method.setRequestHeader("content-type", "application/json"); + } + } + + private static String completeUri(String uri) { + + String urlReg = "^http[\\s\\S]*"; + if (uri != null && !uri.matches(urlReg)) { + uri = PropertyUtil.getBasePath() + uri; + } + + return uri; + } +} |