|
1 |
/**
|
|
2 |
* '$RCSfile$'
|
|
3 |
* Purpose: class that implements the MARINE interface to the
|
|
4 |
* metadata database.
|
|
5 |
* Copyright: 2000 Regents of the University of California and the
|
|
6 |
* National Center for Ecological Analysis and Synthesis
|
|
7 |
* Authors: Chad Berkley
|
|
8 |
*
|
|
9 |
* '$Author$'
|
|
10 |
* '$Date$'
|
|
11 |
* '$Revision$'
|
|
12 |
*/
|
|
13 |
|
1 |
14 |
package edu.ucsb.nceas.metacat.marine;
|
2 |
15 |
|
3 |
16 |
import java.io.*;
|
... | ... | |
9 |
22 |
import oracle.xml.parser.v2.*;
|
10 |
23 |
import org.xml.sax.*;
|
11 |
24 |
import org.xml.sax.helpers.*;
|
12 |
|
// import edu.ucsb.nceas.metacat.*;
|
|
25 |
import edu.ucsb.nceas.metacat.*;
|
13 |
26 |
import oracle.jdbc.driver.*;
|
14 |
27 |
import oracle.xml.parser.v2.*;
|
15 |
28 |
import oracle.xml.parser.v2.XMLParser;
|
16 |
29 |
|
17 |
|
public class marineServlet extends HttpServlet
|
|
30 |
public class marineServlet extends edu.ucsb.nceas.metacat.MetaCatServlet
|
18 |
31 |
{
|
19 |
32 |
private marineUtil util;
|
20 |
33 |
private String resultStyleURL;
|
... | ... | |
90 |
103 |
}
|
91 |
104 |
catch(Exception e)
|
92 |
105 |
{
|
93 |
|
out.println("Exception in handleGetOrPost: ");
|
94 |
|
out.println(e.toString());
|
95 |
|
out.println(msg);
|
|
106 |
System.err.println("Exception in handleGetOrPost: ");
|
|
107 |
System.err.println(e.toString());
|
|
108 |
System.err.println(msg);
|
96 |
109 |
}
|
97 |
110 |
|
98 |
111 |
if (conn == null)
|
... | ... | |
176 |
189 |
{
|
177 |
190 |
handleQueryAction(out, params, response);
|
178 |
191 |
}
|
179 |
|
else if (action.equals("getdocument"))
|
180 |
|
{
|
181 |
|
try
|
182 |
|
{
|
183 |
|
handleGetDocumentAction(out, params, response);
|
184 |
|
}
|
185 |
|
catch(ClassNotFoundException e)
|
186 |
|
{
|
187 |
|
out.println(e.getMessage());
|
188 |
|
}
|
189 |
|
catch
|
190 |
|
(SQLException se)
|
191 |
|
{
|
192 |
|
out.println(se.getMessage());
|
193 |
|
}
|
194 |
|
}
|
195 |
|
else if(action.equals("insert") || action.equals("update"))
|
196 |
|
{
|
197 |
|
handleInsertOrUpdateAction(out, params, response);
|
198 |
|
}
|
199 |
|
else if(action.equals("delete"))
|
200 |
|
{
|
201 |
|
handleDeleteAction(out, params, response);
|
202 |
|
}
|
203 |
|
else if(action.equals("validate"))
|
204 |
|
{
|
205 |
|
handleValidateAction(out, params, response);
|
206 |
|
}
|
207 |
|
else if (action.equals("getdatadoc"))
|
208 |
|
{
|
209 |
|
handleGetDataDocumentAction(out, params, response);
|
210 |
|
}
|
211 |
|
else if (action.equals("Login"))
|
212 |
|
{
|
213 |
|
}
|
214 |
192 |
else
|
215 |
193 |
{
|
216 |
194 |
out.println("Error: action not registered" +
|
... | ... | |
236 |
214 |
String doctype = null;
|
237 |
215 |
Reader xmlquery = null;
|
238 |
216 |
|
239 |
|
// Run the query if it is a structured query
|
240 |
|
// or, if it is a free-text, simple query,
|
241 |
|
// format it first as a structured query and then run it
|
242 |
|
if (action.equals("query"))
|
|
217 |
if(action.equals("query"))
|
243 |
218 |
{
|
244 |
219 |
|
245 |
220 |
doctypeArr = (String[])params.get("doctype");
|
... | ... | |
258 |
233 |
xmlquery = new StringReader(DBQuery.createQuery(query));
|
259 |
234 |
}
|
260 |
235 |
}
|
261 |
|
else if (action.equals("squery"))
|
|
236 |
else if(action.equals("squery"))
|
262 |
237 |
{
|
263 |
238 |
doctypeArr = (String[])params.get("doctype");
|
264 |
239 |
doctype = null;
|
... | ... | |
272 |
247 |
xmlquery = new StringReader(createSQuery(params));
|
273 |
248 |
//msg = createSQuery(params);
|
274 |
249 |
}
|
|
250 |
else if(action.equals("init"))
|
|
251 |
{
|
|
252 |
|
|
253 |
}
|
275 |
254 |
else
|
276 |
255 |
{
|
277 |
256 |
msg = "Error handling query -- illegal action value";
|
... | ... | |
422 |
401 |
return createSQuery(params, "");
|
423 |
402 |
}
|
424 |
403 |
|
425 |
|
/***************************************************************
|
426 |
|
The following code is taken directly from MetaCatServlet.java
|
427 |
|
****************************************************************/
|
428 |
|
/**
|
429 |
|
* Handle the database getdocument request and return a XML document,
|
430 |
|
* possibly transformed from XML into HTML
|
431 |
|
*/
|
432 |
|
private void handleGetDocumentAction(PrintWriter out, Hashtable params,
|
433 |
|
HttpServletResponse response)
|
434 |
|
throws ClassNotFoundException, IOException, SQLException {
|
435 |
|
String docidstr = null;
|
436 |
|
String docid = null;
|
437 |
|
String doc = null;
|
438 |
|
try {
|
439 |
|
// Find the document id number
|
440 |
|
docidstr = ((String[])params.get("docid"))[0];
|
441 |
|
//docid = (new Long(docidstr)).longValue();
|
442 |
|
docid = docidstr;
|
443 |
|
|
444 |
|
// Get the document indicated fromthe db
|
445 |
|
doc = docreader.readXMLDocument(docid);
|
446 |
|
} catch (NullPointerException npe) {
|
447 |
|
response.setContentType("text/html");
|
448 |
|
out.println("Error getting document ID: " + docidstr +" (" + docid + ")");
|
449 |
|
}
|
450 |
|
|
451 |
|
// Return the document in XML or HTML format
|
452 |
|
String qformat = ((String[])params.get("qformat"))[0];
|
453 |
|
if (qformat.equals("xml")) {
|
454 |
|
// set content type and other response header fields first
|
455 |
|
response.setContentType("text/xml");
|
456 |
|
out.println(doc);
|
457 |
|
} else if (qformat.equals("html")) {
|
458 |
|
// set content type and other response header fields first
|
459 |
|
response.setContentType("text/html");
|
460 |
|
|
461 |
|
// Look up the document type
|
462 |
|
String sourcetype = docreader.getDoctypeInfo(docid).getDoctype();
|
463 |
|
|
464 |
|
// Transform the document to the new doctype
|
465 |
|
dbt.transformXMLDocument(doc, sourcetype, "-//W3C//HTML//EN", out);
|
466 |
|
}
|
467 |
|
}
|
468 |
|
|
469 |
|
/**
|
470 |
|
* Handle the database putdocument request and write an XML document
|
471 |
|
* to the database connection
|
472 |
|
*/
|
473 |
|
private void handleInsertOrUpdateAction(PrintWriter out, Hashtable params,
|
474 |
|
HttpServletResponse response) {
|
475 |
|
|
476 |
|
try {
|
477 |
|
// Get the document indicated
|
478 |
|
String[] doctext = (String[])params.get("doctext");
|
479 |
|
StringReader xml = null;
|
480 |
|
try {
|
481 |
|
xml = new StringReader(doctext[0]);
|
482 |
|
|
483 |
|
String[] action = (String[])params.get("action");
|
484 |
|
String[] docid = (String[])params.get("docid");
|
485 |
|
String newdocid = null;
|
486 |
|
|
487 |
|
String doAction = null;
|
488 |
|
if (action[0].equals("insert")) {
|
489 |
|
doAction = "INSERT";
|
490 |
|
} else if (action[0].equals("update")) {
|
491 |
|
doAction = "UPDATE";
|
492 |
|
}
|
493 |
|
|
494 |
|
// write the document to the database
|
495 |
|
DBWriter dbw = new DBWriter(conn, saxparser);
|
496 |
|
|
497 |
|
try {
|
498 |
|
String accNumber = docid[0];
|
499 |
|
if (accNumber.equals("")) {
|
500 |
|
accNumber = null;
|
501 |
|
}
|
502 |
|
newdocid = dbw.write(xml, doAction, accNumber);
|
503 |
|
} catch (NullPointerException npe) {
|
504 |
|
newdocid = dbw.write(xml, doAction, null);
|
505 |
|
}
|
506 |
|
|
507 |
|
// set content type and other response header fields first
|
508 |
|
response.setContentType("text/xml");
|
509 |
|
out.println("<?xml version=\"1.0\"?>");
|
510 |
|
out.println("<success>");
|
511 |
|
out.println("<docid>" + newdocid + "</docid>");
|
512 |
|
out.println("</success>");
|
513 |
|
|
514 |
|
} catch (NullPointerException npe) {
|
515 |
|
response.setContentType("text/xml");
|
516 |
|
out.println("<?xml version=\"1.0\"?>");
|
517 |
|
out.println("<error>");
|
518 |
|
out.println(npe.getMessage());
|
519 |
|
out.println("</error>");
|
520 |
|
}
|
521 |
|
} catch (Exception e) {
|
522 |
|
response.setContentType("text/xml");
|
523 |
|
out.println("<?xml version=\"1.0\"?>");
|
524 |
|
out.println("<error>");
|
525 |
|
out.println(e.getMessage());
|
526 |
|
if (e instanceof SAXException) {
|
527 |
|
Exception e2 = ((SAXException)e).getException();
|
528 |
|
out.println("<error>");
|
529 |
|
out.println(e2.getMessage());
|
530 |
|
out.println("</error>");
|
531 |
|
}
|
532 |
|
//e.printStackTrace(out);
|
533 |
|
out.println("</error>");
|
534 |
|
}
|
535 |
|
}
|
536 |
|
|
537 |
|
/**
|
538 |
|
* Handle the database delete request and delete an XML document
|
539 |
|
* from the database connection
|
540 |
|
*/
|
541 |
|
private void handleDeleteAction(PrintWriter out, Hashtable params,
|
542 |
|
HttpServletResponse response) {
|
543 |
|
|
544 |
|
String[] docid = (String[])params.get("docid");
|
545 |
|
|
546 |
|
// delete the document from the database
|
547 |
|
try {
|
548 |
|
DBWriter dbw = new DBWriter(conn, saxparser);
|
549 |
|
// NOTE -- NEED TO TEST HERE
|
550 |
|
// FOR EXISTENCE OF PARAM
|
551 |
|
// BEFORE ACCESSING ARRAY
|
552 |
|
try {
|
553 |
|
dbw.delete(docid[0]);
|
554 |
|
response.setContentType("text/xml");
|
555 |
|
out.println("<?xml version=\"1.0\"?>");
|
556 |
|
out.println("<success>");
|
557 |
|
out.println("Document deleted.");
|
558 |
|
out.println("</success>");
|
559 |
|
} catch (AccessionNumberException ane) {
|
560 |
|
response.setContentType("text/xml");
|
561 |
|
out.println("<?xml version=\"1.0\"?>");
|
562 |
|
out.println("<error>");
|
563 |
|
out.println("Error deleting document!!!");
|
564 |
|
out.println(ane.getMessage());
|
565 |
|
out.println("</error>");
|
566 |
|
}
|
567 |
|
} catch (Exception e) {
|
568 |
|
response.setContentType("text/xml");
|
569 |
|
out.println("<?xml version=\"1.0\"?>");
|
570 |
|
out.println("<error>");
|
571 |
|
out.println(e.getMessage());
|
572 |
|
out.println("</error>");
|
573 |
|
}
|
574 |
|
}
|
575 |
|
|
576 |
|
/**
|
577 |
|
* Handle the validtion request and return the results to the requestor
|
578 |
|
*/
|
579 |
|
private void handleValidateAction(PrintWriter out, Hashtable params,
|
580 |
|
HttpServletResponse response) {
|
581 |
|
|
582 |
|
// Get the document indicated
|
583 |
|
String valtext = null;
|
584 |
|
try {
|
585 |
|
valtext = ((String[])params.get("valtext"))[0];
|
586 |
|
} catch (Exception nullpe) {
|
587 |
|
|
588 |
|
String docid = null;
|
589 |
|
try {
|
590 |
|
// Find the document id number
|
591 |
|
docid = ((String[])params.get("docid"))[0];
|
592 |
|
|
593 |
|
// Get the document indicated fromthe db
|
594 |
|
valtext = docreader.readXMLDocument(docid);
|
595 |
|
|
596 |
|
} catch (NullPointerException npe) {
|
597 |
|
response.setContentType("text/xml");
|
598 |
|
out.println("<error>Error getting document ID: " + docid + "</error>");
|
599 |
|
}
|
600 |
|
}
|
601 |
|
|
602 |
|
try {
|
603 |
|
DBValidate valobj = new DBValidate(saxparser,conn);
|
604 |
|
boolean valid = valobj.validateString(valtext);
|
605 |
|
|
606 |
|
// set content type and other response header fields first
|
607 |
|
response.setContentType("text/xml");
|
608 |
|
out.println(valobj.returnErrors());
|
609 |
|
|
610 |
|
} catch (NullPointerException npe2) {
|
611 |
|
// set content type and other response header fields first
|
612 |
|
response.setContentType("text/xml");
|
613 |
|
out.println("<error>Error validating document.</error>");
|
614 |
|
}
|
615 |
|
}
|
616 |
|
|
617 |
|
/**
|
618 |
|
* Handle the document request and return the results
|
619 |
|
* to the requestor
|
620 |
|
*/
|
621 |
|
private void handleGetDataDocumentAction(PrintWriter out, Hashtable params,
|
622 |
|
HttpServletResponse response) {
|
623 |
|
boolean error_flag = false;
|
624 |
|
String error_message = "";
|
625 |
|
// Get the document indicated
|
626 |
|
String[] datadoc = (String[])params.get("datadoc");
|
627 |
|
// defaultdatapath = "C:\\Temp\\"; // for testing only!!!
|
628 |
|
// executescript = "test.bat"; // for testing only!!!
|
629 |
|
|
630 |
|
// set content type and other response header fields first
|
631 |
|
response.setContentType("application/octet-stream");
|
632 |
|
if (defaultdatapath!=null) {
|
633 |
|
if(!defaultdatapath.endsWith(System.getProperty("file.separator"))) {
|
634 |
|
defaultdatapath=defaultdatapath+System.getProperty("file.separator");
|
635 |
|
}
|
636 |
|
System.out.println("Path= "+defaultdatapath+datadoc[0]);
|
637 |
|
if (executescript!=null) {
|
638 |
|
String command = null;
|
639 |
|
File scriptfile = new File(executescript);
|
640 |
|
if (scriptfile.exists()) {
|
641 |
|
command=executescript+" "+datadoc[0]; // script includes path
|
642 |
|
} else { // look in defaultdatapath
|
643 |
|
// on Win98 one MUST include the .bat extender
|
644 |
|
command = defaultdatapath+executescript+" "+datadoc[0];
|
645 |
|
}
|
646 |
|
System.out.println(command);
|
647 |
|
try {
|
648 |
|
Process proc = Runtime.getRuntime().exec(command);
|
649 |
|
proc.waitFor();
|
650 |
|
}
|
651 |
|
catch (Exception eee) {
|
652 |
|
System.out.println("Error running process!");
|
653 |
|
error_flag = true;
|
654 |
|
error_message = "Error running process!";}
|
655 |
|
} // end executescript not null if
|
656 |
|
File datafile = new File(defaultdatapath+datadoc[0]);
|
657 |
|
try {
|
658 |
|
FileInputStream fw = new FileInputStream(datafile);
|
659 |
|
int x;
|
660 |
|
while ((x = fw.read())!=-1) {
|
661 |
|
out.write(x); }
|
662 |
|
fw.close();
|
663 |
|
} catch (Exception e) {
|
664 |
|
System.out.println("Error in returning file\n"+e.getMessage());
|
665 |
|
error_flag=true;
|
666 |
|
error_message = error_message+"\nError in returning file\n"+
|
667 |
|
e.getMessage();
|
668 |
|
}
|
669 |
|
} // end defaultdatapath not null if
|
670 |
|
}
|
671 |
|
/**********************************************
|
672 |
|
END code taken from MetaCatServlet.java
|
673 |
|
***********************************************/
|
674 |
|
|
675 |
404 |
}
|
no message