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
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
23 import java
.util
.SortedSet
;
24 import java
.util
.Vector
;
26 import javax
.servlet
.ServletOutputStream
;
27 import javax
.servlet
.http
.HttpServletRequest
;
28 import javax
.servlet
.http
.HttpServletResponse
;
30 import org
.apache
.commons
.io
.FilenameUtils
;
31 import org
.argeo
.slc
.SlcException
;
32 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
33 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
34 import org
.argeo
.slc
.core
.test
.tree
.ResultAttributes
;
35 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResult
;
36 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResultCollection
;
37 import org
.argeo
.slc
.dao
.runtime
.SlcAgentDescriptorDao
;
38 import org
.argeo
.slc
.dao
.test
.tree
.TreeTestResultCollectionDao
;
39 import org
.argeo
.slc
.dao
.test
.tree
.TreeTestResultDao
;
40 import org
.argeo
.slc
.deploy
.DynamicRuntime
;
41 import org
.argeo
.slc
.msg
.ExecutionAnswer
;
42 import org
.argeo
.slc
.msg
.ObjectList
;
43 import org
.argeo
.slc
.msg
.ReferenceList
;
44 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
45 import org
.argeo
.slc
.services
.TestManagerService
;
46 import org
.springframework
.stereotype
.Controller
;
47 import org
.springframework
.ui
.Model
;
48 import org
.springframework
.util
.PatternMatchUtils
;
49 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
50 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
53 public class ServiceController
{
55 // private final static Log log = LogFactory.getLog(ServiceController.class);
58 public final static String KEY_ANSWER
= "__answer";
59 protected final String FORCE_DOWNLOAD
= "Content-Type: application/force-download";
62 // FIXME : why must this be final ??
63 private final TreeTestResultDao treeTestResultDao
;
64 private final TestManagerService testManagerService
;
65 private final TreeTestResultCollectionDao testResultCollectionDao
;
66 private final SlcAgentDescriptorDao slcAgentDescriptorDao
;
67 private AttachmentsStorage attachmentsStorage
;
68 private DynamicRuntime
<?
> dynamicRuntime
;
70 public ServiceController(TreeTestResultDao treeTestResultDao
,
71 TreeTestResultCollectionDao testResultCollectionDao
,
72 TestManagerService testManagerService
,
73 SlcAgentDescriptorDao slcAgentDescriptorDao
) {
75 this.testManagerService
= testManagerService
;
76 this.treeTestResultDao
= treeTestResultDao
;
77 this.testResultCollectionDao
= testResultCollectionDao
;
78 this.slcAgentDescriptorDao
= slcAgentDescriptorDao
;
82 @RequestMapping("/isServerReady.service")
83 protected ExecutionAnswer
isServerReady(Model model
) {
84 // Does nothing for now, it will return an OK answer.
85 return ExecutionAnswer
.ok("Execution completed properly");
88 @RequestMapping("/shutdownRuntime.service")
89 protected ExecutionAnswer
shutdownRuntime(Model model
) {
92 // wait in order to let call return
95 } catch (InterruptedException e
) {
98 dynamicRuntime
.shutdown();
101 return ExecutionAnswer
.ok("Server shutting down...");
104 @RequestMapping("/getResult.service")
105 protected TreeTestResult
getResult(
106 @RequestParam(value
= "uuid", required
= false) String uuid
) {
108 TreeTestResult result
= treeTestResultDao
.getTestResult(uuid
);
110 throw new SlcException("No result found for uuid " + uuid
);
114 @RequestMapping("/addResultToCollection.service")
115 protected ExecutionAnswer
addResultToCollection(
116 @RequestParam String collectionId
, @RequestParam String resultUuid
) {
117 testManagerService
.addResultToCollection(collectionId
, resultUuid
);
118 return ExecutionAnswer
.ok("Execution completed properly");
121 @RequestMapping("/removeResultFromCollection.service")
122 protected ExecutionAnswer
removeResultFromCollection(
123 HttpServletRequest request
) {
124 String collectionId
= request
.getParameter("collectionId");
125 String
[] resultUuids
= request
.getParameterValues("resultUuid");
126 String
[] attrNames
= request
.getParameterValues("attrName");
127 String
[] attrPatterns
= request
.getParameterValues("attrPattern");
130 if (collectionId
== null)
131 throw new SlcException("A collection id must be specified");
132 if (attrNames
!= null
133 && (attrPatterns
== null || attrNames
.length
!= attrPatterns
.length
))
134 throw new SlcException(
135 "There must be as many attrName as attrPatterns");
137 // Remove specified results
138 if (resultUuids
!= null)
139 for (String resultUuid
: resultUuids
)
140 testManagerService
.removeResultFromCollection(collectionId
,
143 if (attrNames
!= null) {
144 TreeTestResultCollection sourceCollection
= testResultCollectionDao
145 .getTestResultCollection(collectionId
);
148 for (String attrName
: attrNames
) {
149 String attrPattern
= attrPatterns
[index
];// safe: checked above
151 List
<TreeTestResult
> results
= new ArrayList
<TreeTestResult
>(
152 sourceCollection
.getResults());
153 for (TreeTestResult treeTestResult
: results
) {
154 if (PatternMatchUtils
.simpleMatch(attrPattern
,
155 treeTestResult
.getAttributes().get(attrName
))) {
156 testManagerService
.removeResultFromCollection(
157 collectionId
, treeTestResult
.getUuid());
163 if (resultUuids
== null) {// no specs
166 TreeTestResultCollection sourceCollection
= testResultCollectionDao
167 .getTestResultCollection(collectionId
);
168 List
<TreeTestResult
> results
= new ArrayList
<TreeTestResult
>(
169 sourceCollection
.getResults());
170 for (TreeTestResult treeTestResult
: results
) {
171 testManagerService
.removeResultFromCollection(collectionId
,
172 treeTestResult
.getUuid());
177 return ExecutionAnswer
.ok("Execution completed properly");
180 @RequestMapping("/listCollectionRefs.service")
181 protected ReferenceList
listCollectionRefs(HttpServletRequest request
,
182 HttpServletResponse response
) {
184 SortedSet
<TreeTestResultCollection
> results
= testResultCollectionDao
187 ReferenceList referenceList
= new ReferenceList();
188 for (TreeTestResultCollection collection
: results
) {
189 referenceList
.getReferences().add(collection
.getId());
191 return referenceList
;
194 @RequestMapping("/listResultAttributes.service")
195 protected ObjectList
listResultAttributes(@RequestParam String id
, Model model
) {
197 List
<ResultAttributes
> resultAttributes
= testResultCollectionDao
198 .listResultAttributes(id
);
199 return new ObjectList(
203 @RequestMapping("/listResults.service")
204 @SuppressWarnings(value
= { "unchecked" })
205 protected ObjectList
listResults(
206 @RequestParam(value
= "collectionId", required
= false) String collectionId
,
207 HttpServletRequest request
) {
208 Map
<String
, String
[]> parameterMap
= request
.getParameterMap();
209 Map
<String
, String
> attributes
= new HashMap
<String
, String
>();
210 for (String parameter
: parameterMap
.keySet()) {
211 if (parameter
.startsWith("attr.")) {
212 String key
= parameter
.substring("attr.".length());
213 attributes
.put(key
, parameterMap
.get(parameter
)[0]);
217 List
<TreeTestResult
> resultAttributes
= testResultCollectionDao
218 .listResults(collectionId
, attributes
);
219 return new ObjectList(resultAttributes
);
222 @RequestMapping("/copyCollectionToCollection.service")
223 protected ExecutionAnswer
copyCollectionToCollection(
224 @RequestParam String sourceCollectionId
,
225 @RequestParam String targetCollectionId
,
226 HttpServletRequest request
) {
228 String
[] attrNames
= request
.getParameterValues("attrName");
229 String
[] attrPatterns
= request
.getParameterValues("attrPattern");
232 if (sourceCollectionId
== null || targetCollectionId
== null)
233 throw new SlcException(
234 "Source and target collection ids must be specified");
235 if (attrNames
!= null
236 && (attrPatterns
== null || attrNames
.length
!= attrPatterns
.length
))
237 throw new SlcException(
238 "There must be as many attrName as attrPatterns");
240 TreeTestResultCollection sourceCollection
= testResultCollectionDao
241 .getTestResultCollection(sourceCollectionId
);
242 if (attrNames
!= null) {
244 for (String attrName
: attrNames
) {
245 String attrPattern
= attrPatterns
[index
];// safe: checked above
247 for (TreeTestResult treeTestResult
: sourceCollection
249 if (PatternMatchUtils
.simpleMatch(attrPattern
,
250 treeTestResult
.getAttributes().get(attrName
))) {
251 testManagerService
.addResultToCollection(
252 targetCollectionId
, treeTestResult
.getUuid());
260 for (TreeTestResult treeTestResult
: sourceCollection
.getResults()) {
261 testManagerService
.addResultToCollection(targetCollectionId
,
262 treeTestResult
.getUuid());
265 return ExecutionAnswer
.ok("Execution completed properly");
268 @RequestMapping("/listAgents.service")
269 protected ObjectList
listAgents() {
270 List
<SlcAgentDescriptor
> list
= slcAgentDescriptorDao
271 .listSlcAgentDescriptors();
272 return new ObjectList(list
);
275 @RequestMapping("/cleanAgents.service")
276 protected ExecutionAnswer
cleanAgents() {
278 List
<SlcAgentDescriptor
> list
= slcAgentDescriptorDao
279 .listSlcAgentDescriptors();
280 for (SlcAgentDescriptor t
: new Vector
<SlcAgentDescriptor
>(list
)) {
281 slcAgentDescriptorDao
.delete(t
);
283 return ExecutionAnswer
.ok("Execution completed properly");
286 @RequestMapping("/getAttachment.service")
287 protected void getAttachment(@RequestParam String uuid
,
288 @RequestParam String contentType
, @RequestParam String name
,
289 HttpServletResponse response
) throws Exception
{
290 if (contentType
== null || "".equals(contentType
.trim())) {
292 contentType
= FORCE_DOWNLOAD
;
293 String ext
= FilenameUtils
.getExtension(name
);
294 // cf. http://en.wikipedia.org/wiki/Internet_media_type
295 if ("csv".equals(ext
))
296 contentType
= "text/csv";
297 else if ("pdf".equals(ext
))
298 contentType
= "application/pdf";
299 else if ("zip".equals(ext
))
300 contentType
= "application/zip";
301 else if ("html".equals(ext
))
302 contentType
= "application/html";
303 else if ("txt".equals(ext
))
304 contentType
= "text/plain";
305 else if ("doc".equals(ext
) || "docx".equals(ext
))
306 contentType
= "application/msword";
307 else if ("xls".equals(ext
) || "xlsx".equals(ext
))
308 contentType
= "application/vnd.ms-excel";
309 else if ("xml".equals(ext
))
310 contentType
= "text/xml";
315 contentType
= contentType
+ ";name=\"" + name
+ "\"";
316 response
.setHeader("Content-Disposition", "attachment; filename=\""
319 response
.setHeader("Expires", "0");
320 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
321 response
.setHeader("Pragma", "no-cache");
323 SimpleAttachment resourceDescriptor
= new SimpleAttachment();
324 resourceDescriptor
.setUuid(uuid
);
325 resourceDescriptor
.setContentType(contentType
);
327 response
.setContentType(contentType
);
328 ServletOutputStream outputStream
= response
.getOutputStream();
329 attachmentsStorage
.retrieveAttachment(resourceDescriptor
, outputStream
);
334 public void setDynamicRuntime(DynamicRuntime
<?
> dynamicRuntime
) {
335 this.dynamicRuntime
= dynamicRuntime
;
338 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
339 this.attachmentsStorage
= attachmentsStorage
;