Project

General

Profile

Download (4.47 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
import org.springframework.web.bind.annotation.ResponseBody;
31

    
32
import eu.etaxonomy.cdm.api.service.IMediaService;
33
import eu.etaxonomy.cdm.api.service.media.MediaInfoFactory;
34
import eu.etaxonomy.cdm.common.URI;
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
@Controller
48
@Api("media")
49
@RequestMapping(value = {"/media/{uuid}"})
50
public class MediaController extends AbstractIdentifiableController<Media, IMediaService>{
51

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

    
54

    
55
    @Autowired
56
    private MediaInfoFactory mediaInfoFactory;
57

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

    
69

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

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

    
82
            Set<MediaRepresentation> representations = media.getRepresentations();
83
            if(media.getRepresentations().isEmpty()) {
84
                return null;
85
            }
86
            MediaRepresentation mediaRepresentation = representations.iterator().next();
87
            URI uri = null;
88
            try {
89
                if(applyFilterPreset) {
90
                   result = service.readResourceMetadataFiltered(mediaRepresentation);
91
                } else {
92
                    uri = mediaRepresentation.getParts().get(0).getUri();
93
                    CdmImageInfo cdmImageInfo = mediaInfoFactory.cdmImageInfoWithMetaData(uri);
94
                    result = cdmImageInfo.getMetaData();
95
                }
96
            } catch (IOException | HttpException e) {
97
                logger.info(e.getMessage());
98
                if(uri != null) {
99
                    // may happen when applyFilterPreset == false
100
                    HttpStatusMessage.create("Reading media file from " + uri.toString() + " failed due to (" + e.getMessage() + ")", 400).send(response);
101
                } else {
102
                    // may happen when applyFilterPreset == true
103
                    HttpStatusMessage.create("Reading media file failed due to (" + e.getMessage() + ")", 400).send(response);
104
                }
105
                return null;
106
            }
107

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

    
116
        }
117
        return result;
118

    
119
    }
120

    
121
}
(31-31/76)