Project

General

Profile

Download (4.35 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
package eu.etaxonomy.cdm.remote.controller;
10

    
11
import java.io.IOException;
12
import java.util.Arrays;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16
import java.util.UUID;
17

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

    
21
import org.apache.http.HttpException;
22
import org.apache.log4j.Logger;
23
//import org.mortbay.log.Log;
24
import org.springframework.beans.factory.annotation.Autowired;
25
import org.springframework.stereotype.Controller;
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.bind.annotation.RequestParam;
30

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

    
40
/**
41
 * TODO write controller documentation
42
 *
43
 * @author a.kohlbecker
44
 * @since 24.03.2009
45
 */
46
@Controller
47
@Api("media")
48
@RequestMapping(value = {"/media/{uuid}"})
49
public class MediaController extends AbstractIdentifiableController<Media, IMediaService>{
50

    
51
    private static final Logger logger = Logger.getLogger(MediaController.class);
52

    
53

    
54
    @Autowired
55
    private MediaInfoFactory mediaInfoFactory;
56

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

    
68

    
69
    @RequestMapping(value = {"metadata"}, method = RequestMethod.GET)
70
    public Map<String, String> doGetMediaMetaData(
71
            @PathVariable("uuid") UUID uuid,
72
            @RequestParam(value = "applyFilterPreset", defaultValue = "true") Boolean applyFilterPreset,
73
            HttpServletRequest request,
74
            HttpServletResponse response) throws IOException {
75

    
76
        Map<String, String> result;
77
        try{
78
            Media media = getCdmBaseInstance(uuid, response, MEDIA_INIT_STRATEGY);
79

    
80
            Set<MediaRepresentation> representations = media.getRepresentations();
81
            if(media.getRepresentations().isEmpty()) {
82
                return null;
83
            }
84
            MediaRepresentation mediaRepresentation = representations.iterator().next();
85
            URI uri = null;
86
            try {
87
                if(applyFilterPreset) {
88
                        result = service.readResourceMetadataFiltered(mediaRepresentation);
89

    
90
                } else {
91
                    uri = mediaRepresentation.getParts().get(0).getUri();
92
                    CdmImageInfo cdmImageInfo = mediaInfoFactory.cdmImageInfoWithMetaData(uri);
93
                    result = cdmImageInfo.getMetaData();
94
                }
95
            } catch (IOException | HttpException e) {
96
                logger.info(e.getMessage());
97
                if(uri != null) {
98
                    // may happen when applyFilterPreset == false
99
                    HttpStatusMessage.create("Reading media file from " + uri.toString() + " failed due to (" + e.getMessage() + ")", 400).send(response);
100
                } else {
101
                    // may happen when applyFilterPreset == true
102
                    HttpStatusMessage.create("Reading media file failed due to (" + e.getMessage() + ")", 400).send(response);
103
                }
104
                return null;
105
            }
106

    
107
        } catch (NoRecordsMatchException e){
108
           /* IGNORE */
109
           /* java.lang.IllegalStateException: STREAM is thrown by the servlet container
110
            * if the model and view is returned after the http error has been send
111
            * so we return null here
112
            */
113
            return null;
114

    
115
        }
116
        return result;
117

    
118
    }
119

    
120
}
(31-31/76)