1 package org
.argeo
.slc
.web
.mvc
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
7 import java
.util
.SortedSet
;
8 import java
.util
.Vector
;
10 import javax
.servlet
.ServletOutputStream
;
11 import javax
.servlet
.http
.HttpServletRequest
;
12 import javax
.servlet
.http
.HttpServletResponse
;
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
;
39 public class ServiceController
{
41 private final static Log log
= LogFactory
.getLog(ServiceController
.class);
44 public final static String KEY_ANSWER
= "__answer";
45 protected final String FORCE_DOWNLOAD
= "Content-Type: application/force-download";
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
;
56 public ServiceController(TreeTestResultDao treeTestResultDao
,
57 TreeTestResultCollectionDao testResultCollectionDao
,
58 TestManagerService testManagerService
,
59 SlcAgentDescriptorDao slcAgentDescriptorDao
) {
60 if (log
.isDebugEnabled())
61 log
.debug("In SlcServiceController Constructor");
63 this.testManagerService
= testManagerService
;
64 this.treeTestResultDao
= treeTestResultDao
;
65 this.testResultCollectionDao
= testResultCollectionDao
;
66 this.slcAgentDescriptorDao
= slcAgentDescriptorDao
;
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"));
81 @RequestMapping("/shutdownRuntime.service")
82 protected String
shutdownRuntime(Model model
) {
83 if (log
.isDebugEnabled())
84 log
.debug("SlcServiceController :: shutdownRuntime");
88 // wait in order to let call return
91 } catch (InterruptedException e
) {
94 dynamicRuntime
.shutdown();
97 ExecutionAnswer answer
= ExecutionAnswer
.ok("Server shutting down...");
98 model
.addAttribute(answer
);
102 @RequestMapping("/getResult.service")
103 protected String
getResult(
104 @RequestParam(value
= "uuid", required
= false) String uuid
,
106 if (log
.isDebugEnabled())
107 log
.debug("In SlcServiceController In GetResultMethod");
109 TreeTestResult result
= treeTestResultDao
.getTestResult(uuid
);
111 throw new SlcException("No result found for uuid " + uuid
);
112 model
.addAttribute("result", result
);
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");
122 testManagerService
.addResultToCollection(collectionId
, resultUuid
);
123 model
.addAttribute(KEY_ANSWER
, ExecutionAnswer
124 .ok("Execution completed properly"));
128 @RequestMapping("/removeResultFromCollection.service")
129 protected String
removeResultFromCollection(HttpServletRequest request
,
131 if (log
.isDebugEnabled())
133 .debug("In SlcServiceController :: removeResultFromCollection.service");
135 String collectionId
= request
.getParameter("collectionId");
136 String
[] resultUuids
= request
.getParameterValues("resultUuid");
137 String
[] attrNames
= request
.getParameterValues("attrName");
138 String
[] attrPatterns
= request
.getParameterValues("attrPattern");
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");
148 // Remove specified results
149 if (resultUuids
!= null)
150 for (String resultUuid
: resultUuids
)
151 testManagerService
.removeResultFromCollection(collectionId
,
154 if (attrNames
!= null) {
155 TreeTestResultCollection sourceCollection
= testResultCollectionDao
156 .getTestResultCollection(collectionId
);
159 for (String attrName
: attrNames
) {
160 String attrPattern
= attrPatterns
[index
];// safe: checked above
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());
174 if (resultUuids
== null) {// no specs
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());
188 model
.addAttribute(KEY_ANSWER
, ExecutionAnswer
189 .ok("Execution completed properly"));
193 @RequestMapping("/listCollectionRefs.service")
194 protected String
listCollectionRefs(HttpServletRequest request
,
195 HttpServletResponse response
, Model model
) {
197 if (log
.isDebugEnabled())
198 log
.debug("In SlcServiceController :: listCollectionRefs.service");
200 SortedSet
<TreeTestResultCollection
> results
= testResultCollectionDao
203 ReferenceList referenceList
= new ReferenceList();
204 for (TreeTestResultCollection collection
: results
) {
205 referenceList
.getReferences().add(collection
.getId());
208 model
.addAttribute("referenceList", referenceList
);
209 return "referenceList";
212 @RequestMapping("/listResultAttributes.service")
213 protected String
listResultAttributes(@RequestParam String id
, Model model
) {
215 if (log
.isDebugEnabled())
217 .debug("In SlcServiceController :: listResultAttributes.service");
219 List
<ResultAttributes
> resultAttributes
= testResultCollectionDao
220 .listResultAttributes(id
);
222 model
.addAttribute("resultAttributesList", new ObjectList(
224 return "resultAttributesList";
227 @RequestMapping("/listResults.service")
228 @SuppressWarnings(value
= { "unchecked" })
229 protected String
listResults(@RequestParam (value
= "collectionId", required
= false)String collectionId
,
230 HttpServletRequest request
, Model model
) {
232 if (log
.isDebugEnabled())
233 log
.debug("In SlcServiceController :: listResults.service");
235 Map
<String
, String
[]> parameterMap
= request
.getParameterMap();
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]);
245 List
<TreeTestResult
> resultAttributes
= testResultCollectionDao
246 .listResults(collectionId
, attributes
);
248 model
.addAttribute("resultList", new ObjectList(resultAttributes
));
252 @RequestMapping("/copyCollectionToCollection.service")
253 protected String
copyCollectionToCollection(
254 @RequestParam String sourceCollectionId
,
255 @RequestParam String targetCollectionId
,
256 HttpServletRequest request
, Model model
) {
258 if (log
.isDebugEnabled())
260 .debug("In SlcServiceController :: copyCollectionToCollection.service");
262 String
[] attrNames
= request
.getParameterValues("attrName");
263 String
[] attrPatterns
= request
.getParameterValues("attrPattern");
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");
274 TreeTestResultCollection sourceCollection
= testResultCollectionDao
275 .getTestResultCollection(sourceCollectionId
);
276 if (attrNames
!= null) {
278 for (String attrName
: attrNames
) {
279 String attrPattern
= attrPatterns
[index
];// safe: checked above
281 for (TreeTestResult treeTestResult
: sourceCollection
283 if (PatternMatchUtils
.simpleMatch(attrPattern
,
284 treeTestResult
.getAttributes().get(attrName
))) {
285 testManagerService
.addResultToCollection(
286 targetCollectionId
, treeTestResult
.getUuid());
294 for (TreeTestResult treeTestResult
: sourceCollection
.getResults()) {
295 testManagerService
.addResultToCollection(targetCollectionId
,
296 treeTestResult
.getUuid());
299 model
.addAttribute(KEY_ANSWER
, ExecutionAnswer
300 .ok("Execution completed properly"));
304 @RequestMapping("/listAgents.service")
305 protected String
listAgents(Model model
) {
306 if (log
.isDebugEnabled())
307 log
.debug("In SlcServiceController :: listAgents.service");
309 List
<SlcAgentDescriptor
> list
= slcAgentDescriptorDao
310 .listSlcAgentDescriptors();
311 model
.addAttribute("list", new ObjectList(list
));
315 @RequestMapping("/cleanAgents.service")
316 protected String
cleanAgents(Model model
) {
318 if (log
.isDebugEnabled())
319 log
.debug("In SlcServiceController :: cleanAgents.service");
321 List
<SlcAgentDescriptor
> list
= slcAgentDescriptorDao
322 .listSlcAgentDescriptors();
323 for (SlcAgentDescriptor t
: new Vector
<SlcAgentDescriptor
>(list
)) {
324 slcAgentDescriptorDao
.delete(t
);
326 model
.addAttribute(KEY_ANSWER
, ExecutionAnswer
327 .ok("Execution completed properly"));
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");
338 if (contentType
== null || "".equals(contentType
.trim())) {
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";
363 contentType
= contentType
+ ";name=\"" + name
+ "\"";
364 response
.setHeader("Content-Disposition", "attachment; filename=\""
367 response
.setHeader("Expires", "0");
368 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
369 response
.setHeader("Pragma", "no-cache");
371 SimpleAttachment resourceDescriptor
= new SimpleAttachment();
372 resourceDescriptor
.setUuid(uuid
);
373 resourceDescriptor
.setContentType(contentType
);
375 response
.setContentType(contentType
);
376 ServletOutputStream outputStream
= response
.getOutputStream();
377 attachmentsStorage
.retrieveAttachment(resourceDescriptor
, outputStream
);
383 public void setDynamicRuntime(DynamicRuntime
<?
> dynamicRuntime
) {
384 this.dynamicRuntime
= dynamicRuntime
;
387 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
388 this.attachmentsStorage
= attachmentsStorage
;