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