/
JobResource.java
277 lines (237 loc) · 10.5 KB
/
JobResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package dk.netarkivet.harvester.webinterface.servlet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.netarchivesuite.heritrix3wrapper.ScriptResult;
public class JobResource implements ResourceAbstract {
private static final String NAS_GROOVY_RESOURCE_PATH = "dk/netarkivet/harvester/webinterface/servlet/nas.groovy";
private NASEnvironment environment;
protected int R_JOB = -1;
protected int R_FRONTIER = -1;
protected int R_CRAWLLOG = -1;
@Override
public void resources_init(NASEnvironment environment) {
this.environment = environment;
}
@Override
public void resources_add(ResourceManagerAbstract resourceManager) {
R_JOB = resourceManager.resource_add(this, "/job/<numeric>/", false);
R_FRONTIER = resourceManager.resource_add(this, "/job/<numeric>/frontier/", false);
R_CRAWLLOG = resourceManager.resource_add(this, "/job/<numeric>/crawllog/", false);
}
@Override
public void resource_service(ServletContext servletContext, NASUser nas_user, HttpServletRequest req, HttpServletResponse resp, int resource_id, List<Integer> numerics, String pathInfo) throws IOException {
if (NASEnvironment.contextPath == null) {
NASEnvironment.contextPath = req.getContextPath();
}
if (NASEnvironment.servicePath == null) {
NASEnvironment.servicePath = req.getContextPath() + req.getServletPath() + "/";
}
String method = req.getMethod().toUpperCase();
if (resource_id == R_JOB) {
if ("GET".equals(method)) {
job(req, resp, numerics);
}
}
if (resource_id == R_FRONTIER) {
if ("GET".equals(method) || "POST".equals(method)) {
frontier_list(req, resp, numerics);
}
}
else if (resource_id == R_CRAWLLOG) {
if ("GET".equals(method) || "POST".equals(method)) {
crawllog_list(req, resp, numerics);
}
}
}
public void job(HttpServletRequest req, HttpServletResponse resp, List<Integer> numerics) throws IOException {
resp.setContentType("text/html; charset=UTF-8");
ServletOutputStream out = resp.getOutputStream();
StringBuilder sb = new StringBuilder();
Heritrix3JobMonitor h3Job = environment.h3JobMonitorThread.getRunningH3Job(numerics.get(0));
if (h3Job != null && h3Job.isReady()) {
sb.append("Job: ");
sb.append(h3Job.jobId);
sb.append("<br />\n");
sb.append("<br />\n");
sb.append("<a href=\"");
sb.append(NASEnvironment.servicePath);
sb.append("job/");
sb.append(h3Job.jobId);
sb.append("/frontier/");
sb.append("\">");
sb.append("Frontier queue");
sb.append("</a>");
sb.append("<br />\n");
sb.append("<a href=\"");
sb.append(NASEnvironment.servicePath);
sb.append("job/");
sb.append(h3Job.jobId);
sb.append("/crawllog/");
sb.append("\">");
sb.append("CrawlLog");
sb.append("</a>");
sb.append("<br />\n");
} else {
sb.append("Job ");
sb.append(numerics.get(0));
sb.append(" is not running.");
}
out.write(sb.toString().getBytes("UTF-8"));
out.flush();
out.close();
}
public void frontier_list(HttpServletRequest req, HttpServletResponse resp, List<Integer> numerics) throws IOException {
resp.setContentType("text/html; charset=UTF-8");
ServletOutputStream out = resp.getOutputStream();
StringBuilder sb = new StringBuilder();
String regex = req.getParameter("regex");
if (regex == null || regex.length() == 0) {
regex =".*";
}
String resource = NAS_GROOVY_RESOURCE_PATH;
InputStream in = JobResource.class.getClassLoader().getResourceAsStream(resource);
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
byte[] tmpArr = new byte[8192];
int read;
while ((read = in.read(tmpArr)) != -1) {
bOut.write(tmpArr, 0, read);
}
in.close();
String script = new String(bOut.toByteArray(), "UTF-8");
/*
//RandomAccessFile raf = new RandomAccessFile("/home/nicl/workspace-nas-h3/heritrix3-scripts/src/main/java/view-frontier-url.groovy", "r");
RandomAccessFile raf = new RandomAccessFile("/home/nicl/workspace-nas-h3/heritrix3-scripts/src/main/java/nas.groovy", "r");
byte[] src = new byte[(int)raf.length()];
raf.readFully(src);
raf.close();
String script = new String(src, "UTF-8");
*/
String tmpStr = req.getParameter("delete");
if (tmpStr != null && "1".equals(tmpStr) ) {
script += "\n\ndeleteFromFrontier '" + regex + "'\n";
} else {
script += "\n\nlistFrontier '" + regex + "'\n";
}
// To use, just remove the initial "//" from any one of these lines.
//
//killToeThread 1 //Kill a toe thread by number
//listFrontier '.*stats.*' //List uris in the frontier matching a given regexp
//deleteFromFrontier '.*foobar.*' //Remove uris matching a given regexp from the frontier
//printCrawlLog '.*' //View already crawled lines uris matching a given regexp
Heritrix3JobMonitor h3Job = environment.h3JobMonitorThread.getRunningH3Job(numerics.get(0));
if (h3Job != null && h3Job.isReady()) {
sb.append("<form class=\"form-horizontal\" action=\"?\" name=\"insert_form\" method=\"post\" enctype=\"application/x-www-form-urlencoded\" accept-charset=\"utf-8\">");
sb.append("<input type=<\"text\" id=\"regex\" name=\"regex\" value=\"" + regex + "\" placeholder=\"content-type\">\n");
sb.append("<button type=\"submit\" name=\"show\" value=\"1\" class=\"btn btn-success\"><i class=\"icon-white icon-thumbs-up\"></i> Show</button>\n");
sb.append("<button type=\"submit\" name=\"delete\" value=\"1\" class=\"btn btn-success\"><i class=\"icon-white icon-thumbs-up\"></i> Delete</button>\n");
sb.append("</form>");
ScriptResult scriptResult = h3Job.h3wrapper.ExecuteShellScriptInJob(h3Job.jobResult.job.shortName, "groovy", script);
//System.out.println(new String(scriptResult.response, "UTF-8"));
if (scriptResult != null && scriptResult.script != null) {
if (scriptResult.script.htmlOutput != null) {
sb.append(scriptResult.script.htmlOutput);
}
if (scriptResult.script.rawOutput != null) {
sb.append(scriptResult.script.rawOutput);
}
}
} else {
sb.append("Job ");
sb.append(numerics.get(0));
sb.append(" is not running.");
}
out.write(sb.toString().getBytes("UTF-8"));
out.flush();
out.close();
}
public void crawllog_list(HttpServletRequest req, HttpServletResponse resp, List<Integer> numerics) throws IOException {
resp.setContentType("text/html; charset=UTF-8");
ServletOutputStream out = resp.getOutputStream();
long lines;
long linesPerPage = 100;
long page = 1;
long pages = 0;
String q = null;
String tmpStr;
tmpStr = req.getParameter("page");
if (tmpStr != null && tmpStr.length() > 0) {
try {
page = Long.parseLong(tmpStr);
} catch (NumberFormatException e) {
}
}
tmpStr = req.getParameter("itemsperpage");
if (tmpStr != null && tmpStr.length() > 0) {
try {
linesPerPage = Long.parseLong(tmpStr);
} catch (NumberFormatException e) {
}
}
if (linesPerPage < 25) {
linesPerPage = 25;
}
if (linesPerPage > 1000) {
linesPerPage = 1000;
}
tmpStr = req.getParameter("q");
if (tmpStr != null && tmpStr.length() > 0 && tmpStr.equalsIgnoreCase(".*")) {
q = tmpStr;
}
if (q == null) {
q = ".*";
}
StringBuilder sb = new StringBuilder();
Heritrix3JobMonitor h3Job = environment.h3JobMonitorThread.getRunningH3Job(numerics.get(0));
Pageable pageable = h3Job;
if (h3Job != null && h3Job.isReady()) {
SearchResult searchResult = null;
if (q != null) {
searchResult = h3Job.getSearchResult(q);
searchResult.update();
pageable = searchResult;
}
lines = pageable.getIndexSize();
if (lines > 0) {
lines = (lines / 8) - 1;
pages = Pagination.getPages(lines, linesPerPage);
}
if (page > pages) {
page = pages;
}
sb.append("Log lines: ");
sb.append(lines);
sb.append("<br />\n");
sb.append("Page: ");
sb.append(page);
sb.append(" of ");
sb.append(pages);
sb.append("<br />\n");
sb.append("<form class=\"form-horizontal\" action=\"?\" name=\"insert_form\" method=\"post\" enctype=\"application/x-www-form-urlencoded\" accept-charset=\"utf-8\">");
sb.append("<input type=<\"text\" id=\"regex\" name=\"regex\" value=\"" + q + "\" placeholder=\"content-type\">\n");
sb.append("<button type=\"submit\" name=\"search\" value=\"1\" class=\"btn btn-success\"><i class=\"icon-white icon-thumbs-up\"></i> Search</button>\n");
sb.append("</form>");
sb.append("<br />\n");
sb.append("<br />\n");
sb.append(Pagination.getPagination(page, linesPerPage, pages, false));
sb.append("<pre>\n");
if (lines > 0) {
byte[] bytes = pageable.readPage(page, linesPerPage, true);
sb.append(new String(bytes, "UTF-8"));
}
sb.append("</pre>\n");
} else {
sb.append("Job ");
sb.append(numerics.get(0));
sb.append(" is not running.");
}
out.write(sb.toString().getBytes("UTF-8"));
out.flush();
out.close();
}
}