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

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

add quotes to ensure that arguments are strings

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