cleanup
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / MediaController.java
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.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;
32
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;
41
42 /**
43 * TODO write controller documentation
44 *
45 * @author a.kohlbecker
46 * @since 24.03.2009
47 */
48 @Controller
49 @Api("media")
50 @RequestMapping(value = {"/media/{uuid}"})
51 public class MediaController extends AbstractIdentifiableController<Media, IMediaService>{
52
53 private static final Logger logger = LogManager.getLogger();
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.cdmImageInfo(uri, true);
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 }