]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/controllers/ProcessController.java
JCR UI can run processes
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / controllers / ProcessController.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.controllers;
18
19 import java.io.BufferedReader;
20 import java.text.SimpleDateFormat;
21 import java.util.Comparator;
22 import java.util.List;
23 import java.util.SortedSet;
24 import java.util.TreeSet;
25 import java.util.UUID;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.argeo.slc.SlcException;
33 import org.argeo.slc.core.attachment.AttachmentsStorage;
34 import org.argeo.slc.dao.process.SlcExecutionDao;
35 import org.argeo.slc.execution.ExecutionModuleDescriptor;
36 import org.argeo.slc.msg.ExecutionAnswer;
37 import org.argeo.slc.msg.MsgConstants;
38 import org.argeo.slc.msg.ObjectList;
39 import org.argeo.slc.process.SlcExecution;
40 import org.argeo.slc.process.SlcExecutionStep;
41 import org.argeo.slc.runtime.SlcAgent;
42 import org.argeo.slc.runtime.SlcAgentFactory;
43 import org.argeo.slc.services.SlcExecutionService;
44 import org.springframework.oxm.Marshaller;
45 import org.springframework.oxm.Unmarshaller;
46 import org.springframework.stereotype.Controller;
47 import org.springframework.ui.Model;
48 import org.springframework.util.Assert;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestParam;
51 import org.springframework.xml.transform.StringSource;
52
53 @Controller
54 public class ProcessController {
55
56 private final static Log log = LogFactory.getLog(ProcessController.class);
57
58 private SlcExecutionDao slcExecutionDao;
59 private SlcAgentFactory agentFactory;
60 private Unmarshaller unmarshaller;
61 private Marshaller marshaller;
62 private SlcExecutionService slcExecutionService;
63 private AttachmentsStorage attachmentsStorage;
64
65 private SlcExecutionManager slcExecutionManager;
66
67 @RequestMapping("/listSlcExecutions.service")
68 protected ObjectList listSlcExecutions() {
69 List<SlcExecution> list = slcExecutionDao.listSlcExecutions();
70 return new ObjectList(list);
71 }
72
73 @RequestMapping("/getExecutionDescriptor.service")
74 protected ExecutionModuleDescriptor getExecutionDescriptor(
75 @RequestParam String agentId, @RequestParam String moduleName,
76 @RequestParam String version) {
77
78 SlcAgent slcAgent = agentFactory.getAgent(agentId);
79
80 ExecutionModuleDescriptor md = slcAgent.getExecutionModuleDescriptor(
81 moduleName, version);
82 return md;
83 }
84
85 @RequestMapping("/listModulesDescriptors.service")
86 protected ObjectList listModulesDescriptors(@RequestParam String agentId) {
87 // TODO: use centralized agentId property (from MsgConstants)?
88 SlcAgent slcAgent = agentFactory.getAgent(agentId);
89
90 List<ExecutionModuleDescriptor> descriptors = slcAgent
91 .listExecutionModuleDescriptors();
92 SortedSet<ExecutionModuleDescriptor> set = new TreeSet<ExecutionModuleDescriptor>(
93 new Comparator<ExecutionModuleDescriptor>() {
94
95 public int compare(ExecutionModuleDescriptor md1,
96 ExecutionModuleDescriptor md2) {
97 String str1 = md1.getLabel() != null ? md1.getLabel()
98 : md1.getName();
99 String str2 = md2.getLabel() != null ? md2.getLabel()
100 : md2.getName();
101 return str1.compareTo(str2);
102 }
103 });
104 set.addAll(descriptors);
105 return new ObjectList(set);
106 }
107
108 @RequestMapping("/getSlcExecution.service")
109 protected SlcExecution getSlcExecution(@RequestParam String uuid) {
110 SlcExecution slcExecution = slcExecutionDao.getSlcExecution(uuid);
111 initializeSEM();
112 slcExecutionManager.retrieveRealizedFlows(slcExecution);
113 return slcExecution;
114 }
115
116 @RequestMapping("/newSlcExecution.service")
117 protected ExecutionAnswer newSlcExecution(HttpServletRequest request,
118 Model model) throws Exception {
119
120 String agentId = request
121 .getParameter(MsgConstants.PROPERTY_SLC_AGENT_ID);
122 Assert.notNull(agentId, "agent id");
123
124 String answer = request.getParameter("body");
125 if (answer == null) {
126 // lets read the message body instead
127 BufferedReader reader = request.getReader();
128 StringBuffer buffer = new StringBuffer();
129 String line = null;
130 while (((line = reader.readLine()) != null)) {
131 buffer.append(line);
132 }
133 answer = buffer.toString();
134 }
135
136 if (log.isTraceEnabled())
137 log.debug("Received message:\n" + answer);
138
139 StringSource source = new StringSource(answer);
140 SlcExecution slcExecution = (SlcExecution) unmarshaller
141 .unmarshal(source);
142
143 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
144 if (slcExecution.getUuid() == null
145 || slcExecution.getUuid().length() < 8)
146 slcExecution.setUuid(UUID.randomUUID().toString());
147
148 slcExecution.setStatus(SlcExecution.SCHEDULED);
149 slcExecution.getSteps().add(
150 new SlcExecutionStep(SlcExecutionStep.START,
151 "Process started from the Web UI"));
152
153 initializeSEM();
154 slcExecutionManager.storeRealizedFlows(slcExecution);
155 slcExecutionService.newExecution(slcExecution);
156 SlcAgent agent = agentFactory.getAgent(agentId);
157 agent.runSlcExecution(slcExecution);
158
159 return ExecutionAnswer.ok("Execution completed properly");
160 }
161
162 @RequestMapping("/tailSlcExecutionStepsCount.service")
163 protected ObjectList tailSlcExecutionSteps(@RequestParam String uuid,
164 @RequestParam Integer stepCount) {
165 List<SlcExecutionStep> list = slcExecutionDao
166 .tailSteps(uuid, stepCount);
167 return new ObjectList(list);
168 }
169
170 @RequestMapping("/tailSlcExecutionStepsOffset.service")
171 protected ObjectList tailSlcExecutionSteps(@RequestParam String uuid,
172 @RequestParam String stepOffsetUuid) {
173 List<SlcExecutionStep> list = slcExecutionDao.tailSteps(uuid,
174 stepOffsetUuid);
175 return new ObjectList(list);
176 }
177
178 @RequestMapping("/downloadSlcExecution.service")
179 protected void downloadSlcExecution(@RequestParam String uuid,
180 @RequestParam String ext, HttpServletResponse response)
181 throws Exception {
182 String contentType;
183 // cf. http://en.wikipedia.org/wikServicei/Internet_media_type
184 if ("csv".equals(ext))
185 contentType = "text/csv";
186 else if ("pdf".equals(ext))
187 contentType = "application/pdf";
188 else if ("zip".equals(ext))
189 contentType = "application/zip";
190 else if ("html".equals(ext))
191 contentType = "application/html";
192 else if ("txt".equals(ext) || "log".equals(ext))
193 contentType = "text/plain";
194 else if ("doc".equals(ext) || "docx".equals(ext))
195 contentType = "application/msword";
196 else if ("xls".equals(ext) || "xlsx".equals(ext))
197 contentType = "application/vnd.ms-excel";
198 else if ("xml".equals(ext))
199 contentType = "text/xml";
200 else
201 contentType = "Content-Type: application/force-download";
202
203 String name = "Process-" + uuid + "." + ext;
204
205 SlcExecution process = slcExecutionDao.getSlcExecution(uuid);
206
207 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
208
209 // TODO: put it in a separate view
210 if ("log".equals(ext)) {
211 StringBuffer buf = new StringBuffer("");
212 buf.append("#\n# PROCESS " + process.getUuid() + "\n#\n\n");
213 buf
214 .append("Started at " + df.format(process.getStartDate())
215 + "\n");
216 buf.append("Ended at " + df.format(process.getEndDate()) + "\n");
217 buf.append("On host " + process.getHost() + "\n");
218 buf.append("\n# LOG\n\n");
219 for (SlcExecutionStep step : process.getSteps()) {
220 buf.append(df.format(step.getTimestamp()));
221 buf.append(" ");
222 for (int i = 0; i < step.getLogLines().size(); i++) {
223 if (i > 0)
224 buf.append('\n');
225 buf.append(step.getLogLines().get(i));
226 }
227 buf.append(" - ");
228 buf.append(step.getType());
229 buf.append(" - ");
230 buf.append('[').append(step.getThread()).append(']');
231 buf.append('\n');
232 }
233 prepareDownloadResponse(name, contentType, response);
234 response.getWriter().print(buf);
235 } else {
236 throw new SlcException("Unsupported content type " + contentType);
237 }
238 }
239
240 protected void prepareDownloadResponse(String name, String contentType,
241 HttpServletResponse response) {
242 response.setHeader("Content-Disposition", "attachment; filename=\""
243 + name + "\"");
244 response.setContentType(contentType + ";name=\"" + name + "\"");
245 response.setHeader("Expires", "0");
246 response.setHeader("Cache-Control", "no-cache, must-revalidate");
247 response.setHeader("Pragma", "no-cache");
248 }
249
250 private void initializeSEM() {
251 slcExecutionManager = new SlcExecutionManager(unmarshaller, marshaller,
252 attachmentsStorage);
253 }
254
255 public void setSlcExecutionDao(SlcExecutionDao slcExecutionDao) {
256 this.slcExecutionDao = slcExecutionDao;
257 }
258
259 public void setSlcExecutionService(SlcExecutionService slcExecutionService) {
260 this.slcExecutionService = slcExecutionService;
261 }
262
263 public void setUnmarshaller(Unmarshaller unmarshaller) {
264 this.unmarshaller = unmarshaller;
265 }
266
267 public void setMarshaller(Marshaller marshaller) {
268 this.marshaller = marshaller;
269 }
270
271 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
272 this.attachmentsStorage = attachmentsStorage;
273 }
274
275 public void setAgentFactory(SlcAgentFactory agentFactory) {
276 this.agentFactory = agentFactory;
277 }
278
279 }