Project

General

Profile

1
package eu.dnetlib.data.objectstore;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import javax.servlet.ServletOutputStream;
6
import javax.servlet.http.HttpServletResponse;
7

    
8
import eu.dnetlib.miscutils.datetime.HumanTime;
9
import eu.dnetlib.rmi.data.ObjectStoreFile;
10
import eu.dnetlib.rmi.data.ObjectStoreServiceException;
11
import org.apache.commons.io.IOUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.stereotype.Controller;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestParam;
18

    
19
// TODO: Auto-generated Javadoc
20

    
21
/**
22
 * The Class ModularObjectStoreRESTService implement the controller REST of the object Store.
23
 */
24
@Controller
25
public class ModularObjectStoreRESTService {
26

    
27
	private static final Log log = LogFactory.getLog(ModularObjectStoreRESTService.class); // NOPMD by marko on 11/24/08 5:02 PM
28
	/**
29
	 * The object store deliver.
30
	 */
31
	@Autowired
32
	ModularObjectStoreDeliver objectStoreDeliver;
33

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

    
48
		final long start = System.currentTimeMillis();
49
		final ObjectStoreFile file = objectStoreDeliver.deliverObject(objectStoreId, objectId);
50

    
51
		log.debug(String.format("deliverObject completed in %s, objId: %s", HumanTime.exactly(System.currentTimeMillis() - start), objectId));
52

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

    
55
		// res.setContentType(file.getMimeType());
56

    
57
		InputStream is = objectStoreDeliver.deliverStream(objectStoreId, objectId);
58

    
59
		ServletOutputStream outputStream = res.getOutputStream();
60

    
61
		IOUtils.copy(is, outputStream);
62

    
63
		try {
64
			is.close();
65
			if (log.isDebugEnabled()) {
66
				log.debug(String.format("retrive.do completed in %s, objId: %s", HumanTime.exactly(System.currentTimeMillis() - start), objectId));
67
			}
68
		} catch (Throwable e) {
69
			log.error("unable to close input Stream", e);
70
		}
71

    
72
		/*
73
		 * while (true) { int readSize = is.read(buffer); if (readSize == -1) { break; } outputStream.write(buffer, 0, readSize); }
74
		 */
75
	}
76
}
(14-14/21)