Project

General

Profile

1
package eu.dnetlib.data.objectstore.modular;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5

    
6
import javax.annotation.Resource;
7
import javax.servlet.ServletOutputStream;
8
import javax.servlet.http.HttpServletResponse;
9

    
10
import org.apache.commons.io.IOUtils;
11
import org.springframework.stereotype.Controller;
12
import org.springframework.web.bind.annotation.RequestMapping;
13
import org.springframework.web.bind.annotation.RequestParam;
14

    
15
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFile;
16
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException;
17

    
18
// TODO: Auto-generated Javadoc
19
/**
20
 * The Class ModularObjectStoreRESTService implement the controller REST of the object Store.
21
 */
22
@Controller
23
public class ModularObjectStoreRESTService {
24

    
25
	/** The object store deliver. */
26
	@Resource
27
	ModularObjectStoreDeliver objectStoreDeliver;
28

    
29
	/**
30
	 * Retrieve.
31
	 *
32
	 * @param res
33
	 *            the res
34
	 * @param objectStoreId
35
	 *            the object store id
36
	 * @param objectId
37
	 *            the object id
38
	 * @throws IOException
39
	 *             Signals that an I/O exception has occurred.
40
	 * @throws ObjectStoreServiceException
41
	 */
42
	@RequestMapping(value = "/**/objectStore/retrieve.do")
43
	public void retrieve(final HttpServletResponse res,
44
			@RequestParam(value = "objectStore", required = true) final String objectStoreId,
45
			@RequestParam(value = "objectId", required = true) final String objectId) throws IOException, ObjectStoreServiceException {
46

    
47
		ObjectStoreFile file = objectStoreDeliver.deliverObject(objectStoreId, objectId);
48

    
49
		if (file == null) throw new RuntimeException("The file with id " + objectId + " doesn't exist");
50

    
51
		// res.setContentType(file.getMimeType());
52

    
53
		InputStream is = objectStoreDeliver.deliverStream(objectStoreId, objectId);
54

    
55
		ServletOutputStream outputStream = res.getOutputStream();
56

    
57
		IOUtils.copy(is, outputStream);
58

    
59
		/*
60
		 * while (true) { int readSize = is.read(buffer); if (readSize == -1) { break; } outputStream.write(buffer, 0, readSize); }
61
		 */
62
	}
63
}
(8-8/13)