Project

General

Profile

Download (3.7 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.cdm.remote.controller;
11

    
12
import java.io.IOException;
13
import java.net.URI;
14
import java.util.Arrays;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import javax.servlet.http.HttpServletRequest;
21
import javax.servlet.http.HttpServletResponse;
22

    
23
import org.apache.http.HttpException;
24
import org.apache.log4j.Logger;
25
//import org.mortbay.log.Log;
26
import org.springframework.beans.factory.annotation.Autowired;
27
import org.springframework.stereotype.Controller;
28
import org.springframework.web.bind.annotation.PathVariable;
29
import org.springframework.web.bind.annotation.RequestMapping;
30
import org.springframework.web.bind.annotation.RequestMethod;
31
import org.springframework.web.servlet.ModelAndView;
32

    
33
import eu.etaxonomy.cdm.api.service.IMediaService;
34
import eu.etaxonomy.cdm.api.service.MediaServiceImpl;
35
import eu.etaxonomy.cdm.common.media.CdmImageInfo;
36
import eu.etaxonomy.cdm.model.media.Media;
37
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
38
import eu.etaxonomy.cdm.remote.exception.NoRecordsMatchException;
39
import io.swagger.annotations.Api;
40

    
41
/**
42
 * TODO write controller documentation
43
 *
44
 * @author a.kohlbecker
45
 * @since 24.03.2009
46
 */
47

    
48
@Controller
49
@Api("media")
50
@RequestMapping(value = {"/media/{uuid}"})
51
public class MediaController extends AbstractIdentifiableController<Media, IMediaService>{
52

    
53
    private static final Logger logger = Logger.getLogger(MediaController.class);
54

    
55
    @Autowired
56
    @Override
57
    public void setService(IMediaService service) {
58
        this.service = service;
59
    }
60
    private static final List<String> MEDIA_INIT_STRATEGY = Arrays.asList(new String []{
61
            "*",
62
            "representations",
63
            "representations.parts"
64
    });
65

    
66
    @RequestMapping(value = {"metadata"}, method = RequestMethod.GET)
67
    public ModelAndView doGetMediaMetaData(@PathVariable("uuid") UUID uuid,
68
            HttpServletRequest request, HttpServletResponse response) throws IOException {
69
        Map<String, String> result;
70

    
71
        ModelAndView mv = new ModelAndView();
72
        try{
73
            Media media = getCdmBaseInstance(uuid, response, MEDIA_INIT_STRATEGY);
74

    
75
            Set<MediaRepresentation> representations = media.getRepresentations();
76
            //get first representation and retrieve the according metadata
77

    
78
            MediaRepresentation mediaRepresentation = representations.iterator().next();
79
            URI uri = mediaRepresentation.getParts().get(0).getUri();
80
            try {
81
                    CdmImageInfo cdmImageInfo = CdmImageInfo.NewInstanceWithMetaData(uri, MediaServiceImpl.IMAGE_READ_TIMEOUT);
82
                    result = cdmImageInfo.getMetaData();
83

    
84
            } catch (HttpException e) {
85
                logger.info(e.getMessage());
86
                HttpStatusMessage.create("Reading media file from " + uri.toString() + " failed due to (" + e.getMessage() + ")", 400).send(response);
87
                return null;
88
            }
89
            if(result != null) {
90
                mv.addObject(result);
91
            }
92
        } catch (NoRecordsMatchException e){
93
           /* IGNORE */
94
           /* java.lang.IllegalStateException: STREAM is thrown by the servlet container
95
            * if the model and view is returned after the http error has been send
96
            * so we return null here
97
            */
98
            return null;
99

    
100
        }
101
        return mv;
102

    
103
    }
104

    
105
}
(31-31/75)