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.mortbay.log.Log;
25
import org.springframework.beans.factory.annotation.Autowired;
26
import org.springframework.stereotype.Controller;
27
import org.springframework.web.bind.annotation.PathVariable;
28
import org.springframework.web.bind.annotation.RequestMapping;
29
import org.springframework.web.bind.annotation.RequestMethod;
30
import org.springframework.web.servlet.ModelAndView;
31

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

    
39
/**
40
 * TODO write controller documentation
41
 *
42
 * @author a.kohlbecker
43
 * @date 24.03.2009
44
 */
45

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

    
51

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

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

    
68
        ModelAndView mv = new ModelAndView();
69
        try{
70
            Media media = getCdmBaseInstance(uuid, response, MEDIA_INIT_STRATEGY);
71

    
72
            Set<MediaRepresentation> representations = media.getRepresentations();
73
            //get first representation and retrieve the according metadata
74

    
75
            Object[] repArray = representations.toArray();
76
            Object mediaRep = repArray[0];
77
            URI uri = null;
78
            try {
79
                if (mediaRep instanceof MediaRepresentation){
80
                    MediaRepresentation medRep = (MediaRepresentation) mediaRep;
81
                    uri = medRep.getParts().get(0).getUri();
82
                    ImageInfo imageInfo = ImageInfo.NewInstanceWithMetaData(uri, 3000);
83
                    result = imageInfo.getMetaData();
84
                    if(result != null) {
85
                        mv.addObject(result);
86
                    }
87
                }
88
            } catch (HttpException e) {
89
                logger.info(e.getMessage());
90
                HttpStatusMessage.create("Reading media file from " + uri.toString() + " failed due to (" + e.getMessage() + ")", 400).send(response);
91
                return null;
92
            }
93
        } catch (NoRecordsMatchException e){
94
           /* IGNORE */
95
           /* java.lang.IllegalStateException: STREAM is thrown by the servlet container
96
            * if the model and view is returned after the http error has been send
97
            * so we return null here
98
            */
99
            return null;
100

    
101
        }
102
        return mv;
103

    
104
    }
105

    
106
}
(35-35/66)