make observer list modifiable #3825 (task1)
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / service / MetadataController.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 package eu.etaxonomy.cdm.remote.service;
11
12 import java.util.StringTokenizer;
13 import java.util.Vector;
14
15 import eu.etaxonomy.cdm.api.service.lsid.LSIDMetadataService;
16 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
17 import eu.etaxonomy.cdm.model.common.LSID;
18 import eu.etaxonomy.cdm.remote.editor.LSIDPropertyEditor;
19
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.stereotype.Controller;
22 import org.springframework.web.bind.WebDataBinder;
23 import org.springframework.web.bind.annotation.InitBinder;
24 import org.springframework.web.bind.annotation.RequestMapping;
25 import org.springframework.web.bind.annotation.RequestParam;
26 import org.springframework.web.servlet.ModelAndView;
27
28 import com.ibm.lsid.LSIDException;
29 import com.ibm.lsid.MetadataResponse;
30 import com.ibm.lsid.server.LSIDServerException;
31
32 /**
33 * Controller which accepts requests for the metadata representation of an object
34 * with a given lsid.
35 *
36 * @author ben
37 * @author Ben Szekely (<a href="mailto:bhszekel@us.ibm.com">bhszekel@us.ibm.com</a>)
38 * @see com.ibm.lsid.server.servlet.MetadataServlet
39 */
40 @Controller
41 public class MetadataController {
42
43 private LSIDMetadataService lsidMetadataService;
44
45 @Autowired
46 public void setLsidMetadataService(LSIDMetadataService lsidMetadataService) {
47 this.lsidMetadataService = lsidMetadataService;
48 }
49
50 @InitBinder
51 public void initBinder(WebDataBinder binder) {
52 binder.registerCustomEditor(LSID.class, new LSIDPropertyEditor());
53 }
54
55 /**
56 * Handle requests for the metadata representation of an object with a given lsid. Will return metadata in any format supported
57 * from a list of formats if specified.
58 *
59 * @param lsid the lsid to get metadata for
60 * @param formats a comma separated list of acceptable formats to the client
61 * @return ModelAndView containing the metadata response as an object with key 'metadataResponse', view name 'Metadata.rdf'
62 * @throws LSIDServerException
63 */
64 @RequestMapping(value = "/authority/metadata.do", params = "lsid")
65 public ModelAndView getMetadata(@RequestParam("lsid") LSID lsid,
66 @RequestParam(value = "acceptedFormats", required = false) String formats) throws LSIDServerException {
67 String[] acceptedFormats = null;
68 if (formats != null) {
69 StringTokenizer st = new StringTokenizer(formats,",",false);
70 Vector<String> v = new Vector<String>();
71 while (st.hasMoreTokens()) {
72 v.add(st.nextToken());
73 }
74 acceptedFormats = new String[v.size()];
75 v.toArray(acceptedFormats);
76 }
77
78 if (acceptedFormats != null) {
79 boolean found = false;
80 for (int i=0;i<acceptedFormats.length;i++) {
81 if (acceptedFormats[i].equals(MetadataResponse.RDF_FORMAT ))
82 found = true;
83 break;
84 }
85 if (!found) {
86 throw new LSIDServerException(LSIDServerException.NO_METADATA_AVAILABLE_FOR_FORMATS,"No metadata found for given format");
87 }
88 }
89
90 IIdentifiableEntity identifiableEntity = lsidMetadataService.getMetadata(lsid);
91 ModelAndView modelAndView = new ModelAndView("Metadata.rdf");
92 modelAndView.addObject(identifiableEntity);
93 return modelAndView;
94 }
95
96 @RequestMapping(value = "/authority/metadata.do", params = "!lsid")
97 public ModelAndView getMetadata() throws LSIDException {
98 throw new LSIDException(LSIDException.INVALID_METHOD_CALL, "Must specify HTTP Parameter 'lsid'");
99 }
100
101 }