1
|
package eu.dnetlib.data.objectstore.s3;
|
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 com.amazonaws.ClientConfiguration;
|
10
|
import com.amazonaws.Protocol;
|
11
|
import com.amazonaws.auth.AWSCredentials;
|
12
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
13
|
import com.amazonaws.auth.BasicAWSCredentials;
|
14
|
import com.amazonaws.client.builder.AwsClientBuilder;
|
15
|
import com.amazonaws.services.s3.AmazonS3;
|
16
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
17
|
import com.amazonaws.services.s3.model.S3Object;
|
18
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException;
|
19
|
import eu.dnetlib.miscutils.datetime.HumanTime;
|
20
|
import org.apache.commons.io.IOUtils;
|
21
|
import org.apache.commons.logging.Log;
|
22
|
import org.apache.commons.logging.LogFactory;
|
23
|
import org.springframework.beans.factory.annotation.Value;
|
24
|
import org.springframework.stereotype.Controller;
|
25
|
import org.springframework.web.bind.annotation.RequestMapping;
|
26
|
import org.springframework.web.bind.annotation.RequestParam;
|
27
|
|
28
|
/**
|
29
|
* The Class ModularObjectStoreRESTService implement the controller REST of the object Store.
|
30
|
*/
|
31
|
@Controller
|
32
|
public class ModularObjectStoreRESTService {
|
33
|
|
34
|
private static final Log log = LogFactory.getLog(ModularObjectStoreRESTService.class); // NOPMD by marko on 11/24/08 5:02 PM
|
35
|
|
36
|
|
37
|
|
38
|
@Value("${dnet.objectStore.s3.accessKey}")
|
39
|
private String accessKey;
|
40
|
|
41
|
|
42
|
@Value("${dnet.objectStore.s3.secretKey}")
|
43
|
private String secretKey;
|
44
|
|
45
|
@Value("${dnet.objectStore.s3.endPoint}")
|
46
|
private String s3EndPoint;
|
47
|
|
48
|
@Value("${dnet.objectStore.s3.objectStoreBucket}")
|
49
|
private String objectStoreBucket;
|
50
|
|
51
|
private AmazonS3 client;
|
52
|
|
53
|
private static final String S3_REGION = "eu-west-3";
|
54
|
|
55
|
|
56
|
private AmazonS3 initializeClient() throws ObjectStoreServiceException {
|
57
|
try {
|
58
|
final AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
|
59
|
final ClientConfiguration cfg = new ClientConfiguration().withProtocol(Protocol.HTTPS);
|
60
|
final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials))
|
61
|
.withClientConfiguration(cfg)
|
62
|
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(this.s3EndPoint, S3_REGION))
|
63
|
.build();
|
64
|
if (s3 == null)
|
65
|
throw new ObjectStoreServiceException("Cannot initialize s3 client because is NULL");
|
66
|
return s3;
|
67
|
} catch (Throwable e) {
|
68
|
log.error("An Error happen on initialize client ", e);
|
69
|
throw new ObjectStoreServiceException("Cannot initialize s3 client", e);
|
70
|
}
|
71
|
}
|
72
|
|
73
|
|
74
|
public static String retrieveURL(final String baseURI, final String basePath, final String objectStoreId, final String objectId)
|
75
|
throws UnsupportedEncodingException {
|
76
|
final StringBuilder sb = new StringBuilder(baseURI)
|
77
|
.append("?objectStore=" + encode(objectStoreId))
|
78
|
.append("&objectId=" + encode(objectId))
|
79
|
.append("&basePath=" + encode(basePath));
|
80
|
return sb.toString();
|
81
|
}
|
82
|
|
83
|
private static String encode(final String s) throws UnsupportedEncodingException {
|
84
|
return URLEncoder.encode(s, "UTF-8");
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
*
|
89
|
* @param res
|
90
|
* @param objectStoreId
|
91
|
* @param objectId
|
92
|
* @throws IOException
|
93
|
* @throws ObjectStoreServiceException
|
94
|
*/
|
95
|
@RequestMapping(value = "/**/objectStore/retrieve.do")
|
96
|
public void retrieve(final HttpServletResponse res,
|
97
|
@RequestParam(value = "objectStore", required = true) final String objectStoreId,
|
98
|
@RequestParam(value = "objectId", required = true) final String objectId) throws IOException, ObjectStoreServiceException {
|
99
|
|
100
|
final long start = System.currentTimeMillis();
|
101
|
if (client == null)
|
102
|
client = initializeClient();
|
103
|
final S3Object object = this.client.getObject(objectStoreBucket, objectStoreId + "/" + objectId);
|
104
|
|
105
|
|
106
|
|
107
|
if (object == null) {
|
108
|
final String msg = String.format("Object with identifier: %s not found the in %s", objectId, objectStoreId);
|
109
|
res.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
|
110
|
log.warn(msg);
|
111
|
} else {
|
112
|
try ( final InputStream is = object.getObjectContent()) {
|
113
|
|
114
|
res.setHeader("Content-Length", String.valueOf(object.getObjectMetadata().getContentLength()));
|
115
|
IOUtils.copyLarge(is, res.getOutputStream());
|
116
|
if (log.isDebugEnabled()) {
|
117
|
log.debug(String.format("retrive.do completed in %s, objId: %s", HumanTime.exactly(System.currentTimeMillis() - start), objectId));
|
118
|
}
|
119
|
} catch (IOException e) {
|
120
|
final String msg = "unable to close input Stream";
|
121
|
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
|
122
|
log.error(msg, e);
|
123
|
}
|
124
|
}
|
125
|
}
|
126
|
}
|