make observer list modifiable #3825 (task1)
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / service / DataController.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.io.IOException;
13 import java.io.InputStream;
14 import java.io.OutputStream;
15
16 import javax.servlet.http.HttpServletResponse;
17
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.stereotype.Controller;
20 import org.springframework.web.bind.WebDataBinder;
21 import org.springframework.web.bind.annotation.InitBinder;
22 import org.springframework.web.bind.annotation.RequestMapping;
23 import org.springframework.web.bind.annotation.RequestParam;
24 import org.springframework.web.servlet.ModelAndView;
25
26 import com.ibm.lsid.LSIDException;
27 import com.ibm.lsid.server.LSIDServerException;
28
29 import eu.etaxonomy.cdm.api.service.lsid.LSIDDataService;
30 import eu.etaxonomy.cdm.model.common.LSID;
31 import eu.etaxonomy.cdm.remote.editor.LSIDPropertyEditor;
32
33 /**
34 * Controller which accepts requests for the data representation of an object
35 * with a given lsid. The response is written directly into the request, rather
36 * than being passed as part of the ModelAndView since data is supposed to be
37 * byte-identical and thus cannot be transformed by the view layer.
38 *
39 * @author ben
40 * @author Ben Szekely (<a href="mailto:bhszekel@us.ibm.com">bhszekel@us.ibm.com</a>)
41 * @see com.ibm.lsid.server.servlet.DataServlet
42 */
43 @Controller
44 public class DataController {
45
46 private LSIDDataService lsidDataService;
47
48 @Autowired
49 public void setLsidDataService(LSIDDataService lsidDataService) {
50 this.lsidDataService = lsidDataService;
51 }
52
53 @InitBinder
54 public void initBinder(WebDataBinder binder) {
55 binder.registerCustomEditor(LSID.class, new LSIDPropertyEditor());
56 }
57
58 /**
59 * Handle requests for the data representation of an object with a given lsid. Can return only part of the
60 * data if the length and offset are specified in the request as per the specification.
61 *
62 * @param LSID lsid the lsid to retrieve data for
63 * @param Integer start the offset in bytes to read from
64 * @param Integer length the number of bytes to return
65 * @return ModelAndView (null)
66 * @throws LSIDServerException
67 * @throws IOException
68 */
69 @RequestMapping(value = "/authority/data.do",params = {"lsid","start","length"})
70 public ModelAndView getData(@RequestParam("lsid") LSID lsid,
71 @RequestParam("start") Integer start,
72 @RequestParam("length") Integer length,
73 HttpServletResponse response) throws LSIDServerException, IOException {
74 //FIXME #3811 fix null pointer access of "out" reference
75 // OutputStream out = null;
76 // InputStream data = null;
77 // try {
78 // data = lsidDataService.getDataByRange(lsid,start,length);
79 // if(data != null) {
80 // response.setContentType("application/octet-stream");
81 // byte[] bytes = new byte[1024];
82 // int numbytes = data.read(bytes);
83 // while (numbytes != -1) {
84 // out.write(bytes,0,numbytes);
85 // numbytes = data.read(bytes);
86 // }
87 // out.flush();
88 // }
89 // } finally {
90 // if (out != null) {
91 // out.close();
92 // }
93 //
94 // if (data != null) {
95 // data.close();
96 // }
97 // }
98 return null;
99 }
100
101 /**
102 * Handle requests for the data representation of an object with a given lsid.
103 *
104 * @param LSID lsid the lsid to retrieve data for
105 * @return ModelAndView (null)
106 * @throws LSIDServerException
107 * @throws IOException
108 */
109 @RequestMapping(value = "/authority/data.do",params = {"lsid"})
110 public ModelAndView getData(@RequestParam("lsid")LSID lsid,
111 HttpServletResponse response) throws LSIDServerException, IOException {
112 OutputStream out = null;
113 InputStream data = null;
114 try {
115 data = lsidDataService.getData(lsid);
116 if(data != null) {
117 response.setContentType("application/octet-stream");
118 out = response.getOutputStream();
119 byte[] bytes = new byte[1024];
120 int numbytes = data.read(bytes);
121 while (numbytes != -1) {
122 out.write(bytes,0,numbytes);
123 numbytes = data.read(bytes);
124 }
125 out.flush();
126 }
127 } finally {
128 if (out != null) {
129 out.close();
130 }
131
132 if (data != null) {
133 data.close();
134 }
135 }
136 return null;
137 }
138
139 /**
140 * Handle requests for the data representation of an object without an lsid.
141 *
142 * @throws LSIDServerException
143 */
144 @RequestMapping(value = "/authority/data.do")
145 public ModelAndView getData() throws LSIDException {
146 throw new LSIDException(LSIDException.INVALID_METHOD_CALL, "Must specify HTTP Parameter 'lsid'");
147 }
148
149 }