3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.remote
.service
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStream
;
14 import java
.io
.OutputStream
;
16 import javax
.servlet
.http
.HttpServletResponse
;
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
;
26 import com
.ibm
.lsid
.LSIDException
;
27 import com
.ibm
.lsid
.server
.LSIDServerException
;
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
;
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.
40 * @author Ben Szekely (<a href="mailto:bhszekel@us.ibm.com">bhszekel@us.ibm.com</a>)
41 * @see com.ibm.lsid.server.servlet.DataServlet
44 public class DataController
{
46 private LSIDDataService lsidDataService
;
49 public void setLsidDataService(LSIDDataService lsidDataService
) {
50 this.lsidDataService
= lsidDataService
;
54 public void initBinder(WebDataBinder binder
) {
55 binder
.registerCustomEditor(LSID
.class, new LSIDPropertyEditor());
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.
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
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;
78 // data = lsidDataService.getDataByRange(lsid,start,length);
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);
94 // if (data != null) {
102 * Handle requests for the data representation of an object with a given lsid.
104 * @param LSID lsid the lsid to retrieve data for
105 * @return ModelAndView (null)
106 * @throws LSIDServerException
107 * @throws IOException
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;
115 data
= lsidDataService
.getData(lsid
);
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
);
140 * Handle requests for the data representation of an object without an lsid.
142 * @throws LSIDServerException
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'");