Project

General

Profile

Download (5.31 KB) Statistics
| Branch: | Tag: | Revision:
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 io.swagger.annotations.Api;
13

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

    
18
import javax.servlet.http.HttpServletResponse;
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.RequestMethod;
26
import org.springframework.web.bind.annotation.RequestParam;
27
import org.springframework.web.servlet.ModelAndView;
28

    
29
import com.ibm.lsid.LSIDException;
30
import com.ibm.lsid.server.LSIDServerException;
31

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

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

    
51
    private LSIDDataService lsidDataService;
52

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

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

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

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

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

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

    
154
}
(3-3/5)