1 package org
.argeo
.slc
.web
.mvc
.process
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.ByteArrayInputStream
;
5 import java
.io
.InputStream
;
8 import javax
.servlet
.http
.HttpServletRequest
;
9 import javax
.servlet
.http
.HttpServletResponse
;
11 import org
.apache
.commons
.io
.IOUtils
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.slc
.core
.attachment
.Attachment
;
15 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
16 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
17 import org
.argeo
.slc
.msg
.MsgConstants
;
18 import org
.argeo
.slc
.msg
.ObjectList
;
19 import org
.argeo
.slc
.process
.SlcExecution
;
20 import org
.argeo
.slc
.process
.SlcExecutionStep
;
21 import org
.argeo
.slc
.runtime
.SlcAgent
;
22 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
23 import org
.argeo
.slc
.services
.SlcExecutionService
;
24 import org
.argeo
.slc
.web
.mvc
.AbstractServiceController
;
25 import org
.springframework
.oxm
.Marshaller
;
26 import org
.springframework
.oxm
.Unmarshaller
;
27 import org
.springframework
.util
.Assert
;
28 import org
.springframework
.web
.servlet
.ModelAndView
;
29 import org
.springframework
.xml
.transform
.StringResult
;
30 import org
.springframework
.xml
.transform
.StringSource
;
32 /** Send a new SlcExecution. */
33 public class NewSlcExecutionController
extends AbstractServiceController
{
34 private final static Log log
= LogFactory
35 .getLog(NewSlcExecutionController
.class);
37 private SlcAgentFactory agentFactory
;
38 private Unmarshaller unmarshaller
;
39 private Marshaller marshaller
;
40 private SlcExecutionService slcExecutionService
;
42 private AttachmentsStorage attachmentsStorage
;
45 protected void handleServiceRequest(HttpServletRequest request
,
46 HttpServletResponse response
, ModelAndView modelAndView
)
49 if (log
.isTraceEnabled()) {
50 log
.debug("Content-Type: " + request
.getContentType());
51 log
.debug("Content-Length: " + request
.getContentLength());
54 String agentId
= request
55 .getParameter(MsgConstants
.PROPERTY_SLC_AGENT_ID
);
56 Assert
.notNull(agentId
, "agent id");
58 String answer
= request
.getParameter("body");
60 // lets read the message body instead
61 BufferedReader reader
= request
.getReader();
62 StringBuffer buffer
= new StringBuffer();
64 while (((line
= reader
.readLine()) != null)) {
67 answer
= buffer
.toString();
70 if (log
.isTraceEnabled())
71 log
.debug("Received message:\n" + answer
);
73 StringSource source
= new StringSource(answer
);
74 SlcExecution slcExecution
= (SlcExecution
) unmarshaller
77 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
78 if (slcExecution
.getUuid() == null
79 || slcExecution
.getUuid().length() < 8)
80 slcExecution
.setUuid(UUID
.randomUUID().toString());
82 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
83 slcExecution
.getSteps().add(
84 new SlcExecutionStep(SlcExecutionStep
.TYPE_START
,
85 "Process started from the Web UI"));
87 // ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());
88 // StringResult result = new StringResult();
89 // marshaller.marshal(ol, result);
90 // slcExecution.setRealizedFlowsXml(result.toString());
91 storeRealizedFlows(slcExecution
);
93 slcExecutionService
.newExecution(slcExecution
);
95 SlcAgent agent
= agentFactory
.getAgent(agentId
);
96 agent
.runSlcExecution(slcExecution
);
99 protected void storeRealizedFlows(SlcExecution slcExecution
) {
100 Attachment attachment
= realizedFlowsAttachment(UUID
.randomUUID()
101 .toString(), slcExecution
);
102 InputStream in
= null;
105 ObjectList ol
= new ObjectList(slcExecution
.getRealizedFlows());
106 StringResult result
= new StringResult();
107 marshaller
.marshal(ol
, result
);
109 in
= new ByteArrayInputStream(result
.toString().getBytes());
110 attachmentsStorage
.storeAttachment(attachment
, in
);
112 slcExecution
.setRealizedFlowsXml(attachment
.getUuid());
114 } catch (Exception e
) {
115 log
.error("Could not store realized flows as attachment #"
116 + attachment
.getUuid(), e
);
118 IOUtils
.closeQuietly(in
);
122 public void setUnmarshaller(Unmarshaller unmarshaller
) {
123 this.unmarshaller
= unmarshaller
;
126 public void setAgentFactory(SlcAgentFactory agentFactory
) {
127 this.agentFactory
= agentFactory
;
130 public void setSlcExecutionService(SlcExecutionService slcExecutionService
) {
131 this.slcExecutionService
= slcExecutionService
;
134 public void setMarshaller(Marshaller marshaller
) {
135 this.marshaller
= marshaller
;
138 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
139 this.attachmentsStorage
= attachmentsStorage
;
142 /** Unify labelling in the package */
143 static Attachment
realizedFlowsAttachment(String attachmentUuid
,
144 SlcExecution slcExecution
) {
145 return new SimpleAttachment(attachmentUuid
,
146 "RealizedFlows of SlcExecution #" + slcExecution
.getUuid(),