source: gs3-extensions/mat/trunk/src/org/greenstone/mat/servlet/MatServlet.java@ 22203

Last change on this file since 22203 was 22203, checked in by sjm84, 14 years ago

These files now use the original /greenstone3/mat rather than /mat

  • Property svn:executable set to *
File size: 29.6 KB
Line 
1package org.greenstone.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.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[11];
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] = cacheDir;
471 arrays[7] = gs3Root;
472 arrays[8] = collectDir;
473 arrays[9] = logFile;
474 arrays[10] = hostName + ":" + port_number;
475
476 Process p2 = processBatch(arrays,out,wd);
477 out.print("</div></body></html>");
478 p2 = null;
479 p2.destroy();
480 }
481 else{
482 Process p2 = processShell(matShell2, out);
483
484 if (p2.exitValue() == 0){
485 out.println("<p>Collection built.</p>");
486 }
487 else{
488 out.println("<p><b>Collection not built.</b></p>");
489 }
490
491 out.print("</div></body></html>");
492 p2 = null;
493 p2.destroy();
494 }
495 out.close();
496 }
497
498 private void downloadCollection( PrintWriter out, HttpServletRequest req, HttpServletResponse res, String oaiPrefix, String Records) throws ServletException, IOException
499 {
500 String oaiURLString = req.getParameter("oaiURL");
501 String metadataprefix = req.getParameter("metadataPrefix");
502 String collName = generateCollName();
503 String host = "http://"+hostName+":"+port_number+"/";
504
505 out.println("<br><br><div id=\"hideshow\" style=\"visibility:hidden\">");
506 out.println("<input type=\"button\" onClick=\"hidediv()\" value=\"hide debug infomation\"/>");
507 out.println("<p>Downloading OAI documents...</p>");
508 out.flush();
509 // move to correct directory
510
511
512 String os = "linux";
513 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
514 gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
515 os = "windows";
516 }
517 String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
518
519 if(os.equals("windows")){
520 gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
521 gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
522 }
523 String maxRecords = Records;
524 String cacheDir = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + collName;
525
526
527 String gs3Root = gsdl3Root;
528 String collectDir = gsdl3Home + fileSeparator +"sites"+fileSeparator+"localsite"+ fileSeparator +"collect";
529 String logFile = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + "log.txt";
530 String proxy_settings = " ";
531 String matShell = "";
532
533
534 if(!proxyHostContent.equals("") && !proxyPortContent.equals("")){
535 proxy_settings = "\"-proxy_on -proxy_host "+proxyHostContent+" -proxy_port "+proxyPortContent+"\"";
536
537
538 if(!proxyUserName.equals("") && !proxyUserPassword.equals("")){
539 proxy_settings = proxy_settings.substring(1,proxy_settings.length()-1);
540 proxy_settings = "\""+ proxy_settings + " -user_name " +proxyUserName+" -user_password "+proxyUserPassword +"\"";
541 }
542 }
543
544
545
546 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
547
548 File wd = new File(gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script");
549 ArrayList alist = new ArrayList();
550 alist.add("cmd");
551 alist.add("/c");
552 alist.add("start");
553 alist.add("/MIN");
554 alist.add("mat-colbuild-download.bat");
555 alist.add(collName);
556 alist.add(oaiURLString);
557 alist.add(cacheDir);
558 alist.add(maxRecords);
559 alist.add(metadataprefix);
560 alist.add(gs3Root);
561 alist.add(collectDir);
562 alist.add(logFile);
563 alist.add(collection_creator);
564 alist.add(proxy_settings);
565 String[] arrays = new String[alist.size()];
566 for(int i = 0; i< arrays.length; i++){
567 arrays[i] = (String)alist.get(i);
568 }
569 Process p = processBatch(arrays,out,wd);
570 out.flush();
571
572
573 p.destroy();
574
575 }
576 else{
577 matShell = gsdl3Root + fileSeparator + "ext" + fileSeparator + "mat" + fileSeparator + "bin" + fileSeparator +"script" + fileSeparator + "mat-colbuild-download.bash "
578 + collName
579 + " "
580 + oaiURLString
581 + " "
582 + cacheDir
583 + " "
584 + maxRecords
585 + " "
586 + metadataprefix
587 + " "
588 + gs3Root
589 + " "
590 + collectDir
591 + " "
592 + logFile
593 + " "
594// + collection_creator
595// + " "
596 + proxy_settings;
597
598 out.println("<p><pre>" + matShell + "</pre></p>");
599 out.flush();
600 Process p = processShell(matShell, out);
601
602 if (p.exitValue() == 0){
603 out.println("<p>Collection downloaded.</p>");
604 }
605 else {
606 out.println("<p><b>Collection not downloaded properly.</b></p>");
607 }
608 out.flush();
609 p.destroy();
610
611 }
612
613 String matShell2 = "";
614
615 matShell2 = gsdl3Root + fileSeparator + "ext" + fileSeparator +"mat"+ fileSeparator +"bin" + fileSeparator + "script" + fileSeparator + "mat-colbuild.bash "
616 + collName
617 + " "
618 + oaiURLString
619 + " "
620 + cacheDir
621 + " "
622 + gs3Root
623 + " "
624 + collectDir
625 + " "
626 + logFile
627 + " "
628 + hostName + ":" + port_number;
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
738 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
739 BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
740 StringBuffer stdInputBuffer = new StringBuffer();
741
742 while ((s = stdInput.readLine()) != null) {
743 stdInputBuffer.append(s+"\n");
744 if(s.indexOf("-->")!=-1){
745 s = s.replaceAll("-->", "");
746 //out.println("<!-- " + s + " -->");
747 }
748 else{
749 //out.println("<!-- " + s + " -->");
750 }
751 out.flush();
752 }
753
754 StringBuffer stdErrorBuffer = new StringBuffer();
755
756 while ((s = stdError.readLine()) != null) {
757 stdErrorBuffer.append(s+"\n");
758
759 if(s.indexOf("-->")!=-1){
760 s = s.replaceAll("-->", "");
761 out.println("<!-- " + s + " -->");
762 }
763 else{
764 out.println("<!-- " + s + " -->");
765 }
766 out.flush();
767 }
768
769 out.println("<p>Here is the standard output:</p>\n");
770 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
771 out.println("<p>Here is the standard error (if any):</p>\n");
772 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
773 out.flush();
774 //if (p.exitValue() != 0)
775 if (false){
776 out.println("<p>An error occurred while building the collection.</p>");
777 out.println("<p>Here is the standard output:</p>\n");
778 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
779 out.println("<p>Here is the standard error (if any):</p>\n");
780 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
781 }
782
783 InputStream is = p.getInputStream();
784 is.close();
785
786 OutputStream os = p.getOutputStream();
787 os.close();
788
789 InputStream es = p.getErrorStream();
790 es.close();
791
792 stdInput.close();
793 stdError.close();
794
795 return p;
796 }
797 catch (IOException e) {
798 out.println("exception happened - here's what I know: ");
799 out.println(e.toString());
800 out.flush();
801 }
802 return null;
803 }
804
805 private void analyzeCollection(PrintWriter out, String collectionName,String collectionURL,String collectionHost, String Prefix){
806
807 String collName = collectionName;
808 String oaiURLString = collectionURL;
809 String host = collectionHost;
810
811 try{
812 DescribeMessenger dm = new DescribeMessenger(collName,oaiURLString);
813 out.println("<p>Generating statistics and visualisations...</p>");
814 out.flush();
815 out.println("<p>please wait.</p>");
816 out.flush();
817 boolean status = dm.describeMatadata(out,collName,oaiURLString,Prefix,port_number);
818
819 if(status){
820
821 out.println("<script type=\"text/javascript\" language=\"JavaScript\" src = \"http://"+hostName+":"+port_number+"/greenstone3/mat/script/getInfomation.js\">");
822 out.println("reconfig();");
823 out.println("</script>");
824
825 out.println("<a href=\""+host+collName+"/Overall.html\">View the report</a>");
826 out.flush();
827 }
828 out.println("</body></html>");
829 out.flush();
830 }catch(Exception e){e.printStackTrace(out);
831 out.println("<p><pre>"+e.toString()+"</pre></p>");
832 }
833 out.close();
834 }
835
836 private void loadRuntimeSettings(){
837
838
839 fileSeparator = File.separator;
840 gsdl3Home = globalProperty.getGSDL3Home();
841 globalProperty = new GlobalProperties();
842 maxRecord ="10";
843
844 String os = "linux";
845 if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
846 gsdl3Home = gsdl3Home.replaceAll("\\\\", "/");
847 os = "windows";
848 }
849 String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
850
851 if(os.equals("windows")){
852 gsdl3Root = gsdl3Root.replaceAll("/","\\\\");
853 gsdl3Home = gsdl3Home.replaceAll("/", "\\\\");
854 }
855
856 try{
857 Properties prop = new Properties();
858 FileInputStream fis = new FileInputStream(gsdl3Root+fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"properties.xml");
859 prop.load(fis);
860
861 titleString ="<title>"+ prop.getProperty("Servlet.Title")+"</title>";
862 h1String ="<h2>"+ prop.getProperty("Servlet.Head")+"</h2>";
863 cssString = "<link rel=\"stylesheet\" href=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/mat.css\" type=\"text/css\" >";
864 javaScript = "<script type=\"text/javascript\" src=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/status3.js\"></script>";;
865 headerString2 = "<head>" + titleString + "\n" + javaScript + cssString+"</head>\n";
866 headerString ="<head>" + titleString + "\n" + cssString + "</head>\n";
867 logoURL =prop.getProperty("Servlet.Logo");
868 description = prop.getProperty("Servlet.Description");
869
870 proxyHostContent = prop.getProperty("Servlet.proxyHost");
871 proxyPortContent = prop.getProperty("Servlet.proxyPort");
872
873 proxyUserName = prop.getProperty("Servlet.proxyUserName");
874 proxyUserPassword = prop.getProperty("Servlet.proxyUserPassword");
875
876 //collection_creator = prop.getProperty("Servlet.Collection.Creator");
877
878 }catch(Exception ex){
879 ex.printStackTrace();
880 }
881
882
883 try {
884 java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
885 hostName = localMachine.getHostName();
886 }catch (java.net.UnknownHostException uhe) {
887 uhe.printStackTrace();
888 }
889
890 }
891}
892
893class ProxyAuthenticator extends Authenticator
894 {
895
896 private String username;
897 private char[] password;
898
899
900 public ProxyAuthenticator (String username, String password){
901 this.username = username;
902 this.password = password.toCharArray();
903 }
904
905
906 protected PasswordAuthentication getPasswordAuthentication(){
907 return new PasswordAuthentication (username, password );
908 }
909 }
Note: See TracBrowser for help on using the repository browser.