]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/process/NewSlcExecutionController.java
Revert to Equinox v3.4.2
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / process / NewSlcExecutionController.java
index c551005bc2e93d7b5643a54692c770c3df97c9e9..d4a67b19d91ed51f1cdcb061b8bf692b73a3dd22 100644 (file)
@@ -1,34 +1,88 @@
 package org.argeo.slc.web.mvc.process;\r
 \r
+import java.io.BufferedReader;\r
+import java.util.UUID;\r
+\r
 import javax.servlet.http.HttpServletRequest;\r
 import javax.servlet.http.HttpServletResponse;\r
-import javax.xml.transform.stream.StreamSource;\r
 \r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
 import org.argeo.slc.msg.MsgConstants;\r
+import org.argeo.slc.msg.ObjectList;\r
 import org.argeo.slc.process.SlcExecution;\r
+import org.argeo.slc.process.SlcExecutionStep;\r
 import org.argeo.slc.runtime.SlcAgent;\r
 import org.argeo.slc.runtime.SlcAgentFactory;\r
+import org.argeo.slc.services.SlcExecutionService;\r
 import org.argeo.slc.web.mvc.AbstractServiceController;\r
+import org.springframework.oxm.Marshaller;\r
 import org.springframework.oxm.Unmarshaller;\r
+import org.springframework.util.Assert;\r
 import org.springframework.web.servlet.ModelAndView;\r
+import org.springframework.xml.transform.StringResult;\r
+import org.springframework.xml.transform.StringSource;\r
 \r
 /** Send a new SlcExecution. */\r
 public class NewSlcExecutionController extends AbstractServiceController {\r
+       private final static Log log = LogFactory\r
+                       .getLog(NewSlcExecutionController.class);\r
+\r
        private SlcAgentFactory agentFactory;\r
        private Unmarshaller unmarshaller;\r
+       private Marshaller marshaller;\r
+       private SlcExecutionService slcExecutionService;\r
 \r
        @Override\r
        protected void handleServiceRequest(HttpServletRequest request,\r
                        HttpServletResponse response, ModelAndView modelAndView)\r
                        throws Exception {\r
 \r
+               if (log.isTraceEnabled()) {\r
+                       log.debug("Content-Type: " + request.getContentType());\r
+                       log.debug("Content-Length: " + request.getContentLength());\r
+               }\r
+\r
                String agentId = request\r
                                .getParameter(MsgConstants.PROPERTY_SLC_AGENT_ID);\r
+               Assert.notNull(agentId, "agent id");\r
+\r
+               String answer = request.getParameter("body");\r
+               if (answer == null) {\r
+                       // lets read the message body instead\r
+                       BufferedReader reader = request.getReader();\r
+                       StringBuffer buffer = new StringBuffer();\r
+                       String line = null;\r
+                       while (((line = reader.readLine()) != null)) {\r
+                               buffer.append(line);\r
+                       }\r
+                       answer = buffer.toString();\r
+               }\r
 \r
-               StreamSource source = new StreamSource(request.getInputStream());\r
+               if (log.isTraceEnabled())\r
+                       log.debug("Received message:\n" + answer);\r
+\r
+               StringSource source = new StringSource(answer);\r
                SlcExecution slcExecution = (SlcExecution) unmarshaller\r
                                .unmarshal(source);\r
 \r
+               // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86\r
+               if (slcExecution.getUuid() == null\r
+                               || slcExecution.getUuid().length() < 8)\r
+                       slcExecution.setUuid(UUID.randomUUID().toString());\r
+\r
+               slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);\r
+               slcExecution.getSteps().add(\r
+                               new SlcExecutionStep(SlcExecutionStep.TYPE_START,\r
+                                               "Process started from the Web UI"));\r
+\r
+               ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());\r
+               StringResult result = new StringResult();\r
+               marshaller.marshal(ol, result);\r
+               slcExecution.setRealizedFlowsXml(result.toString());\r
+\r
+               slcExecutionService.newExecution(slcExecution);\r
+\r
                SlcAgent agent = agentFactory.getAgent(agentId);\r
                agent.runSlcExecution(slcExecution);\r
        }\r
@@ -41,4 +95,12 @@ public class NewSlcExecutionController extends AbstractServiceController {
                this.agentFactory = agentFactory;\r
        }\r
 \r
+       public void setSlcExecutionService(SlcExecutionService slcExecutionService) {\r
+               this.slcExecutionService = slcExecutionService;\r
+       }\r
+\r
+       public void setMarshaller(Marshaller marshaller) {\r
+               this.marshaller = marshaller;\r
+       }\r
+\r
 }\r