1
|
package eu.dnetlib.validator.service.impls.persistance;
|
2
|
|
3
|
import java.io.BufferedInputStream;
|
4
|
import java.io.File;
|
5
|
import java.io.FileInputStream;
|
6
|
import java.io.IOException;
|
7
|
import java.io.InputStream;
|
8
|
import java.io.PrintWriter;
|
9
|
|
10
|
import javax.servlet.ServletException;
|
11
|
import javax.servlet.http.HttpServlet;
|
12
|
import javax.servlet.http.HttpServletRequest;
|
13
|
import javax.servlet.http.HttpServletResponse;
|
14
|
|
15
|
import org.apache.commons.io.IOUtils;
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.context.ApplicationContext;
|
18
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
19
|
|
20
|
public class BlacklistServlet extends HttpServlet {
|
21
|
|
22
|
/**
|
23
|
*
|
24
|
*/
|
25
|
private static final long serialVersionUID = 1788389574262137242L;
|
26
|
private static Logger logger = Logger.getLogger(BlacklistServlet.class);
|
27
|
|
28
|
private static final String REQUEST_GETBLACKLIST = "GetBlacklistedRecords";
|
29
|
// private TasksDAO tasksDao;
|
30
|
private String path;
|
31
|
|
32
|
@Override
|
33
|
public void init() throws ServletException {
|
34
|
ApplicationContext context = WebApplicationContextUtils
|
35
|
.getWebApplicationContext(getServletContext());
|
36
|
|
37
|
// this.tasksDao = (TasksDAO) context.getBean("tasksDao");
|
38
|
// this.path = "/tmp/validator-wf/";
|
39
|
this.path = "/var/lib/dnet/validator/workflow_blacklists/";
|
40
|
// this.path = context.getEnvironment().getProperty("blacklistedRecordsPath");
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
public void doGet(HttpServletRequest req, HttpServletResponse resp)
|
45
|
throws ServletException, IOException {
|
46
|
this.handleRequest(req, resp);
|
47
|
}
|
48
|
|
49
|
@Override
|
50
|
public void doPost(HttpServletRequest req, HttpServletResponse resp)
|
51
|
throws ServletException, IOException {
|
52
|
this.handleRequest(req, resp);
|
53
|
}
|
54
|
|
55
|
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
56
|
|
57
|
InputStream is = null;
|
58
|
|
59
|
|
60
|
String dataSourceId = req.getParameter("datasourceId");
|
61
|
String requestType = req.getParameter("request");
|
62
|
try {
|
63
|
if (requestType.equals(REQUEST_GETBLACKLIST)) {
|
64
|
logger.debug("parameter: " + dataSourceId);
|
65
|
File file = new File(path + dataSourceId);
|
66
|
if (file.isFile()) {
|
67
|
logger.debug("file exists");
|
68
|
is = new BufferedInputStream(new FileInputStream(file));
|
69
|
resp.setStatus(200);
|
70
|
resp.setContentType("text/plain");
|
71
|
resp.setContentLength((int) file.length());
|
72
|
|
73
|
// InputStream fileStream = new FileInputStream(file);
|
74
|
// InputStream gzipStream = new GZIPInputStream(fileStream);
|
75
|
// Reader decoder = new InputStreamReader(gzipStream, "UTF-8");
|
76
|
// BufferedReader buffered = new BufferedReader(decoder);
|
77
|
// IOUtils.copyLarge(buffered, resp.getWriter());
|
78
|
|
79
|
IOUtils.copyLarge(is, resp.getOutputStream());
|
80
|
// tasksDao.get(4335);
|
81
|
} else {
|
82
|
PrintWriter out = resp.getWriter();
|
83
|
resp.setStatus(400);
|
84
|
resp.setContentType("text/plain");
|
85
|
out.write("No file for this id: " + dataSourceId);
|
86
|
out.close();
|
87
|
}
|
88
|
} else {
|
89
|
PrintWriter out = resp.getWriter();
|
90
|
resp.setStatus(500);
|
91
|
resp.setContentType("text/plain");
|
92
|
out.write("Request: " + requestType + " is not supported");
|
93
|
out.close();
|
94
|
}
|
95
|
} catch (Exception e) {
|
96
|
resp.setStatus(500);
|
97
|
e.printStackTrace(resp.getWriter());
|
98
|
} finally {
|
99
|
if (is != null)
|
100
|
is.close();
|
101
|
}
|
102
|
|
103
|
}
|
104
|
}
|