2 * Copyright (C) 2012 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.remote
.controller
;
11 import java
.io
.IOException
;
13 import java
.util
.UUID
;
15 import javax
.servlet
.http
.HttpServletRequest
;
16 import javax
.servlet
.http
.HttpServletResponse
;
18 import org
.apache
.commons
.io
.FilenameUtils
;
19 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
20 import org
.springframework
.stereotype
.Controller
;
21 import org
.springframework
.web
.bind
.WebDataBinder
;
22 import org
.springframework
.web
.bind
.annotation
.CrossOrigin
;
23 import org
.springframework
.web
.bind
.annotation
.InitBinder
;
24 import org
.springframework
.web
.bind
.annotation
.PathVariable
;
25 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
26 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
27 import org
.springframework
.web
.servlet
.ModelAndView
;
29 import eu
.etaxonomy
.cdm
.api
.service
.ProgressMonitorManager
;
30 import eu
.etaxonomy
.cdm
.common
.monitor
.IRestServiceProgressMonitor
;
31 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDPropertyEditor
;
32 import io
.swagger
.annotations
.Api
;
35 * @author Andreas Kohlbecker
39 @CrossOrigin(origins
="*")
40 @Api(value
="progress", description
="Provides access to information on long running processes. "
41 + "URIs to the resources exposed by this controller are provided in the responses to the"
42 + "HTTP requests that trigger long term processes.")
43 @RequestMapping(value
="/progress/")
44 public class ProgressMonitorController
{
47 private ProgressMonitorManager
<IRestServiceProgressMonitor
> progressMonitorManager
;
50 public void initBinder(WebDataBinder binder
) {
51 binder
.registerCustomEditor(UUID
.class, new UUIDPropertyEditor());
54 public UUID
registerMonitor(IRestServiceProgressMonitor monitor
){
55 return progressMonitorManager
.registerMonitor(monitor
);
58 public IRestServiceProgressMonitor
getMonitor(UUID uuid
) {
59 return progressMonitorManager
.getMonitor(uuid
);
63 * returns true if the {@link IRestServiceProgressMonitor} identified by the <code>uuid</code>
64 * exists and if it is still indicating a running thread
68 public boolean isMonitorRunning(UUID uuid
) {
69 return progressMonitorManager
.isMonitorRunning(uuid
);
73 * provides the relative path to the ProgressMonitor specified by its UUID.
74 * File extensions like .xml, .json used during the initial request will be
75 * preserved in order to not to break the content type negotiation.
78 * the request for which to create he path for. The file
79 * extension will be read from the servlet path and is appended
80 * to the resulting path.
82 * the uuid key of the monitor
83 * @return the path of the ProgressMonitor
85 public String
pathFor(HttpServletRequest request
, UUID uuid
){
86 String fileExtension
= FilenameUtils
.getExtension(request
.getServletPath());
87 return "/progress/" + uuid
.toString() + (fileExtension
.length() > 0 ?
'.': "") + fileExtension
;
90 @RequestMapping(value
= "{uuid}", method
= RequestMethod
.GET
)
91 public ModelAndView
doProgressMonitor(@PathVariable("uuid") UUID uuid
, HttpServletRequest request
, HttpServletResponse response
)
94 ModelAndView mv
= new ModelAndView();
95 Map
<UUID
, IRestServiceProgressMonitor
> monitors
= progressMonitorManager
.getMonitors();
96 if (monitors
.containsKey(uuid
)) {
97 mv
.addObject(monitors
.get(uuid
));
99 response
.sendError(404, "No such progress monitor found. The process being monitored may "
100 + "have been completed and the according monitor may have been removed by "
101 + "the clean up timeout.");