2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.web
.mvc
.process
;
19 import java
.io
.BufferedReader
;
20 import java
.io
.ByteArrayInputStream
;
21 import java
.io
.InputStream
;
22 import java
.util
.UUID
;
24 import javax
.servlet
.http
.HttpServletRequest
;
25 import javax
.servlet
.http
.HttpServletResponse
;
27 import org
.apache
.commons
.io
.IOUtils
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.slc
.core
.attachment
.Attachment
;
31 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
32 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
33 import org
.argeo
.slc
.msg
.MsgConstants
;
34 import org
.argeo
.slc
.msg
.ObjectList
;
35 import org
.argeo
.slc
.process
.SlcExecution
;
36 import org
.argeo
.slc
.process
.SlcExecutionStep
;
37 import org
.argeo
.slc
.runtime
.SlcAgent
;
38 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
39 import org
.argeo
.slc
.services
.SlcExecutionService
;
40 import org
.argeo
.slc
.web
.mvc
.AbstractServiceController
;
41 import org
.springframework
.oxm
.Marshaller
;
42 import org
.springframework
.oxm
.Unmarshaller
;
43 import org
.springframework
.util
.Assert
;
44 import org
.springframework
.web
.servlet
.ModelAndView
;
45 import org
.springframework
.xml
.transform
.StringResult
;
46 import org
.springframework
.xml
.transform
.StringSource
;
48 /** Send a new SlcExecution. */
49 public class NewSlcExecutionController
extends AbstractServiceController
{
50 private final static Log log
= LogFactory
51 .getLog(NewSlcExecutionController
.class);
53 private SlcAgentFactory agentFactory
;
54 private Unmarshaller unmarshaller
;
55 private Marshaller marshaller
;
56 private SlcExecutionService slcExecutionService
;
58 private AttachmentsStorage attachmentsStorage
;
61 protected void handleServiceRequest(HttpServletRequest request
,
62 HttpServletResponse response
, ModelAndView modelAndView
)
65 if (log
.isTraceEnabled()) {
66 log
.debug("Content-Type: " + request
.getContentType());
67 log
.debug("Content-Length: " + request
.getContentLength());
70 String agentId
= request
71 .getParameter(MsgConstants
.PROPERTY_SLC_AGENT_ID
);
72 Assert
.notNull(agentId
, "agent id");
74 String answer
= request
.getParameter("body");
76 // lets read the message body instead
77 BufferedReader reader
= request
.getReader();
78 StringBuffer buffer
= new StringBuffer();
80 while (((line
= reader
.readLine()) != null)) {
83 answer
= buffer
.toString();
86 if (log
.isTraceEnabled())
87 log
.debug("Received message:\n" + answer
);
89 StringSource source
= new StringSource(answer
);
90 SlcExecution slcExecution
= (SlcExecution
) unmarshaller
93 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
94 if (slcExecution
.getUuid() == null
95 || slcExecution
.getUuid().length() < 8)
96 slcExecution
.setUuid(UUID
.randomUUID().toString());
98 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
99 slcExecution
.getSteps().add(
100 new SlcExecutionStep(SlcExecutionStep
.TYPE_START
,
101 "Process started from the Web UI"));
103 // ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());
104 // StringResult result = new StringResult();
105 // marshaller.marshal(ol, result);
106 // slcExecution.setRealizedFlowsXml(result.toString());
107 storeRealizedFlows(slcExecution
);
109 slcExecutionService
.newExecution(slcExecution
);
111 SlcAgent agent
= agentFactory
.getAgent(agentId
);
112 agent
.runSlcExecution(slcExecution
);
115 protected void storeRealizedFlows(SlcExecution slcExecution
) {
116 Attachment attachment
= realizedFlowsAttachment(UUID
.randomUUID()
117 .toString(), slcExecution
);
118 InputStream in
= null;
121 ObjectList ol
= new ObjectList(slcExecution
.getRealizedFlows());
122 StringResult result
= new StringResult();
123 marshaller
.marshal(ol
, result
);
125 in
= new ByteArrayInputStream(result
.toString().getBytes());
126 attachmentsStorage
.storeAttachment(attachment
, in
);
128 slcExecution
.setRealizedFlowsXml(attachment
.getUuid());
130 } catch (Exception e
) {
131 log
.error("Could not store realized flows as attachment #"
132 + attachment
.getUuid(), e
);
134 IOUtils
.closeQuietly(in
);
138 public void setUnmarshaller(Unmarshaller unmarshaller
) {
139 this.unmarshaller
= unmarshaller
;
142 public void setAgentFactory(SlcAgentFactory agentFactory
) {
143 this.agentFactory
= agentFactory
;
146 public void setSlcExecutionService(SlcExecutionService slcExecutionService
) {
147 this.slcExecutionService
= slcExecutionService
;
150 public void setMarshaller(Marshaller marshaller
) {
151 this.marshaller
= marshaller
;
154 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
155 this.attachmentsStorage
= attachmentsStorage
;
158 /** Unify labelling in the package */
159 static Attachment
realizedFlowsAttachment(String attachmentUuid
,
160 SlcExecution slcExecution
) {
161 return new SimpleAttachment(attachmentUuid
,
162 "RealizedFlows of SlcExecution #" + slcExecution
.getUuid(),