]> git.argeo.org Git - gpl/argeo-slc.git/blob - NewSlcExecutionController.java
4c6249a0a90d8a19da2d3cbae5a4b3ac360c79a8
[gpl/argeo-slc.git] / NewSlcExecutionController.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.web.mvc.process;
18
19 import java.io.BufferedReader;
20 import java.io.ByteArrayInputStream;
21 import java.io.InputStream;
22 import java.util.UUID;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
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;
47
48 /** Send a new SlcExecution. */
49 public class NewSlcExecutionController extends AbstractServiceController {
50 private final static Log log = LogFactory
51 .getLog(NewSlcExecutionController.class);
52
53 private SlcAgentFactory agentFactory;
54 private Unmarshaller unmarshaller;
55 private Marshaller marshaller;
56 private SlcExecutionService slcExecutionService;
57
58 private AttachmentsStorage attachmentsStorage;
59
60 @Override
61 protected void handleServiceRequest(HttpServletRequest request,
62 HttpServletResponse response, ModelAndView modelAndView)
63 throws Exception {
64
65 if (log.isTraceEnabled()) {
66 log.debug("Content-Type: " + request.getContentType());
67 log.debug("Content-Length: " + request.getContentLength());
68 }
69
70 String agentId = request
71 .getParameter(MsgConstants.PROPERTY_SLC_AGENT_ID);
72 Assert.notNull(agentId, "agent id");
73
74 String answer = request.getParameter("body");
75 if (answer == null) {
76 // lets read the message body instead
77 BufferedReader reader = request.getReader();
78 StringBuffer buffer = new StringBuffer();
79 String line = null;
80 while (((line = reader.readLine()) != null)) {
81 buffer.append(line);
82 }
83 answer = buffer.toString();
84 }
85
86 if (log.isTraceEnabled())
87 log.debug("Received message:\n" + answer);
88
89 StringSource source = new StringSource(answer);
90 SlcExecution slcExecution = (SlcExecution) unmarshaller
91 .unmarshal(source);
92
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());
97
98 slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
99 slcExecution.getSteps().add(
100 new SlcExecutionStep(SlcExecutionStep.TYPE_START,
101 "Process started from the Web UI"));
102
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);
108
109 slcExecutionService.newExecution(slcExecution);
110
111 SlcAgent agent = agentFactory.getAgent(agentId);
112 agent.runSlcExecution(slcExecution);
113 }
114
115 protected void storeRealizedFlows(SlcExecution slcExecution) {
116 Attachment attachment = realizedFlowsAttachment(UUID.randomUUID()
117 .toString(), slcExecution);
118 InputStream in = null;
119 try {
120
121 ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());
122 StringResult result = new StringResult();
123 marshaller.marshal(ol, result);
124
125 in = new ByteArrayInputStream(result.toString().getBytes());
126 attachmentsStorage.storeAttachment(attachment, in);
127
128 slcExecution.setRealizedFlowsXml(attachment.getUuid());
129
130 } catch (Exception e) {
131 log.error("Could not store realized flows as attachment #"
132 + attachment.getUuid(), e);
133 } finally {
134 IOUtils.closeQuietly(in);
135 }
136 }
137
138 public void setUnmarshaller(Unmarshaller unmarshaller) {
139 this.unmarshaller = unmarshaller;
140 }
141
142 public void setAgentFactory(SlcAgentFactory agentFactory) {
143 this.agentFactory = agentFactory;
144 }
145
146 public void setSlcExecutionService(SlcExecutionService slcExecutionService) {
147 this.slcExecutionService = slcExecutionService;
148 }
149
150 public void setMarshaller(Marshaller marshaller) {
151 this.marshaller = marshaller;
152 }
153
154 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
155 this.attachmentsStorage = attachmentsStorage;
156 }
157
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(),
163 "text/xml");
164 }
165 }