2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.remote
.controller
;
11 import java
.io
.IOException
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
16 import java
.util
.UUID
;
18 import javax
.servlet
.http
.HttpServletRequest
;
19 import javax
.servlet
.http
.HttpServletResponse
;
21 import org
.apache
.http
.HttpException
;
22 import org
.apache
.logging
.log4j
.LogManager
;
23 import org
.apache
.logging
.log4j
.Logger
;
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
.bind
.annotation
.RequestParam
;
31 import org
.springframework
.web
.bind
.annotation
.ResponseBody
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IMediaService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.media
.MediaInfoFactory
;
35 import eu
.etaxonomy
.cdm
.common
.URI
;
36 import eu
.etaxonomy
.cdm
.common
.media
.CdmImageInfo
;
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 import io
.swagger
.annotations
.Api
;
43 * TODO write controller documentation
45 * @author a.kohlbecker
50 @RequestMapping(value
= {"/media/{uuid}"})
51 public class MediaController
extends AbstractIdentifiableController
<Media
, IMediaService
>{
53 private static final Logger logger
= LogManager
.getLogger();
56 private MediaInfoFactory mediaInfoFactory
;
60 public void setService(IMediaService service
) {
61 this.service
= service
;
63 private static final List
<String
> MEDIA_INIT_STRATEGY
= Arrays
.asList(new String
[]{
66 "representations.parts"
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
{
78 Map
<String
, String
> result
;
80 Media media
= getCdmBaseInstance(uuid
, response
, MEDIA_INIT_STRATEGY
);
82 Set
<MediaRepresentation
> representations
= media
.getRepresentations();
83 if(media
.getRepresentations().isEmpty()) {
86 MediaRepresentation mediaRepresentation
= representations
.iterator().next();
89 if(applyFilterPreset
) {
90 result
= service
.readResourceMetadataFiltered(mediaRepresentation
);
92 uri
= mediaRepresentation
.getParts().get(0).getUri();
93 CdmImageInfo cdmImageInfo
= mediaInfoFactory
.cdmImageInfo(uri
, true);
94 result
= cdmImageInfo
.getMetaData();
96 } catch (IOException
| HttpException e
) {
97 logger
.info(e
.getMessage());
99 // may happen when applyFilterPreset == false
100 HttpStatusMessage
.create("Reading media file from " + uri
.toString() + " failed due to (" + e
.getMessage() + ")", 400).send(response
);
102 // may happen when applyFilterPreset == true
103 HttpStatusMessage
.create("Reading media file failed due to (" + e
.getMessage() + ")", 400).send(response
);
108 } catch (NoRecordsMatchException e
){
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