]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/ServiceController.java
Introduction of annotation to handle MVC flows
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / ServiceController.java
1 package org.argeo.slc.web.mvc;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.SortedSet;
8 import java.util.Vector;
9
10 import javax.servlet.ServletOutputStream;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.apache.commons.io.FilenameUtils;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.argeo.slc.SlcException;
18 import org.argeo.slc.core.attachment.AttachmentsStorage;
19 import org.argeo.slc.core.attachment.SimpleAttachment;
20 import org.argeo.slc.core.test.tree.ResultAttributes;
21 import org.argeo.slc.core.test.tree.TreeTestResult;
22 import org.argeo.slc.core.test.tree.TreeTestResultCollection;
23 import org.argeo.slc.dao.runtime.SlcAgentDescriptorDao;
24 import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao;
25 import org.argeo.slc.dao.test.tree.TreeTestResultDao;
26 import org.argeo.slc.deploy.DynamicRuntime;
27 import org.argeo.slc.msg.ExecutionAnswer;
28 import org.argeo.slc.msg.ObjectList;
29 import org.argeo.slc.msg.ReferenceList;
30 import org.argeo.slc.runtime.SlcAgentDescriptor;
31 import org.argeo.slc.services.TestManagerService;
32 import org.springframework.stereotype.Controller;
33 import org.springframework.ui.Model;
34 import org.springframework.util.PatternMatchUtils;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.springframework.web.bind.annotation.RequestParam;
37
38 @Controller
39 public class ServiceController {
40
41 private final static Log log = LogFactory.getLog(ServiceController.class);
42
43 // Constants
44 public final static String KEY_ANSWER = "__answer";
45 protected final String FORCE_DOWNLOAD = "Content-Type: application/force-download";
46
47 // IoC
48 // FIXME : why must this be final ??
49 private final TreeTestResultDao treeTestResultDao;
50 private final TestManagerService testManagerService;
51 private final TreeTestResultCollectionDao testResultCollectionDao;
52 private final SlcAgentDescriptorDao slcAgentDescriptorDao;
53 private AttachmentsStorage attachmentsStorage;
54 private DynamicRuntime<?> dynamicRuntime;
55
56 public ServiceController(TreeTestResultDao treeTestResultDao,
57 TreeTestResultCollectionDao testResultCollectionDao,
58 TestManagerService testManagerService,
59 SlcAgentDescriptorDao slcAgentDescriptorDao) {
60 if (log.isDebugEnabled())
61 log.debug("In SlcServiceController Constructor");
62
63 this.testManagerService = testManagerService;
64 this.treeTestResultDao = treeTestResultDao;
65 this.testResultCollectionDao = testResultCollectionDao;
66 this.slcAgentDescriptorDao = slcAgentDescriptorDao;
67 }
68
69 // Business Methods
70
71 @RequestMapping("/isServerReady.service")
72 protected String isServerReady(Model model) {
73 if (log.isDebugEnabled())
74 log.debug("SlcServiceController :: isServerReady ");
75 // Does nothing for now, it will return an OK answer.
76 model.addAttribute(KEY_ANSWER, ExecutionAnswer
77 .ok("Execution completed properly"));
78 return KEY_ANSWER;
79 }
80
81 @RequestMapping("/shutdownRuntime.service")
82 protected String shutdownRuntime(Model model) {
83 if (log.isDebugEnabled())
84 log.debug("SlcServiceController :: shutdownRuntime");
85
86 new Thread() {
87 public void run() {
88 // wait in order to let call return
89 try {
90 Thread.sleep(3000);
91 } catch (InterruptedException e) {
92 // silent
93 }
94 dynamicRuntime.shutdown();
95 }
96 }.start();
97 ExecutionAnswer answer = ExecutionAnswer.ok("Server shutting down...");
98 model.addAttribute(answer);
99 return KEY_ANSWER;
100 }
101
102 @RequestMapping("/getResult.service")
103 protected String getResult(
104 @RequestParam(value = "uuid", required = false) String uuid,
105 Model model) {
106 if (log.isDebugEnabled())
107 log.debug("In SlcServiceController In GetResultMethod");
108
109 TreeTestResult result = treeTestResultDao.getTestResult(uuid);
110 if (result == null)
111 throw new SlcException("No result found for uuid " + uuid);
112 model.addAttribute("result", result);
113 return "result";
114 }
115
116 @RequestMapping("/addResultToCollection.service")
117 protected String addResultToCollection(@RequestParam String collectionId,
118 @RequestParam String resultUuid, Model model) {
119 if (log.isDebugEnabled())
120 log.debug("In SlcServiceController :: addResultToCollection");
121
122 testManagerService.addResultToCollection(collectionId, resultUuid);
123 model.addAttribute(KEY_ANSWER, ExecutionAnswer
124 .ok("Execution completed properly"));
125 return KEY_ANSWER;
126 }
127
128 @RequestMapping("/removeResultFromCollection.service")
129 protected String removeResultFromCollection(HttpServletRequest request,
130 Model model) {
131 if (log.isDebugEnabled())
132 log
133 .debug("In SlcServiceController :: removeResultFromCollection.service");
134
135 String collectionId = request.getParameter("collectionId");
136 String[] resultUuids = request.getParameterValues("resultUuid");
137 String[] attrNames = request.getParameterValues("attrName");
138 String[] attrPatterns = request.getParameterValues("attrPattern");
139
140 // Checks
141 if (collectionId == null)
142 throw new SlcException("A collection id must be specified");
143 if (attrNames != null
144 && (attrPatterns == null || attrNames.length != attrPatterns.length))
145 throw new SlcException(
146 "There must be as many attrName as attrPatterns");
147
148 // Remove specified results
149 if (resultUuids != null)
150 for (String resultUuid : resultUuids)
151 testManagerService.removeResultFromCollection(collectionId,
152 resultUuid);
153
154 if (attrNames != null) {
155 TreeTestResultCollection sourceCollection = testResultCollectionDao
156 .getTestResultCollection(collectionId);
157
158 int index = 0;
159 for (String attrName : attrNames) {
160 String attrPattern = attrPatterns[index];// safe: checked above
161
162 List<TreeTestResult> results = new ArrayList<TreeTestResult>(
163 sourceCollection.getResults());
164 for (TreeTestResult treeTestResult : results) {
165 if (PatternMatchUtils.simpleMatch(attrPattern,
166 treeTestResult.getAttributes().get(attrName))) {
167 testManagerService.removeResultFromCollection(
168 collectionId, treeTestResult.getUuid());
169 }
170 }
171 index++;
172 }
173 } else {
174 if (resultUuids == null) {// no specs
175 // remove all
176 // TODO: optimize
177 TreeTestResultCollection sourceCollection = testResultCollectionDao
178 .getTestResultCollection(collectionId);
179 List<TreeTestResult> results = new ArrayList<TreeTestResult>(
180 sourceCollection.getResults());
181 for (TreeTestResult treeTestResult : results) {
182 testManagerService.removeResultFromCollection(collectionId,
183 treeTestResult.getUuid());
184 }
185
186 }
187 }
188 model.addAttribute(KEY_ANSWER, ExecutionAnswer
189 .ok("Execution completed properly"));
190 return KEY_ANSWER;
191 }
192
193 @RequestMapping("/listCollectionRefs.service")
194 protected String listCollectionRefs(HttpServletRequest request,
195 HttpServletResponse response, Model model) {
196
197 if (log.isDebugEnabled())
198 log.debug("In SlcServiceController :: listCollectionRefs.service");
199
200 SortedSet<TreeTestResultCollection> results = testResultCollectionDao
201 .listCollections();
202
203 ReferenceList referenceList = new ReferenceList();
204 for (TreeTestResultCollection collection : results) {
205 referenceList.getReferences().add(collection.getId());
206 }
207
208 model.addAttribute("referenceList", referenceList);
209 return "referenceList";
210 }
211
212 @RequestMapping("/listResultAttributes.service")
213 protected String listResultAttributes(@RequestParam String id, Model model) {
214
215 if (log.isDebugEnabled())
216 log
217 .debug("In SlcServiceController :: listResultAttributes.service");
218
219 List<ResultAttributes> resultAttributes = testResultCollectionDao
220 .listResultAttributes(id);
221
222 model.addAttribute("resultAttributesList", new ObjectList(
223 resultAttributes));
224 return "resultAttributesList";
225 }
226
227 @RequestMapping("/listResults.service")
228 @SuppressWarnings(value = { "unchecked" })
229 protected String listResults(@RequestParam (value = "collectionId", required = false)String collectionId,
230 HttpServletRequest request, Model model) {
231
232 if (log.isDebugEnabled())
233 log.debug("In SlcServiceController :: listResults.service");
234
235 Map<String, String[]> parameterMap = request.getParameterMap();
236
237 Map<String, String> attributes = new HashMap<String, String>();
238 for (String parameter : parameterMap.keySet()) {
239 if (parameter.startsWith("attr.")) {
240 String key = parameter.substring("attr.".length());
241 attributes.put(key, parameterMap.get(parameter)[0]);
242 }
243 }
244
245 List<TreeTestResult> resultAttributes = testResultCollectionDao
246 .listResults(collectionId, attributes);
247
248 model.addAttribute("resultList", new ObjectList(resultAttributes));
249 return "resultList";
250 }
251
252 @RequestMapping("/copyCollectionToCollection.service")
253 protected String copyCollectionToCollection(
254 @RequestParam String sourceCollectionId,
255 @RequestParam String targetCollectionId,
256 HttpServletRequest request, Model model) {
257
258 if (log.isDebugEnabled())
259 log
260 .debug("In SlcServiceController :: copyCollectionToCollection.service");
261
262 String[] attrNames = request.getParameterValues("attrName");
263 String[] attrPatterns = request.getParameterValues("attrPattern");
264
265 // Checks
266 if (sourceCollectionId == null || targetCollectionId == null)
267 throw new SlcException(
268 "Source and target collection ids must be specified");
269 if (attrNames != null
270 && (attrPatterns == null || attrNames.length != attrPatterns.length))
271 throw new SlcException(
272 "There must be as many attrName as attrPatterns");
273
274 TreeTestResultCollection sourceCollection = testResultCollectionDao
275 .getTestResultCollection(sourceCollectionId);
276 if (attrNames != null) {
277 int index = 0;
278 for (String attrName : attrNames) {
279 String attrPattern = attrPatterns[index];// safe: checked above
280
281 for (TreeTestResult treeTestResult : sourceCollection
282 .getResults()) {
283 if (PatternMatchUtils.simpleMatch(attrPattern,
284 treeTestResult.getAttributes().get(attrName))) {
285 testManagerService.addResultToCollection(
286 targetCollectionId, treeTestResult.getUuid());
287 }
288 }
289 index++;
290 }
291 } else {
292 // remove all
293 // TODO: optimize
294 for (TreeTestResult treeTestResult : sourceCollection.getResults()) {
295 testManagerService.addResultToCollection(targetCollectionId,
296 treeTestResult.getUuid());
297 }
298 }
299 model.addAttribute(KEY_ANSWER, ExecutionAnswer
300 .ok("Execution completed properly"));
301 return KEY_ANSWER;
302 }
303
304 @RequestMapping("/listAgents.service")
305 protected String listAgents(Model model) {
306 if (log.isDebugEnabled())
307 log.debug("In SlcServiceController :: listAgents.service");
308
309 List<SlcAgentDescriptor> list = slcAgentDescriptorDao
310 .listSlcAgentDescriptors();
311 model.addAttribute("list", new ObjectList(list));
312 return "list";
313 }
314
315 @RequestMapping("/cleanAgents.service")
316 protected String cleanAgents(Model model) {
317
318 if (log.isDebugEnabled())
319 log.debug("In SlcServiceController :: cleanAgents.service");
320
321 List<SlcAgentDescriptor> list = slcAgentDescriptorDao
322 .listSlcAgentDescriptors();
323 for (SlcAgentDescriptor t : new Vector<SlcAgentDescriptor>(list)) {
324 slcAgentDescriptorDao.delete(t);
325 }
326 model.addAttribute(KEY_ANSWER, ExecutionAnswer
327 .ok("Execution completed properly"));
328 return KEY_ANSWER;
329 }
330
331 @RequestMapping("/getAttachment.service")
332 protected String getAttachment(@RequestParam String uuid,
333 @RequestParam String contentType, @RequestParam String name,
334 HttpServletResponse response, Model model) throws Exception {
335 if (log.isDebugEnabled())
336 log.debug("In SlcServiceController :: getAttachment");
337
338 if (contentType == null || "".equals(contentType.trim())) {
339 if (name != null) {
340 contentType = FORCE_DOWNLOAD;
341 String ext = FilenameUtils.getExtension(name);
342 // cf. http://en.wikipedia.org/wiki/Internet_media_type
343 if ("csv".equals(ext))
344 contentType = "text/csv";
345 else if ("pdf".equals(ext))
346 contentType = "application/pdf";
347 else if ("zip".equals(ext))
348 contentType = "application/zip";
349 else if ("html".equals(ext))
350 contentType = "application/html";
351 else if ("txt".equals(ext))
352 contentType = "text/plain";
353 else if ("doc".equals(ext) || "docx".equals(ext))
354 contentType = "application/msword";
355 else if ("xls".equals(ext) || "xlsx".equals(ext))
356 contentType = "application/vnd.ms-excel";
357 else if ("xml".equals(ext))
358 contentType = "text/xml";
359 }
360 }
361
362 if (name != null) {
363 contentType = contentType + ";name=\"" + name + "\"";
364 response.setHeader("Content-Disposition", "attachment; filename=\""
365 + name + "\"");
366 }
367 response.setHeader("Expires", "0");
368 response.setHeader("Cache-Control", "no-cache, must-revalidate");
369 response.setHeader("Pragma", "no-cache");
370
371 SimpleAttachment resourceDescriptor = new SimpleAttachment();
372 resourceDescriptor.setUuid(uuid);
373 resourceDescriptor.setContentType(contentType);
374
375 response.setContentType(contentType);
376 ServletOutputStream outputStream = response.getOutputStream();
377 attachmentsStorage.retrieveAttachment(resourceDescriptor, outputStream);
378 return null;
379 }
380
381 // IoC
382
383 public void setDynamicRuntime(DynamicRuntime<?> dynamicRuntime) {
384 this.dynamicRuntime = dynamicRuntime;
385 }
386
387 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
388 this.attachmentsStorage = attachmentsStorage;
389 }
390 }