Project

General

Profile

Download (5.31 KB) Statistics
| Branch: | Tag: | Revision:
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.service;
10

    
11
import io.swagger.annotations.Api;
12

    
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.io.OutputStream;
16

    
17
import javax.servlet.http.HttpServletResponse;
18

    
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.stereotype.Controller;
21
import org.springframework.web.bind.WebDataBinder;
22
import org.springframework.web.bind.annotation.InitBinder;
23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
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.server.LSIDServerException;
30

    
31
import eu.etaxonomy.cdm.api.service.lsid.LSIDDataService;
32
import eu.etaxonomy.cdm.model.common.LSID;
33
import eu.etaxonomy.cdm.remote.editor.LSIDPropertyEditor;
34

    
35
/**
36
 * Controller which accepts requests for the data representation of an object
37
 * with a given lsid. The response is written directly into the request, rather
38
 * than being passed as part of the ModelAndView since data is supposed to be
39
 * byte-identical and thus cannot be transformed by the view layer.
40
 *
41
 * @author ben
42
 * @author Ben Szekely (<a href="mailto:bhszekel@us.ibm.com">bhszekel@us.ibm.com</a>)
43
 * @see com.ibm.lsid.server.servlet.DataServlet
44
 */
45
@Controller
46
@Api(value="lsid_authority_data",
47
description="Controller which accepts incoming requests to the LSIDDataService.")
48
public class DataController {
49

    
50
    private LSIDDataService lsidDataService;
51

    
52
    @Autowired
53
    public void setLsidDataService(LSIDDataService lsidDataService) {
54
        this.lsidDataService = lsidDataService;
55
    }
56

    
57
    @InitBinder
58
    public void initBinder(WebDataBinder binder) {
59
        binder.registerCustomEditor(LSID.class, new LSIDPropertyEditor());
60
    }
61

    
62
    /**
63
     * Handle requests for the data representation of an object with a given lsid. Can return only part of the
64
     * data if the length and offset are specified in the request as per the specification.
65
     *
66
     * @param LSID lsid the lsid to retrieve data for
67
     * @param Integer start the offset in bytes to read from
68
     * @param Integer length the number of bytes to return
69
     * @return ModelAndView (null)
70
     * @throws LSIDServerException
71
     * @throws IOException
72
     */
73
    @RequestMapping(value = "/authority/data.do",params = {"lsid","start","length"}, method = RequestMethod.GET)
74
    public ModelAndView getData(@RequestParam("lsid") LSID lsid,
75
                                @RequestParam("start") Integer start,
76
                                @RequestParam("length") Integer length,
77
                                HttpServletResponse response) throws LSIDServerException, IOException  {
78
        //FIXME #3811 fix null pointer access of "out" reference
79
//		OutputStream out = null;
80
//		InputStream data = null;
81
//		try {
82
//			data = lsidDataService.getDataByRange(lsid,start,length);
83
//		    if(data != null) {
84
//		    	response.setContentType("application/octet-stream");
85
//		    	byte[] bytes = new byte[1024];
86
//		    	int numbytes = data.read(bytes);
87
//		    	while (numbytes != -1) {
88
//		    		out.write(bytes,0,numbytes);
89
//		    		numbytes = data.read(bytes);
90
//		    	}
91
//		    	out.flush();
92
//		    }
93
//		} finally {
94
//			if (out != null) {
95
//				out.close();
96
//			}
97
//
98
//			if (data != null) {
99
//				data.close();
100
//			}
101
//		}
102
        return null;
103
    }
104

    
105
    /**
106
     * Handle requests for the data representation of an object with a given lsid.
107
     *
108
     * @param LSID lsid the lsid to retrieve data for
109
     * @return ModelAndView (null)
110
     * @throws LSIDServerException
111
     * @throws IOException
112
     */
113
    @RequestMapping(value = "/authority/data.do",params = {"lsid"}, method = RequestMethod.GET)
114
    public ModelAndView getData(@RequestParam("lsid")LSID lsid,
115
                                HttpServletResponse response) throws LSIDServerException, IOException  {
116
        OutputStream out = null;
117
        InputStream data = null;
118
        try {
119
            data = lsidDataService.getData(lsid);
120
            if(data != null) {
121
                response.setContentType("application/octet-stream");
122
                out = response.getOutputStream();
123
                byte[] bytes = new byte[1024];
124
                int numbytes = data.read(bytes);
125
                while (numbytes != -1) {
126
                    out.write(bytes,0,numbytes);
127
                    numbytes = data.read(bytes);
128
                }
129
                out.flush();
130
            }
131
        } finally {
132
            if (out != null) {
133
                out.close();
134
            }
135

    
136
            if (data != null) {
137
                data.close();
138
            }
139
        }
140
        return null;
141
    }
142

    
143
    /**
144
     * Handle requests for the data representation of an object without an lsid.
145
     *
146
     * @throws LSIDServerException
147
     */
148
    @RequestMapping(value = "/authority/data.do", method = RequestMethod.GET)
149
    public ModelAndView getData() throws LSIDException {
150
        throw new LSIDException(LSIDException.INVALID_METHOD_CALL, "Must specify HTTP Parameter 'lsid'");
151
    }
152

    
153
}
(3-3/5)