1 | package org.greenstone.mat.servlet;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 | import java.net.*;
|
---|
5 | import java.util.*;
|
---|
6 | import java.util.regex.Matcher;
|
---|
7 | import java.util.regex.Pattern;
|
---|
8 |
|
---|
9 | import javax.servlet.*;
|
---|
10 | import javax.servlet.http.*;
|
---|
11 | import javax.xml.parsers.*;
|
---|
12 |
|
---|
13 | import org.xml.sax.*;
|
---|
14 | import org.w3c.dom.*;
|
---|
15 | import org.greenstone.gsdl3.util.GlobalProperties;
|
---|
16 | import org.greenstone.gsdl3.util.GSPath;
|
---|
17 | import java.net.Authenticator;
|
---|
18 | import java.net.PasswordAuthentication;
|
---|
19 |
|
---|
20 | public class MatServlet extends HttpServlet {
|
---|
21 |
|
---|
22 | private int port_number = 0;
|
---|
23 | private String oaiPrefix ="";
|
---|
24 | private String titleString;
|
---|
25 | private String h1String;
|
---|
26 | private String maxRecord;
|
---|
27 | private GlobalProperties globalProperty;
|
---|
28 | private String fileSeparator;
|
---|
29 | private String cssString;
|
---|
30 | private String headerString;
|
---|
31 | private String javaScript;
|
---|
32 | private String headerString2;
|
---|
33 | private String gsdl3Home;
|
---|
34 | private String logoURL;
|
---|
35 | private String description;
|
---|
36 | private String hostName;
|
---|
37 | private String proxyHost="http.proxyHost";
|
---|
38 | private String proxyPort="http.proxyPort";
|
---|
39 | private String proxyHostContent = "";
|
---|
40 | private String proxyPortContent = "";
|
---|
41 | //private String collection_creator = "";
|
---|
42 | private String proxyUserName= "";
|
---|
43 | private String proxyUserPassword = "";
|
---|
44 |
|
---|
45 | public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
|
---|
46 |
|
---|
47 | port_number = request.getLocalPort();
|
---|
48 | loadRuntimeSettings();
|
---|
49 | PrintWriter out = response.getWriter();
|
---|
50 |
|
---|
51 | response.setContentType("text/html");
|
---|
52 | response.setHeader("pragma", "no-cache");
|
---|
53 |
|
---|
54 | out.println("<html>");
|
---|
55 | out.println(headerString);
|
---|
56 | out.println("<body>");
|
---|
57 | out.println("<img src="+logoURL+">");
|
---|
58 | out.println(h1String);
|
---|
59 | out.println("<p>"+description+"</p>");
|
---|
60 | out.println("<div><form method=\"post\" action=\"http://"+hostName+":"+port_number+"/mat/analyse\">");
|
---|
61 |
|
---|
62 | out.println("<p>OAI URL: <input type=\"text\" name=\"oaiurl\" size=\"140\"></p>");
|
---|
63 | out.println("<p><input type=\"submit\" value=\"Analyse repository\"></p>");
|
---|
64 | out.println("</form>");
|
---|
65 |
|
---|
66 | out.println("<script type=\"text/javaacript\"></script>");
|
---|
67 | out.println("<noscript>Sorry, your browser does not support JavaScript! You need to enable JavaScript in your browser.</noscript>");
|
---|
68 |
|
---|
69 | out.println ("</body>");
|
---|
70 | out.println ("</html>");
|
---|
71 | out.close();
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {
|
---|
75 |
|
---|
76 | port_number = req.getLocalPort();
|
---|
77 | loadRuntimeSettings();
|
---|
78 |
|
---|
79 | res.setContentType("text/html");
|
---|
80 | res.setHeader("pragma", "no-cache");
|
---|
81 | PrintWriter out = res.getWriter();
|
---|
82 |
|
---|
83 | out.println("<html>");
|
---|
84 |
|
---|
85 | if (req.getParameter("metadataPrefix") != null) {
|
---|
86 |
|
---|
87 | oaiPrefix = req.getParameter("metadataPrefix");
|
---|
88 | String maxRecords = req.getParameter("maxrecords");
|
---|
89 | Pattern pa = Pattern.compile("[0-9]{1,5}");
|
---|
90 | Matcher ma = pa.matcher(maxRecords);
|
---|
91 |
|
---|
92 | out.println(headerString2);
|
---|
93 | out.println("<body onLoad=\"autoSubmit()\">");
|
---|
94 | out.println(h1String);
|
---|
95 | out.println("<p>Please wait .... <br> It's downloading OAI records </p>");
|
---|
96 | out.println("<input type=\"button\" onClick=\"showdiv()\" value=\"show debug infomation\">");
|
---|
97 |
|
---|
98 | if(ma.matches()){
|
---|
99 | int num = Integer.parseInt(maxRecords);
|
---|
100 | if(num>0){
|
---|
101 | downloadCollection(out, req, res, oaiPrefix, maxRecords);
|
---|
102 | }
|
---|
103 | else{
|
---|
104 | downloadCollection(out, req, res, oaiPrefix, maxRecord);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | else{
|
---|
108 | downloadCollection(out, req, res, oaiPrefix, maxRecord);
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 | else if(req.getParameter("matShell") != null){
|
---|
113 | out.println(headerString2);
|
---|
114 | out.println("<body onLoad=\"autoSubmit2()\">");
|
---|
115 | out.println(h1String);
|
---|
116 | out.println("<p>Please wait ...</p><p>It's building collection now.</p>");
|
---|
117 | out.println("<input type=\"button\" onClick=\"showdiv()\" value=\"show debug infomation\">");
|
---|
118 | buildCollection(out,req.getParameter("matShell"),req.getParameter("collectionName"),req.getParameter("collectionURL"),req.getParameter("oaiPrefix"));
|
---|
119 | }
|
---|
120 |
|
---|
121 | else if (req.getParameter("collName") != null){
|
---|
122 | out.println(headerString);
|
---|
123 | out.println("<body>");
|
---|
124 | out.println(h1String);
|
---|
125 | analyzeCollection(out, req.getParameter("collName"),req.getParameter("collURL"),req.getParameter("collHost"),req.getParameter("oaiPrefix"));
|
---|
126 | }
|
---|
127 |
|
---|
128 | else {
|
---|
129 | out.println(headerString);
|
---|
130 | out.println("<body>");
|
---|
131 | out.println(h1String);
|
---|
132 |
|
---|
133 | java.net.URL oaiURL;
|
---|
134 | String oaiURLString = req.getParameter("oaiurl");
|
---|
135 |
|
---|
136 | if (! (oaiURLString.startsWith("http://"))) {
|
---|
137 | oaiURLString = "http://" + oaiURLString; // add on protocol if missing
|
---|
138 | }
|
---|
139 |
|
---|
140 | // URL checks
|
---|
141 | // check if Java can make a URL from the string
|
---|
142 |
|
---|
143 | try {
|
---|
144 | oaiURL = new URL(oaiURLString);
|
---|
145 | }
|
---|
146 | catch (MalformedURLException e) {
|
---|
147 | out.println("<p>Malformed URL Exception caught: " + e.getMessage() + "</p>");
|
---|
148 | out.println("<p>The system cannot recognise the URL you have entered.</p>");
|
---|
149 | return; // go no further
|
---|
150 | }
|
---|
151 |
|
---|
152 | //out.println("<p>host: " + oaiURL.getHost() + "</p>" );
|
---|
153 | //out.println("<p>protocol: " + oaiURL.getProtocol() + "</p>" );
|
---|
154 |
|
---|
155 | // disallow anything with waikato as part of the host
|
---|
156 | // note: this blocks things like waikato.uiuc.edu -
|
---|
157 | // but guess this is highly unlikely to occur
|
---|
158 | // (also turns off the researchcommons)
|
---|
159 | // could use either of these approaches, waikato or waikato.ac.nz
|
---|
160 |
|
---|
161 | if (oaiURL.getHost().indexOf("researchcommons.waikato.ac.nz") == -1 ) { // not the RC
|
---|
162 | /*
|
---|
163 | if((oaiURL.getHost().indexOf("waikato") != -1 ) ||
|
---|
164 | (oaiURL.getHost().indexOf("waikato.ac.nz") != -1 ) ) {
|
---|
165 | out.println("<p>This service cannot be used to access Waikato URLs</p>");
|
---|
166 | return;
|
---|
167 | }*/
|
---|
168 | }
|
---|
169 |
|
---|
170 | // need to prevent machine names on their own, e.g. smith
|
---|
171 | // being allowed through
|
---|
172 |
|
---|
173 | // approach 1: explicit blacklisting
|
---|
174 |
|
---|
175 | if (oaiURL.getHost().equals("smith") ||
|
---|
176 | oaiURL.getHost().equals("wesson") ) {
|
---|
177 | out.println("<p>This service cannot be used to access these URLs</p>");
|
---|
178 | return;
|
---|
179 | }
|
---|
180 |
|
---|
181 | // approach 2: require at least one . in the host URL
|
---|
182 | if (oaiURL.getHost().indexOf(".") == -1 ) { // i.e. no . in URL host
|
---|
183 | out.println("<p>This service cannot be used to access URLs of this form.</p>");
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | // jones.cs would get through to this point, might this mean something
|
---|
188 | // from the perspective of the host machine?? Does it matter?
|
---|
189 | // will automatic domain completion be applied? by java? by 'the network'?
|
---|
190 | // require 2 dots in the host? does that help at all ?
|
---|
191 | // domain suffix whitelisting is impractical here, as we'd have to
|
---|
192 | // list all countries in the world
|
---|
193 |
|
---|
194 | // do an OAI verb=identify check to make sure it is an OAI server
|
---|
195 | // trim the URL back and then add on verb=Identify
|
---|
196 |
|
---|
197 |
|
---|
198 | Properties systemSettings = System.getProperties();
|
---|
199 |
|
---|
200 | if( proxyHostContent.equals("") || proxyPortContent.equals("")){}
|
---|
201 | else{
|
---|
202 | systemSettings.put(proxyHost, proxyHostContent);
|
---|
203 | systemSettings.put(proxyPort, proxyPortContent);
|
---|
204 | }
|
---|
205 |
|
---|
206 | URL url = oaiURL;
|
---|
207 | String identifyVerb = "";
|
---|
208 |
|
---|
209 | if (! url.getPath().endsWith("?")) {
|
---|
210 | identifyVerb += "?";
|
---|
211 | }
|
---|
212 |
|
---|
213 | identifyVerb += "verb=Identify";
|
---|
214 |
|
---|
215 | URL identifyURL = new URL (url.toString() + identifyVerb);
|
---|
216 |
|
---|
217 | if( proxyUserName.equals("") || proxyUserPassword.equals("")){}
|
---|
218 | else{
|
---|
219 | Authenticator.setDefault( new ProxyAuthenticator(proxyUserName, proxyUserPassword) );
|
---|
220 | }
|
---|
221 |
|
---|
222 | HttpURLConnection connection = (HttpURLConnection)identifyURL.openConnection();
|
---|
223 | connection.connect();
|
---|
224 |
|
---|
225 |
|
---|
226 | Document identifyDocument;
|
---|
227 |
|
---|
228 | try {
|
---|
229 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
230 | DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
231 | identifyDocument = builder.parse( connection.getInputStream() );
|
---|
232 |
|
---|
233 | Element oaiElement = identifyDocument.getDocumentElement();
|
---|
234 |
|
---|
235 | if (oaiElement.getTagName() == "OAI-PMH" ) {
|
---|
236 | //out.println("<p>OAI-PMH element found...</p>");
|
---|
237 | }
|
---|
238 | else {
|
---|
239 | out.println("<p>Error: OAI-PMH element not found..exiting</p>");
|
---|
240 | return;
|
---|
241 | }
|
---|
242 |
|
---|
243 | NodeList identifyNodeList = identifyDocument.getElementsByTagName("Identify");
|
---|
244 | Node identifyNode = null;
|
---|
245 |
|
---|
246 | if (identifyNodeList.getLength() == 1 ) {
|
---|
247 | identifyNode = identifyNodeList.item(0);
|
---|
248 | System.out.println("<Identify>\n");
|
---|
249 | }
|
---|
250 | else {
|
---|
251 | out.println("<p>Error: Identify node not found... exiting</p>");
|
---|
252 | return;
|
---|
253 | }
|
---|
254 |
|
---|
255 | NodeList identifyChildList = identifyNode.getChildNodes();
|
---|
256 | out.println("<table rules =\"none\">");
|
---|
257 |
|
---|
258 | for (int i=0; i < identifyChildList.getLength(); i++) {
|
---|
259 | if (identifyChildList.item(i).getNodeName() == "repositoryName" ) {
|
---|
260 | out.println("<tr><td><strong>Repository Name:</strong></td><td> " +getTextContent(identifyChildList.item(i)) + "</td></tr>");
|
---|
261 | }
|
---|
262 | if (identifyChildList.item(i).getNodeName() == "baseURL" ) {
|
---|
263 |
|
---|
264 | Node n = identifyChildList.item(i);
|
---|
265 | String baseURL = getTextContent(n);
|
---|
266 |
|
---|
267 |
|
---|
268 |
|
---|
269 | if(baseURL.startsWith("http://")){
|
---|
270 |
|
---|
271 | out.println("<tr><td><strong>Base URL:</strong></td><td><code><a href=\"" + baseURL + "\">" + baseURL + "</a></code></td></tr>");
|
---|
272 | }
|
---|
273 | else{
|
---|
274 |
|
---|
275 | out.println("<tr><td><strong>Base URL:</strong></td><td><code><a href=\"" + oaiURLString + "\">" + oaiURLString + "</a></code></td></tr>");
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 | out.println("</table>");
|
---|
280 | }
|
---|
281 | catch (SAXParseException spe) {
|
---|
282 | out.println(spe.getMessage());
|
---|
283 | }
|
---|
284 | catch (SAXException sxe) {
|
---|
285 | out.println(sxe.toString());
|
---|
286 | }
|
---|
287 | catch (ParserConfigurationException pce) {
|
---|
288 | out.println(pce.toString());
|
---|
289 | }
|
---|
290 | catch (IOException ioe) {
|
---|
291 | out.println(ioe.toString());
|
---|
292 | }
|
---|
293 |
|
---|
294 | // now get the metadata prefixes
|
---|
295 |
|
---|
296 | String metadataFormats = "";
|
---|
297 |
|
---|
298 | if (! url.getPath().endsWith("?")) {
|
---|
299 | metadataFormats += "?";
|
---|
300 | }
|
---|
301 | metadataFormats += "verb=ListMetadataFormats";
|
---|
302 |
|
---|
303 | URL metadataFormatsURL = new URL (url.toString() + metadataFormats);
|
---|
304 | URLConnection connection2 = metadataFormatsURL.openConnection();
|
---|
305 | connection.connect();
|
---|
306 | Document document2;
|
---|
307 |
|
---|
308 | try {
|
---|
309 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
310 | DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
311 | document2 = builder.parse( connection2.getInputStream() );
|
---|
312 |
|
---|
313 | NodeList prefixList = document2.getElementsByTagName("metadataPrefix");
|
---|
314 |
|
---|
315 | // create UI based on response
|
---|
316 |
|
---|
317 | if ( prefixList.getLength() > 0 ) {
|
---|
318 |
|
---|
319 | out.println("<p>Choose one metadata prefix to use:</p>");
|
---|
320 | out.println("<form method=\"post\" action=\"http://"+hostName+":"+port_number+"/mat/analyse\">");
|
---|
321 | out.println("<table rules =\"none\">");
|
---|
322 |
|
---|
323 | for (int i=0; i < prefixList.getLength(); i++) {
|
---|
324 | out.println("<tr><td>");
|
---|
325 | String id = "radioID" + i;
|
---|
326 |
|
---|
327 | Node n = prefixList.item(i);
|
---|
328 | String prefix = getTextContent(n);
|
---|
329 |
|
---|
330 | //String prefix = prefixList.item(i).getNodeValue();
|
---|
331 |
|
---|
332 | if (prefix.equals("oai_dc")) {
|
---|
333 | out.println("<label for=\"" + id + "\">" + prefix + " (Dublin Core)</label>");
|
---|
334 | //System.out.println("<MetadataElement>Dublin Core<\\MetadataElement>\n");
|
---|
335 | }
|
---|
336 | else {
|
---|
337 | out.println("<label for=\"" + id + "\">" + prefix + "</label>");
|
---|
338 | //System.out.println("<MetadataElement>"+prefix+"<\\MetadataElement>\n");
|
---|
339 | }
|
---|
340 |
|
---|
341 | out.print("</td><td><input type=\"radio\" name=\"metadataPrefix\" value=\"" + prefix + "\" ");
|
---|
342 |
|
---|
343 | if (prefix.equals("oai_dc")) {
|
---|
344 | out.println("checked=\"checked\" ");
|
---|
345 | }
|
---|
346 | out.println(" id=\"" + id + "\"><br>");
|
---|
347 | out.println("</td></tr>");
|
---|
348 | }
|
---|
349 |
|
---|
350 | //System.out.println("</Identify>");
|
---|
351 | out.println("</table>");
|
---|
352 |
|
---|
353 | // need to pass oaiurl through as well (again)
|
---|
354 |
|
---|
355 | out.println("<input type=\"hidden\" name=\"oaiURL\" value=\"" + url.toString() + "\"><br>");
|
---|
356 | out.println("Max records: <input type=\"text\" name=\"maxrecords\" value=\"500\"><br>");
|
---|
357 |
|
---|
358 | // submit button
|
---|
359 |
|
---|
360 | out.println("<p><input type=\"submit\" value=\"Continue\"></p>");
|
---|
361 | out.println("</form>");
|
---|
362 |
|
---|
363 | out.println("<p> Warning: Generating the statistics and visualization will take some time: </p> ");
|
---|
364 |
|
---|
365 | /*
|
---|
366 | out.println("<table border = \"1\">");
|
---|
367 | out.println("<tr align=\"right\"><td> No.of Records <td> Estimated Time");
|
---|
368 | out.println("<tr align=\"right\"><td>100<td> 5 minutes");
|
---|
369 | out.println("<tr align=\"right\"><td>500 <td> 10 minutes");
|
---|
370 | out.println("<tr align=\"right\"><td>1000 <td> 18 minutes");
|
---|
371 | out.println("<tr align=\"right\"><td>2000<td> 30 minutes");
|
---|
372 | out.println("</table>");
|
---|
373 | */
|
---|
374 |
|
---|
375 | out.println("<p> This tool is designed to work with Dublin Core metadata: note that the mapping of qualified Dublin Core to simple Dublin Core (as in <code>oai_dc</code>) may affect the results.");
|
---|
376 | }
|
---|
377 | else {
|
---|
378 | out.println("<p>Error: no metadata prefixes found... exiting</p>");
|
---|
379 | return;
|
---|
380 | }
|
---|
381 | }
|
---|
382 | catch (SAXParseException spe) {
|
---|
383 | out.println(spe.getMessage());
|
---|
384 | }
|
---|
385 | catch (SAXException sxe) {
|
---|
386 | out.println(sxe.toString());
|
---|
387 | }
|
---|
388 | catch (ParserConfigurationException pce) {
|
---|
389 | out.println(pce.toString());
|
---|
390 | }
|
---|
391 | catch (IOException ioe) {
|
---|
392 | out.println(ioe.toString());
|
---|
393 | }
|
---|
394 |
|
---|
395 | // check we get a valid XML document back
|
---|
396 | // check OAI-PMH element
|
---|
397 | // check we have a <repositoryName> and <baseURL> elements
|
---|
398 | // maybe check the baseURL against the query we issued?
|
---|
399 |
|
---|
400 | out.print("</body></html>");
|
---|
401 | out.close();
|
---|
402 | } // endif
|
---|
403 | } // end doPost
|
---|
404 |
|
---|
405 |
|
---|
406 | private static String getTextContent(Node node) {
|
---|
407 | Node child;
|
---|
408 | String sContent = node.getNodeValue() != null ? node.getNodeValue() : "";
|
---|
409 |
|
---|
410 | NodeList nodes = node.getChildNodes();
|
---|
411 | for(int i = 0; i < nodes.getLength(); i++) {
|
---|
412 | child = nodes.item(i);
|
---|
413 | sContent += child.getNodeValue() != null ? child.getNodeValue() : "";
|
---|
414 | if(nodes.item(i).getChildNodes().getLength() > 0) {
|
---|
415 | sContent += getTextContent(nodes.item(i));
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | return sContent;
|
---|
420 |
|
---|
421 | }
|
---|
422 | protected void buildCollection( PrintWriter out, String matShell2, String collName, String oaiURLString, String oaiPrefix) throws ServletException, IOException{
|
---|
423 |
|
---|
424 | out.println("<br><br><div id=\"hideshow\" style=\"visibility:hidden\">");
|
---|
425 | out.println("<input type=\"button\" onClick=\"hidediv()\" value=\"hide debug infomation\"/>");
|
---|
426 | out.println("<p>Building collection...</p>");
|
---|
427 |
|
---|
428 | String host = "http://"+hostName+":"+port_number+"/mat/";
|
---|
429 |
|
---|
430 | out.println("<form method=\"post\" name=\"aForm\">");
|
---|
431 | out.println("<input type=\"hidden\" name=\"collName\" value=\"" + collName + "\"><br>");
|
---|
432 | out.println("<input type=\"hidden\" name=\"collURL\" value=\"" + oaiURLString + "\"><br>");
|
---|
433 | out.println("<input type=\"hidden\" name=\"collHost\" value=\"" + host + "\"><br>");
|
---|
434 | out.println("<input type=\"hidden\" name=\"oaiPrefix\" value=\"" + oaiPrefix + "\"><br>");
|
---|
435 | out.println("</form>");
|
---|
436 | out.println("<p><pre>" + matShell2 + "</pre></p>");
|
---|
437 | out.flush();
|
---|
438 |
|
---|
439 |
|
---|
440 | String os = "linux";
|
---|
441 | if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
|
---|
442 | gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
|
---|
443 | os = "windows";
|
---|
444 | }
|
---|
445 | String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
|
---|
446 |
|
---|
447 | if(os.equals("windows")){
|
---|
448 | gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
|
---|
449 | gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
|
---|
450 | }
|
---|
451 |
|
---|
452 | // downloading
|
---|
453 | //String gs3Root = "/research/cc108/greenstone3Project";
|
---|
454 | String gs3Root = gsdl3Root;
|
---|
455 | String collectDir = gsdl3Home + fileSeparator +"sites"+fileSeparator+"localsite"+ fileSeparator +"collect";
|
---|
456 | String logFile = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + "log.txt";
|
---|
457 | String cacheDir = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + collName;
|
---|
458 |
|
---|
459 | if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
|
---|
460 | File wd = new File(gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script");
|
---|
461 |
|
---|
462 |
|
---|
463 | String[] arrays = new String[15];
|
---|
464 | arrays[0] = "cmd";
|
---|
465 | arrays[1] = "/c";
|
---|
466 | arrays[2] = "start";
|
---|
467 | arrays[3] = "/MIN";
|
---|
468 | arrays[4] = "mat-colbuild.bat";
|
---|
469 | arrays[5] = collName;
|
---|
470 | arrays[6] = oaiURLString;
|
---|
471 | arrays[7] = cacheDir;
|
---|
472 | arrays[8] = "100";
|
---|
473 | arrays[9] = oaiPrefix;
|
---|
474 | arrays[10] = gs3Root;
|
---|
475 | arrays[11] = collectDir;
|
---|
476 | arrays[12] = logFile;
|
---|
477 | arrays[13] = hostName;
|
---|
478 | arrays[14] = "" + port_number;
|
---|
479 |
|
---|
480 | Process p2 = processBatch(arrays,out,wd);
|
---|
481 | out.print("</div></body></html>");
|
---|
482 | p2 = null;
|
---|
483 | p2.destroy();
|
---|
484 | }
|
---|
485 | else{
|
---|
486 | Process p2 = processShell(matShell2, out);
|
---|
487 |
|
---|
488 | if (p2.exitValue() == 0){
|
---|
489 | out.println("<p>Collection built.</p>");
|
---|
490 | }
|
---|
491 | else{
|
---|
492 | out.println("<p><b>Collection not built.</b></p>");
|
---|
493 | }
|
---|
494 |
|
---|
495 | out.print("</div></body></html>");
|
---|
496 | p2 = null;
|
---|
497 | p2.destroy();
|
---|
498 | }
|
---|
499 | out.close();
|
---|
500 | }
|
---|
501 |
|
---|
502 | private void downloadCollection( PrintWriter out, HttpServletRequest req, HttpServletResponse res, String oaiPrefix, String Records) throws ServletException, IOException
|
---|
503 | {
|
---|
504 | String oaiURLString = req.getParameter("oaiURL");
|
---|
505 | String metadataprefix = req.getParameter("metadataPrefix");
|
---|
506 | String collName = generateCollName();
|
---|
507 | String host = "http://"+hostName+":"+port_number+"/";
|
---|
508 |
|
---|
509 | out.println("<br><br><div id=\"hideshow\" style=\"visibility:hidden\">");
|
---|
510 | out.println("<input type=\"button\" onClick=\"hidediv()\" value=\"hide debug infomation\"/>");
|
---|
511 | out.println("<p>Downloading OAI documents...</p>");
|
---|
512 | out.flush();
|
---|
513 | // move to correct directory
|
---|
514 |
|
---|
515 |
|
---|
516 | String os = "linux";
|
---|
517 | if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
|
---|
518 | gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
|
---|
519 | os = "windows";
|
---|
520 | }
|
---|
521 | String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
|
---|
522 |
|
---|
523 | if(os.equals("windows")){
|
---|
524 | gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
|
---|
525 | gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
|
---|
526 | }
|
---|
527 | String maxRecords = Records;
|
---|
528 | String cacheDir = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + collName;
|
---|
529 |
|
---|
530 |
|
---|
531 | String gs3Root = gsdl3Root;
|
---|
532 | String collectDir = gsdl3Home + fileSeparator +"sites"+fileSeparator+"localsite"+ fileSeparator +"collect";
|
---|
533 | String logFile = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + "log.txt";
|
---|
534 | String proxy_settings = " ";
|
---|
535 | String matShell = "";
|
---|
536 |
|
---|
537 |
|
---|
538 | if(!proxyHostContent.equals("") && !proxyPortContent.equals("")){
|
---|
539 | proxy_settings = "\"-proxy_on -proxy_host "+proxyHostContent+" -proxy_port "+proxyPortContent+"\"";
|
---|
540 |
|
---|
541 |
|
---|
542 | if(!proxyUserName.equals("") && !proxyUserPassword.equals("")){
|
---|
543 | proxy_settings = proxy_settings.substring(1,proxy_settings.length()-1);
|
---|
544 | proxy_settings = "\""+ proxy_settings + " -user_name " +proxyUserName+" -user_password "+proxyUserPassword +"\"";
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 |
|
---|
550 | if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
|
---|
551 |
|
---|
552 | File wd = new File(gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script");
|
---|
553 | ArrayList alist = new ArrayList();
|
---|
554 | alist.add("cmd");
|
---|
555 | alist.add("/c");
|
---|
556 | alist.add("start");
|
---|
557 | alist.add("/MIN");
|
---|
558 | alist.add("mat-colbuild-download.bat");
|
---|
559 | alist.add(collName);
|
---|
560 | alist.add(oaiURLString);
|
---|
561 | alist.add(cacheDir);
|
---|
562 | alist.add(maxRecords);
|
---|
563 | alist.add(metadataprefix);
|
---|
564 | alist.add(gs3Root);
|
---|
565 | alist.add(collectDir);
|
---|
566 | alist.add(logFile);
|
---|
567 | //alist.add(collection_creator);
|
---|
568 | alist.add(proxy_settings);
|
---|
569 | String[] arrays = new String[alist.size()];
|
---|
570 | for(int i = 0; i< arrays.length; i++){
|
---|
571 | arrays[i] = (String)alist.get(i);
|
---|
572 | }
|
---|
573 | Process p = processBatch(arrays,out,wd);
|
---|
574 | out.flush();
|
---|
575 |
|
---|
576 |
|
---|
577 | p.destroy();
|
---|
578 |
|
---|
579 | }
|
---|
580 | else{
|
---|
581 | matShell = gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script" + fileSeparator + "mat-colbuild-download.bash "
|
---|
582 | + collName
|
---|
583 | + " "
|
---|
584 | + oaiURLString
|
---|
585 | + " "
|
---|
586 | + cacheDir
|
---|
587 | + " "
|
---|
588 | + maxRecords
|
---|
589 | + " "
|
---|
590 | + metadataprefix
|
---|
591 | + " "
|
---|
592 | + gs3Root
|
---|
593 | + " "
|
---|
594 | + collectDir
|
---|
595 | + " "
|
---|
596 | + logFile
|
---|
597 | + " "
|
---|
598 | // + collection_creator
|
---|
599 | // + " "
|
---|
600 | + proxy_settings;
|
---|
601 |
|
---|
602 | out.println("<p><pre>" + matShell + "</pre></p>");
|
---|
603 | out.flush();
|
---|
604 | Process p = processShell(matShell, out);
|
---|
605 |
|
---|
606 | if (p.exitValue() == 0){
|
---|
607 | out.println("<p>Collection downloaded.</p>");
|
---|
608 | }
|
---|
609 | else {
|
---|
610 | out.println("<p><b>Collection not downloaded properly.</b></p>");
|
---|
611 | }
|
---|
612 | out.flush();
|
---|
613 | p.destroy();
|
---|
614 |
|
---|
615 | }
|
---|
616 |
|
---|
617 | String matShell2 = "";
|
---|
618 |
|
---|
619 | matShell2 = gsdl3Root + fileSeparator + "ext" + fileSeparator +"mat"+ fileSeparator +"bin" + fileSeparator + "script" + fileSeparator + "mat-colbuild.bash "
|
---|
620 | + collName
|
---|
621 | + " "
|
---|
622 | + oaiURLString
|
---|
623 | + " "
|
---|
624 | + cacheDir
|
---|
625 | + " "
|
---|
626 | + maxRecords
|
---|
627 | + " "
|
---|
628 | + metadataprefix
|
---|
629 | + " "
|
---|
630 | + gs3Root
|
---|
631 | + " "
|
---|
632 | + collectDir
|
---|
633 | + " "
|
---|
634 | + logFile
|
---|
635 | + " "
|
---|
636 | + hostName + ":" + port_number;
|
---|
637 |
|
---|
638 | out.println("<form method=\"post\" name=\"collectionForm\">");
|
---|
639 | out.println("<input type=\"hidden\" name=\"matShell\" value=\"" + matShell2 + "\">");
|
---|
640 | out.println("<input type=\"hidden\" name=\"collectionName\" value=\"" + collName + "\">");
|
---|
641 | out.println("<input type=\"hidden\" name=\"collectionURL\" value=\"" + oaiURLString + "\">");
|
---|
642 | out.println("<input type=\"hidden\" name=\"oaiPrefix\" value=\"" + oaiPrefix + "\">");
|
---|
643 | out.println("<input type=\"hidden\" name=\"host\" value=\"" + host + "\">");
|
---|
644 | //out.println("<input type=\"submit\" value=\"start\"><br>");
|
---|
645 | out.println("</form>");
|
---|
646 | out.println("</div>");
|
---|
647 | out.print("</body></html>");
|
---|
648 | out.flush();
|
---|
649 | out.close();
|
---|
650 |
|
---|
651 | }
|
---|
652 |
|
---|
653 | /* produce a random 7 letter collection name */
|
---|
654 |
|
---|
655 | private String generateCollName () {
|
---|
656 | Random random = new Random();
|
---|
657 | StringBuffer message = new StringBuffer();
|
---|
658 | int offset = 97; // = "a"
|
---|
659 | message.append( (char) ( random.nextInt( 26 ) + offset ) );
|
---|
660 | message.append( (char) ( random.nextInt( 26 ) + offset ) );
|
---|
661 | message.append( (char) ( random.nextInt( 26 ) + offset ) );
|
---|
662 | message.append( (char) ( random.nextInt( 26 ) + offset ) );
|
---|
663 | message.append( (char) ( random.nextInt( 26 ) + offset ) );
|
---|
664 | message.append( (char) ( random.nextInt( 26 ) + offset ) );
|
---|
665 | message.append( (char) ( random.nextInt( 26 ) + offset ) );
|
---|
666 | return message.toString();
|
---|
667 | }
|
---|
668 |
|
---|
669 | private Process processBatch(String[] command, PrintWriter out, File wd){
|
---|
670 |
|
---|
671 | Process proc;
|
---|
672 | String s;
|
---|
673 | try{
|
---|
674 | proc = Runtime.getRuntime().exec(command,null,wd);
|
---|
675 | BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
|
---|
676 | BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
|
---|
677 | StringBuffer stdInputBuffer = new StringBuffer();
|
---|
678 | while ((s = stdInput.readLine()) != null) {
|
---|
679 | stdInputBuffer.append(s+"\n");
|
---|
680 | if(s.indexOf("-->")!=-1){
|
---|
681 | s = s.replaceAll("-->", "");
|
---|
682 | //out.println("<!-- " + s + " -->");
|
---|
683 | }
|
---|
684 | else{
|
---|
685 | //out.println("<!-- " + s + " -->");
|
---|
686 | }
|
---|
687 | out.flush();
|
---|
688 | }
|
---|
689 |
|
---|
690 | StringBuffer stdErrorBuffer = new StringBuffer();
|
---|
691 |
|
---|
692 | while ((s = stdError.readLine()) != null) {
|
---|
693 | stdErrorBuffer.append(s+"\n");
|
---|
694 |
|
---|
695 | if(s.indexOf("-->")!=-1){
|
---|
696 | s = s.replaceAll("-->", "");
|
---|
697 | out.println("<!-- " + s + " -->");
|
---|
698 | }
|
---|
699 | else{
|
---|
700 | out.println("<!-- " + s + " -->");
|
---|
701 | }
|
---|
702 | out.flush();
|
---|
703 | }
|
---|
704 |
|
---|
705 |
|
---|
706 | out.println("<p>Here is the standard output:</p>\n");
|
---|
707 | out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
|
---|
708 | out.println("<p>Here is the standard error (if any):</p>\n");
|
---|
709 | out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
|
---|
710 | out.flush();
|
---|
711 | //if (p.exitValue() != 0)
|
---|
712 | if (false){
|
---|
713 | out.println("<p>An error occurred while building the collection.</p>");
|
---|
714 | out.println("<p>Here is the standard output:</p>\n");
|
---|
715 | out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
|
---|
716 | out.println("<p>Here is the standard error (if any):</p>\n");
|
---|
717 | out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
|
---|
718 | }
|
---|
719 |
|
---|
720 | InputStream is = proc.getInputStream();
|
---|
721 | is.close();
|
---|
722 |
|
---|
723 | OutputStream os = proc.getOutputStream();
|
---|
724 | os.close();
|
---|
725 |
|
---|
726 | InputStream es = proc.getErrorStream();
|
---|
727 | es.close();
|
---|
728 |
|
---|
729 | stdInput.close();
|
---|
730 | stdError.close();
|
---|
731 | proc.waitFor();
|
---|
732 | return proc;
|
---|
733 |
|
---|
734 | }catch(Exception ex){
|
---|
735 | ex.printStackTrace();
|
---|
736 | }
|
---|
737 | return null;
|
---|
738 | }
|
---|
739 | private Process processShell( String command, PrintWriter out) {
|
---|
740 |
|
---|
741 | String s= "";
|
---|
742 | try {
|
---|
743 | String[] args = new String[]{"sh", "-c", command};
|
---|
744 | Process p = Runtime.getRuntime().exec(args);
|
---|
745 |
|
---|
746 | BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
---|
747 | BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
|
---|
748 | StringBuffer stdInputBuffer = new StringBuffer();
|
---|
749 |
|
---|
750 | while ((s = stdInput.readLine()) != null) {
|
---|
751 | stdInputBuffer.append(s+"\n");
|
---|
752 | if(s.indexOf("-->")!=-1){
|
---|
753 | s = s.replaceAll("-->", "");
|
---|
754 | //out.println("<!-- " + s + " -->");
|
---|
755 | }
|
---|
756 | else{
|
---|
757 | //out.println("<!-- " + s + " -->");
|
---|
758 | }
|
---|
759 | out.flush();
|
---|
760 | }
|
---|
761 |
|
---|
762 | StringBuffer stdErrorBuffer = new StringBuffer();
|
---|
763 |
|
---|
764 | while ((s = stdError.readLine()) != null) {
|
---|
765 | stdErrorBuffer.append(s+"\n");
|
---|
766 |
|
---|
767 | if(s.indexOf("-->")!=-1){
|
---|
768 | s = s.replaceAll("-->", "");
|
---|
769 | out.println("<!-- " + s + " -->");
|
---|
770 | }
|
---|
771 | else{
|
---|
772 | out.println("<!-- " + s + " -->");
|
---|
773 | }
|
---|
774 | out.flush();
|
---|
775 | }
|
---|
776 |
|
---|
777 | out.println("<p>Here is the standard output:</p>\n");
|
---|
778 | out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
|
---|
779 | out.println("<p>Here is the standard error (if any):</p>\n");
|
---|
780 | out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
|
---|
781 | out.flush();
|
---|
782 | //if (p.exitValue() != 0)
|
---|
783 | if (false){
|
---|
784 | out.println("<p>An error occurred while building the collection.</p>");
|
---|
785 | out.println("<p>Here is the standard output:</p>\n");
|
---|
786 | out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
|
---|
787 | out.println("<p>Here is the standard error (if any):</p>\n");
|
---|
788 | out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
|
---|
789 | }
|
---|
790 |
|
---|
791 | InputStream is = p.getInputStream();
|
---|
792 | is.close();
|
---|
793 |
|
---|
794 | OutputStream os = p.getOutputStream();
|
---|
795 | os.close();
|
---|
796 |
|
---|
797 | InputStream es = p.getErrorStream();
|
---|
798 | es.close();
|
---|
799 |
|
---|
800 | stdInput.close();
|
---|
801 | stdError.close();
|
---|
802 |
|
---|
803 | return p;
|
---|
804 | }
|
---|
805 | catch (IOException e) {
|
---|
806 | out.println("exception happened - here's what I know: ");
|
---|
807 | out.println(e.toString());
|
---|
808 | out.flush();
|
---|
809 | }
|
---|
810 | return null;
|
---|
811 | }
|
---|
812 |
|
---|
813 | private void analyzeCollection(PrintWriter out, String collectionName,String collectionURL,String collectionHost, String Prefix){
|
---|
814 |
|
---|
815 | String collName = collectionName;
|
---|
816 | String oaiURLString = collectionURL;
|
---|
817 | String host = collectionHost;
|
---|
818 |
|
---|
819 | try{
|
---|
820 | DescribeMessenger dm = new DescribeMessenger(collName,oaiURLString);
|
---|
821 | out.println("<p>Generating statistics and visualisations...</p>");
|
---|
822 | out.flush();
|
---|
823 | out.println("<p>please wait.</p>");
|
---|
824 | out.flush();
|
---|
825 | boolean status = dm.describeMatadata(out,collName,oaiURLString,Prefix,port_number);
|
---|
826 |
|
---|
827 | if(status){
|
---|
828 |
|
---|
829 | out.println("<script type=\"text/javascript\" language=\"JavaScript\" src = \"http://"+hostName+":"+port_number+"/mat/script/getInfomation.js\">");
|
---|
830 | out.println("reconfig();");
|
---|
831 | out.println("</script>");
|
---|
832 |
|
---|
833 | out.println("<a href=\""+host+collName+"/Overall.html\">View the report</a>");
|
---|
834 | out.flush();
|
---|
835 | }
|
---|
836 | out.println("</body></html>");
|
---|
837 | out.flush();
|
---|
838 | }catch(Exception e){e.printStackTrace(out);
|
---|
839 | out.println("<p><pre>"+e.toString()+"</pre></p>");
|
---|
840 | }
|
---|
841 | out.close();
|
---|
842 | }
|
---|
843 |
|
---|
844 | private void loadRuntimeSettings(){
|
---|
845 |
|
---|
846 |
|
---|
847 | fileSeparator = File.separator;
|
---|
848 | gsdl3Home = globalProperty.getGSDL3Home();
|
---|
849 | globalProperty = new GlobalProperties();
|
---|
850 | maxRecord ="10";
|
---|
851 |
|
---|
852 | String os = "linux";
|
---|
853 | if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
|
---|
854 | gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
|
---|
855 | os = "windows";
|
---|
856 | }
|
---|
857 | String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
|
---|
858 |
|
---|
859 | if(os.equals("windows")){
|
---|
860 | gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
|
---|
861 | gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
|
---|
862 | }
|
---|
863 |
|
---|
864 | try{
|
---|
865 | Properties prop = new Properties();
|
---|
866 | FileInputStream fis = new FileInputStream(gsdl3Root+fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"properties.xml");
|
---|
867 | prop.load(fis);
|
---|
868 |
|
---|
869 | titleString ="<title>"+ prop.getProperty("Servlet.Title")+"</title>";
|
---|
870 | h1String ="<h2>"+ prop.getProperty("Servlet.Head")+"</h2>";
|
---|
871 | cssString = "<link rel=\"stylesheet\" href=\"http://"+hostName+":"+port_number+"/mat/script/mat.css\" type=\"text/css\" >";
|
---|
872 | javaScript = "<script type=\"text/javascript\" src=\"http://"+hostName+":"+port_number+"/mat/script/status3.js\"></script>";;
|
---|
873 | headerString2 = "<head>" + titleString + "\n" + javaScript + cssString+"</head>\n";
|
---|
874 | headerString ="<head>" + titleString + "\n" + cssString + "</head>\n";
|
---|
875 | logoURL =prop.getProperty("Servlet.Logo");
|
---|
876 | description = prop.getProperty("Servlet.Description");
|
---|
877 |
|
---|
878 | proxyHostContent = prop.getProperty("Servlet.proxyHost");
|
---|
879 | proxyPortContent = prop.getProperty("Servlet.proxyPort");
|
---|
880 |
|
---|
881 | proxyUserName = prop.getProperty("Servlet.proxyUserName");
|
---|
882 | proxyUserPassword = prop.getProperty("Servlet.proxyUserPassword");
|
---|
883 |
|
---|
884 | //collection_creator = prop.getProperty("Servlet.Collection.Creator");
|
---|
885 |
|
---|
886 | }catch(Exception ex){
|
---|
887 | ex.printStackTrace();
|
---|
888 | }
|
---|
889 |
|
---|
890 |
|
---|
891 | try {
|
---|
892 | java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
|
---|
893 | hostName = localMachine.getHostName();
|
---|
894 | }catch (java.net.UnknownHostException uhe) {
|
---|
895 | uhe.printStackTrace();
|
---|
896 | }
|
---|
897 |
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 | class ProxyAuthenticator extends Authenticator
|
---|
902 | {
|
---|
903 |
|
---|
904 | private String username;
|
---|
905 | private char[] password;
|
---|
906 |
|
---|
907 |
|
---|
908 | public ProxyAuthenticator (String username, String password){
|
---|
909 | this.username = username;
|
---|
910 | this.password = password.toCharArray();
|
---|
911 | }
|
---|
912 |
|
---|
913 |
|
---|
914 | protected PasswordAuthentication getPasswordAuthentication(){
|
---|
915 | return new PasswordAuthentication (username, password );
|
---|
916 | }
|
---|
917 | }
|
---|