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.
10 package eu
.etaxonomy
.cdm
.remote
.controller
;
12 import java
.io
.IOException
;
13 import eu
.etaxonomy
.cdm
.common
.URI
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import javax
.servlet
.http
.HttpServletRequest
;
21 import javax
.servlet
.http
.HttpServletResponse
;
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
;
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
;
42 * TODO write controller documentation
44 * @author a.kohlbecker
50 @RequestMapping(value
= {"/media/{uuid}"})
51 public class MediaController
extends AbstractIdentifiableController
<Media
, IMediaService
>{
53 private static final Logger logger
= Logger
.getLogger(MediaController
.class);
57 public void setService(IMediaService service
) {
58 this.service
= service
;
60 private static final List
<String
> MEDIA_INIT_STRATEGY
= Arrays
.asList(new String
[]{
63 "representations.parts"
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
{
73 Map
<String
, String
> result
;
75 Media media
= getCdmBaseInstance(uuid
, response
, MEDIA_INIT_STRATEGY
);
77 Set
<MediaRepresentation
> representations
= media
.getRepresentations();
78 if(media
.getRepresentations().isEmpty()) {
81 MediaRepresentation mediaRepresentation
= representations
.iterator().next();
84 if(applyFilterPreset
) {
85 result
= service
.readResourceMetadataFiltered(mediaRepresentation
);
88 uri
= mediaRepresentation
.getParts().get(0).getUri();
89 CdmImageInfo cdmImageInfo
= CdmImageInfo
.NewInstanceWithMetaData(uri
, MediaServiceImpl
.IMAGE_READ_TIMEOUT
);
90 result
= cdmImageInfo
.getMetaData();
92 } catch (IOException
| HttpException e
) {
93 logger
.info(e
.getMessage());
95 // may happen when applyFilterPreset == false
96 HttpStatusMessage
.create("Reading media file from " + uri
.toString() + " failed due to (" + e
.getMessage() + ")", 400).send(response
);
98 // may happen when applyFilterPreset == true
99 HttpStatusMessage
.create("Reading media file failed due to (" + e
.getMessage() + ")", 400).send(response
);
104 } catch (NoRecordsMatchException e
){
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