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

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

adding new regular expreesion to this class

  • Property svn:executable set to *
File size: 30.4 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).getNodeValue() + "</td></tr>");
259 }
260 if (identifyChildList.item(i).getNodeName() == "baseURL" ) {
261
262 Node n = identifyChildList.item(i);
263 String baseURL = getTextContent(n);
264
265
266
267 if(baseURL.startsWith("http://")){
268
269 out.println("<tr><td><strong>Base URL:</strong></td><td><code><a href=\"" + baseURL + "\">" + baseURL + "</a></code></td></tr>");
270 }
271 else{
272
273 out.println("<tr><td><strong>Base URL:</strong></td><td><code><a href=\"" + oaiURLString + "\">" + oaiURLString + "</a></code></td></tr>");
274 }
275 }
276 }
277 out.println("</table>");
278 }
279 catch (SAXParseException spe) {
280 out.println(spe.getMessage());
281 }
282 catch (SAXException sxe) {
283 out.println(sxe.toString());
284 }
285 catch (ParserConfigurationException pce) {
286 out.println(pce.toString());
287 }
288 catch (IOException ioe) {
289 out.println(ioe.toString());
290 }
291
292 // now get the metadata prefixes
293
294 String metadataFormats = "";
295
296 if (! url.getPath().endsWith("?")) {
297 metadataFormats += "?";
298 }
299 metadataFormats += "verb=ListMetadataFormats";
300
301 URL metadataFormatsURL = new URL (url.toString() + metadataFormats);
302 URLConnection connection2 = metadataFormatsURL.openConnection();
303 connection.connect();
304 Document document2;
305
306 try {
307 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
308 DocumentBuilder builder = factory.newDocumentBuilder();
309 document2 = builder.parse( connection2.getInputStream() );
310
311 NodeList prefixList = document2.getElementsByTagName("metadataPrefix");
312
313 // create UI based on response
314
315 if ( prefixList.getLength() > 0 ) {
316
317 out.println("<p>Choose one metadata prefix to use:</p>");
318 out.println("<form method=\"post\" action=\"http://"+hostName+":"+port_number+"/greenstone3/mat\">");
319 out.println("<table rules =\"none\">");
320
321 for (int i=0; i < prefixList.getLength(); i++) {
322 out.println("<tr><td>");
323 String id = "radioID" + i;
324
325 Node n = prefixList.item(i);
326 String prefix = getTextContent(n);
327
328 //String prefix = prefixList.item(i).getNodeValue();
329
330 if (prefix.equals("oai_dc")) {
331 out.println("<label for=\"" + id + "\">" + prefix + " (Dublin Core)</label>");
332 //System.out.println("<MetadataElement>Dublin Core<\\MetadataElement>\n");
333 }
334 else {
335 out.println("<label for=\"" + id + "\">" + prefix + "</label>");
336 //System.out.println("<MetadataElement>"+prefix+"<\\MetadataElement>\n");
337 }
338
339 out.print("</td><td><input type=\"radio\" name=\"metadataPrefix\" value=\"" + prefix + "\" ");
340
341 if (prefix.equals("oai_dc")) {
342 out.println("checked=\"checked\" ");
343 }
344 out.println(" id=\"" + id + "\"><br>");
345 out.println("</td></tr>");
346 }
347
348 //System.out.println("</Identify>");
349 out.println("</table>");
350
351 // need to pass oaiurl through as well (again)
352
353 out.println("<input type=\"hidden\" name=\"oaiURL\" value=\"" + url.toString() + "\"><br>");
354 out.println("Max records: <input type=\"text\" name=\"maxrecords\" value=\"500\"><br>");
355
356 // submit button
357
358 out.println("<p><input type=\"submit\" value=\"Continue\"></p>");
359 out.println("</form>");
360
361 out.println("<p> Warning: Generating the statistics and visualization will take some time: </p> ");
362
363 /*
364 out.println("<table border = \"1\">");
365 out.println("<tr align=\"right\"><td> No.of Records <td> Estimated Time");
366 out.println("<tr align=\"right\"><td>100<td> 5 minutes");
367 out.println("<tr align=\"right\"><td>500 <td> 10 minutes");
368 out.println("<tr align=\"right\"><td>1000 <td> 18 minutes");
369 out.println("<tr align=\"right\"><td>2000<td> 30 minutes");
370 out.println("</table>");
371 */
372
373 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.");
374 }
375 else {
376 out.println("<p>Error: no metadata prefixes found... exiting</p>");
377 return;
378 }
379 }
380 catch (SAXParseException spe) {
381 out.println(spe.getMessage());
382 }
383 catch (SAXException sxe) {
384 out.println(sxe.toString());
385 }
386 catch (ParserConfigurationException pce) {
387 out.println(pce.toString());
388 }
389 catch (IOException ioe) {
390 out.println(ioe.toString());
391 }
392
393 // check we get a valid XML document back
394 // check OAI-PMH element
395 // check we have a <repositoryName> and <baseURL> elements
396 // maybe check the baseURL against the query we issued?
397
398
399 // if we get here we have a valid non-waikato non-local OAI server
400 // /* process stuff here */
401 // test with University home page - as it doesn't require
402 // dealing with the proxy server
403 // URL url = new URL("http://waikato.ac.nz");
404 // URL url = new URL(oaiURL);
405 // need to go through the proxy here
406 // http://dn.codegear.com/article/29783
407 // http://java.ittoolbox.com/groups/technical-functional/java-l/response-to-proxy-authentication-exception-71438
408 // http://www.jguru.com/faq/view.jsp?EID=13186
409 // http://www.developer.com/java/other/article.php/1551421
410 // System.setProperty("http.proxyHost","http://proxy.scms.waikato.ac.nz");
411 // System.setProperty("http.proxyPort","80");
412 // System.setProperty("http.nonProxyHosts", "localhost|*.waikato.ac.nz");
413 // build a GS 3 collection from the OAI URL
414 // http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
415 // http://codon.kribb.re.kr/wiki/display/Java/Using+Runtime.exec+to+invoke+child+process
416
417 out.print("</body></html>");
418 out.close();
419 } // endif
420 } // end doPost
421
422
423 private static String getTextContent(Node node) {
424 Node child;
425 String sContent = node.getNodeValue() != null ? node.getNodeValue() : "";
426
427 NodeList nodes = node.getChildNodes();
428 for(int i = 0; i < nodes.getLength(); i++) {
429 child = nodes.item(i);
430 sContent += child.getNodeValue() != null ? child.getNodeValue() : "";
431 if(nodes.item(i).getChildNodes().getLength() > 0) {
432 sContent += getTextContent(nodes.item(i));
433 }
434 }
435
436 return sContent;
437
438 }
439 protected void buildCollection( PrintWriter out, String matShell2, String collName, String oaiURLString, String oaiPrefix) throws ServletException, IOException{
440
441 out.println("<br><br><div id=\"hideshow\" style=\"visibility:hidden\">");
442 out.println("<input type=\"button\" onClick=\"hidediv()\" value=\"hide debug infomation\"/>");
443 out.println("<p>Building collection...</p>");
444
445 String host = "http://"+hostName+":"+port_number+"/greenstone3/mat/";
446
447 out.println("<form method=\"post\" name=\"aForm\">");
448 out.println("<input type=\"hidden\" name=\"collName\" value=\"" + collName + "\"><br>");
449 out.println("<input type=\"hidden\" name=\"collURL\" value=\"" + oaiURLString + "\"><br>");
450 out.println("<input type=\"hidden\" name=\"collHost\" value=\"" + host + "\"><br>");
451 out.println("<input type=\"hidden\" name=\"oaiPrefix\" value=\"" + oaiPrefix + "\"><br>");
452 out.println("</form>");
453 out.println("<p><pre>" + matShell2 + "</pre></p>");
454 out.flush();
455
456
457 String os = "linux";
458 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
459 gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
460 os = "windows";
461 }
462 String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
463
464 if(os.equals("windows")){
465 gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
466 gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
467 }
468
469 // downloading
470 //String gs3Root = "/research/cc108/greenstone3Project";
471 String gs3Root = gsdl3Root;
472 String collectDir = gsdl3Home + fileSeparator +"sites"+fileSeparator+"localsite"+ fileSeparator +"collect";
473 String logFile = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + "log.txt";
474 String cacheDir = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + collName;
475
476 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
477 File wd = new File(gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script");
478
479 String[] arrays = new String[12];
480 arrays[0] = "cmd";
481 arrays[1] = "/c";
482 arrays[2] = "start";
483 arrays[3] = "mat-colbuild.bat";
484 arrays[4] = collName;
485 arrays[5] = oaiURLString;
486 arrays[6] = cacheDir;
487 arrays[7] = "100";
488 arrays[8] = oaiPrefix;
489 arrays[9] = gs3Root;
490 arrays[10] = collectDir;
491 arrays[11] = logFile;
492
493 Process p2 = processBatch(arrays,out,wd);
494 out.print("</div></body></html>");
495 p2 = null;
496 p2.destroy();
497 }
498 else{
499 Process p2 = processShell(matShell2, out);
500
501 if (p2.exitValue() == 0){
502 out.println("<p>Collection built.</p>");
503 }
504 else{
505 out.println("<p><b>Collection not built.</b></p>");
506 }
507
508 out.print("</div></body></html>");
509 p2 = null;
510 p2.destroy();
511 }
512 out.close();
513 }
514
515 private void downloadCollection( PrintWriter out, HttpServletRequest req, HttpServletResponse res, String oaiPrefix, String Records) throws ServletException, IOException
516 {
517 String oaiURLString = req.getParameter("oaiURL");
518 String metadataprefix = req.getParameter("metadataPrefix");
519 String collName = generateCollName();
520 String host = "http://"+hostName+":"+port_number+"/";
521
522 out.println("<br><br><div id=\"hideshow\" style=\"visibility:hidden\">");
523 out.println("<input type=\"button\" onClick=\"hidediv()\" value=\"hide debug infomation\"/>");
524 out.println("<p>Downloading OAI documents...</p>");
525 out.flush();
526 // move to correct directory
527
528
529 String os = "linux";
530 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
531 gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
532 os = "windows";
533 }
534 String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
535
536 if(os.equals("windows")){
537 gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
538 gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
539 }
540 String maxRecords = Records;
541 String cacheDir = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + collName;
542
543 // downloading
544 //String gs3Root = "/research/cc108/greenstone3Project";
545 String gs3Root = gsdl3Root;
546 String collectDir = gsdl3Home + fileSeparator +"sites"+fileSeparator+"localsite"+ fileSeparator +"collect";
547 String logFile = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + "log.txt";
548 //source /research/cc108/greenstone3Project/gs2build/bin/script/mat-colbuild-download.bash
549 //source /research/cc108/greenstone3Project/ext/mat/bin/script/mat-colbuild-download.bash
550
551 String matShell = "";
552
553 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
554
555 File wd = new File(gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script");
556 ArrayList alist = new ArrayList();
557 alist.add("cmd");
558 alist.add("/c");
559 alist.add("start");
560 alist.add("mat-colbuild-download.bat");
561 alist.add(collName);
562 alist.add(oaiURLString);
563 alist.add(cacheDir);
564 alist.add(maxRecords);
565 alist.add(metadataprefix);
566 alist.add(gs3Root);
567 alist.add(collectDir);
568 alist.add(logFile);
569 alist.add(collection_creator);
570
571 String[] arrays = new String[alist.size()];
572 for(int i = 0; i< arrays.length; i++){
573 arrays[i] = (String)alist.get(i);
574 System.out.println(arrays[i]);
575 }
576 System.out.println(wd.getAbsolutePath());
577 Process p = processBatch(arrays,out,wd);
578 out.flush();
579
580
581 p.destroy();
582
583 }
584 else{
585 matShell = "source "+ gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script" + fileSeparator + "mat-colbuild-download.bash "
586 + collName
587 + " "
588 + oaiURLString
589 + " "
590 + cacheDir
591 + " "
592 + maxRecords
593 + " "
594 + metadataprefix
595 + " "
596 + gs3Root
597 + " "
598 + collectDir
599 + " "
600 + logFile
601 + " "
602 + collection_creator;
603
604 out.println("<p><pre>" + matShell + "</pre></p>");
605 out.flush();
606 Process p = processShell(matShell, out);
607
608 if (p.exitValue() == 0){
609 out.println("<p>Collection downloaded.</p>");
610 }
611 else {
612 out.println("<p><b>Collection not downloaded properly.</b></p>");
613 }
614 out.flush();
615 //p = null;
616 p.destroy();
617
618 }
619
620 String matShell2 = "";
621
622 matShell2 = "source "+ gsdl3Root + fileSeparator + "ext" + fileSeparator +"mat"+ fileSeparator +"bin" + fileSeparator + "script" + fileSeparator + "mat-colbuild.bash "
623 + collName
624 + " "
625 + oaiURLString
626 + " "
627 + cacheDir
628 + " "
629 + maxRecords
630 + " "
631 + metadataprefix
632 + " "
633 + gs3Root
634 + " "
635 + collectDir
636 + " "
637 + logFile;
638
639
640 out.println("<form method=\"post\" name=\"collectionForm\">");
641 out.println("<input type=\"hidden\" name=\"matShell\" value=\"" + matShell2 + "\">");
642 out.println("<input type=\"hidden\" name=\"collectionName\" value=\"" + collName + "\">");
643 out.println("<input type=\"hidden\" name=\"collectionURL\" value=\"" + oaiURLString + "\">");
644 out.println("<input type=\"hidden\" name=\"oaiPrefix\" value=\"" + oaiPrefix + "\">");
645 out.println("<input type=\"hidden\" name=\"host\" value=\"" + host + "\">");
646 //out.println("<input type=\"submit\" value=\"start\"><br>");
647 out.println("</form>");
648 out.println("</div>");
649 out.print("</body></html>");
650 out.flush();
651 out.close();
652
653}
654
655 /* produce a random 7 letter collection name */
656
657 private String generateCollName () {
658 Random random = new Random();
659 StringBuffer message = new StringBuffer();
660 int offset = 97; // = "a"
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 message.append( (char) ( random.nextInt( 26 ) + offset ) );
667 message.append( (char) ( random.nextInt( 26 ) + offset ) );
668 return message.toString();
669 }
670
671 private Process processBatch(String[] command, PrintWriter out, File wd){
672
673 Process proc;
674 String s;
675 try{
676 proc = Runtime.getRuntime().exec(command,null,wd);
677 BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
678 BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
679 StringBuffer stdInputBuffer = new StringBuffer();
680 while ((s = stdInput.readLine()) != null) {
681 stdInputBuffer.append(s+"\n");
682 if(s.indexOf("-->")!=-1){
683 s = s.replaceAll("-->", "");
684 out.println("<!-- " + s + " -->");
685 }
686 else{
687 out.println("<!-- " + s + " -->");
688 }
689 out.flush();
690 }
691
692 StringBuffer stdErrorBuffer = new StringBuffer();
693
694 while ((s = stdError.readLine()) != null) {
695 stdErrorBuffer.append(s+"\n");
696
697 if(s.indexOf("-->")!=-1){
698 s = s.replaceAll("-->", "");
699 out.println("<!-- " + s + " -->");
700 }
701 else{
702 out.println("<!-- " + s + " -->");
703 }
704 out.flush();
705 }
706
707
708 out.println("<p>Here is the standard output:</p>\n");
709 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
710 out.println("<p>Here is the standard error (if any):</p>\n");
711 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
712 out.flush();
713 //if (p.exitValue() != 0)
714 if (false){
715 out.println("<p>An error occurred while building the collection.</p>");
716 out.println("<p>Here is the standard output:</p>\n");
717 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
718 out.println("<p>Here is the standard error (if any):</p>\n");
719 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
720 }
721
722 InputStream is = proc.getInputStream();
723 is.close();
724
725 OutputStream os = proc.getOutputStream();
726 os.close();
727
728 InputStream es = proc.getErrorStream();
729 es.close();
730
731 stdInput.close();
732 stdError.close();
733 proc.waitFor();
734 return proc;
735
736 }catch(Exception ex){
737 ex.printStackTrace();
738 }
739 return null;
740 }
741 private Process processShell( String command, PrintWriter out) {
742
743 String s= "";
744 try {
745 String[] args = new String[]{"sh", "-c", command};
746 Process p = Runtime.getRuntime().exec(args);
747 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
748 BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
749 StringBuffer stdInputBuffer = new StringBuffer();
750
751 while ((s = stdInput.readLine()) != null) {
752 stdInputBuffer.append(s+"\n");
753 if(s.indexOf("-->")!=-1){
754 s = s.replaceAll("-->", "");
755 out.println("<!-- " + s + " -->");
756 }
757 else{
758 out.println("<!-- " + s + " -->");
759 }
760 out.flush();
761 }
762
763 StringBuffer stdErrorBuffer = new StringBuffer();
764
765 while ((s = stdError.readLine()) != null) {
766 stdErrorBuffer.append(s+"\n");
767
768 if(s.indexOf("-->")!=-1){
769 s = s.replaceAll("-->", "");
770 out.println("<!-- " + s + " -->");
771 }
772 else{
773 out.println("<!-- " + s + " -->");
774 }
775 out.flush();
776 }
777
778 out.println("<p>Here is the standard output:</p>\n");
779 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
780 out.println("<p>Here is the standard error (if any):</p>\n");
781 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
782 out.flush();
783 //if (p.exitValue() != 0)
784 if (false){
785 out.println("<p>An error occurred while building the collection.</p>");
786 out.println("<p>Here is the standard output:</p>\n");
787 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
788 out.println("<p>Here is the standard error (if any):</p>\n");
789 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
790 }
791
792 InputStream is = p.getInputStream();
793 is.close();
794
795 OutputStream os = p.getOutputStream();
796 os.close();
797
798 InputStream es = p.getErrorStream();
799 es.close();
800
801 stdInput.close();
802 stdError.close();
803
804 return p;
805 }
806 catch (IOException e) {
807 out.println("exception happened - here's what I know: ");
808 out.println(e.toString());
809 out.flush();
810 }
811 return null;
812 }
813
814 private void analyzeCollection(PrintWriter out, String collectionName,String collectionURL,String collectionHost, String Prefix){
815
816 String collName = collectionName;
817 String oaiURLString = collectionURL;
818 String host = collectionHost;
819
820 try{
821 DescribeMessenger dm = new DescribeMessenger(collName,oaiURLString);
822 out.println("<p>Generating statistics and visualisations...</p>");
823 out.flush();
824 out.println("<p>please wait.</p>");
825 out.flush();
826 boolean status = dm.describeMatadata(out,collName,oaiURLString,Prefix,port_number);
827
828 if(status){
829
830 out.println("<script type=\"text/javascript\" language=\"JavaScript\">");
831 out.println("reconfig();");
832 out.println("</script>");
833
834 out.println("<a href=\""+host+collName+"/Overall.html\">View the report</a>");
835 out.flush();
836 }
837 out.println("</body></html>");
838 out.flush();
839 }catch(Exception e){e.printStackTrace(out);
840 out.println("<p><pre>"+e.toString()+"</pre></p>");
841 }
842 out.close();
843 }
844
845 private void loadRuntimeSettings(){
846
847
848 fileSeparator = File.separator;
849 gsdl3Home = globalProperty.getGSDL3Home();
850 globalProperty = new GlobalProperties();
851 maxRecord ="10";
852
853 String os = "linux";
854 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
855 gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
856 os = "windows";
857 }
858 String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
859
860 if(os.equals("windows")){
861 gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
862 gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
863 }
864
865 try{
866 Properties prop = new Properties();
867 FileInputStream fis = new FileInputStream(gsdl3Root+fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"properties.xml");
868 prop.load(fis);
869
870 titleString ="<title>"+ prop.getProperty("Servlet.Title")+"</title>";
871 h1String ="<h2>"+ prop.getProperty("Servlet.Head")+"</h2>";
872 cssString = "<link rel=\"stylesheet\" href=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/mat.css\" type=\"text/css\" >";
873 javaScript = "<script type=\"text/javascript\" src=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/status3.js\"></script>";;
874 headerString2 = "<head>" + titleString + "\n" + javaScript + cssString+"</head>\n";
875 headerString ="<head>" + titleString + "\n" + cssString + "</head>\n";
876 logoURL =prop.getProperty("Servlet.Logo");
877 description = prop.getProperty("Servlet.Description");
878
879 proxyHostContent = prop.getProperty("Servlet.proxyHost");
880 proxyPortContent = prop.getProperty("Servlet.proxyPort");
881
882 collection_creator = prop.getProperty("Servlet.Collection.Creator");
883
884 }catch(Exception ex){
885 ex.printStackTrace();
886 }
887
888
889 try {
890 java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
891 hostName = localMachine.getHostName();
892 }catch (java.net.UnknownHostException uhe) {
893 uhe.printStackTrace();
894 }
895
896 }
897}
Note: See TracBrowser for help on using the repository browser.