1
|
package eu.dnetlib.data.download;
|
2
|
|
3
|
import java.util.List;
|
4
|
import javax.annotation.Resource;
|
5
|
|
6
|
import com.google.gson.Gson;
|
7
|
import com.google.gson.reflect.TypeToken;
|
8
|
import eu.dnetlib.data.download.rmi.DownloadServiceActions;
|
9
|
import eu.dnetlib.data.download.rmi.DownloadServiceException;
|
10
|
import eu.dnetlib.data.download.rmi.DownloadServiceFeeder;
|
11
|
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
|
12
|
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerAction;
|
13
|
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
|
14
|
import org.apache.commons.codec.binary.Base64;
|
15
|
import org.apache.commons.lang.StringUtils;
|
16
|
import org.apache.commons.logging.Log;
|
17
|
import org.apache.commons.logging.LogFactory;
|
18
|
|
19
|
public class DownloadAction implements BlackboardServerAction<DownloadServiceActions> {
|
20
|
|
21
|
/** The Constant log. */
|
22
|
private static final Log log = LogFactory.getLog(DownloadAction.class);
|
23
|
private static String DEFAULT_NUMBER_OF_THREAD = "5";
|
24
|
private static int DEFAULT_TIMEOUT_MS = 5000;
|
25
|
@Resource
|
26
|
private DownloadServiceFeeder downloadServiceFeeder;
|
27
|
|
28
|
@Override
|
29
|
public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws Exception {
|
30
|
int connectTimeoutMs = DEFAULT_TIMEOUT_MS;
|
31
|
int readTimeoutMs = DEFAULT_TIMEOUT_MS;
|
32
|
int sleepTimeMs = 0;
|
33
|
try {
|
34
|
String epr = job.getParameters().get("epr");
|
35
|
String plugin = job.getParameters().get("plugin");
|
36
|
String objectStoreID = job.getParameters().get("objectStoreID");
|
37
|
String protocol = job.getParameters().get("protocol");
|
38
|
String mimeType = job.getParameters().get("mimeType");
|
39
|
String numberOfThreads = job.getParameters().get("numberOfThreads");
|
40
|
String basePath = job.getParameters().get("basePath");
|
41
|
String regularExpression = job.getParameters().get("regularExpressions");
|
42
|
String connectTimeoutMsStr = job.getParameters().get("connectTimeoutMs");
|
43
|
String readTimeoutMsStr = job.getParameters().get("readTimeoutMs");
|
44
|
String sleepTimeMsStr = job.getParameters().get("sleepTimeMs");
|
45
|
log.debug("regular Expression: " + regularExpression);
|
46
|
|
47
|
List<String> expressions = null;
|
48
|
|
49
|
if (!StringUtils.isBlank(regularExpression)) {
|
50
|
expressions = parseRegexList(regularExpression);
|
51
|
}
|
52
|
|
53
|
if (StringUtils.isBlank(numberOfThreads)) {
|
54
|
log.warn("Cannot find numberOfThread, using default value " + DEFAULT_NUMBER_OF_THREAD);
|
55
|
numberOfThreads = DEFAULT_NUMBER_OF_THREAD;
|
56
|
}
|
57
|
|
58
|
if (StringUtils.isNotBlank(connectTimeoutMsStr)) connectTimeoutMs = Integer.parseInt(connectTimeoutMsStr);
|
59
|
if (StringUtils.isNotBlank(readTimeoutMsStr)) readTimeoutMs = Integer.parseInt(readTimeoutMsStr);
|
60
|
if (StringUtils.isNotBlank(sleepTimeMsStr)) sleepTimeMs = Integer.parseInt(sleepTimeMsStr);
|
61
|
|
62
|
log.info(String.format("downloading using plugin %s , protocol : %s into ObjectStore id %s ", plugin, protocol, objectStoreID));
|
63
|
handler.ongoing(job);
|
64
|
DownloadReportMap response = downloadServiceFeeder.download(epr, plugin, objectStoreID, protocol, mimeType, Integer.parseInt(numberOfThreads),
|
65
|
basePath, expressions, connectTimeoutMs, readTimeoutMs, sleepTimeMs);
|
66
|
if (response.getStatus()) {
|
67
|
log.info(String.format("Completed Download, plugin: %s, protocol: %s, objectStoreID: %s", plugin, protocol, objectStoreID));
|
68
|
job.getParameters().put("total", "" + response.getTotalDownloaded());
|
69
|
job.getParameters().put("report", Base64.encodeBase64String(response.toString().getBytes()));
|
70
|
handler.done(job);
|
71
|
} else {
|
72
|
log.error("download response is false" + response);
|
73
|
|
74
|
handler.failed(job, new DownloadServiceException(
|
75
|
"oops! something bad happen to our download hamsters, downloaded so far: " + response.getTotalDownloaded()));
|
76
|
}
|
77
|
} catch (Exception e) {
|
78
|
log.error("An error occur while starting download", e);
|
79
|
handler.failed(job, new DownloadServiceException(e));
|
80
|
}
|
81
|
}
|
82
|
|
83
|
private List<String> parseRegexList(final String regularExpression) {
|
84
|
log.info("parsing regex list: " + regularExpression);
|
85
|
return new Gson().fromJson(regularExpression, new TypeToken<List<String>>() {}.getType());
|
86
|
}
|
87
|
|
88
|
}
|