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

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

new source code

File size: 28.1 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;
17
18public class MatServlet extends HttpServlet {
19
20 private int port_number = 0;
21 private String oaiPrefix ="";
22 private String titleString;
23 private String h1String;
24 private String maxRecord;
25 private GlobalProperties globalProperty;
26 private String fileSeparator;
27 private String cssString;
28 private String headerString;
29 private String javaScript;
30 private String headerString2;
31 private String gsdl3Home;
32 private String logoURL;
33 private String description;
34 private String hostName;
35 private String proxyHost="http.proxyHost";
36 private String proxyPort="http.proxyPort";
37 private String proxyHostContent = "";
38 private String proxyPortContent = "";
39
40
41 public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
42
43 port_number = request.getLocalPort();
44 loadRuntimeSettings();
45 PrintWriter out = response.getWriter();
46
47 response.setContentType("text/html");
48 response.setHeader("pragma", "no-cache");
49
50 out.println("<html>");
51 out.println(headerString);
52 out.println("<body>");
53 out.println("<img src="+logoURL+">");
54 out.println(h1String);
55 out.println("<p>"+description+"</p>");
56 out.println("<div><form method=\"post\" action=\"http://"+hostName+":"+port_number+"/greenstone3/mat\">");
57 out.println("<p>OAI URL: <input type=\"text\" name=\"oaiurl\" size=\"140\"></p>");
58 out.println("<p><input type=\"submit\" value=\"Analyse repository\"></p>");
59 out.println("</form>");
60
61 out.println ("</body>");
62 out.println ("</html>");
63 out.close();
64 }
65
66 protected void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {
67
68 port_number = req.getLocalPort();
69 loadRuntimeSettings();
70
71 res.setContentType("text/html");
72 res.setHeader("pragma", "no-cache");
73 PrintWriter out = res.getWriter();
74
75 out.println("<html>");
76
77 if (req.getParameter("metadataPrefix") != null) {
78
79 oaiPrefix = req.getParameter("metadataPrefix");
80 String maxRecords = req.getParameter("maxrecords");
81 Pattern pa = Pattern.compile("[0-9]{1,5}");
82 Matcher ma = pa.matcher(maxRecords);
83
84 out.println(headerString2);
85 out.println("<body onLoad=\"autoSubmit()\">");
86 out.println(h1String);
87 out.println("<p>Please wait .... <br> It's downloading OAI records </p>");
88 out.println("<input type=\"button\" onClick=\"showdiv()\" value=\"show debug infomation\">");
89
90 if(ma.matches()){
91 int num = Integer.parseInt(maxRecords);
92 if(num>0){
93 downloadCollection(out, req, res, oaiPrefix, maxRecords);
94 }
95 else{
96 downloadCollection(out, req, res, oaiPrefix, maxRecord);
97 }
98 }
99 else{
100 downloadCollection(out, req, res, oaiPrefix, maxRecord);
101 }
102
103 }
104 else if(req.getParameter("matShell") != null){
105 out.println(headerString2);
106 out.println("<body onLoad=\"autoSubmit2()\">");
107 out.println(h1String);
108 out.println("<p>Please wait ...</p><p>It's building collection now.</p>");
109 out.println("<input type=\"button\" onClick=\"showdiv()\" value=\"show debug infomation\">");
110 buildCollection(out,req.getParameter("matShell"),req.getParameter("collectionName"),req.getParameter("collectionURL"),req.getParameter("oaiPrefix"));
111 }
112
113 else if (req.getParameter("collName") != null){
114 out.println(headerString);
115 out.write("<script type=\"text/javascript\" src=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/getInfomation.js\"></script>\r\n");
116 out.write("<script type=\"text/javascript\" src=\"http://yui.yahooapis.com/2.4.1/build/yahoo/yahoo-min.js\"></script>\r\n");
117 out.write("<script type=\"text/javascript\" src=\"http://yui.yahooapis.com/2.4.1/build/event/event-min.js\"></script>\r\n");
118 out.write("<script type=\"text/javascript\" src=\"http://yui.yahooapis.com/2.4.1/build/connection/connection-min.js\"></script>\r\n");
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 //System.setProperty("http.proxyHost", "wwwcache.cs.waikato.ac.nz");
195 //System.setProperty("http.proxyPort","80");
196 //System.setProperty("http.nonProxyHosts", "localhost|*.waikato.ac.nz");
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 HttpURLConnection connection = (HttpURLConnection)identifyURL.openConnection();
217 //URLConnection connection = identifyURL.openConnection();
218 connection.connect();
219
220 //BufferedReader inIdentify = new BufferedReader(new InputStreamReader(
221 //connection.getInputStream()));
222
223 Document identifyDocument;
224
225 try {
226 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
227 DocumentBuilder builder = factory.newDocumentBuilder();
228 identifyDocument = builder.parse( connection.getInputStream() );
229
230 Element oaiElement = identifyDocument.getDocumentElement();
231
232 if (oaiElement.getTagName() == "OAI-PMH" ) {
233 //out.println("<p>OAI-PMH element found...</p>");
234 }
235 else {
236 out.println("<p>Error: OAI-PMH element not found..exiting</p>");
237 return;
238 }
239
240 NodeList identifyNodeList = identifyDocument.getElementsByTagName("Identify");
241 Node identifyNode = null;
242
243 if (identifyNodeList.getLength() == 1 ) {
244 identifyNode = identifyNodeList.item(0);
245 System.out.println("<Identify>\n");
246 }
247 else {
248 out.println("<p>Error: Identify node not found... exiting</p>");
249 return;
250 }
251
252 NodeList identifyChildList = identifyNode.getChildNodes();
253 out.println("<table rules =\"none\">");
254
255 for (int i=0; i < identifyChildList.getLength(); i++) {
256 if (identifyChildList.item(i).getNodeName() == "repositoryName" ) {
257 out.println("<tr><td><strong>Repository Name:</strong></td><td> " + identifyChildList.item(i).getTextContent() + "</td></tr>");
258 }
259 if (identifyChildList.item(i).getNodeName() == "baseURL" ) {
260 String baseURL = identifyChildList.item(i).getTextContent();
261
262 if(baseURL.startsWith("http://")){
263 out.println("<tr><td><strong>Base URL:</strong></td><td><code><a href=\"" + baseURL + "\">" + baseURL + "</a></code></td></tr>");
264 }
265 else{
266 out.println("<tr><td><strong>Base URL:</strong></td><td><code><a href=\"" + oaiURLString + "\">" + oaiURLString + "</a></code></td></tr>");
267 }
268 }
269 }
270 out.println("</table>");
271 }
272 catch (SAXParseException spe) {
273 out.println(spe.getMessage());
274 }
275 catch (SAXException sxe) {
276 out.println(sxe.toString());
277 }
278 catch (ParserConfigurationException pce) {
279 out.println(pce.toString());
280 }
281 catch (IOException ioe) {
282 out.println(ioe.toString());
283 }
284
285 // now get the metadata prefixes
286
287 String metadataFormats = "";
288
289 if (! url.getPath().endsWith("?")) {
290 metadataFormats += "?";
291 }
292 metadataFormats += "verb=ListMetadataFormats";
293
294 URL metadataFormatsURL = new URL (url.toString() + metadataFormats);
295 URLConnection connection2 = metadataFormatsURL.openConnection();
296 connection.connect();
297 Document document2;
298
299 try {
300 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
301 DocumentBuilder builder = factory.newDocumentBuilder();
302 document2 = builder.parse( connection2.getInputStream() );
303
304 NodeList prefixList = document2.getElementsByTagName("metadataPrefix");
305
306 // create UI based on response
307
308 if ( prefixList.getLength() > 0 ) {
309
310 out.println("<p>Choose one metadata prefix to use:</p>");
311 out.println("<form method=\"post\" action=\"http://"+hostName+":"+port_number+"/greenstone3/mat\">");
312 out.println("<table rules =\"none\">");
313
314 for (int i=0; i < prefixList.getLength(); i++) {
315 out.println("<tr><td>");
316 String id = "radioID" + i;
317 String prefix = prefixList.item(i).getTextContent();
318
319 if (prefix.equals("oai_dc")) {
320 out.println("<label for=\"" + id + "\">" + prefix + " (Dublin Core)</label>");
321 //System.out.println("<MetadataElement>Dublin Core<\\MetadataElement>\n");
322 }
323 else {
324 out.println("<label for=\"" + id + "\">" + prefix + "</label>");
325 //System.out.println("<MetadataElement>"+prefix+"<\\MetadataElement>\n");
326 }
327
328 out.print("</td><td><input type=\"radio\" name=\"metadataPrefix\" value=\"" + prefix + "\" ");
329
330 if (prefix.equals("oai_dc")) {
331 out.println("checked=\"checked\" ");
332 }
333 out.println(" id=\"" + id + "\"><br>");
334 out.println("</td></tr>");
335 }
336
337 //System.out.println("</Identify>");
338 out.println("</table>");
339
340 // need to pass oaiurl through as well (again)
341
342 out.println("<input type=\"hidden\" name=\"oaiURL\" value=\"" + url.toString() + "\"><br>");
343 out.println("Max records: <input type=\"text\" name=\"maxrecords\" value=\"500\"><br>");
344
345 // submit button
346
347 out.println("<p><input type=\"submit\" value=\"Continue\"></p>");
348 out.println("</form>");
349
350 out.println("<p> Warning: Generating the statistics and visualization will take some time: </p> ");
351
352 /*
353 out.println("<table border = \"1\">");
354 out.println("<tr align=\"right\"><td> No.of Records <td> Estimated Time");
355 out.println("<tr align=\"right\"><td>100<td> 5 minutes");
356 out.println("<tr align=\"right\"><td>500 <td> 10 minutes");
357 out.println("<tr align=\"right\"><td>1000 <td> 18 minutes");
358 out.println("<tr align=\"right\"><td>2000<td> 30 minutes");
359 out.println("</table>");
360 */
361
362 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.");
363 }
364 else {
365 out.println("<p>Error: no metadata prefixes found... exiting</p>");
366 return;
367 }
368 }
369 catch (SAXParseException spe) {
370 out.println(spe.getMessage());
371 }
372 catch (SAXException sxe) {
373 out.println(sxe.toString());
374 }
375 catch (ParserConfigurationException pce) {
376 out.println(pce.toString());
377 }
378 catch (IOException ioe) {
379 out.println(ioe.toString());
380 }
381
382 // check we get a valid XML document back
383 // check OAI-PMH element
384 // check we have a <repositoryName> and <baseURL> elements
385 // maybe check the baseURL against the query we issued?
386
387
388 // if we get here we have a valid non-waikato non-local OAI server
389 // /* process stuff here */
390 // test with University home page - as it doesn't require
391 // dealing with the proxy server
392 // URL url = new URL("http://waikato.ac.nz");
393 // URL url = new URL(oaiURL);
394 // need to go through the proxy here
395 // http://dn.codegear.com/article/29783
396 // http://java.ittoolbox.com/groups/technical-functional/java-l/response-to-proxy-authentication-exception-71438
397 // http://www.jguru.com/faq/view.jsp?EID=13186
398 // http://www.developer.com/java/other/article.php/1551421
399 // System.setProperty("http.proxyHost","http://proxy.scms.waikato.ac.nz");
400 // System.setProperty("http.proxyPort","80");
401 // System.setProperty("http.nonProxyHosts", "localhost|*.waikato.ac.nz");
402 // build a GS 3 collection from the OAI URL
403 // http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
404 // http://codon.kribb.re.kr/wiki/display/Java/Using+Runtime.exec+to+invoke+child+process
405
406 out.print("</body></html>");
407 out.close();
408 } // endif
409 } // end doPost
410
411 protected void buildCollection( PrintWriter out, String matShell2, String collName, String oaiURLString, String oaiPrefix) throws ServletException, IOException{
412
413 out.println("<br><br><div id=\"hideshow\" style=\"visibility:hidden\">");
414 out.println("<input type=\"button\" onClick=\"hidediv()\" value=\"hide debug infomation\"/>");
415 out.println("<p>Building collection...</p>");
416
417 String host = "http://"+hostName+":"+port_number+"/greenstone3/mat/";
418
419 out.println("<form method=\"post\" name=\"aForm\">");
420 out.println("<input type=\"hidden\" name=\"collName\" value=\"" + collName + "\"><br>");
421 out.println("<input type=\"hidden\" name=\"collURL\" value=\"" + oaiURLString + "\"><br>");
422 out.println("<input type=\"hidden\" name=\"collHost\" value=\"" + host + "\"><br>");
423 out.println("<input type=\"hidden\" name=\"oaiPrefix\" value=\"" + oaiPrefix + "\"><br>");
424 out.println("</form>");
425 out.println("<p><pre>" + matShell2 + "</pre></p>");
426 out.flush();
427
428 Process p2 = processShell(matShell2, out);
429
430 if (p2.exitValue() == 0){
431 out.println("<p>Collection built.</p>");
432 }
433 else{
434 out.println("<p><b>Collection not built.</b></p>");
435 }
436
437 out.print("</div></body></html>");
438 p2 = null;
439 p2.destroy();
440 out.close();
441 }
442
443 private void downloadCollection( PrintWriter out, HttpServletRequest req, HttpServletResponse res, String oaiPrefix, String Records) throws ServletException, IOException
444 {
445 String oaiURLString = req.getParameter("oaiURL");
446 String metadataprefix = req.getParameter("metadataPrefix");
447 String collName = generateCollName();
448 String host = "http://"+hostName+":"+port_number+"/";
449
450 out.println("<br><br><div id=\"hideshow\" style=\"visibility:hidden\">");
451 out.println("<input type=\"button\" onClick=\"hidediv()\" value=\"hide debug infomation\"/>");
452 out.println("<p>Downloading OAI documents...</p>");
453 out.flush();
454 // move to correct directory
455
456
457 String os = "linux";
458 if(fileSeparator == "\\"){
459 gsdl3Home.replace("\\", "/");
460 os = "windows";
461 }
462 String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
463
464 if(os.equals("windows")){
465 gsdl3Root = gsdl3Root.replace("/","\\");
466 gsdl3Home = gsdl3Home.replace("/", "\\");
467 }
468 String maxRecords = Records;
469 String cacheDir = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + collName;
470
471 // downloading
472 //String gs3Root = "/research/cc108/greenstone3Project";
473 String gs3Root = gsdl3Root;
474 String collectDir = gsdl3Home + fileSeparator +"sites"+fileSeparator+"localsite"+ fileSeparator +"collect";
475 String logFile = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + "log.txt";
476 //source /research/cc108/greenstone3Project/gs2build/bin/script/mat-colbuild-download.bash
477 String matShell = "source "+ gsdl3Root + fileSeparator + "gs2build" + fileSeparator + "bin" + fileSeparator + "script" + fileSeparator + "mat-colbuild-download.bash "
478 + collName
479 + " "
480 + oaiURLString
481 + " "
482 + cacheDir
483 + " "
484 + maxRecords
485 + " "
486 + metadataprefix
487 + " "
488 + gs3Root
489 + " "
490 + collectDir
491 + " "
492 + logFile;
493
494 out.println("<p><pre>" + matShell + "</pre></p>");
495 out.flush();
496 Process p = processShell(matShell, out);
497
498 if (p.exitValue() == 0){
499 out.println("<p>Collection downloaded.</p>");
500 }
501 else {
502 out.println("<p><b>Collection not downloaded properly.</b></p>");
503 }
504 out.flush();
505 //p = null;
506 p.destroy();
507 // return the web page
508 //out.println(req.getRequestURL());
509 // building
510 //source "+ gsdl3Root + fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator + "bin" + fileSeparator + "script"+ fileSeparator + "mat-colbuild-download.bash "
511 //String matShell2 = "source /research/cc108/greenstone3Project/gs2build/bin/script/mat-colbuild.bash "
512 String matShell2 = "source "+ gsdl3Root + fileSeparator + "gs2build" + fileSeparator + "bin" + fileSeparator + "script" + fileSeparator + "mat-colbuild.bash "
513 + collName
514 + " "
515 + oaiURLString
516 + " "
517 + cacheDir
518 + " "
519 + maxRecords
520 + " "
521 + metadataprefix
522 + " "
523 + gs3Root
524 + " "
525 + collectDir
526 + " "
527 + logFile;
528
529 out.println("<form method=\"post\" name=\"collectionForm\">");
530 out.println("<input type=\"hidden\" name=\"matShell\" value=\"" + matShell2 + "\">");
531 out.println("<input type=\"hidden\" name=\"collectionName\" value=\"" + collName + "\">");
532 out.println("<input type=\"hidden\" name=\"collectionURL\" value=\"" + oaiURLString + "\">");
533 out.println("<input type=\"hidden\" name=\"oaiPrefix\" value=\"" + oaiPrefix + "\">");
534 out.println("<input type=\"hidden\" name=\"host\" value=\"" + host + "\">");
535 //out.println("<input type=\"submit\" value=\"start\"><br>");
536 out.println("</form>");
537 out.println("</div>");
538 out.print("</body></html>");
539 out.flush();
540 out.close();
541
542 // return page
543 //javascript to submit button in 5 secs
544 //form
545 // hidden inputs
546 // collname
547 // host
548 /////////////////////////////////////////////////////////////////////////////////////////////
549 ////////////////////////////////////////////////////////////////////////////////////////////
550
551 // specify plugins as parameter to mkcol.pl
552 //String makeColl = "perl -S mkcol.pl -creator [email protected] " + collName;
553 //out.println("<p><pre>" + makeColl + "</pre></p>");
554 // specify the OAI server in a config file???
555 // downloadfrom.pl -download_mode OAI -cache_dir -gli -url oaiURL.toString() -get_doc no
556 // what about that 500 doc limit in OAIDownload.pm
557 /*
558 String downloadFrom = "downloadfrom.pl -download_mode OAI -cache_dir "
559 + cacheDir
560 + " -url "
561 + oaiURL.toString()
562 + " -max_records 10"
563 + " -proxy_on"
564 + " -proxy_host "
565 + proxyHost
566 + " -proxy_port "
567 + proxyPort;
568 */
569
570 // -proxy_on -proxy_host wwwcache.cs.waikato.ac.nz -proxy_port 80
571 // out.println("<p><pre>" + downloadFrom + "</pre></p>");
572 // /home/daven/research/greenstone3/gs2build/bin/script/build [options] collection-name
573
574 /*
575 String build = "build -indextype lucene -download file://"
576 + cacheDir
577 + " -log_events " //event log goes to greenstone3/gs2build/etc/events.txt
578 + collName;
579 */
580
581 // out.println("<p><pre>" + build + "</pre></p>");
582 // perl -S importfrom.pl collname
583 // String importFrom = " perl -S importfrom.pl " + collName;
584 // perl -S buildcol.pl collname
585 // String buildColl = "perl -S buildcol.pl " + collName;
586 // need to be in correct directory
587 // String changeDir = "cd $GSDLHOME/collect/" + collName;
588 // build.pl ??
589 // rebuild ?
590 // rm -r index/*
591 // String removeOld = "rm -r index/*";
592 // mv building/* index/
593 // String moveToIndex = "mv building/* index/";
594
595 //////////////////////////////////////
596 /*
597 URLConnection connection = url.openConnection();
598 connection.connect();
599 Map headerMap = connection.getHeaderFields(); // gets the HTTP headers
600
601 out.print("<html><head><title>Analysing...</title></head><body>");
602 out.println ("<img src=http://www.cs.waikato.ac.nz/images-cs/uni.gif>");
603 out.println ("<h1>Metadata Analysis Tool - Alpha</h1>");
604 out.println("<p>Analysing the OAI URL: <code>" + url.toString() + "</code></p>");
605 out.println("<code><pre>");
606
607 Iterator keyValuePairs = headerMap.entrySet().iterator();
608 out.println("size = " + headerMap.size());
609 for (int i = 0; i < headerMap.size(); i++) {
610 out.println("i = " + i);
611 Map.Entry entry = (Map.Entry) keyValuePairs.next();
612 out.println(entry.getKey());
613 out.println(entry.getValue());
614 out.println();
615 }
616
617 out.println(headerMap.toString());
618 out.println("</pre></code>");
619 */
620
621 /*
622 // get the source HTML and insert it into the page - messy
623 out.println("<p>HTML source:</p>");
624 out.println("<pre><code>");
625 BufferedReader in = new BufferedReader(new InputStreamReader(
626 connection.getInputStream()));
627 String inputLine;
628 while ((inputLine = in.readLine()) != null)
629 out.println(inputLine);
630 in.close();
631 out.println("</pre></code>");
632 */
633
634 //out.print("</body></html>");
635
636}
637
638 /* produce a random 7 letter collection name */
639
640 private String generateCollName () {
641 Random random = new Random();
642 StringBuffer message = new StringBuffer();
643 int offset = 97; // = "a"
644 message.append( (char) ( random.nextInt( 26 ) + offset ) );
645 message.append( (char) ( random.nextInt( 26 ) + offset ) );
646 message.append( (char) ( random.nextInt( 26 ) + offset ) );
647 message.append( (char) ( random.nextInt( 26 ) + offset ) );
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 return message.toString();
652 }
653
654 private Process processShell( String command, PrintWriter out) {
655
656 String s= "";
657 try {
658 String[] args = new String[]{"sh", "-c", command};
659 Process p = Runtime.getRuntime().exec(args);
660 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
661 BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
662 StringBuffer stdInputBuffer = new StringBuffer();
663
664 while ((s = stdInput.readLine()) != null) {
665 stdInputBuffer.append(s+"\n");
666 if(s.contains("-->")){
667 s = s.replace("-->", "");
668 out.println("<!-- " + s + " -->");
669 }
670 else{
671 out.println("<!-- " + s + " -->");
672 }
673 out.flush();
674 }
675
676 StringBuffer stdErrorBuffer = new StringBuffer();
677
678 while ((s = stdError.readLine()) != null) {
679 stdErrorBuffer.append(s+"\n");
680
681 if(s.contains("-->")){
682 s = s.replace("-->", "");
683 out.println("<!-- " + s + " -->");
684 }
685 else{
686 out.println("<!-- " + s + " -->");
687 }
688 out.flush();
689 }
690
691 out.println("<p>Here is the standard output:</p>\n");
692 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
693 out.println("<p>Here is the standard error (if any):</p>\n");
694 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
695 out.flush();
696 //if (p.exitValue() != 0)
697 if (false){
698 out.println("<p>An error occurred while building the collection.</p>");
699 out.println("<p>Here is the standard output:</p>\n");
700 out.println("<p><pre>" + stdInputBuffer + "</pre></p>");
701 out.println("<p>Here is the standard error (if any):</p>\n");
702 out.println("<p><pre>" + stdErrorBuffer + "</pre></p>");
703 }
704
705 InputStream is = p.getInputStream();
706 is.close();
707
708 OutputStream os = p.getOutputStream();
709 os.close();
710
711 InputStream es = p.getErrorStream();
712 es.close();
713
714 stdInput.close();
715 stdError.close();
716
717 return p;
718 }
719 catch (IOException e) {
720 out.println("exception happened - here's what I know: ");
721 out.println(e.toString());
722 out.flush();
723 }
724 return null;
725 }
726
727 private void analyzeCollection(PrintWriter out, String collectionName,String collectionURL,String collectionHost, String Prefix){
728
729 String collName = collectionName;
730 String oaiURLString = collectionURL;
731 String host = collectionHost;
732
733 try{
734 DescribeMessager dm = new DescribeMessager(collName,oaiURLString);
735 out.println("<p>Generating statistics and visualisations...</p>");
736 out.flush();
737 out.println("<p>please wait.</p>");
738 out.flush();
739 boolean status = dm.describeMatadata(out,collName,oaiURLString,Prefix);
740
741 if(status){
742 out.println("<script type=\"text/javascript\" language=\"JavaScript\">");
743 out.println("reconfig();");
744 out.println("</script>");
745 out.println("<a href=\""+host+collName+"/Overall.html\">View the report</a>");
746 }
747 out.println("</body></html>");
748 }catch(Exception e){e.printStackTrace(out);
749 out.println("<p><pre>"+e.toString()+"</pre></p>");
750 }
751 out.close();
752 }
753
754 private void loadRuntimeSettings(){
755
756
757 fileSeparator = File.separator;
758 gsdl3Home = globalProperty.getGSDL3Home();
759 globalProperty = new GlobalProperties();
760 maxRecord ="10";
761
762 String os = "linux";
763 if(fileSeparator == "\\"){
764 gsdl3Home.replace("\\", "/");
765 os = "windows";
766 }
767 String gsdl3Root = GSPath.removeLastLink(gsdl3Home);
768
769 if(os.equals("windows")){
770 gsdl3Root = gsdl3Root.replace("/","\\");
771 gsdl3Home = gsdl3Home.replace("/", "\\");
772 }
773
774 try{
775 Properties prop = new Properties();
776 FileInputStream fis = new FileInputStream(gsdl3Root+fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"properties.xml");
777 prop.load(fis);
778
779 titleString ="<title>"+ prop.getProperty("Servlet.Title")+"</title>";
780 h1String ="<h2>"+ prop.getProperty("Servlet.Head")+"</h2>";
781 cssString = "<link rel=\"stylesheet\" href=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/mat.css\" type=\"text/css\" >";
782 javaScript = "<script type=\"text/javascript\" src=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/status3.js\"></script>";;
783 headerString2 = "<head>" + titleString + "\n" + javaScript + cssString+"</head>\n";
784 headerString ="<head>" + titleString + "\n" + cssString + "</head>\n";
785 logoURL =prop.getProperty("Servlet.Logo");
786 description = prop.getProperty("Servlet.Description");
787
788 proxyHostContent = prop.getProperty("Servlet.proxyHost");
789 proxyPortContent = prop.getProperty("Servlet.proxyPort");
790
791 }catch(Exception ex){
792 ex.printStackTrace();
793 }
794
795
796 try {
797 java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
798 hostName = localMachine.getHostName();
799 }catch (java.net.UnknownHostException uhe) {
800 uhe.printStackTrace();
801 }
802
803 }
804}
Note: See TracBrowser for help on using the repository browser.