1
|
package eu.dnetlib.data.objectstore.filesystem;
|
2
|
|
3
|
import java.io.*;
|
4
|
import java.net.URLEncoder;
|
5
|
import java.nio.file.Files;
|
6
|
import java.nio.file.Path;
|
7
|
import javax.servlet.http.HttpServletResponse;
|
8
|
|
9
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException;
|
10
|
import eu.dnetlib.miscutils.datetime.HumanTime;
|
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.stereotype.Controller;
|
15
|
import org.springframework.web.bind.annotation.RequestMapping;
|
16
|
import org.springframework.web.bind.annotation.RequestParam;
|
17
|
|
18
|
/**
|
19
|
* The Class ModularObjectStoreRESTService implement the controller REST of the object Store.
|
20
|
*/
|
21
|
@Controller
|
22
|
public class ModularObjectStoreRESTService {
|
23
|
|
24
|
private static final Log log = LogFactory.getLog(ModularObjectStoreRESTService.class); // NOPMD by marko on 11/24/08 5:02 PM
|
25
|
|
26
|
public static String retrieveURL(final String baseURI, final String basePath, final String objectStoreId, final String objectId)
|
27
|
throws UnsupportedEncodingException {
|
28
|
final StringBuilder sb = new StringBuilder(baseURI)
|
29
|
.append("?objectStore=" + encode(objectStoreId))
|
30
|
.append("&objectId=" + encode(objectId))
|
31
|
.append("&basePath=" + encode(basePath));
|
32
|
return sb.toString();
|
33
|
}
|
34
|
|
35
|
private static String encode(final String s) throws UnsupportedEncodingException {
|
36
|
return URLEncoder.encode(s, "UTF-8");
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
*
|
41
|
* @param res
|
42
|
* @param basePath
|
43
|
* @param objectStoreId
|
44
|
* @param objectId
|
45
|
* @throws IOException
|
46
|
* @throws ObjectStoreServiceException
|
47
|
*/
|
48
|
@RequestMapping(value = "/**/objectStore/retrieve.do")
|
49
|
public void retrieve(final HttpServletResponse res,
|
50
|
@RequestParam(value = "basePath", required = true) final String basePath,
|
51
|
@RequestParam(value = "objectStore", required = true) final String objectStoreId,
|
52
|
@RequestParam(value = "objectId", required = true) final String objectId) throws IOException, ObjectStoreServiceException {
|
53
|
|
54
|
final long start = System.currentTimeMillis();
|
55
|
final Path path = FileSystemUtility.objectStoreFilePath(basePath, objectStoreId, objectId);
|
56
|
|
57
|
if (!Files.exists(path) || !Files.isReadable(path)) {
|
58
|
final String msg = String.format("Object with identifier: %s not found the in %s", objectId, path);
|
59
|
res.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
|
60
|
log.warn(msg);
|
61
|
} else {
|
62
|
try (final InputStream is = new BufferedInputStream(new FileInputStream(path.toFile()))) {
|
63
|
|
64
|
final long size = Files.size(path);
|
65
|
|
66
|
res.setHeader("Content-Length", String.valueOf(size));
|
67
|
IOUtils.copyLarge(is, res.getOutputStream());
|
68
|
|
69
|
if (log.isDebugEnabled()) {
|
70
|
log.debug(String.format("retrive.do completed in %s, objId: %s", HumanTime.exactly(System.currentTimeMillis() - start), objectId));
|
71
|
}
|
72
|
} catch (IOException e) {
|
73
|
final String msg = "unable to close input Stream";
|
74
|
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
|
75
|
log.error(msg, e);
|
76
|
}
|
77
|
}
|
78
|
}
|
79
|
}
|