Add unit test for vfc-nfvo-wfengine
[vfc/nfvo/wfengine.git] / activiti-extension / src / main / java / org / onap / workflow / activitiext / restservicetask / HighLevelRestApi.java
index 31001b7..cd8517b 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright 2016-2017 ZTE Corporation.
+ * 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.
@@ -23,6 +23,8 @@ 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;
 
@@ -37,41 +39,43 @@ public class HighLevelRestApi {
        /**
         * invoke http service
         * 
-        * @param methodValue
+        * @param method
         * @param uri
-        * @param requestPayload
-        * @param acceptValue
-        * @param contentTypeValue
+        * @param requestBody
+        * @param accept
+        * @param contentType
         * @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);
-
+       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 (methodValue.toUpperCase()) {
+               switch (method.toUpperCase()) {
                case ConstString.HTTP_GET:
-                       msg = HighLevelRestApi.Get(uri, acceptValue, contentTypeValue);
+                       msg = HighLevelRestApi.Get(realUri, accept, contentType);
                        break;
                case ConstString.HTTP_POST:
-                       msg = HighLevelRestApi.Post(uri, requestPayload, acceptValue, contentTypeValue);
+                       msg = HighLevelRestApi.Post(realUri, requestBody, accept, contentType);
                        break;
                case ConstString.HTTP_DELETE:
-                       msg = HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue);
+                       msg = HighLevelRestApi.Delete(realUri, accept, contentType);
                        break;
                case ConstString.HTTP_PUT:
-                       msg = HighLevelRestApi.Put(uri, requestPayload, acceptValue, contentTypeValue);
+                       msg = HighLevelRestApi.Put(realUri, requestBody, accept, contentType);
                        break;
                default:
-                       throw new ActivitiException("can not find the http method type '" + methodValue + "'!");
+                       throw new ActivitiException("can not find the http method type '" + method + "'!");
                }
                return msg;
        }
@@ -81,19 +85,19 @@ public class HighLevelRestApi {
         * 
         * @param uri
         *            Resource URI
-        * @param requestPayload
+        * @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 requestPayload, String acceptValue,
-                       String contentTypeValue) {
+       public static HttpResponseMessage Put(String uri, String requestBody, String accept,
+                       String contentType) throws Exception {
 
                PutMethod method = new PutMethod(uri);
 
-               HighLevelRestApi.setAcceptHeader(method, acceptValue);
-               HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
-               method.setRequestBody(requestPayload);
+               HighLevelRestApi.setAcceptHeader(method, accept);
+               HighLevelRestApi.setContentTypeHeader(method, contentType);
+               method.setRequestBody(requestBody);
 
                HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
 
@@ -105,13 +109,13 @@ public class HighLevelRestApi {
         * 
         * @param uri
         *            Resource URI
-        * @param requestPayload
+        * @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 requestPayload, String acceptValue,
-                       String contentTypeValue) {
+       public static HttpResponseMessage Post(String uri, String requestBody, String accept,
+                       String contentType) throws Exception {
 
                PostMethod method = null;
                if (uri.contains("?")) {
@@ -121,9 +125,9 @@ public class HighLevelRestApi {
                } else {
                        method = new PostMethod(uri);
                }
-               method.setRequestBody(requestPayload);
-               HighLevelRestApi.setAcceptHeader(method, acceptValue);
-               HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
+               method.setRequestBody(requestBody);
+               HighLevelRestApi.setAcceptHeader(method, accept);
+               HighLevelRestApi.setContentTypeHeader(method, contentType);
                HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
                return responseMessage;
        }
@@ -135,7 +139,7 @@ public class HighLevelRestApi {
         *            Resource URI
         * @return Content represented by the Resource URI
         */
-       public static HttpResponseMessage Get(String uri, String acceptValue, String contentTypeValue) {
+       public static HttpResponseMessage Get(String uri, String accept, String contentType) throws Exception {
                GetMethod method = null;
                if (uri.contains("?")) {
                        String[] split = uri.split("\\?");
@@ -145,13 +149,13 @@ public class HighLevelRestApi {
                        method = new GetMethod(uri);
                }
 
-               HighLevelRestApi.setAcceptHeader(method, acceptValue);
-               HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
+               HighLevelRestApi.setAcceptHeader(method, accept);
+               HighLevelRestApi.setContentTypeHeader(method, contentType);
                HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
                return responseMessage;
        }
 
-       private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
+       static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
                String[] pairs = query.trim().split("&");
                NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
                int count = 0;
@@ -173,38 +177,32 @@ public class HighLevelRestApi {
         *            Resource URI
         * @return ResponseCode of HTTP Interaction
         */
-       public static HttpResponseMessage Delete(String uri, String acceptValue, String contentTypeValue) {
+       public static HttpResponseMessage Delete(String uri, String accept, String contentType)  throws Exception {
 
                DeleteMethod method = new DeleteMethod(uri);
-               HighLevelRestApi.setAcceptHeader(method, acceptValue);
-               HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
+               HighLevelRestApi.setAcceptHeader(method, accept);
+               HighLevelRestApi.setContentTypeHeader(method, contentType);
                HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
                return responseMessage;
        }
 
        private static void setAcceptHeader(HttpMethodBase method, String value) {
-               if (!StringUtils.isEmpty(value)) {
+               if (StringUtils.isNotEmpty(value)) {
+                       if(value.startsWith("[")&&value.endsWith("]")){
+                               value = value.substring(1, value.length()-1);
+                       }
+                       
                        method.setRequestHeader("Accept", value);
-               } else {
-                       method.setRequestHeader("Accept", "application/xml");
                }
        }
 
        private static void setContentTypeHeader(HttpMethodBase method, String value) {
-               if (!StringUtils.isEmpty(value)) {
+               if (StringUtils.isNotEmpty(value)) {
+                       if(value.startsWith("[")&&value.endsWith("]")){
+                               value = value.substring(1, value.length()-1);
+                       }
+                       
                        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;
-       }
 }