Project

General

Profile

Download (3.99 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2012 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.remote.controller;
11

    
12
import io.swagger.annotations.Api;
13

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

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

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

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

    
35
/**
36
 * @author Andreas Kohlbecker
37
 * @date Jul 16, 2012
38
 *
39
 */
40
@Controller
41
@Api(value="progress", description="provides access to information on long running processes. "
42
        + "URIs to the resources exposed by this controller are provided in the responses to the"
43
        + "HTTP requests that trigger long term processes.")
44
@RequestMapping(value="/progress/")
45
public class ProgressMonitorController {
46

    
47

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

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

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

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

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

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

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

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

    
106
        return mv;
107
    }
108
}
(49-49/63)