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

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

minimize the command prompt

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