]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/ServiceController.java
Remove unused classes in org.argeo.slc.server package
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / ServiceController.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;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.SortedSet;
24 import java.util.Vector;
25
26 import javax.servlet.ServletOutputStream;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
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;
51
52 @Controller
53 public class ServiceController {
54
55 // private final static Log log = LogFactory.getLog(ServiceController.class);
56
57 // Constants
58 public final static String KEY_ANSWER = "__answer";
59 protected final String FORCE_DOWNLOAD = "Content-Type: application/force-download";
60
61 // IoC
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;
69
70 public ServiceController(TreeTestResultDao treeTestResultDao,
71 TreeTestResultCollectionDao testResultCollectionDao,
72 TestManagerService testManagerService,
73 SlcAgentDescriptorDao slcAgentDescriptorDao) {
74
75 this.testManagerService = testManagerService;
76 this.treeTestResultDao = treeTestResultDao;
77 this.testResultCollectionDao = testResultCollectionDao;
78 this.slcAgentDescriptorDao = slcAgentDescriptorDao;
79 }
80
81 // Business Methods
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");
86 }
87
88 @RequestMapping("/shutdownRuntime.service")
89 protected ExecutionAnswer shutdownRuntime(Model model) {
90 new Thread() {
91 public void run() {
92 // wait in order to let call return
93 try {
94 Thread.sleep(3000);
95 } catch (InterruptedException e) {
96 // silent
97 }
98 dynamicRuntime.shutdown();
99 }
100 }.start();
101 return ExecutionAnswer.ok("Server shutting down...");
102 }
103
104 @RequestMapping("/getResult.service")
105 protected TreeTestResult getResult(
106 @RequestParam(value = "uuid", required = false) String uuid) {
107
108 TreeTestResult result = treeTestResultDao.getTestResult(uuid);
109 if (result == null)
110 throw new SlcException("No result found for uuid " + uuid);
111 return result;
112 }
113
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");
119 }
120
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");
128
129 // Checks
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");
136
137 // Remove specified results
138 if (resultUuids != null)
139 for (String resultUuid : resultUuids)
140 testManagerService.removeResultFromCollection(collectionId,
141 resultUuid);
142
143 if (attrNames != null) {
144 TreeTestResultCollection sourceCollection = testResultCollectionDao
145 .getTestResultCollection(collectionId);
146
147 int index = 0;
148 for (String attrName : attrNames) {
149 String attrPattern = attrPatterns[index];// safe: checked above
150
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());
158 }
159 }
160 index++;
161 }
162 } else {
163 if (resultUuids == null) {// no specs
164 // remove all
165 // TODO: optimize
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());
173 }
174
175 }
176 }
177 return ExecutionAnswer.ok("Execution completed properly");
178 }
179
180 @RequestMapping("/listCollectionRefs.service")
181 protected ReferenceList listCollectionRefs(HttpServletRequest request,
182 HttpServletResponse response) {
183
184 SortedSet<TreeTestResultCollection> results = testResultCollectionDao
185 .listCollections();
186
187 ReferenceList referenceList = new ReferenceList();
188 for (TreeTestResultCollection collection : results) {
189 referenceList.getReferences().add(collection.getId());
190 }
191 return referenceList;
192 }
193
194 @RequestMapping("/listResultAttributes.service")
195 protected ObjectList listResultAttributes(@RequestParam String id, Model model) {
196
197 List<ResultAttributes> resultAttributes = testResultCollectionDao
198 .listResultAttributes(id);
199 return new ObjectList(
200 resultAttributes);
201 }
202
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]);
214 }
215 }
216
217 List<TreeTestResult> resultAttributes = testResultCollectionDao
218 .listResults(collectionId, attributes);
219 return new ObjectList(resultAttributes);
220 }
221
222 @RequestMapping("/copyCollectionToCollection.service")
223 protected ExecutionAnswer copyCollectionToCollection(
224 @RequestParam String sourceCollectionId,
225 @RequestParam String targetCollectionId,
226 HttpServletRequest request) {
227
228 String[] attrNames = request.getParameterValues("attrName");
229 String[] attrPatterns = request.getParameterValues("attrPattern");
230
231 // Checks
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");
239
240 TreeTestResultCollection sourceCollection = testResultCollectionDao
241 .getTestResultCollection(sourceCollectionId);
242 if (attrNames != null) {
243 int index = 0;
244 for (String attrName : attrNames) {
245 String attrPattern = attrPatterns[index];// safe: checked above
246
247 for (TreeTestResult treeTestResult : sourceCollection
248 .getResults()) {
249 if (PatternMatchUtils.simpleMatch(attrPattern,
250 treeTestResult.getAttributes().get(attrName))) {
251 testManagerService.addResultToCollection(
252 targetCollectionId, treeTestResult.getUuid());
253 }
254 }
255 index++;
256 }
257 } else {
258 // remove all
259 // TODO: optimize
260 for (TreeTestResult treeTestResult : sourceCollection.getResults()) {
261 testManagerService.addResultToCollection(targetCollectionId,
262 treeTestResult.getUuid());
263 }
264 }
265 return ExecutionAnswer.ok("Execution completed properly");
266 }
267
268 @RequestMapping("/listAgents.service")
269 protected ObjectList listAgents() {
270 List<SlcAgentDescriptor> list = slcAgentDescriptorDao
271 .listSlcAgentDescriptors();
272 return new ObjectList(list);
273 }
274
275 @RequestMapping("/cleanAgents.service")
276 protected ExecutionAnswer cleanAgents() {
277
278 List<SlcAgentDescriptor> list = slcAgentDescriptorDao
279 .listSlcAgentDescriptors();
280 for (SlcAgentDescriptor t : new Vector<SlcAgentDescriptor>(list)) {
281 slcAgentDescriptorDao.delete(t);
282 }
283 return ExecutionAnswer.ok("Execution completed properly");
284 }
285
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())) {
291 if (name != null) {
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";
311 }
312 }
313
314 if (name != null) {
315 contentType = contentType + ";name=\"" + name + "\"";
316 response.setHeader("Content-Disposition", "attachment; filename=\""
317 + name + "\"");
318 }
319 response.setHeader("Expires", "0");
320 response.setHeader("Cache-Control", "no-cache, must-revalidate");
321 response.setHeader("Pragma", "no-cache");
322
323 SimpleAttachment resourceDescriptor = new SimpleAttachment();
324 resourceDescriptor.setUuid(uuid);
325 resourceDescriptor.setContentType(contentType);
326
327 response.setContentType(contentType);
328 ServletOutputStream outputStream = response.getOutputStream();
329 attachmentsStorage.retrieveAttachment(resourceDescriptor, outputStream);
330 }
331
332 // IoC
333
334 public void setDynamicRuntime(DynamicRuntime<?> dynamicRuntime) {
335 this.dynamicRuntime = dynamicRuntime;
336 }
337
338 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
339 this.attachmentsStorage = attachmentsStorage;
340 }
341 }