]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/process/NewSlcExecutionController.java
d4a67b19d91ed51f1cdcb061b8bf692b73a3dd22
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / process / NewSlcExecutionController.java
1 package org.argeo.slc.web.mvc.process;
2
3 import java.io.BufferedReader;
4 import java.util.UUID;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.slc.msg.MsgConstants;
12 import org.argeo.slc.msg.ObjectList;
13 import org.argeo.slc.process.SlcExecution;
14 import org.argeo.slc.process.SlcExecutionStep;
15 import org.argeo.slc.runtime.SlcAgent;
16 import org.argeo.slc.runtime.SlcAgentFactory;
17 import org.argeo.slc.services.SlcExecutionService;
18 import org.argeo.slc.web.mvc.AbstractServiceController;
19 import org.springframework.oxm.Marshaller;
20 import org.springframework.oxm.Unmarshaller;
21 import org.springframework.util.Assert;
22 import org.springframework.web.servlet.ModelAndView;
23 import org.springframework.xml.transform.StringResult;
24 import org.springframework.xml.transform.StringSource;
25
26 /** Send a new SlcExecution. */
27 public class NewSlcExecutionController extends AbstractServiceController {
28 private final static Log log = LogFactory
29 .getLog(NewSlcExecutionController.class);
30
31 private SlcAgentFactory agentFactory;
32 private Unmarshaller unmarshaller;
33 private Marshaller marshaller;
34 private SlcExecutionService slcExecutionService;
35
36 @Override
37 protected void handleServiceRequest(HttpServletRequest request,
38 HttpServletResponse response, ModelAndView modelAndView)
39 throws Exception {
40
41 if (log.isTraceEnabled()) {
42 log.debug("Content-Type: " + request.getContentType());
43 log.debug("Content-Length: " + request.getContentLength());
44 }
45
46 String agentId = request
47 .getParameter(MsgConstants.PROPERTY_SLC_AGENT_ID);
48 Assert.notNull(agentId, "agent id");
49
50 String answer = request.getParameter("body");
51 if (answer == null) {
52 // lets read the message body instead
53 BufferedReader reader = request.getReader();
54 StringBuffer buffer = new StringBuffer();
55 String line = null;
56 while (((line = reader.readLine()) != null)) {
57 buffer.append(line);
58 }
59 answer = buffer.toString();
60 }
61
62 if (log.isTraceEnabled())
63 log.debug("Received message:\n" + answer);
64
65 StringSource source = new StringSource(answer);
66 SlcExecution slcExecution = (SlcExecution) unmarshaller
67 .unmarshal(source);
68
69 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
70 if (slcExecution.getUuid() == null
71 || slcExecution.getUuid().length() < 8)
72 slcExecution.setUuid(UUID.randomUUID().toString());
73
74 slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
75 slcExecution.getSteps().add(
76 new SlcExecutionStep(SlcExecutionStep.TYPE_START,
77 "Process started from the Web UI"));
78
79 ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());
80 StringResult result = new StringResult();
81 marshaller.marshal(ol, result);
82 slcExecution.setRealizedFlowsXml(result.toString());
83
84 slcExecutionService.newExecution(slcExecution);
85
86 SlcAgent agent = agentFactory.getAgent(agentId);
87 agent.runSlcExecution(slcExecution);
88 }
89
90 public void setUnmarshaller(Unmarshaller unmarshaller) {
91 this.unmarshaller = unmarshaller;
92 }
93
94 public void setAgentFactory(SlcAgentFactory agentFactory) {
95 this.agentFactory = agentFactory;
96 }
97
98 public void setSlcExecutionService(SlcExecutionService slcExecutionService) {
99 this.slcExecutionService = slcExecutionService;
100 }
101
102 public void setMarshaller(Marshaller marshaller) {
103 this.marshaller = marshaller;
104 }
105
106 }