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

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

add no script element to provide an alternate content for users that have disabled scripts in their browser.

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