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

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

change node content

  • Property svn:executable set to *
File size: 29.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;
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 String[] arrays = new String[12];
459 arrays[0] = "cmd";
460 arrays[1] = "/c";
461 arrays[2] = "start";
462 arrays[3] = "mat-colbuild.bat";
463 arrays[4] = collName;
464 arrays[5] = oaiURLString;
465 arrays[6] = cacheDir;
466 arrays[7] = "100";
467 arrays[8] = oaiPrefix;
468 arrays[9] = gs3Root;
469 arrays[10] = collectDir;
470 arrays[11] = logFile;
471
472 Process p2 = processBatch(arrays,out,wd);
473 out.print("</div></body></html>");
474 p2 = null;
475 p2.destroy();
476 }
477 else{
478 Process p2 = processShell(matShell2, out);
479
480 if (p2.exitValue() == 0){
481 out.println("<p>Collection built.</p>");
482 }
483 else{
484 out.println("<p><b>Collection not built.</b></p>");
485 }
486
487 out.print("</div></body></html>");
488 p2 = null;
489 p2.destroy();
490 }
491 out.close();
492 }
493
494 private void downloadCollection( PrintWriter out, HttpServletRequest req, HttpServletResponse res, String oaiPrefix, String Records) throws ServletException, IOException
495 {
496 String oaiURLString = req.getParameter("oaiURL");
497 String metadataprefix = req.getParameter("metadataPrefix");
498 String collName = generateCollName();
499 String host = "http://"+hostName+":"+port_number+"/";
500
501 out.println("<br><br><div id=\"hideshow\" style=\"visibility:hidden\">");
502 out.println("<input type=\"button\" onClick=\"hidediv()\" value=\"hide debug infomation\"/>");
503 out.println("<p>Downloading OAI documents...</p>");
504 out.flush();
505 // move to correct directory
506
507
508 String os = "linux";
509 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
510 gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
511 os = "windows";
512 }
513 String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
514
515 if(os.equals("windows")){
516 gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
517 gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
518 }
519 String maxRecords = Records;
520 String cacheDir = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + collName;
521
522
523 String gs3Root = gsdl3Root;
524 String collectDir = gsdl3Home + fileSeparator +"sites"+fileSeparator+"localsite"+ fileSeparator +"collect";
525 String logFile = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + "log.txt";
526 String proxy_settings = " ";
527 String matShell = "";
528
529
530 if(!proxyHostContent.equals("") && !proxyPortContent.equals("")){
531 proxy_settings = "-proxy_on -proxy_host "+proxyHostContent+" -proxy_port "+proxyPortContent;
532 }
533
534 if(!proxyUserName.equals("") && !proxyUserPassword.equals("")){
535 proxy_settings = proxy_settings + " -user_name " +proxyUserName+" -user_password "+proxyUserPassword;
536 }
537
538
539 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
540
541 File wd = new File(gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script");
542 ArrayList alist = new ArrayList();
543 alist.add("cmd");
544 alist.add("/c");
545 alist.add("start");
546 alist.add("mat-colbuild-download.bat");
547 alist.add(collName);
548 alist.add(oaiURLString);
549 alist.add(cacheDir);
550 alist.add(maxRecords);
551 alist.add(metadataprefix);
552 alist.add(gs3Root);
553 alist.add(collectDir);
554 alist.add(logFile);
555 //alist.add(collection_creator);
556 alist.add(proxy_settings);
557 String[] arrays = new String[alist.size()];
558 for(int i = 0; i< arrays.length; i++){
559 arrays[i] = (String)alist.get(i);
560 System.out.println(arrays[i]);
561 }
562 System.out.println(wd.getAbsolutePath());
563 Process p = processBatch(arrays,out,wd);
564 out.flush();
565
566
567 p.destroy();
568
569 }
570 else{
571 matShell = "source "+ gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script" + fileSeparator + "mat-colbuild-download.bash "
572 + collName
573 + " "
574 + oaiURLString
575 + " "
576 + cacheDir
577 + " "
578 + maxRecords
579 + " "
580 + metadataprefix
581 + " "
582 + gs3Root
583 + " "
584 + collectDir
585 + " "
586 + logFile
587 + " "
588// + collection_creator
589// + " "
590 + proxy_settings;
591
592 out.println("<p><pre>" + matShell + "</pre></p>");
593 out.flush();
594 Process p = processShell(matShell, out);
595
596 if (p.exitValue() == 0){
597 out.println("<p>Collection downloaded.</p>");
598 }
599 else {
600 out.println("<p><b>Collection not downloaded properly.</b></p>");
601 }
602 out.flush();
603 p.destroy();
604
605 }
606
607 String matShell2 = "";
608
609 matShell2 = "source "+ gsdl3Root + fileSeparator + "ext" + fileSeparator +"mat"+ fileSeparator +"bin" + fileSeparator + "script" + fileSeparator + "mat-colbuild.bash "
610 + collName
611 + " "
612 + oaiURLString
613 + " "
614 + cacheDir
615 + " "
616 + maxRecords
617 + " "
618 + metadataprefix
619 + " "
620 + gs3Root
621 + " "
622 + collectDir
623 + " "
624 + logFile;
625
626
627 out.println("<form method=\"post\" name=\"collectionForm\">");
628 out.println("<input type=\"hidden\" name=\"matShell\" value=\"" + matShell2 + "\">");
629 out.println("<input type=\"hidden\" name=\"collectionName\" value=\"" + collName + "\">");
630 out.println("<input type=\"hidden\" name=\"collectionURL\" value=\"" + oaiURLString + "\">");
631 out.println("<input type=\"hidden\" name=\"oaiPrefix\" value=\"" + oaiPrefix + "\">");
632 out.println("<input type=\"hidden\" name=\"host\" value=\"" + host + "\">");
633 //out.println("<input type=\"submit\" value=\"start\"><br>");
634 out.println("</form>");
635 out.println("</div>");
636 out.print("</body></html>");
637 out.flush();
638 out.close();
639
640}
641
642 /* produce a random 7 letter collection name */
643
644 private String generateCollName () {
645 Random random = new Random();
646 StringBuffer message = new StringBuffer();
647 int offset = 97; // = "a"
648 message.append( (char) ( random.nextInt( 26 ) + offset ) );
649 message.append( (char) ( random.nextInt( 26 ) + offset ) );
650 message.append( (char) ( random.nextInt( 26 ) + offset ) );
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 return message.toString();
656 }
657
658 private Process processBatch(String[] command, PrintWriter out, File wd){
659
660 Process proc;
661 String s;
662 try{
663 proc = Runtime.getRuntime().exec(command,null,wd);
664 BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
665 BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
666 StringBuffer stdInputBuffer = new StringBuffer();
667 while ((s = stdInput.readLine()) != null) {
668 stdInputBuffer.append(s+"\n");
669 if(s.indexOf("-->")!=-1){
670 s = s.replaceAll("-->", "");
671 out.println("<!-- " + s + " -->");
672 }
673 else{
674 out.println("<!-- " + s + " -->");
675 }
676 out.flush();
677 }
678
679 StringBuffer stdErrorBuffer = new StringBuffer();
680
681 while ((s = stdError.readLine()) != null) {
682 stdErrorBuffer.append(s+"\n");
683
684 if(s.indexOf("-->")!=-1){
685 s = s.replaceAll("-->", "");
686 out.println("<!-- " + s + " -->");
687 }
688 else{
689 out.println("<!-- " + s + " -->");
690 }
691 out.flush();
692 }
693
694
695 out.println("<p>Here is the standard output:</p>\n");
696 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
697 out.println("<p>Here is the standard error (if any):</p>\n");
698 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
699 out.flush();
700 //if (p.exitValue() != 0)
701 if (false){
702 out.println("<p>An error occurred while building the collection.</p>");
703 out.println("<p>Here is the standard output:</p>\n");
704 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
705 out.println("<p>Here is the standard error (if any):</p>\n");
706 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
707 }
708
709 InputStream is = proc.getInputStream();
710 is.close();
711
712 OutputStream os = proc.getOutputStream();
713 os.close();
714
715 InputStream es = proc.getErrorStream();
716 es.close();
717
718 stdInput.close();
719 stdError.close();
720 proc.waitFor();
721 return proc;
722
723 }catch(Exception ex){
724 ex.printStackTrace();
725 }
726 return null;
727 }
728 private Process processShell( String command, PrintWriter out) {
729
730 String s= "";
731 try {
732 String[] args = new String[]{"sh", "-c", command};
733 Process p = Runtime.getRuntime().exec(args);
734 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
735 BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
736 StringBuffer stdInputBuffer = new StringBuffer();
737
738 while ((s = stdInput.readLine()) != null) {
739 stdInputBuffer.append(s+"\n");
740 if(s.indexOf("-->")!=-1){
741 s = s.replaceAll("-->", "");
742 out.println("<!-- " + s + " -->");
743 }
744 else{
745 out.println("<!-- " + s + " -->");
746 }
747 out.flush();
748 }
749
750 StringBuffer stdErrorBuffer = new StringBuffer();
751
752 while ((s = stdError.readLine()) != null) {
753 stdErrorBuffer.append(s+"\n");
754
755 if(s.indexOf("-->")!=-1){
756 s = s.replaceAll("-->", "");
757 out.println("<!-- " + s + " -->");
758 }
759 else{
760 out.println("<!-- " + s + " -->");
761 }
762 out.flush();
763 }
764
765 out.println("<p>Here is the standard output:</p>\n");
766 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
767 out.println("<p>Here is the standard error (if any):</p>\n");
768 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
769 out.flush();
770 //if (p.exitValue() != 0)
771 if (false){
772 out.println("<p>An error occurred while building the collection.</p>");
773 out.println("<p>Here is the standard output:</p>\n");
774 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
775 out.println("<p>Here is the standard error (if any):</p>\n");
776 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
777 }
778
779 InputStream is = p.getInputStream();
780 is.close();
781
782 OutputStream os = p.getOutputStream();
783 os.close();
784
785 InputStream es = p.getErrorStream();
786 es.close();
787
788 stdInput.close();
789 stdError.close();
790
791 return p;
792 }
793 catch (IOException e) {
794 out.println("exception happened - here's what I know: ");
795 out.println(e.toString());
796 out.flush();
797 }
798 return null;
799 }
800
801 private void analyzeCollection(PrintWriter out, String collectionName,String collectionURL,String collectionHost, String Prefix){
802
803 String collName = collectionName;
804 String oaiURLString = collectionURL;
805 String host = collectionHost;
806
807 try{
808 DescribeMessenger dm = new DescribeMessenger(collName,oaiURLString);
809 out.println("<p>Generating statistics and visualisations...</p>");
810 out.flush();
811 out.println("<p>please wait.</p>");
812 out.flush();
813 boolean status = dm.describeMatadata(out,collName,oaiURLString,Prefix,port_number);
814
815 if(status){
816
817 out.println("<script type=\"text/javascript\" language=\"JavaScript\">");
818 out.println("reconfig();");
819 out.println("</script>");
820
821 out.println("<a href=\""+host+collName+"/Overall.html\">View the report</a>");
822 out.flush();
823 }
824 out.println("</body></html>");
825 out.flush();
826 }catch(Exception e){e.printStackTrace(out);
827 out.println("<p><pre>"+e.toString()+"</pre></p>");
828 }
829 out.close();
830 }
831
832 private void loadRuntimeSettings(){
833
834
835 fileSeparator = File.separator;
836 gsdl3Home = globalProperty.getGSDL3Home();
837 globalProperty = new GlobalProperties();
838 maxRecord ="10";
839
840 String os = "linux";
841 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
842 gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
843 os = "windows";
844 }
845 String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
846
847 if(os.equals("windows")){
848 gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
849 gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
850 }
851
852 try{
853 Properties prop = new Properties();
854 FileInputStream fis = new FileInputStream(gsdl3Root+fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"properties.xml");
855 prop.load(fis);
856
857 titleString ="<title>"+ prop.getProperty("Servlet.Title")+"</title>";
858 h1String ="<h2>"+ prop.getProperty("Servlet.Head")+"</h2>";
859 cssString = "<link rel=\"stylesheet\" href=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/mat.css\" type=\"text/css\" >";
860 javaScript = "<script type=\"text/javascript\" src=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/status3.js\"></script>";;
861 headerString2 = "<head>" + titleString + "\n" + javaScript + cssString+"</head>\n";
862 headerString ="<head>" + titleString + "\n" + cssString + "</head>\n";
863 logoURL =prop.getProperty("Servlet.Logo");
864 description = prop.getProperty("Servlet.Description");
865
866 proxyHostContent = prop.getProperty("Servlet.proxyHost");
867 proxyPortContent = prop.getProperty("Servlet.proxyPort");
868
869 proxyUserName = prop.getProperty("Servlet.proxyUserName");
870 proxyUserPassword = prop.getProperty("Servlet.proxyUserPassword");
871
872 //collection_creator = prop.getProperty("Servlet.Collection.Creator");
873
874 }catch(Exception ex){
875 ex.printStackTrace();
876 }
877
878
879 try {
880 java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
881 hostName = localMachine.getHostName();
882 }catch (java.net.UnknownHostException uhe) {
883 uhe.printStackTrace();
884 }
885
886 }
887}
888
889class ProxyAuthenticator extends Authenticator
890 {
891
892 private String username;
893 private char[] password;
894
895
896 public ProxyAuthenticator (String username, String password){
897 this.username = username;
898 this.password = password.toCharArray();
899 }
900
901
902 protected PasswordAuthentication getPasswordAuthentication(){
903 return new PasswordAuthentication (username, password );
904 }
905 }
Note: See TracBrowser for help on using the repository browser.