1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.remote.controller;
|
12
|
|
13
|
import io.swagger.annotations.Api;
|
14
|
|
15
|
import java.io.IOException;
|
16
|
import java.net.URI;
|
17
|
import java.util.Arrays;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
import java.util.Set;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import javax.servlet.http.HttpServletRequest;
|
24
|
import javax.servlet.http.HttpServletResponse;
|
25
|
|
26
|
import org.apache.http.HttpException;
|
27
|
//import org.mortbay.log.Log;
|
28
|
import org.springframework.beans.factory.annotation.Autowired;
|
29
|
import org.springframework.stereotype.Controller;
|
30
|
import org.springframework.web.bind.annotation.PathVariable;
|
31
|
import org.springframework.web.bind.annotation.RequestMapping;
|
32
|
import org.springframework.web.bind.annotation.RequestMethod;
|
33
|
import org.springframework.web.servlet.ModelAndView;
|
34
|
|
35
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
36
|
import eu.etaxonomy.cdm.common.media.ImageInfo;
|
37
|
import eu.etaxonomy.cdm.model.media.Media;
|
38
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
39
|
import eu.etaxonomy.cdm.remote.exception.NoRecordsMatchException;
|
40
|
|
41
|
/**
|
42
|
* TODO write controller documentation
|
43
|
*
|
44
|
* @author a.kohlbecker
|
45
|
* @date 24.03.2009
|
46
|
*/
|
47
|
|
48
|
@Controller
|
49
|
@Api("media")
|
50
|
@RequestMapping(value = {"/media/{uuid}"})
|
51
|
public class MediaController extends BaseController<Media, IMediaService>
|
52
|
{
|
53
|
|
54
|
/* (non-Javadoc)
|
55
|
* @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
|
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
|
@RequestMapping(value = {"metadata"}, method = RequestMethod.GET)
|
69
|
public ModelAndView doGetMediaMetaData(@PathVariable("uuid") UUID uuid,
|
70
|
HttpServletRequest request, HttpServletResponse response) throws IOException {
|
71
|
Map<String, String> result;
|
72
|
|
73
|
ModelAndView mv = new ModelAndView();
|
74
|
try{
|
75
|
Media media = getCdmBaseInstance(uuid, response, MEDIA_INIT_STRATEGY);
|
76
|
|
77
|
Set<MediaRepresentation> representations = media.getRepresentations();
|
78
|
//get first representation and retrieve the according metadata
|
79
|
|
80
|
Object[] repArray = representations.toArray();
|
81
|
Object mediaRep = repArray[0];
|
82
|
URI uri = null;
|
83
|
try {
|
84
|
if (mediaRep instanceof MediaRepresentation){
|
85
|
MediaRepresentation medRep = (MediaRepresentation) mediaRep;
|
86
|
uri = medRep.getParts().get(0).getUri();
|
87
|
ImageInfo imageInfo = ImageInfo.NewInstanceWithMetaData(uri, 3000);
|
88
|
result = imageInfo.getMetaData();
|
89
|
if(result != null) {
|
90
|
mv.addObject(result);
|
91
|
}
|
92
|
}
|
93
|
} catch (HttpException e) {
|
94
|
logger.info(e.getMessage());
|
95
|
HttpStatusMessage.create("Reading media file from " + uri.toString() + " failed due to (" + e.getMessage() + ")", 400).send(response);
|
96
|
return null;
|
97
|
}
|
98
|
} catch (NoRecordsMatchException e){
|
99
|
/* IGNORE */
|
100
|
/* java.lang.IllegalStateException: STREAM is thrown by the servlet container
|
101
|
* if the model and view is returned after the http error has been send
|
102
|
* so we return null here
|
103
|
*/
|
104
|
return null;
|
105
|
|
106
|
}
|
107
|
return mv;
|
108
|
|
109
|
}
|
110
|
|
111
|
}
|