/** * Copyright 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.onap.workflow.activitiext.common.ConstString; import org.onap.workflow.activitiext.common.RestInfo; 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 method * @param uri * @param requestBody * @param accept * @param contentType * @return */ public static HttpResponseMessage invoke(RestInfo restInfo) throws Exception { String realUri = restInfo.getRealUri(); String method = restInfo.getMethod(); String requestBody = restInfo.getRequestBody(); String accept = restInfo.getAccept(); String contentType = restInfo.getContentType(); logger.info("uri: " + realUri); logger.info("method: " + method); logger.info("requestbody: " + requestBody); logger.info("accept: " + accept); logger.info("content-type: " + contentType); HttpResponseMessage msg = new HttpResponseMessage(); switch (method.toUpperCase()) { case ConstString.HTTP_GET: msg = HighLevelRestApi.Get(realUri, accept, contentType); break; case ConstString.HTTP_POST: msg = HighLevelRestApi.Post(realUri, requestBody, accept, contentType); break; case ConstString.HTTP_DELETE: msg = HighLevelRestApi.Delete(realUri, accept, contentType); break; case ConstString.HTTP_PUT: msg = HighLevelRestApi.Put(realUri, requestBody, accept, contentType); break; default: throw new ActivitiException("can not find the http method type '" + method + "'!"); } return msg; } /** * This method implements the HTTP Put Method * * @param uri * Resource URI * @param requestBody * Content which has to be put into the Resource * @return ResponseCode of HTTP Interaction */ @SuppressWarnings("deprecation") public static HttpResponseMessage Put(String uri, String requestBody, String accept, String contentType) throws Exception { PutMethod method = new PutMethod(uri); HighLevelRestApi.setAcceptHeader(method, accept); HighLevelRestApi.setContentTypeHeader(method, contentType); method.setRequestBody(requestBody); HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); return responseMessage; } /** * This method implements the HTTP Post Method * * @param uri * Resource URI * @param requestBody * Content which has to be posted into the Resource * @return ResponseCode of HTTP Interaction */ @SuppressWarnings("deprecation") public static HttpResponseMessage Post(String uri, String requestBody, String accept, String contentType) throws Exception { 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(requestBody); HighLevelRestApi.setAcceptHeader(method, accept); HighLevelRestApi.setContentTypeHeader(method, contentType); 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 accept, String contentType) throws Exception { 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, accept); HighLevelRestApi.setContentTypeHeader(method, contentType); HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); return responseMessage; } 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 accept, String contentType) throws Exception { DeleteMethod method = new DeleteMethod(uri); HighLevelRestApi.setAcceptHeader(method, accept); HighLevelRestApi.setContentTypeHeader(method, contentType); HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); return responseMessage; } public static void setAcceptHeader(HttpMethodBase method, String value) { if (StringUtils.isNotEmpty(value)) { if(value.startsWith("[")&&value.endsWith("]")){ value = value.substring(1, value.length()-1); } method.setRequestHeader("Accept", value); } } public static void setContentTypeHeader(HttpMethodBase method, String value) { if (StringUtils.isNotEmpty(value)) { if(value.startsWith("[")&&value.endsWith("]")){ value = value.substring(1, value.length()-1); } method.setRequestHeader("content-type", value); } } }