loading annotations with distribution elements - #3348 (should annotations always...
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / MediaController.java
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 java.io.IOException;
14 import java.net.URI;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.UUID;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.apache.http.HttpException;
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.servlet.ModelAndView;
30
31 import eu.etaxonomy.cdm.api.service.IMediaService;
32 import eu.etaxonomy.cdm.common.media.ImageInfo;
33 import eu.etaxonomy.cdm.model.media.Media;
34 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
35 import eu.etaxonomy.cdm.remote.exception.NoRecordsMatchException;
36
37 /**
38 * TODO write controller documentation
39 *
40 * @author a.kohlbecker
41 * @date 24.03.2009
42 */
43
44 @Controller
45 @RequestMapping(value = {"/media/{uuid}"})
46 public class MediaController extends BaseController<Media, IMediaService>
47 {
48
49 /* (non-Javadoc)
50 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
51 */
52 @Autowired
53 @Override
54 public void setService(IMediaService service) {
55 this.service = service;
56 }
57 private static final List<String> MEDIA_INIT_STRATEGY = Arrays.asList(new String []{
58 "*",
59 "representations",
60 "representations.parts"
61 });
62
63 @RequestMapping(value = {"metadata"})
64 public ModelAndView doGetMediaMetaData(@PathVariable("uuid") UUID uuid,
65 HttpServletRequest request, HttpServletResponse response) throws IOException {
66 Map<String, String> result;
67
68 ModelAndView mv = new ModelAndView();
69 try{
70 Media media = getCdmBaseInstance(uuid, response, MEDIA_INIT_STRATEGY);
71
72 Set<MediaRepresentation> representations = media.getRepresentations();
73 //get first representation and retrieve the according metadata
74
75 Object[] repArray = representations.toArray();
76 Object mediaRep = repArray[0];
77 URI uri = null;
78 try {
79 if (mediaRep instanceof MediaRepresentation){
80 MediaRepresentation medRep = (MediaRepresentation) mediaRep;
81 uri = medRep.getParts().get(0).getUri();
82 ImageInfo imageInfo = ImageInfo.NewInstanceWithMetaData(uri, 3000);
83 result = imageInfo.getMetaData();
84 if(result != null) {
85 mv.addObject(result);
86 }
87 }
88 } catch (HttpException e) {
89 HttpStatusMessage.fromString("Reading media file from " + uri.toString() + " failed").setStatusCode(400).send(response);
90 }
91 } catch (NoRecordsMatchException e){
92 /* IGNORE */
93 /* java.lang.IllegalStateException: STREAM is thrown by the servlet container
94 * if the model and view is returned after the http error has been send
95 * so we return null here
96 */
97 return null;
98
99 }
100 return mv;
101
102 }
103
104 }