-// $Id$\r
/**\r
* Copyright (C) 2007 EDIT\r
-* European Distributed Institute of Taxonomy \r
+* European Distributed Institute of Taxonomy\r
* http://www.e-taxonomy.eu\r
-* \r
+*\r
* The contents of this file are subject to the Mozilla Public License Version 1.1\r
* See LICENSE.TXT at the top of this package for the full license terms.\r
*/\r
-\r
package eu.etaxonomy.cdm.remote.controller;\r
\r
import java.io.IOException;\r
-import java.net.URI;\r
import java.util.Arrays;\r
import java.util.List;\r
import java.util.Map;\r
import javax.servlet.http.HttpServletResponse;\r
\r
import org.apache.http.HttpException;\r
+import org.apache.log4j.Logger;\r
+//import org.mortbay.log.Log;\r
import org.springframework.beans.factory.annotation.Autowired;\r
import org.springframework.stereotype.Controller;\r
import org.springframework.web.bind.annotation.PathVariable;\r
import org.springframework.web.bind.annotation.RequestMapping;\r
-import org.springframework.web.servlet.ModelAndView;\r
+import org.springframework.web.bind.annotation.RequestMethod;\r
+import org.springframework.web.bind.annotation.RequestParam;\r
\r
import eu.etaxonomy.cdm.api.service.IMediaService;\r
-import eu.etaxonomy.cdm.common.media.ImageInfo;\r
+import eu.etaxonomy.cdm.api.service.media.MediaInfoFactory;\r
+import eu.etaxonomy.cdm.common.URI;\r
+import eu.etaxonomy.cdm.common.media.CdmImageInfo;\r
import eu.etaxonomy.cdm.model.media.Media;\r
import eu.etaxonomy.cdm.model.media.MediaRepresentation;\r
+import eu.etaxonomy.cdm.remote.exception.NoRecordsMatchException;\r
+import io.swagger.annotations.Api;\r
\r
/**\r
* TODO write controller documentation\r
- * \r
+ *\r
* @author a.kohlbecker\r
- * @date 24.03.2009\r
+ * @since 24.03.2009\r
*/\r
-\r
@Controller\r
+@Api("media")\r
@RequestMapping(value = {"/media/{uuid}"})\r
-public class MediaController extends AnnotatableController<Media, IMediaService>\r
-{\r
- \r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)\r
- */\r
- @Autowired\r
- @Override\r
- public void setService(IMediaService service) {\r
- this.service = service;\r
- }\r
- private static final List<String> MEDIA_INIT_STRATEGY = Arrays.asList(new String []{\r
- "*",\r
- "representations",\r
- "representations.parts"\r
- });\r
- \r
- @RequestMapping(value = {"metadata"})\r
- public ModelAndView doGetMediaMetaData(@PathVariable("uuid") UUID uuid,\r
- HttpServletRequest request, HttpServletResponse response) throws IOException {\r
- Map<String, String> result;\r
- Media media = getCdmBaseInstance(uuid, response, MEDIA_INIT_STRATEGY);\r
- \r
- Set<MediaRepresentation> representations = media.getRepresentations();\r
- //get first representation and retrieve the according metadata\r
-\r
- Object[] repArray = representations.toArray();\r
- Object mediaRep = repArray[0];\r
- ModelAndView mv = new ModelAndView();\r
- try {\r
- if (mediaRep instanceof MediaRepresentation){\r
- MediaRepresentation medRep = (MediaRepresentation) mediaRep;\r
- URI uri = medRep.getParts().get(0).getUri();\r
- ImageInfo imageInfo = ImageInfo.NewInstance(uri, 3000);\r
- imageInfo.readMetaData(3000);\r
- result = imageInfo.getMetaData();\r
- mv.addObject(result);\r
- }\r
- } catch (HttpException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- }\r
- return mv;\r
- \r
- }\r
+public class MediaController extends AbstractIdentifiableController<Media, IMediaService>{\r
+\r
+ private static final Logger logger = Logger.getLogger(MediaController.class);\r
+\r
+\r
+ @Autowired\r
+ private MediaInfoFactory mediaInfoFactory;\r
+\r
+ @Autowired\r
+ @Override\r
+ public void setService(IMediaService service) {\r
+ this.service = service;\r
+ }\r
+ private static final List<String> MEDIA_INIT_STRATEGY = Arrays.asList(new String []{\r
+ "*",\r
+ "representations",\r
+ "representations.parts"\r
+ });\r
+\r
+\r
+ @RequestMapping(value = {"metadata"}, method = RequestMethod.GET)\r
+ public Map<String, String> doGetMediaMetaData(\r
+ @PathVariable("uuid") UUID uuid,\r
+ @RequestParam(value = "applyFilterPreset", defaultValue = "true") Boolean applyFilterPreset,\r
+ HttpServletRequest request,\r
+ HttpServletResponse response) throws IOException {\r
+\r
+ Map<String, String> result;\r
+ try{\r
+ Media media = getCdmBaseInstance(uuid, response, MEDIA_INIT_STRATEGY);\r
+\r
+ Set<MediaRepresentation> representations = media.getRepresentations();\r
+ if(media.getRepresentations().isEmpty()) {\r
+ return null;\r
+ }\r
+ MediaRepresentation mediaRepresentation = representations.iterator().next();\r
+ URI uri = null;\r
+ try {\r
+ if(applyFilterPreset) {\r
+ result = service.readResourceMetadataFiltered(mediaRepresentation);\r
+\r
+ } else {\r
+ uri = mediaRepresentation.getParts().get(0).getUri();\r
+ CdmImageInfo cdmImageInfo = mediaInfoFactory.cdmImageInfoWithMetaData(uri);\r
+ result = cdmImageInfo.getMetaData();\r
+ }\r
+ } catch (IOException | HttpException e) {\r
+ logger.info(e.getMessage());\r
+ if(uri != null) {\r
+ // may happen when applyFilterPreset == false\r
+ HttpStatusMessage.create("Reading media file from " + uri.toString() + " failed due to (" + e.getMessage() + ")", 400).send(response);\r
+ } else {\r
+ // may happen when applyFilterPreset == true\r
+ HttpStatusMessage.create("Reading media file failed due to (" + e.getMessage() + ")", 400).send(response);\r
+ }\r
+ return null;\r
+ }\r
+\r
+ } catch (NoRecordsMatchException e){\r
+ /* IGNORE */\r
+ /* java.lang.IllegalStateException: STREAM is thrown by the servlet container\r
+ * if the model and view is returned after the http error has been send\r
+ * so we return null here\r
+ */\r
+ return null;\r
+\r
+ }\r
+ return result;\r
+\r
+ }\r
\r
}\r