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

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

new MatServlet source code

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