Project

General

Profile

Download (4.33 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 eu.etaxonomy.cdm.common.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.bind.annotation.RequestParam;
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 Map<String, String> doGetMediaMetaData(
68
            @PathVariable("uuid") UUID uuid,
69
            @RequestParam(value = "applyFilterPreset", defaultValue = "true") Boolean applyFilterPreset,
70
            HttpServletRequest request,
71
            HttpServletResponse response) throws IOException {
72

    
73
        Map<String, String> result;
74
        try{
75
            Media media = getCdmBaseInstance(uuid, response, MEDIA_INIT_STRATEGY);
76

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

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

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

    
112
        }
113
        return result;
114

    
115
    }
116

    
117
}
(31-31/76)