source: other-projects/trunk/greenstone3-extension/mat/src/org/greenstone/gsdl3_extension/mat/servlet/MatServlet.java@ 18093

Last change on this file since 18093 was 18093, checked in by cc108, 15 years ago

new Mat source code

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