Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / bpmn / common / workflow / service / WorkflowAsyncResource.java
index 9787ec0..b4543b1 100644 (file)
@@ -21,6 +21,8 @@ package org.openecomp.mso.bpmn.common.workflow.service;
 \r
 import java.util.HashMap;\r
 import java.util.Map;\r
+import java.util.Objects;\r
+import java.util.Optional;\r
 import java.util.UUID;\r
 \r
 import javax.ws.rs.Consumes;\r
@@ -31,7 +33,6 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.Response;\r
 \r
 import org.camunda.bpm.engine.ProcessEngineServices;\r
-import org.camunda.bpm.engine.ProcessEngines;\r
 import org.camunda.bpm.engine.RuntimeService;\r
 import org.camunda.bpm.engine.runtime.ProcessInstance;\r
 import org.camunda.bpm.engine.variable.impl.VariableMapImpl;\r
@@ -52,15 +53,15 @@ import org.slf4j.MDC;
  * For asynchronous process - the activity may send a acknowledgement response and then proceed further on executing the process\r
  */\r
 @Path("/async")\r
-public class WorkflowAsyncResource {\r
+public class WorkflowAsyncResource extends ProcessEngineAwareService {\r
 \r
-       private WorkflowContextHolder contextHolder = WorkflowContextHolder.getInstance();\r
-       protected ProcessEngineServices pes4junit = null;\r
+       private static final WorkflowContextHolder contextHolder = WorkflowContextHolder.getInstance();\r
+       protected Optional<ProcessEngineServices> pes4junit = Optional.empty();\r
 \r
-       private MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);\r
+       private final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);\r
 \r
        private static final String logMarker = "[WRKFLOW-RESOURCE]";\r
-       private static final int DEFAULT_WAIT_TIME = 30000;     //default wait time\r
+       private static final long DEFAULT_WAIT_TIME = 30000;    //default wait time\r
        \r
        /**\r
         * Asynchronous JAX-RS method that starts a process instance.\r
@@ -75,7 +76,6 @@ public class WorkflowAsyncResource {
        public void startProcessInstanceByKey(final @Suspend(180000) AsynchronousResponse asyncResponse,\r
                        @PathParam("processKey") String processKey, VariableMapImpl variableMap) {\r
        \r
-               WorkflowResponse response = new WorkflowResponse();\r
                long startTime = System.currentTimeMillis();\r
                Map<String, Object> inputVariables = null;\r
                WorkflowContext workflowContext = null;\r
@@ -107,14 +107,15 @@ public class WorkflowAsyncResource {
                        }\r
 \r
                        msoLogger.debug(logMarker + "Exception in startProcessInstance by key");\r
+               WorkflowResponse response = new WorkflowResponse();\r
                        response.setMessage("Fail" );\r
-                       response.setResponse("Error occurred while executing the process: " + e);\r
+                       response.setContent("Error occurred while executing the process: " + e);\r
                        response.setMessageCode(500);\r
                        recordEvents(processKey, response, startTime);\r
                        \r
                        msoLogger.error (MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, logMarker \r
                                        + response.getMessage() + " for processKey: " \r
-                                       + processKey + " with response: " + response.getResponse());\r
+                                       + processKey + " with content: " + response.getContent());\r
                        \r
                        Response errorResponse = Response.serverError().entity(response).build();\r
                        asyncResponse.setResponse(errorResponse);\r
@@ -205,29 +206,28 @@ public class WorkflowAsyncResource {
                return contextHolder.processCallback(processKey, processInstanceId, requestId, callbackResponse);\r
        }\r
        \r
+    private static String getOrCreate(Map<String, Object> inputVariables, String key) {\r
+        String value = Objects.toString(inputVariables.get(key), null);\r
+        if (value == null) {\r
+            value = UUID.randomUUID().toString();\r
+            inputVariables.put(key, value);\r
+        }\r
+        return value;\r
+    }\r
+       \r
        // Note: the business key is used to identify the process in unit tests\r
-       private String getBusinessKey(Map<String, Object> inputVariables) {\r
-               Object businessKey = inputVariables.get("mso-business-key");\r
-               if (businessKey == null ) {\r
-                       businessKey = UUID.randomUUID().toString();\r
-                       inputVariables.put("mso-business-key",  businessKey);\r
-               }\r
-               return businessKey.toString();\r
+       private static String getBusinessKey(Map<String, Object> inputVariables) {\r
+        return getOrCreate(inputVariables, "mso-business-key");\r
        }\r
 \r
-       private String getRequestId(Map<String, Object> inputVariables) {\r
-               Object requestId = inputVariables.get("mso-request-id");\r
-               if (requestId == null ) {\r
-                       requestId = UUID.randomUUID().toString();\r
-                       inputVariables.put("mso-request-id",  requestId);\r
-               } \r
-               return requestId.toString();\r
+       private static String getRequestId(Map<String, Object> inputVariables) {\r
+        return getOrCreate(inputVariables, "mso-request-id");\r
        }\r
 \r
        private long getWaitTime(Map<String, Object> inputVariables)\r
        {\r
-               String timeout = inputVariables.get("mso-service-request-timeout") == null\r
-                               ? null : inputVariables.get("mso-service-request-timeout").toString();          \r
+           \r
+               String timeout = Objects.toString(inputVariables.get("mso-service-request-timeout"), null);\r
 \r
                if (timeout != null) {\r
                        try {\r
@@ -245,14 +245,14 @@ public class WorkflowAsyncResource {
                \r
                msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, \r
                                logMarker + response.getMessage() + " for processKey: "\r
-                               + processKey + " with response: " + response.getResponse(), "BPMN", MDC.get(processKey), null);\r
+                               + processKey + " with content: " + response.getContent(), "BPMN", MDC.get(processKey), null);\r
                \r
                msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, \r
-                               logMarker + response.getMessage() + "for processKey: " + processKey + " with response: " + response.getResponse());\r
+                               logMarker + response.getMessage() + "for processKey: " + processKey + " with content: " + response.getContent());\r
                \r
        }\r
 \r
-       private void setLogContext(String processKey,\r
+       private static void setLogContext(String processKey,\r
                        Map<String, Object> inputVariables) {\r
                MsoLogger.setServiceName("MSO." + processKey);\r
                if (inputVariables != null) {\r
@@ -260,38 +260,28 @@ public class WorkflowAsyncResource {
                }\r
        }\r
 \r
-       private String getKeyValueFromInputVariables(Map<String,Object> inputVariables, String key) {\r
-               if (inputVariables == null) return "";\r
-               Object requestId = inputVariables.get(key);\r
-               if (requestId != null) return requestId.toString();\r
-               return "N/A";\r
+       private static String getKeyValueFromInputVariables(Map<String,Object> inputVariables, String key) {\r
+               if (inputVariables == null) {\r
+                       return "";\r
+               }\r
+\r
+               return Objects.toString(inputVariables.get(key), "N/A");\r
        }\r
 \r
        private boolean isProcessEnded(String processInstanceId) {\r
                ProcessEngineServices pes = getProcessEngineServices();\r
-               return pes.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null ? true : false ;                \r
-       }\r
-       \r
-       \r
-       protected ProcessEngineServices getProcessEngineServices() {\r
-               if (pes4junit == null) {\r
-                       return ProcessEngines.getDefaultProcessEngine();\r
-               } else {\r
-                       return pes4junit;\r
-               }\r
+               return pes.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null;\r
        }\r
        \r
-       public void setProcessEngineServices4junit(ProcessEngineServices pes) {\r
-               pes4junit = pes;\r
-       }\r
-\r
-       private Map<String, Object> getInputVariables(VariableMapImpl variableMap) {\r
-               Map<String, Object> inputVariables = new HashMap<String,Object>();\r
+       private static Map<String, Object> getInputVariables(VariableMapImpl variableMap) {\r
+               Map<String, Object> inputVariables = new HashMap<>();\r
                @SuppressWarnings("unchecked")\r
                Map<String, Object> vMap = (Map<String, Object>) variableMap.get("variables");\r
-               for (String vName : vMap.keySet()) {\r
+               for (Map.Entry<String, Object> entry : vMap.entrySet()) {\r
+                       String vName = entry.getKey();\r
+                       Object value = entry.getValue();\r
                        @SuppressWarnings("unchecked")\r
-                       Map<String, Object> valueMap = (Map<String,Object>)vMap.get(vName); // value, type\r
+                       Map<String, Object> valueMap = (Map<String,Object>)value; // value, type\r
                        inputVariables.put(vName, valueMap.get("value"));\r
                }\r
                return inputVariables;\r