Revision 370
Added by berkley over 24 years ago
src/edu/ucsb/nceas/metacat/MetaCatServlet.java | ||
---|---|---|
79 | 79 |
*/ |
80 | 80 |
public class MetaCatServlet extends HttpServlet { |
81 | 81 |
|
82 |
private ServletConfig config = null;
|
|
83 |
private ServletContext context = null;
|
|
84 |
private Hashtable connectionPool = new Hashtable();
|
|
85 |
private String resultStyleURL = null;
|
|
86 |
private String xmlcatalogfile = null;
|
|
87 |
private String saxparser = null;
|
|
88 |
private String defaultdatapath = null;
|
|
89 |
private String servletpath = null;
|
|
90 |
private String htmlpath = null;
|
|
82 |
private ServletConfig config = null;
|
|
83 |
private ServletContext context = null;
|
|
84 |
private Hashtable connectionPool = new Hashtable(); |
|
85 |
private String resultStyleURL = null; |
|
86 |
private String xmlcatalogfile = null; |
|
87 |
private String saxparser = null; |
|
88 |
private String defaultdatapath = null; |
|
89 |
private String servletpath = null; |
|
90 |
private String htmlpath = null; |
|
91 | 91 |
// path to directory where data files |
92 | 92 |
// that can be downloaded will be stored |
93 |
private String executescript = null;
|
|
93 |
private String executescript = null; |
|
94 | 94 |
// script to get data file and put it |
95 | 95 |
// in defaultdocpath dir |
96 | 96 |
private PropertyResourceBundle options = null; |
... | ... | |
244 | 244 |
} |
245 | 245 |
// End of Jivka added |
246 | 246 |
|
247 |
try |
|
248 |
{ |
|
249 |
Class[] x = {Class.forName("PrintWriter"), |
|
250 |
Class.forName("Hashtable"), |
|
251 |
Class.forName("HttpServletResponse")}; |
|
252 |
Class c = Class.forName("edu.ucsb.nceas.metacat.MetaCatServlet"); |
|
253 |
Method m = c.getDeclaredMethod("handleQueryAction", x); |
|
254 |
|
|
255 |
} |
|
256 |
catch(Exception e) |
|
257 |
{ |
|
258 |
|
|
259 |
} |
|
260 |
|
|
261 | 247 |
if(action.equals("query")) |
262 | 248 |
{ |
263 | 249 |
handleQuery(out, params, response); |
... | ... | |
410 | 396 |
out.println("<error>"); |
411 | 397 |
out.println("SRB Connection failed. " + re.getMessage()); |
412 | 398 |
out.println("</error>"); |
413 |
} |
|
414 |
} |
|
415 |
|
|
416 |
/** |
|
417 |
*Create the squery xml and return it as a String. |
|
399 |
} |
|
400 |
} |
|
401 |
|
|
402 |
/** |
|
403 |
*Create the squery xml, execute it and display it |
|
404 |
* @param out is the output stream to the client |
|
418 | 405 |
* @param params is the Hashtable of parameters that should be included |
419 | 406 |
* in the squery. |
407 |
* @param response is the response object linked to the client |
|
408 |
* @param conn the database connection |
|
420 | 409 |
*/ |
421 |
private void handleSQuery(PrintWriter out, Hashtable params, |
|
422 |
HttpServletResponse response) |
|
423 |
{ |
|
410 |
private void handleSQuery(PrintWriter out, Hashtable params,
|
|
411 |
HttpServletResponse response)
|
|
412 |
{
|
|
424 | 413 |
//create the squery and return it to be processed |
425 | 414 |
String doctype = null; |
426 | 415 |
String[] doctypeArr = null; |
416 |
Connection conn = null; |
|
427 | 417 |
doctypeArr = (String[])params.get("doctype"); |
428 | 418 |
doctype = null; |
429 | 419 |
if (doctypeArr != null) |
... | ... | |
436 | 426 |
} |
437 | 427 |
String xmlquery = DBQuery .createSQuery(params, doctype); |
438 | 428 |
Hashtable doclist = runQuery(xmlquery); |
439 |
String qformat = ((String[])params.get("qformat"))[0]; |
|
440 |
transformDocument(doclist, qformat, xmlquery, out, response); |
|
429 |
String qformat = ((String[])params.get("qformat"))[0]; |
|
430 |
String resultdoc = createResultDocument(doclist, qformat, xmlquery, |
|
431 |
out, response); |
|
432 |
if(qformat.equals("html")) |
|
433 |
{ |
|
434 |
transformResultset(resultdoc, response, out); |
|
435 |
} |
|
436 |
else if(qformat.equals("xml")) |
|
437 |
{ |
|
438 |
response.setContentType("text/xml"); |
|
439 |
out.println(resultdoc); |
|
440 |
} |
|
441 |
else |
|
442 |
{ |
|
443 |
out.println("invalid qformat: " + qformat); |
|
444 |
} |
|
445 |
//conn.close(); |
|
441 | 446 |
} |
442 | 447 |
|
443 | 448 |
/** |
444 |
*Create the query xml and return it as a String. |
|
445 |
* @param query is the free text query parameter returned through the CGI. |
|
446 |
* @param doctype is the doctype parameter returned through the CGI. |
|
447 |
* If no doctype filter is required, set it to null or "". |
|
448 |
*/ |
|
449 |
*Create the xml query, execute it and display the results. |
|
450 |
* @param out is the output stream to the client |
|
451 |
* @param params is the Hashtable of parameters that should be included |
|
452 |
* in the squery. |
|
453 |
* @param response is the response object linked to the client |
|
454 |
*/ |
|
449 | 455 |
private void handleQuery(PrintWriter out, Hashtable params, |
450 | 456 |
HttpServletResponse response) |
451 | 457 |
{ |
452 | 458 |
String doctype=null; |
453 | 459 |
String[] doctypeArr=null; |
454 | 460 |
String query=null; |
461 |
Connection conn = null; |
|
462 |
|
|
455 | 463 |
if(params.containsKey("query")) |
456 |
{ |
|
464 |
{ //get the query parameter
|
|
457 | 465 |
query = ((String[])params.get("query"))[0]; |
458 | 466 |
} |
459 | 467 |
else |
... | ... | |
462 | 470 |
} |
463 | 471 |
|
464 | 472 |
if(params.containsKey("doctype")) |
465 |
{ |
|
473 |
{ //check for the doctype
|
|
466 | 474 |
doctypeArr = (String[])params.get("doctype"); |
467 | 475 |
doctype = null; |
468 | 476 |
} |
... | ... | |
479 | 487 |
{ |
480 | 488 |
doctype="ANY"; |
481 | 489 |
} |
490 |
|
|
491 |
//create the query and run it |
|
482 | 492 |
String xmlquery = DBQuery.createQuery(query, doctype); |
483 | 493 |
Hashtable doclist = runQuery(xmlquery); |
484 | 494 |
String qformat = ((String[])params.get("qformat"))[0]; |
485 |
transformDocument(doclist, qformat, xmlquery, out, response); |
|
495 |
String resultdoc = createResultDocument(doclist, qformat, xmlquery, |
|
496 |
out, response); |
|
497 |
//format and transform the results |
|
498 |
if(qformat.equals("html")) |
|
499 |
{ |
|
500 |
transformResultset(resultdoc, response, out); |
|
501 |
} |
|
502 |
else if(qformat.equals("xml")) |
|
503 |
{ |
|
504 |
response.setContentType("text/xml"); |
|
505 |
out.println(resultdoc); |
|
506 |
} |
|
507 |
else |
|
508 |
{ |
|
509 |
out.println("invalid qformat: " + qformat); |
|
510 |
} |
|
511 |
//conn.close(); |
|
486 | 512 |
} |
487 | 513 |
|
488 | 514 |
/** |
489 | 515 |
* Run the query and return a hashtable of results. |
516 |
* @param xmlquery the query to run |
|
490 | 517 |
*/ |
491 | 518 |
private Hashtable runQuery(String xmlquery) |
492 | 519 |
{ |
... | ... | |
512 | 539 |
} |
513 | 540 |
} |
514 | 541 |
|
542 |
/** |
|
543 |
* Transorms an xml resultset document to html and sends it to the browser |
|
544 |
* @param resultdoc the string representation of the document that needs |
|
545 |
* to be transformed. |
|
546 |
* @param response the HttpServletResponse object bound to the client. |
|
547 |
* @param out the output stream to the client |
|
548 |
*/ |
|
549 |
private void transformResultset(String resultdoc, |
|
550 |
HttpServletResponse response, |
|
551 |
PrintWriter out) |
|
552 |
{ |
|
553 |
Connection conn = null; |
|
554 |
try |
|
555 |
{ |
|
556 |
conn = util.getConnection(); |
|
557 |
DBTransform trans = new DBTransform(conn); |
|
558 |
response.setContentType("text/html"); |
|
559 |
trans.transformXMLDocument(resultdoc, "-//NCEAS//resultset//EN", |
|
560 |
"-//W3C//HTML//EN", out); |
|
561 |
} |
|
562 |
catch(Exception e) |
|
563 |
{ |
|
564 |
if (conn != null) |
|
565 |
{ |
|
566 |
util.returnConnection(conn); |
|
567 |
} |
|
568 |
} |
|
569 |
//conn.close(); |
|
570 |
} |
|
571 |
|
|
515 | 572 |
/** |
516 | 573 |
* Transforms a hashtable of documents to an xml or html result. |
517 | 574 |
* @param doclist- the hashtable to transform |
... | ... | |
520 | 577 |
* @param out- the printwriter object used to write to the client |
521 | 578 |
* @param response- the response stream to write back to the client. |
522 | 579 |
*/ |
523 |
private void transformDocument(Hashtable doclist, String qformat,
|
|
580 |
private String createResultDocument(Hashtable doclist, String qformat,
|
|
524 | 581 |
String xmlquery, PrintWriter out, |
525 | 582 |
HttpServletResponse response) |
526 | 583 |
{ |
... | ... | |
541 | 598 |
resultset.append(" <document>" + document + "</document>"); |
542 | 599 |
} |
543 | 600 |
resultset.append("</resultset>"); |
544 |
|
|
601 |
return resultset.toString(); |
|
602 |
/* |
|
545 | 603 |
if(qformat.equals("xml")) |
546 | 604 |
{ |
547 | 605 |
// set content type and other response header fields first |
... | ... | |
566 | 624 |
out.println("Error transforming document:\n" + e.getMessage()); |
567 | 625 |
} |
568 | 626 |
} |
627 |
*/ |
|
569 | 628 |
} |
570 | 629 |
|
571 | 630 |
/** |
... | ... | |
933 | 992 |
|
934 | 993 |
/** |
935 | 994 |
* '$Log$ |
995 |
* 'Revision 1.71 2000/08/15 20:48:05 berkley |
|
996 |
* 'remove handleQueryAction() in favor of directly calling handleQuery() and handleSQuery() from doGetOrPost() |
|
997 |
* ' |
|
936 | 998 |
* 'Revision 1.70 2000/08/15 20:02:15 bojilova |
937 | 999 |
* 'Cleared hardcoded paths for the location of .html files and use |
938 | 1000 |
* 'the new "htmlpath" property from metacat.properties file |
Also available in: Unified diff
- created transformResultset() which transforms an xml resultset document and displays it to the client useing DBTransform
- renamed transformDocument() to createResultDocument() and modified its functionality to only return a restultset xml document
- changed handleSQuery() and handleQuery() to use the new methods