Project

General

Profile

Download (3.98 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 io.swagger.annotations.Api;
12

    
13
import java.io.IOException;
14
import java.util.Map;
15
import java.util.UUID;
16

    
17
import javax.servlet.http.HttpServletRequest;
18
import javax.servlet.http.HttpServletResponse;
19

    
20
import org.apache.commons.io.FilenameUtils;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.stereotype.Controller;
23
import org.springframework.web.bind.WebDataBinder;
24
import org.springframework.web.bind.annotation.InitBinder;
25
import org.springframework.web.bind.annotation.PathVariable;
26
import org.springframework.web.bind.annotation.RequestMapping;
27
import org.springframework.web.bind.annotation.RequestMethod;
28
import org.springframework.web.servlet.ModelAndView;
29

    
30
import eu.etaxonomy.cdm.api.service.ProgressMonitorManager;
31
import eu.etaxonomy.cdm.common.monitor.IRestServiceProgressMonitor;
32
import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
33

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

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

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

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

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

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

    
73
    /**
74
     * provides the relative path to the ProgressMonitor specified by its UUID.
75
     * File extensions like .xml, .json used during the initial request will be
76
     * preserved in order to not to break the content type negotiation.
77
     *
78
     * @param request
79
     *            the request for which to create he path for. The file
80
     *            extension will be read from the servlet path and is appended
81
     *            to the resulting path.
82
     * @param uuid
83
     *            the uuid key of the monitor
84
     * @return the path of the ProgressMonitor
85
     */
86
    public String pathFor(HttpServletRequest request, UUID uuid){
87
        String fileExtension = FilenameUtils.getExtension(request.getServletPath());
88
        return "/progress/" + uuid.toString() + (fileExtension.length() > 0 ? '.': "") + fileExtension;
89
    }
90

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

    
95
        ModelAndView mv = new ModelAndView();
96
        Map<UUID, IRestServiceProgressMonitor> monitors = progressMonitorManager.getMonitors();
97
        if (monitors.containsKey(uuid)) {
98
            mv.addObject(monitors.get(uuid));
99
        } else {
100
            response.sendError(404, "No such progress monitor found. The process being monitored may "
101
                    + "have been completed and the according monitor may have been removed due to "
102
                    + "the clean up timepout.");
103
        }
104

    
105
        return mv;
106
    }
107
}
(50-50/66)