1
|
package eu.dnetlib.data.objectstore.filesystem;
|
2
|
|
3
|
import java.io.UnsupportedEncodingException;
|
4
|
|
5
|
import com.mongodb.DBObject;
|
6
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFile;
|
7
|
import eu.dnetlib.data.objectstore.rmi.Protocols;
|
8
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
9
|
import org.apache.commons.lang.StringUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
|
13
|
/**
|
14
|
* The Class ObjectStoreFileBuilder generates an objectStoreFile bean
|
15
|
*
|
16
|
*/
|
17
|
public class ObjectStoreFileUtility {
|
18
|
|
19
|
private static final int KB_SIZE = 1024;
|
20
|
|
21
|
/** The Constant log. */
|
22
|
private static final Log log = LogFactory.getLog(ObjectStoreFileUtility.class);
|
23
|
|
24
|
public static ObjectStoreFile build(final DBObject metadata, final String baseURI, final String objectStoreID, final String basePath) {
|
25
|
|
26
|
String originalFile = (String) metadata.get("originalObject");
|
27
|
ObjectStoreFile original = ObjectStoreFile.createObject(originalFile);
|
28
|
ObjectStoreFile newFile = new ObjectStoreFile();
|
29
|
newFile.setObjectID((String) metadata.get("id"));
|
30
|
newFile.setAccessProtocol(Protocols.HTTP);
|
31
|
newFile.setMimeType((String) metadata.get("mime"));
|
32
|
newFile.setMd5Sum((String) metadata.get("md5Sum"));
|
33
|
try {
|
34
|
newFile.setFileSizeKB(Long.parseLong(metadata.get("size").toString()) / KB_SIZE);
|
35
|
} catch (Throwable e) {
|
36
|
log.error("Error on getting file size", e);
|
37
|
}
|
38
|
if (originalFile != null) {
|
39
|
newFile.setMetadataRelatedID(original.getMetadataRelatedID());
|
40
|
if (StringUtils.isBlank(original.getDownloadedURL())) {
|
41
|
newFile.setDownloadedURL(original.getURI());
|
42
|
} else {
|
43
|
newFile.setDownloadedURL(original.getDownloadedURL());
|
44
|
}
|
45
|
}
|
46
|
try {
|
47
|
newFile.setURI(ModularObjectStoreRESTService.retrieveURL(baseURI, basePath, objectStoreID, newFile.getObjectID()));
|
48
|
} catch (UnsupportedEncodingException e) {
|
49
|
log.error("Error on Build objectStoreFile ", e);
|
50
|
}
|
51
|
return newFile;
|
52
|
}
|
53
|
|
54
|
public static UnaryFunction<String, DBObject> asJSON(final String baseURI, final String objectStoreID, final String basePath) {
|
55
|
return input -> build(input, baseURI, objectStoreID, basePath).toJSON();
|
56
|
}
|
57
|
}
|