Project

General

Profile

Download (4.05 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2012 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9
package eu.etaxonomy.cdm.remote.controller;
10

    
11
import java.io.IOException;
12
import java.util.Map;
13
import java.util.UUID;
14

    
15
import javax.servlet.http.HttpServletRequest;
16
import javax.servlet.http.HttpServletResponse;
17

    
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;
28

    
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;
33

    
34
/**
35
 * @author Andreas Kohlbecker
36
 * @since Jul 16, 2012
37
 */
38
@Controller
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 {
45

    
46
    @Autowired
47
    private ProgressMonitorManager<IRestServiceProgressMonitor> progressMonitorManager;
48

    
49
    @InitBinder
50
    public void initBinder(WebDataBinder binder) {
51
        binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
52
    }
53

    
54
    public UUID registerMonitor(IRestServiceProgressMonitor monitor){
55
        return progressMonitorManager.registerMonitor(monitor);
56
    }
57

    
58
    public IRestServiceProgressMonitor getMonitor(UUID uuid) {
59
        return progressMonitorManager.getMonitor(uuid);
60
    }
61

    
62
    /**
63
     * returns true if the {@link IRestServiceProgressMonitor} identified by the <code>uuid</code>
64
     * exists and if it is still indicating a running thread
65
     * @param uuid
66
     * @return
67
     */
68
    public boolean isMonitorRunning(UUID uuid) {
69
        return progressMonitorManager.isMonitorRunning(uuid);
70
    }
71

    
72
    /**
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.
76
     *
77
     * @param request
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.
81
     * @param uuid
82
     *            the uuid key of the monitor
83
     * @return the path of the ProgressMonitor
84
     */
85
    public String pathFor(HttpServletRequest request, UUID uuid){
86
        String fileExtension = FilenameUtils.getExtension(request.getServletPath());
87
        return "/progress/" + uuid.toString() + (fileExtension.length() > 0 ? '.': "") + fileExtension;
88
    }
89

    
90
    @RequestMapping(value = "{uuid}", method = RequestMethod.GET)
91
    public ModelAndView doProgressMonitor(@PathVariable("uuid") UUID uuid, HttpServletRequest request, HttpServletResponse response)
92
            throws IOException {
93

    
94
        ModelAndView mv = new ModelAndView();
95
        Map<UUID, IRestServiceProgressMonitor> monitors = progressMonitorManager.getMonitors();
96
        if (monitors.containsKey(uuid)) {
97
            mv.addObject(monitors.get(uuid));
98
        } else {
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.");
102
        }
103

    
104
        return mv;
105
    }
106
}
(50-50/76)