Revision 731
Added by bojilova almost 23 years ago
MetaCatServlet.java | ||
---|---|---|
29 | 29 |
|
30 | 30 |
import java.io.PrintWriter; |
31 | 31 |
import java.io.IOException; |
32 |
import java.io.Reader; |
|
33 | 32 |
import java.io.StringReader; |
34 |
import java.io.BufferedReader; |
|
35 |
import java.io.File; |
|
36 | 33 |
import java.io.FileInputStream; |
37 |
import java.io.FileOutputStream; |
|
38 |
import java.io.InputStreamReader; |
|
39 |
import java.io.DataInputStream; |
|
34 |
import java.io.BufferedInputStream; |
|
40 | 35 |
import java.util.Enumeration; |
41 | 36 |
import java.util.Hashtable; |
42 | 37 |
import java.util.ResourceBundle; |
... | ... | |
77 | 72 |
* action=query -- query the values of all elements and attributes |
78 | 73 |
* and return a result set of nodes<br> |
79 | 74 |
* action=squery -- structured query (see pathquery.dtd)<br> |
75 |
* action=read -- read any metadata/data file from Metacat and from Internet<br> |
|
80 | 76 |
* action=insert -- insert an XML document into the database store<br> |
81 | 77 |
* action=update -- update an XML document that is in the database store<br> |
82 | 78 |
* action=delete -- delete an XML document from the database store<br> |
83 | 79 |
* action=validate -- vallidate the xml contained in valtext<br> |
84 |
* action=read -- display an XML document in XML or HTML<br> |
|
85 | 80 |
* doctype -- document type list returned by the query (publicID)<br> |
86 | 81 |
* qformat=xml -- display resultset from query in XML<br> |
87 | 82 |
* qformat=html -- display resultset from query in HTML<br> |
83 |
* qformat=zip -- zip resultset from query<br> |
|
88 | 84 |
* docid=34 -- display the document with the document ID number 34<br> |
89 | 85 |
* doctext -- XML text of the document to load into the database<br> |
90 | 86 |
* acltext -- XML access text for a document to load into the database<br> |
91 | 87 |
* dtdtext -- XML DTD text for a new DTD to load into Metacat XML Catalog<br> |
92 | 88 |
* query -- actual query text (to go with 'action=query' or 'action=squery')<br> |
93 | 89 |
* valtext -- XML text to be validated<br> |
94 |
* action=getdatadoc -- retrieve a stored datadocument<br>
|
|
90 |
* abstractpath -- XPath in metadata document to read from<br>
|
|
95 | 91 |
* action=getaccesscontrol -- retrieve acl info for Metacat document<br> |
96 | 92 |
* action=getdoctypes -- retrieve all doctypes (publicID)<br> |
97 | 93 |
* action=getdtdschema -- retrieve a DTD or Schema file<br> |
... | ... | |
144 | 140 |
servletpath = util.getOption("servletpath"); |
145 | 141 |
htmlpath = util.getOption("htmlpath"); |
146 | 142 |
|
143 |
// MOVED IT TO doGet() & doPost() |
|
147 | 144 |
// try { |
148 | 145 |
// // Open a pool of db connections |
149 | 146 |
// connectionPool = util.getConnectionPool(); |
... | ... | |
219 | 216 |
// Decode the docid and mouse click information |
220 | 217 |
if (name.endsWith(".y")) { |
221 | 218 |
docid[0] = name.substring(0,name.length()-2); |
222 |
//out.println("docid => " + docid[0]); |
|
223 | 219 |
params.put("docid", docid); |
224 | 220 |
name = "ypos"; |
225 | 221 |
} |
... | ... | |
227 | 223 |
name = "xpos"; |
228 | 224 |
} |
229 | 225 |
|
230 |
//pwout.println(name + " => " + value[0]); |
|
231 | 226 |
params.put(name,value); |
232 | 227 |
} |
233 | 228 |
|
... | ... | |
236 | 231 |
String action = ((String[])params.get("action"))[0]; |
237 | 232 |
util.debugMessage("Line 213: Action is: " + action); |
238 | 233 |
|
239 |
//MBJELIMINATE String action = decodeMouseAction(params); |
|
240 |
//if(action.equals("error")) |
|
241 |
//{ |
|
242 |
//util.debugMessage("Line 218: Action is: " + action); |
|
243 |
//action = ((String[])params.get("action"))[0]; |
|
244 |
//} |
|
245 |
|
|
246 | 234 |
// This block handles session management for the servlet |
247 | 235 |
// by looking up the current session information for all actions |
248 | 236 |
// other than "login" and "logout" |
... | ... | |
273 | 261 |
username = (String)sess.getAttribute("username"); |
274 | 262 |
password = (String)sess.getAttribute("password"); |
275 | 263 |
groupname = (String)sess.getAttribute("groupname"); |
276 |
try |
|
277 |
{ |
|
264 |
try { |
|
278 | 265 |
sess_id = (String)sess.getId(); |
279 |
} |
|
280 |
catch(IllegalStateException ise) |
|
281 |
{ |
|
266 |
} catch(IllegalStateException ise) { |
|
282 | 267 |
System.out.println("error in handleGetOrPost: this shouldn't " + |
283 | 268 |
"happen: the session should be valid: " + |
284 | 269 |
ise.getMessage()); |
... | ... | |
288 | 273 |
|
289 | 274 |
// Now that we know the session is valid, we can delegate the request |
290 | 275 |
// to a particular action handler |
291 |
if(action.equals("query")) |
|
292 |
{ |
|
276 |
if(action.equals("query")) { |
|
293 | 277 |
handleQuery(response.getWriter(), params, response, username, groupname); |
294 |
} |
|
295 |
else if(action.equals("squery")) |
|
296 |
{ |
|
297 |
if(params.containsKey("query")) |
|
298 |
{ |
|
278 |
} else if(action.equals("squery")) { |
|
279 |
if(params.containsKey("query")) { |
|
299 | 280 |
handleSQuery(response.getWriter(), params, response, username, groupname); |
300 |
} |
|
301 |
else |
|
302 |
{ |
|
281 |
} else { |
|
303 | 282 |
PrintWriter out = response.getWriter(); |
304 | 283 |
out.println("Illegal action squery without \"query\" parameter"); |
305 | 284 |
} |
306 |
} |
|
307 |
else if (action.equals("read")) { |
|
308 |
//PrintWriter out = response.getWriter(); |
|
309 |
try { |
|
310 |
handleReadAction(/*out,*/ params, response, username); |
|
311 |
} catch (ClassNotFoundException e) { |
|
312 |
System.out.println("Error in MetacatServlet.handlGetOrPost: " + |
|
313 |
e.getMessage()); |
|
314 |
} catch (SQLException se) { |
|
315 |
System.out.println("Error in MetaCatServlet.handleGetOrPost: " + |
|
316 |
se.getMessage()); |
|
317 |
} |
|
318 |
} |
|
319 |
/* |
|
320 |
else if (action.equals("getrelateddocument")) { |
|
285 |
} else if (action.equals("read")) { |
|
286 |
handleReadAction(params, response, username, groupname); |
|
287 |
} else if (action.equals("insert") || action.equals("update")) { |
|
321 | 288 |
PrintWriter out = response.getWriter(); |
322 |
try { |
|
323 |
handleGetRelatedDocumentAction(out, params, response); |
|
324 |
} catch (ClassNotFoundException e) { |
|
325 |
out.println(e.getMessage()); |
|
326 |
} catch (SQLException se) { |
|
327 |
out.println(se.getMessage()); |
|
328 |
} |
|
329 |
} |
|
330 |
*/ |
|
331 |
else if (action.equals("insert") || action.equals("update")) { |
|
332 |
PrintWriter out = response.getWriter(); |
|
333 | 289 |
if ( (username != null) && !username.equals("public") ) { |
334 | 290 |
handleInsertOrUpdateAction(out, params, response, username, groupname); |
335 | 291 |
} else { |
... | ... | |
345 | 301 |
} else if (action.equals("validate")) { |
346 | 302 |
PrintWriter out = response.getWriter(); |
347 | 303 |
handleValidateAction(out, params, response); |
348 |
} else if (action.equals("getabstract")) { |
|
349 |
PrintWriter out = response.getWriter(); |
|
350 |
try{ |
|
351 |
handleViewAbstractAction(out, params, response); |
|
352 |
} |
|
353 |
catch(Exception e) |
|
354 |
{ |
|
355 |
out.println("error viewing abstract from " + |
|
356 |
"MetacatServlet.handleGetorPost: " + e.getMessage()); |
|
357 |
} |
|
358 |
} else if (action.equals("getdatadoc")) { |
|
359 |
response.setContentType("application/zip"); |
|
360 |
ServletOutputStream out = response.getOutputStream(); |
|
361 |
handleGetDataDocumentAction(out, params, response); |
|
362 |
} else if (action.equals("getaccesscontrol")) { |
|
363 |
PrintWriter out = response.getWriter(); |
|
364 |
handleGetAccessControlAction(out, params, response, username, groupname); |
|
365 |
} else if (action.equals("getdoctypes")) { |
|
366 |
PrintWriter out = response.getWriter(); |
|
367 |
handleGetDoctypesAction(out, params, response); |
|
368 | 304 |
} else if (action.equals("getdataport")) { |
369 | 305 |
PrintWriter out = response.getWriter(); |
370 | 306 |
if ( (username != null) && !username.equals("public") ) { |
... | ... | |
374 | 310 |
out.println("You must be authenticated to perform the getdataport " + |
375 | 311 |
"action!"); |
376 | 312 |
} |
313 |
} else if (action.equals("getaccesscontrol")) { |
|
314 |
PrintWriter out = response.getWriter(); |
|
315 |
handleGetAccessControlAction(out, params, response, username, groupname); |
|
316 |
} else if (action.equals("getprincipals")) { |
|
317 |
PrintWriter out = response.getWriter(); |
|
318 |
handleGetPrincipalsAction(out, username, password); |
|
319 |
} else if (action.equals("getdoctypes")) { |
|
320 |
PrintWriter out = response.getWriter(); |
|
321 |
handleGetDoctypesAction(out, params, response); |
|
377 | 322 |
} else if (action.equals("getdtdschema")) { |
378 | 323 |
PrintWriter out = response.getWriter(); |
379 | 324 |
handleGetDTDSchemaAction(out, params, response); |
380 | 325 |
} else if (action.equals("getdataguide")) { |
381 | 326 |
PrintWriter out = response.getWriter(); |
382 | 327 |
handleGetDataGuideAction(out, params, response); |
383 |
} else if (action.equals("getprincipals")) { |
|
384 |
PrintWriter out = response.getWriter(); |
|
385 |
handleGetPrincipalsAction(out, username, password); |
|
386 | 328 |
} else if (action.equals("login") || action.equals("logout")) { |
387 | 329 |
} else if (action.equals("protocoltest")) { |
388 | 330 |
String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9"; |
... | ... | |
429 | 371 |
PrintWriter out = response.getWriter(); |
430 | 372 |
out.println("Error: action not registered. Please report this error."); |
431 | 373 |
} |
374 |
|
|
432 | 375 |
util.closeConnections(); |
433 | 376 |
// Close the stream to the client |
434 |
//out.close(); |
|
377 |
// out.close();
|
|
435 | 378 |
} |
436 | 379 |
|
437 |
/** |
|
438 |
* decodes the mouse click information coming from the client. |
|
439 |
* This function may be overwritten to provide specific functionality |
|
440 |
* for different applications. |
|
441 |
* @param params the parameters from the CGI |
|
442 |
* @return action the action to be performed or "error" if an error was |
|
443 |
* generated |
|
444 |
*/ |
|
445 |
protected String decodeMouseAction(Hashtable params) |
|
446 |
{ |
|
447 |
// Determine what type of request the user made |
|
448 |
// if the action parameter is set, use it as a default |
|
449 |
// but if the ypos param is set, calculate the action needed |
|
450 |
String action=null; |
|
451 |
long ypos = 0; |
|
452 |
try { |
|
453 |
ypos = (new Long(((String[])params.get("ypos"))[0]).longValue()); |
|
454 |
//out.println("<P>YPOS IS " + ypos); |
|
455 |
if (ypos <= 13) { |
|
456 |
action = "read"; |
|
457 |
} else if (ypos > 13 && ypos <= 27) { |
|
458 |
action = "validate"; |
|
459 |
} else if (ypos > 27) { |
|
460 |
action = "transform"; |
|
461 |
} |
|
462 |
return action; |
|
463 |
} catch (Exception npe) { |
|
464 |
// |
|
465 |
// MBJ -- NOTE that this should be handled more gracefully with |
|
466 |
// the new exception infrastructure -- this "error" return |
|
467 |
// value is inappropriate |
|
468 |
//out.println("<P>Caught exception looking for Y value."); |
|
469 |
return "error"; |
|
470 |
} |
|
471 |
} |
|
472 |
|
|
473 |
/** |
|
474 |
* sends the port number that the data socket is running on. This is a |
|
475 |
* parameter set in the metacat.properties file. |
|
476 |
*/ |
|
477 |
private void handleGetDataPortAction(PrintWriter out, Hashtable params, |
|
478 |
HttpServletResponse response, |
|
479 |
String username, String groupname, |
|
480 |
String sess_id) |
|
481 |
{ |
|
482 |
int port; |
|
483 |
String filedir = null; |
|
484 |
try |
|
485 |
{ |
|
486 |
filedir = util.getOption("datafilepath"); |
|
487 |
|
|
488 |
Random r = new Random(); |
|
489 |
port = r.nextInt(65000); //pick a random port between 0-65000 |
|
490 |
//System.out.println("random port is: " + port); |
|
491 |
while(!DataFileServer.portIsAvailable(port)) |
|
492 |
{ |
|
493 |
port = r.nextInt(65000); |
|
494 |
//System.out.println("next port used: " + port); |
|
495 |
} |
|
496 |
DataFileServer dfs = new DataFileServer(port, username, sess_id); |
|
497 |
dfs.start(); |
|
498 |
|
|
499 |
//System.out.println("filedir: " + filedir); |
|
500 |
//System.out.println("port: " + port); |
|
501 |
response.setContentType("text/xml"); |
|
502 |
out.println("<?xml version=\"1.0\"?>"); |
|
503 |
out.println("<port>"); |
|
504 |
out.print(port); |
|
505 |
out.print("</port>"); |
|
506 |
|
|
507 |
} |
|
508 |
catch (Exception e) |
|
509 |
{ |
|
510 |
System.out.println("error in MetacatServlet.handleGetDataPortAction: " + |
|
511 |
e.getMessage()); |
|
512 |
} |
|
513 |
} |
|
514 |
|
|
380 |
// LOGIN & LOGOUT SECTION |
|
515 | 381 |
/** |
516 | 382 |
* Handle the login request. Create a new session object. |
517 | 383 |
* Do user authentication through the session. |
... | ... | |
555 | 421 |
response.setContentType("text/xml"); |
556 | 422 |
out.println(sess.getMessage()); |
557 | 423 |
} |
558 |
|
|
559 |
/* WITHOUT XSLT transformation |
|
560 |
// redirects response to html page |
|
561 |
if (qformat.equals("html")) { |
|
562 |
// user authentication successful |
|
563 |
if (isValid) { |
|
564 |
response.sendRedirect( |
|
565 |
response.encodeRedirectUrl(htmlpath + "/metacat.html")); |
|
566 |
// unsuccessful user authentication |
|
567 |
} else { |
|
568 |
response.sendRedirect(htmlpath + "/login.html"); |
|
569 |
} |
|
570 |
// any output is returned |
|
571 |
} else { |
|
572 |
response.setContentType("text/xml"); |
|
573 |
out.println(sess.getMessage()); |
|
574 |
} |
|
575 |
*/ |
|
576 |
|
|
577 | 424 |
} |
578 | 425 |
|
579 | 426 |
/** |
... | ... | |
613 | 460 |
response.setContentType("text/xml"); |
614 | 461 |
out.println(output.toString()); |
615 | 462 |
} |
616 |
|
|
617 |
/* WITHOUT XSLT transformation |
|
618 |
// redirects response to html page |
|
619 |
if (qformat.equals("html")) { |
|
620 |
response.sendRedirect(htmlpath + "/index.html"); |
|
621 |
} catch(Exception e) { |
|
622 |
util.returnConnection(conn); |
|
623 |
} |
|
624 |
// any output is returned |
|
625 |
} else { |
|
626 |
response.setContentType("text/xml"); |
|
627 |
out.println(output.toString()); |
|
628 |
} |
|
629 |
*/ |
|
630 | 463 |
} |
631 |
|
|
464 |
// END OF LOGIN & LOGOUT SECTION |
|
632 | 465 |
|
466 |
// SQUERY & QUERY SECTION |
|
633 | 467 |
/** |
634 | 468 |
* Retreive the squery xml, execute it and display it |
635 | 469 |
* |
... | ... | |
829 | 663 |
//System.out.println(resultset.toString()); |
830 | 664 |
return resultset.toString(); |
831 | 665 |
} |
666 |
// END OF SQUERY & QUERY SECTION |
|
832 | 667 |
|
833 |
/** |
|
834 |
* Handle the request to view the abstract of a document. |
|
835 |
* The abstractpath CGI parameter gives the xml path to the abstract |
|
836 |
* node. |
|
668 |
// READ SECTION |
|
669 |
/** |
|
670 |
* Handle the "read" request of metadata/data files from Metacat |
|
671 |
* or any files from Internet; |
|
672 |
* transformed metadata XML document into HTML presentation if requested; |
|
673 |
* zip files when more than one were requested. |
|
674 |
* |
|
675 |
* @param params the Hashtable of HTTP request parameters |
|
676 |
* @param response the HTTP response object linked to the client |
|
677 |
* @param user the username sent the request |
|
678 |
* @param group the user's groupname |
|
837 | 679 |
*/ |
838 |
private void handleViewAbstractAction(PrintWriter out, Hashtable params,
|
|
839 |
HttpServletResponse response) throws IOException, SQLException
|
|
680 |
private void handleReadAction(Hashtable params, HttpServletResponse response,
|
|
681 |
String user, String group)
|
|
840 | 682 |
{ |
841 |
String abstractpath = null;
|
|
842 |
String docid = null;
|
|
843 |
Connection conn = null; |
|
844 |
response.setContentType("text/html");
|
|
845 |
try
|
|
846 |
{
|
|
847 |
docid = ((String[])params.get("docid"))[0];
|
|
848 |
if(params.containsKey("abstractpath"))
|
|
849 |
{
|
|
850 |
//the CGI parameter abstractpath holds the path to the abstract
|
|
851 |
//that should be displayed.
|
|
852 |
abstractpath = ((String[])params.get("abstractpath"))[0];
|
|
683 |
ServletOutputStream out = null;
|
|
684 |
ZipOutputStream zout = null;
|
|
685 |
|
|
686 |
try {
|
|
687 |
String[] docs = new String[0];
|
|
688 |
String docid = "";
|
|
689 |
String qformat = "";
|
|
690 |
String abstrpath = null;
|
|
691 |
boolean zip = false;
|
|
692 |
// read the params
|
|
693 |
if (params.containsKey("docid")) {
|
|
694 |
docs = (String[])params.get("docid");
|
|
853 | 695 |
} |
854 |
else |
|
855 |
{ |
|
856 |
out.println("error: no abstractpath parameter"); |
|
696 |
if (params.containsKey("qformat")) { |
|
697 |
qformat = ((String[])params.get("qformat"))[0]; |
|
857 | 698 |
} |
858 |
conn = util.getConnection(); |
|
859 |
|
|
860 |
Object[] abstracts = DBQuery.getNodeContent(abstractpath, docid, conn); |
|
861 |
|
|
862 |
out.println("<html><head><title>Abstract</title></head>"); |
|
863 |
out.println("<body bgcolor=\"white\"><h1>Abstract</h1>"); |
|
864 |
for(int i=0; i<abstracts.length; i++) |
|
865 |
{ |
|
866 |
out.println("<p>" + (String)abstracts[i] + "</p>"); |
|
699 |
if (params.containsKey("abstractpath")) { |
|
700 |
abstrpath = ((String[])params.get("abstractpath"))[0]; |
|
701 |
viewAbstract(response, abstrpath, docs[0]); |
|
867 | 702 |
} |
868 |
out.println("</body></html>"); |
|
869 |
} |
|
870 |
catch (IOException ioe) |
|
871 |
{ |
|
872 |
util.debugMessage("error in MetacatServlet.handlegetabstract: " + |
|
873 |
ioe.getMessage()); |
|
874 |
System.out.println("IO error in MetacatServlet.handlegetabstract: " + |
|
875 |
ioe.getMessage()); |
|
876 |
} |
|
877 |
catch(SQLException sqle) |
|
878 |
{ |
|
879 |
util.debugMessage("sql error in MetacatServLet.handlegetabstract: " + |
|
880 |
sqle.getMessage()); |
|
881 |
System.out.println("sql error in MetacatServLet.handlegetabstract: " + |
|
882 |
sqle.getMessage()); |
|
883 |
} |
|
884 |
catch(Exception e) |
|
885 |
{ |
|
886 |
util.debugMessage("error in handlegetabstract: " + e.getMessage()); |
|
887 |
System.out.println("error in MetacatServlEt.handlegetabstract: " + |
|
703 |
if ( (docs.length > 1) || qformat.equals("zip") ) { |
|
704 |
zip = true; |
|
705 |
out = response.getOutputStream(); |
|
706 |
response.setContentType("application/zip"); //MIME type |
|
707 |
zout = new ZipOutputStream(out); |
|
708 |
} |
|
709 |
// go through the list of docs to read |
|
710 |
for (int i=0; i < docs.length; i++ ) { |
|
711 |
try { |
|
712 |
|
|
713 |
URL murl = new URL(docs[i]); |
|
714 |
Hashtable murlQueryStr = util.parseQuery(murl.getQuery()); |
|
715 |
// case docid="http://.../?docid=aaa" |
|
716 |
// or docid="metacat://.../?docid=bbb" |
|
717 |
if (murlQueryStr.containsKey("docid")) { |
|
718 |
// get only docid, eliminate the rest |
|
719 |
docid = (String)murlQueryStr.get("docid"); |
|
720 |
if ( zip ) { |
|
721 |
addDocToZip(docid, zout); |
|
722 |
} else { |
|
723 |
readFromMetacat(response, docid, qformat, abstrpath, |
|
724 |
user, group, zip, zout); |
|
725 |
} |
|
726 |
|
|
727 |
// case docid="http://.../filename" |
|
728 |
} else { |
|
729 |
docid = docs[i]; |
|
730 |
if ( zip ) { |
|
731 |
addDocToZip(docid, zout); |
|
732 |
} else { |
|
733 |
readFromURLConnection(response, docid); |
|
734 |
} |
|
735 |
} |
|
736 |
|
|
737 |
// case docid="ccc" |
|
738 |
} catch (MalformedURLException mue) { |
|
739 |
docid = docs[i]; |
|
740 |
if ( zip ) { |
|
741 |
addDocToZip(docid, zout); |
|
742 |
} else { |
|
743 |
readFromMetacat(response, docid, qformat, abstrpath, |
|
744 |
user, group, zip, zout); |
|
745 |
} |
|
746 |
} |
|
747 |
|
|
748 |
} /* end for */ |
|
749 |
|
|
750 |
if ( zip ) { |
|
751 |
zout.finish(); //terminate the zip file |
|
752 |
zout.close(); //close the zip stream |
|
753 |
} |
|
754 |
|
|
755 |
} catch (Exception e) { |
|
756 |
try { |
|
757 |
if ( out != null ) { out.close(); } |
|
758 |
if ( zout != null ) { zout.close(); } |
|
759 |
response.setContentType("text/xml"); //MIME type |
|
760 |
PrintWriter pw = response.getWriter(); |
|
761 |
pw.println(e.getMessage()); |
|
762 |
} catch (IOException ioe) { |
|
763 |
System.out.println("Problem with the servlet output " + |
|
764 |
"in MetacatServlet.handleReadAction: " + |
|
765 |
ioe.getMessage()); |
|
766 |
ioe.printStackTrace(System.out); |
|
767 |
|
|
768 |
} |
|
769 |
|
|
770 |
System.out.println("Error in MetacatServlet.handleReadAction: " + |
|
888 | 771 |
e.getMessage()); |
772 |
e.printStackTrace(System.out); |
|
889 | 773 |
} |
890 | 774 |
|
891 |
util.returnConnection(conn); |
|
892 | 775 |
} |
893 |
|
|
894 |
/** |
|
895 |
* Handle the database getrelateddocument request and return a XML document, |
|
896 |
* possibly transformed from XML into HTML |
|
897 |
*/ |
|
898 |
private void handleGetRelatedDocumentAction(PrintWriter out, Hashtable params, |
|
899 |
HttpServletResponse response, URL murl) |
|
900 |
throws ClassNotFoundException, IOException, SQLException |
|
776 |
|
|
777 |
// read metadata or data from Metacat |
|
778 |
private void readFromMetacat(HttpServletResponse response, String docid, |
|
779 |
String qformat, String abstrpath, String user, |
|
780 |
String group, boolean zip, ZipOutputStream zout) |
|
781 |
throws ClassNotFoundException, IOException, SQLException, |
|
782 |
McdbException, Exception |
|
901 | 783 |
{ |
902 |
String docid = null; |
|
903 | 784 |
Connection conn = null; |
785 |
try { |
|
786 |
conn = util.getConnection(); |
|
787 |
DocumentImpl doc = new DocumentImpl(conn, docid); |
|
904 | 788 |
|
905 |
//if(params.containsKey("url")) |
|
906 |
//{//the identifier for the related document is contained in the URL param |
|
907 |
try |
|
908 |
{ |
|
909 |
DocumentImpl xmldoc=null; |
|
910 |
//MetacatURL murl = new MetacatURL(((String[])params.get("url"))[0]); |
|
911 |
if(murl.getProtocol().equals("metacat")) |
|
912 |
{//get the document from the database if it is the right type of url |
|
913 |
//Hashtable murlParams = murl.getHashParams(); |
|
914 |
Hashtable murlParams = util.parseQuery(murl.getQuery()); |
|
915 |
if(murlParams.containsKey("docid")) |
|
916 |
{//the docid should be first |
|
917 |
docid = (String)murlParams.get("docid"); //get the docid value |
|
918 |
conn = util.getConnection(); |
|
919 |
xmldoc = new DocumentImpl(conn, docid); |
|
920 |
String qformat = ((String[])params.get("qformat"))[0]; |
|
921 |
if (qformat.equals("xml")) |
|
922 |
{ |
|
923 |
// set content type and other response header fields first |
|
924 |
response.setContentType("text/xml"); |
|
925 |
xmldoc.toXml(out); |
|
926 |
//out.println(xmldoc); |
|
927 |
} |
|
928 |
else if (qformat.equals("html")) |
|
929 |
{ |
|
930 |
response.setContentType("text/html"); |
|
931 |
// Look up the document type |
|
932 |
String sourcetype = xmldoc.getDoctype(); |
|
933 |
// Transform the document to the new doctype |
|
934 |
DBTransform dbt = new DBTransform(conn); |
|
935 |
dbt.transformXMLDocument(xmldoc.toString(), sourcetype, |
|
936 |
"-//W3C//HTML//EN", out); |
|
937 |
} |
|
938 |
|
|
939 |
util.returnConnection(conn); |
|
789 |
if ( doc.getRootNodeID() == 0 ) { |
|
790 |
// this is data file |
|
791 |
// ??? look at handleGetData |
|
792 |
ServletOutputStream out = response.getOutputStream(); |
|
793 |
String filepath = util.getOption("datafilepath"); |
|
794 |
if(!filepath.endsWith("/")) { |
|
795 |
filepath += "/"; |
|
796 |
} |
|
797 |
String filename = filepath + doc.getDocname(); //MIME type |
|
798 |
String contentType = getServletContext().getMimeType(filename); |
|
799 |
if (contentType == null) { |
|
800 |
if (filename.endsWith(".xml")) { |
|
801 |
contentType="text/xml"; |
|
802 |
} else if (filename.endsWith(".css")) { |
|
803 |
contentType="text/css"; |
|
804 |
} else if (filename.endsWith(".dtd")) { |
|
805 |
contentType="text/plain"; |
|
806 |
} else if (filename.endsWith(".xsd")) { |
|
807 |
contentType="text/plain"; |
|
808 |
} else if (filename.endsWith("/")) { |
|
809 |
contentType="text/directory"; |
|
810 |
} else { |
|
811 |
contentType="text/plain"; |
|
940 | 812 |
} |
941 |
else |
|
942 |
{ |
|
943 |
//throw new Exception("handleGetDocument: bad URL"); |
|
944 |
System.err.println("handleGetDocument: bad URL"); |
|
945 |
} |
|
946 | 813 |
} |
947 |
else if(murl.getProtocol().equals("http")) |
|
948 |
{//get the document from the internet |
|
949 |
//Hashtable murlParams = murl.getHashParams(); |
|
950 |
Hashtable murlParams = util.parseQuery(murl.getQuery()); |
|
951 |
if(murlParams.containsKey("httpurl")) |
|
952 |
{//httpurl is the param name for an http url. |
|
953 |
URL urlconn = new URL((String)murlParams.get("httpurl")); |
|
954 |
//create a new url obj. |
|
955 |
//DataInputStream htmldoc = new DataInputStream(urlconn.openStream()); |
|
956 |
BufferedReader htmldoc = new BufferedReader( |
|
957 |
new InputStreamReader(urlconn.openStream())); |
|
958 |
//bind a data stream. |
|
959 |
try |
|
960 |
{ //display the document |
|
961 |
String line=null; |
|
962 |
while((line = htmldoc.readLine()) != null) |
|
963 |
{ |
|
964 |
out.println(line); |
|
965 |
} |
|
966 |
} |
|
967 |
catch(Exception e) |
|
968 |
{ |
|
969 |
System.out.println("error viewing html document from " + |
|
970 |
"MetacatServlet.handleGetRelatedDocumentAction: " + |
|
971 |
e.getMessage()); |
|
972 |
} |
|
814 |
response.setContentType(contentType); |
|
815 |
FileInputStream fin = null; |
|
816 |
try { |
|
817 |
fin = new FileInputStream(filename); |
|
818 |
byte[] buf = new byte[4 * 1024]; // 4K buffer |
|
819 |
int b = fin.read(buf); |
|
820 |
while (b != -1) { |
|
821 |
out.write(buf, 0, b); |
|
822 |
b = fin.read(buf); |
|
973 | 823 |
} |
824 |
} finally { |
|
825 |
if (fin != null) fin.close(); |
|
974 | 826 |
} |
827 |
|
|
828 |
} else { |
|
829 |
// this is metadata doc |
|
830 |
if ( qformat.equals("html") ) { |
|
831 |
response.setContentType("text/html"); //MIME type |
|
832 |
PrintWriter out = response.getWriter(); |
|
833 |
|
|
834 |
// Look up the document type |
|
835 |
String doctype = doc.getDoctype(); |
|
836 |
// Transform the document to the new doctype |
|
837 |
DBTransform dbt = new DBTransform(conn); |
|
838 |
dbt.transformXMLDocument(doc.toString(), |
|
839 |
doctype,"-//W3C//HTML//EN",out); |
|
840 |
} else { |
|
841 |
// set content type first |
|
842 |
response.setContentType("text/xml"); //MIME type |
|
843 |
PrintWriter out = response.getWriter(); |
|
844 |
doc.toXml(out); |
|
845 |
} |
|
846 |
|
|
975 | 847 |
} |
976 |
catch (McdbException e) { |
|
977 |
response.setContentType("text/xml"); |
|
978 |
e.toXml(out); |
|
979 |
} catch (Throwable t) { |
|
980 |
response.setContentType("text/html"); |
|
981 |
out.println(t.getMessage()); |
|
982 |
} finally { |
|
983 |
util.returnConnection(conn); |
|
984 |
} |
|
985 |
//} // end if |
|
986 |
} |
|
848 |
} finally { |
|
849 |
util.returnConnection(conn); |
|
850 |
} |
|
851 |
|
|
852 |
} |
|
987 | 853 |
|
988 |
/** |
|
989 |
* Handle the database read request and return an XML document, |
|
990 |
* possibly transformed from XML into HTML |
|
991 |
*/ |
|
992 |
private void handleReadAction(/*PrintWriter out,*/ Hashtable params, |
|
993 |
HttpServletResponse response, String username) |
|
994 |
throws ClassNotFoundException, IOException, SQLException |
|
854 |
// read data from URLConnection |
|
855 |
private void readFromURLConnection(HttpServletResponse response, String docid) |
|
856 |
throws IOException, MalformedURLException |
|
995 | 857 |
{ |
996 |
PrintWriter out; |
|
858 |
ServletOutputStream out = response.getOutputStream(); |
|
859 |
String contentType = getServletContext().getMimeType(docid); //MIME type |
|
860 |
if (contentType == null) { |
|
861 |
if (docid.endsWith(".xml")) { |
|
862 |
contentType="text/xml"; |
|
863 |
} else if (docid.endsWith(".css")) { |
|
864 |
contentType="text/css"; |
|
865 |
} else if (docid.endsWith(".dtd")) { |
|
866 |
contentType="text/plain"; |
|
867 |
} else if (docid.endsWith(".xsd")) { |
|
868 |
contentType="text/plain"; |
|
869 |
} else if (docid.endsWith("/")) { |
|
870 |
contentType="text/directory"; |
|
871 |
} else { |
|
872 |
contentType="text/plain"; |
|
873 |
} |
|
874 |
} |
|
875 |
response.setContentType(contentType); |
|
876 |
|
|
877 |
// this is http url |
|
878 |
URL url = new URL(docid); |
|
879 |
BufferedInputStream bis = null; |
|
997 | 880 |
try { |
998 |
//MetacatURL murl = new MetacatURL(((String[])params.get("docid"))[0]); |
|
999 |
if(params.containsKey(new String("qformat")) && |
|
1000 |
((String[])params.get("qformat"))[0].equals("bin")) |
|
1001 |
{ |
|
1002 |
handleGetData(params, response, username); |
|
881 |
bis = new BufferedInputStream(url.openStream()); |
|
882 |
byte[] buf = new byte[4 * 1024]; // 4K buffer |
|
883 |
int b = bis.read(buf); |
|
884 |
while (b != -1) { |
|
885 |
out.write(buf, 0, b); |
|
886 |
b = bis.read(buf); |
|
1003 | 887 |
} |
1004 |
else |
|
1005 |
{ |
|
1006 |
out = response.getWriter(); |
|
1007 |
URL murl = new URL(((String[])params.get("docid"))[0]); |
|
1008 |
handleGetRelatedDocumentAction(out, params, response, murl); |
|
1009 |
} |
|
1010 |
} catch (MalformedURLException mue) { |
|
1011 |
out = response.getWriter(); |
|
1012 |
handleGetDocumentAction(out, params, response); |
|
888 |
} finally { |
|
889 |
if (bis != null) bis.close(); |
|
1013 | 890 |
} |
891 |
|
|
1014 | 892 |
} |
1015 | 893 |
|
1016 |
/** |
|
1017 |
* Handle the read of a data file. |
|
1018 |
*/ |
|
1019 |
private void handleGetData(Hashtable params, |
|
1020 |
HttpServletResponse response, String username) |
|
894 |
// read file/doc and write to ZipOutputStream |
|
895 |
private void addDocToZip(String docid, ZipOutputStream zout) |
|
896 |
throws ClassNotFoundException, IOException, SQLException, |
|
897 |
McdbException, Exception |
|
1021 | 898 |
{ |
1022 |
String docid = null; |
|
1023 |
try |
|
1024 |
{ |
|
1025 |
URL murl = new URL(((String[])params.get("docid"))[0]); |
|
1026 |
Hashtable murlParams = util.parseQuery(murl.getQuery()); |
|
1027 |
if(murlParams.containsKey("docid")) |
|
1028 |
{ |
|
1029 |
docid = (String)murlParams.get("docid"); |
|
899 |
byte[] bytestring = null; |
|
900 |
ZipEntry zentry = null; |
|
901 |
|
|
902 |
try { |
|
903 |
URL url = new URL(docid); |
|
904 |
|
|
905 |
// this http url; read from URLConnection; add to zip |
|
906 |
zentry = new ZipEntry(docid); |
|
907 |
zout.putNextEntry(zentry); |
|
908 |
BufferedInputStream bis = null; |
|
909 |
try { |
|
910 |
bis = new BufferedInputStream(url.openStream()); |
|
911 |
byte[] buf = new byte[4 * 1024]; // 4K buffer |
|
912 |
int b = bis.read(buf); |
|
913 |
while(b != -1) { |
|
914 |
zout.write(buf, 0, b); |
|
915 |
b = bis.read(buf); |
|
916 |
} |
|
917 |
} finally { |
|
918 |
if (bis != null) bis.close(); |
|
1030 | 919 |
} |
1031 |
} |
|
1032 |
catch(MalformedURLException mue) |
|
1033 |
{ |
|
1034 |
docid = ((String[])params.get("docid"))[0]; |
|
1035 |
} |
|
1036 |
|
|
1037 |
File f = null; |
|
1038 |
FileInputStream fin = null; |
|
1039 |
Connection conn; |
|
1040 |
|
|
1041 |
try |
|
1042 |
{ |
|
1043 |
StringBuffer sql = new StringBuffer(); |
|
1044 |
sql.append("select docname from xml_documents where docid like '"); |
|
1045 |
sql.append(docid).append("'"); |
|
1046 |
conn = util.openDBConnection(); |
|
920 |
zout.closeEntry(); |
|
921 |
|
|
922 |
} catch (MalformedURLException mue) { |
|
1047 | 923 |
|
1048 |
AccessControlList aclobj = new AccessControlList(conn); |
|
1049 |
boolean hasPermission = aclobj.hasPermission("READ",username,docid); |
|
924 |
// this is metacat doc (data file or metadata doc) |
|
925 |
Connection conn = null; |
|
926 |
try { |
|
927 |
conn = util.getConnection(); |
|
928 |
DocumentImpl doc = new DocumentImpl(conn, docid); |
|
1050 | 929 |
|
1051 |
if(!hasPermission) |
|
1052 |
{ |
|
1053 |
response.setContentType("text/html"); |
|
1054 |
PrintWriter out = response.getWriter(); |
|
1055 |
out.println("Error: you do not have permission to view this document"); |
|
1056 |
return; |
|
1057 |
} |
|
1058 |
response.setContentType("application/octet-stream"); |
|
1059 |
ServletOutputStream sosout = response.getOutputStream(); |
|
1060 |
PreparedStatement pstmt = conn.prepareStatement(sql.toString()); |
|
1061 |
pstmt.execute(); |
|
1062 |
ResultSet rs = pstmt.getResultSet(); |
|
1063 |
boolean tablehasrows = rs.next(); |
|
1064 |
while(tablehasrows) |
|
1065 |
{ |
|
1066 |
//get the file stream from the file then send it to the output stream |
|
1067 |
String filepath = util.getOption("datafilepath"); |
|
1068 |
if(!filepath.endsWith("/")) |
|
1069 |
{ |
|
1070 |
filepath += "/"; |
|
930 |
if ( doc.getRootNodeID() == 0 ) { |
|
931 |
// this is data file; add file to zip |
|
932 |
String filepath = util.getOption("datafilepath"); |
|
933 |
if(!filepath.endsWith("/")) { |
|
934 |
filepath += "/"; |
|
935 |
} |
|
936 |
String filename = filepath + doc.getDocname(); |
|
937 |
zentry = new ZipEntry(filename); |
|
938 |
zout.putNextEntry(zentry); |
|
939 |
FileInputStream fin = null; |
|
940 |
try { |
|
941 |
fin = new FileInputStream(filename); |
|
942 |
byte[] buf = new byte[4 * 1024]; // 4K buffer |
|
943 |
int b = fin.read(buf); |
|
944 |
while (b != -1) { |
|
945 |
zout.write(buf, 0, b); |
|
946 |
b = fin.read(buf); |
|
947 |
} |
|
948 |
} finally { |
|
949 |
if (fin != null) fin.close(); |
|
950 |
} |
|
951 |
zout.closeEntry(); |
|
952 |
|
|
953 |
} else { |
|
954 |
// this is metadata doc; add doc to zip |
|
955 |
bytestring = doc.toString().getBytes(); |
|
956 |
zentry = new ZipEntry(docid + ".xml"); |
|
957 |
zentry.setSize(bytestring.length); |
|
958 |
zout.putNextEntry(zentry); |
|
959 |
zout.write(bytestring, 0, bytestring.length); |
|
960 |
zout.closeEntry(); |
|
1071 | 961 |
} |
1072 |
f = new File(filepath + rs.getString(1)); |
|
1073 |
fin = new FileInputStream(f); |
|
1074 |
|
|
1075 |
int b = fin.read(); |
|
1076 |
while(b != -1) |
|
1077 |
{ |
|
1078 |
sosout.write(b); |
|
1079 |
b = fin.read(); |
|
1080 |
} |
|
1081 |
tablehasrows = rs.next(); |
|
962 |
} finally { |
|
963 |
util.returnConnection(conn); |
|
1082 | 964 |
} |
1083 | 965 |
|
1084 |
fin.close(); |
|
1085 |
pstmt.close(); |
|
1086 |
conn.close(); |
|
1087 | 966 |
} |
1088 |
catch(Exception e) |
|
1089 |
{ |
|
1090 |
System.out.println("error in MetacatServlet.handleGetData: " + |
|
1091 |
e.getMessage()); |
|
1092 |
e.printStackTrace(System.out); |
|
1093 |
} |
|
967 |
|
|
1094 | 968 |
} |
1095 |
|
|
1096 |
/** |
|
1097 |
* Handle the database read request and return an XML document, |
|
1098 |
* possibly transformed from XML into HTML |
|
1099 |
*/ |
|
1100 |
private void handleGetDocumentAction(PrintWriter out, Hashtable params, |
|
1101 |
HttpServletResponse response) |
|
1102 |
throws ClassNotFoundException, IOException, SQLException { |
|
1103 |
String docidstr = null; |
|
1104 |
String docid = null; |
|
1105 |
String doc = null; |
|
969 |
|
|
970 |
// view abstract within document |
|
971 |
private void viewAbstract(HttpServletResponse response, |
|
972 |
String abstractpath, String docid) |
|
973 |
throws ClassNotFoundException, IOException, SQLException, |
|
974 |
McdbException, Exception |
|
975 |
{ |
|
1106 | 976 |
Connection conn = null; |
1107 |
|
|
1108 | 977 |
try { |
1109 |
// Find the document id number |
|
1110 |
docidstr = ((String[])params.get("docid"))[0]; |
|
1111 |
docid = docidstr; |
|
1112 |
|
|
1113 | 978 |
conn = util.getConnection(); |
1114 |
DocumentImpl xmldoc = new DocumentImpl(conn, docid); |
|
1115 |
// Get the document indicated from the db |
|
1116 |
//doc = docreader.readXMLDocument(docid); |
|
979 |
DocumentImpl doc = new DocumentImpl(conn, docid); |
|
980 |
|
|
981 |
Object[] abstracts = DBQuery.getNodeContent(abstractpath, docid, conn); |
|
982 |
|
|
983 |
response.setContentType("text/html"); //MIME type |
|
984 |
PrintWriter out = response.getWriter(); |
|
985 |
out.println("<html><head><title>Abstract</title></head>"); |
|
986 |
out.println("<body bgcolor=\"white\"><h1>Abstract</h1>"); |
|
987 |
for (int i=0; i<abstracts.length; i++) { |
|
988 |
out.println("<p>" + (String)abstracts[i] + "</p>"); |
|
989 |
} |
|
990 |
out.println("</body></html>"); |
|
1117 | 991 |
|
1118 |
// Return the document in XML or HTML format |
|
1119 |
String qformat=null; |
|
1120 |
if(params.containsKey("qformat")) |
|
1121 |
{ |
|
1122 |
qformat = ((String[])params.get("qformat"))[0]; |
|
1123 |
} |
|
1124 |
else |
|
1125 |
{ |
|
1126 |
qformat = "html"; |
|
1127 |
} |
|
1128 |
if (qformat.equals("xml")) { |
|
1129 |
// set content type and other response header fields first |
|
1130 |
response.setContentType("text/xml"); |
|
1131 |
xmldoc.toXml(out); |
|
1132 |
//out.println(xmldoc); |
|
1133 |
} else if (qformat.equals("html")) { |
|
1134 |
response.setContentType("text/html"); |
|
1135 |
// Look up the document type |
|
1136 |
String sourcetype = xmldoc.getDoctype(); |
|
1137 |
// Transform the document to the new doctype |
|
1138 |
DBTransform dbt = new DBTransform(conn); |
|
1139 |
dbt.transformXMLDocument(xmldoc.toString(), sourcetype, |
|
1140 |
"-//W3C//HTML//EN", out); |
|
1141 |
} |
|
1142 |
} catch (McdbException e) { |
|
1143 |
response.setContentType("text/xml"); |
|
1144 |
e.toXml(out); |
|
1145 |
} catch (Throwable t) { |
|
1146 |
response.setContentType("text/html"); |
|
1147 |
out.println(t.getMessage()); |
|
1148 | 992 |
} finally { |
1149 | 993 |
util.returnConnection(conn); |
1150 |
} |
|
1151 |
|
|
994 |
} |
|
1152 | 995 |
} |
1153 |
|
|
996 |
// END OF READ SECTION |
|
997 |
|
|
998 |
// INSERT/UPDATE SECTION |
|
1154 | 999 |
/** |
1155 | 1000 |
* Handle the database putdocument request and write an XML document |
1156 | 1001 |
* to the database connection |
... | ... | |
1306 | 1151 |
System.out.println("Validation is " + validate); |
1307 | 1152 |
return validate; |
1308 | 1153 |
} |
1154 |
// END OF INSERT/UPDATE SECTION |
|
1309 | 1155 |
|
1156 |
// DELETE SECTION |
|
1310 | 1157 |
/** |
1311 | 1158 |
* Handle the database delete request and delete an XML document |
1312 | 1159 |
* from the database connection |
... | ... | |
1349 | 1196 |
util.returnConnection(conn); |
1350 | 1197 |
} |
1351 | 1198 |
} |
1199 |
// END OF DELETE SECTION |
|
1352 | 1200 |
|
1201 |
// VALIDATE SECTION |
|
1353 | 1202 |
/** |
1354 | 1203 |
* Handle the validation request and return the results to the requestor |
1355 | 1204 |
*/ |
... | ... | |
1411 | 1260 |
util.returnConnection(conn); |
1412 | 1261 |
} |
1413 | 1262 |
} |
1414 |
|
|
1415 |
/** |
|
1416 |
* Handle the document request and return the results to the requestor |
|
1417 |
* If a docid is passed in through the params then that document |
|
1418 |
* will be retrieved form the DB and put in the zip file. |
|
1419 |
* In addition if 1 or more relations parameters are passed, those file |
|
1420 |
* will be zipped as well. Currently this is only implemented for |
|
1421 |
* metacat:// and http:// files. Support should be added for srb:// files |
|
1422 |
* as well. |
|
1263 |
// END OF VALIDATE SECTION |
|
1264 |
|
|
1265 |
// OTHER ACTION HANDLERS |
|
1266 |
/** |
|
1267 |
* sends the port number that the data socket is running on. |
|
1268 |
* This is a parameter set in the metacat.properties file. |
|
1423 | 1269 |
*/ |
1424 |
private void handleGetDataDocumentAction(ServletOutputStream out, |
|
1425 |
Hashtable params, |
|
1426 |
HttpServletResponse response) { |
|
1427 |
//find the related files, get them from their source and zip them into |
|
1428 |
//a zip file. |
|
1429 |
try |
|
1270 |
private void handleGetDataPortAction(PrintWriter out, Hashtable params, |
|
1271 |
HttpServletResponse response, |
|
1272 |
String username, String groupname, |
|
1273 |
String sess_id) |
|
1430 | 1274 |
{ |
1431 |
Connection conn = util.getConnection(); |
|
1432 |
String currentDocid = ((String[])params.get("docid"))[0]; |
|
1433 |
ZipOutputStream zout = new ZipOutputStream(out); |
|
1434 |
byte[] bytestring = null; |
|
1435 |
ZipEntry zentry = null; |
|
1436 |
DocumentImpl xmldoc = null; |
|
1437 |
String[] reldocs = null; |
|
1438 |
|
|
1439 |
if(params.containsKey("relation")) |
|
1440 |
{ //get the relations from the parameters. |
|
1441 |
reldocs = ((String[])params.get("relation")); |
|
1442 |
} |
|
1443 |
else |
|
1444 |
{ //let the for loop know that there are no relations to zip |
|
1445 |
reldocs = new String[0]; |
|
1446 |
} |
|
1447 |
|
|
1448 |
//write the base file to the zip file. |
|
1449 |
xmldoc = new DocumentImpl(conn, currentDocid); |
|
1450 |
bytestring = (xmldoc.toString()).getBytes(); |
|
1451 |
zentry = new ZipEntry(currentDocid + ".xml"); |
|
1452 |
//create a new zip entry and write the file to the stream |
|
1453 |
zentry.setSize(bytestring.length); |
|
1454 |
zout.putNextEntry(zentry); |
|
1455 |
zout.write(bytestring, 0, bytestring.length); |
|
1456 |
zout.closeEntry(); //get ready for the next entry. |
|
1457 |
|
|
1458 |
//zip up the related documents |
|
1459 |
for(int i=0; i<reldocs.length; i++) |
|
1275 |
int port; |
|
1276 |
String filedir = null; |
|
1277 |
try |
|
1460 | 1278 |
{ |
1461 |
//MetacatURL murl = new MetacatURL(((String)reldocs[i])); |
|
1462 |
URL murl = new URL(((String)reldocs[i])); |
|
1463 |
Hashtable qparams = util.parseQuery(murl.getQuery()); |
|
1464 |
if(murl.getProtocol().equals("metacat")) |
|
1279 |
filedir = util.getOption("datafilepath"); |
|
1280 |
|
|
1281 |
Random r = new Random(); |
|
1282 |
port = r.nextInt(65000); //pick a random port between 0-65000 |
|
1283 |
//System.out.println("random port is: " + port); |
|
1284 |
while(!DataFileServer.portIsAvailable(port)) |
|
1465 | 1285 |
{ |
1466 |
//get the document from the database |
|
1467 |
//xmldoc = new DocumentImpl(conn, (String)murl.getHashParam("docid")); |
|
1468 |
xmldoc = new DocumentImpl(conn, (String)qparams.get("docid")); |
|
1469 |
bytestring = (xmldoc.toString()).getBytes(); |
|
1470 |
zentry = new ZipEntry(qparams.get("docid") + ".xml"); |
|
1471 |
//create a new zip entry and write the file to the stream |
|
1472 |
zentry.setSize(bytestring.length); |
|
1473 |
zout.putNextEntry(zentry); |
|
1474 |
zout.write(bytestring, 0, bytestring.length); |
|
1475 |
zout.closeEntry(); //get ready for the next entry. |
|
1286 |
port = r.nextInt(65000); |
|
1287 |
//System.out.println("next port used: " + port); |
|
1476 | 1288 |
} |
1477 |
else if(murl.getProtocol().equals("http")) |
|
1478 |
{ |
|
1479 |
//Hashtable murlParams = murl.getHashParams(); |
|
1480 |
if(qparams.containsKey("httpurl")) |
|
1481 |
{//httpurl is the param name for an http url. |
|
1482 |
URL urlconn = new URL((String)qparams.get("httpurl")); |
|
1483 |
//create a new url obj. |
|
1484 |
BufferedReader htmldoc = new BufferedReader( |
|
1485 |
new InputStreamReader(urlconn.openStream())); |
|
1486 |
//get the data from the web server |
|
1487 |
try |
|
1488 |
{ //zip the document |
|
1489 |
String line=null; |
|
1490 |
zentry = new ZipEntry((String)qparams.get("filename")); |
|
1491 |
//get just the filename from the URL. |
|
1492 |
zout.putNextEntry(zentry); |
|
1493 |
//make a new entry in the zip file stream |
|
1494 |
while((line = htmldoc.readLine()) != null) |
|
1495 |
{ |
|
1496 |
bytestring = (line.toString()).getBytes(); |
|
1497 |
zout.write(bytestring, 0, bytestring.length); |
|
1498 |
//write out the file line by line |
|
1499 |
} |
|
1500 |
zout.closeEntry(); //close the entry in the file |
|
1501 |
} |
|
1502 |
catch(Exception e) |
|
1503 |
{ |
|
1504 |
System.out.println("error downloading html document "+ |
|
1505 |
"in metacatServlet.handleGetDataDocumentAction"); |
|
1506 |
} |
|
1507 |
} |
|
1508 |
} |
|
1289 |
DataFileServer dfs = new DataFileServer(port, username, sess_id); |
|
1290 |
dfs.start(); |
|
1291 |
|
|
1292 |
//System.out.println("filedir: " + filedir); |
|
1293 |
//System.out.println("port: " + port); |
|
1294 |
response.setContentType("text/xml"); |
|
1295 |
out.println("<?xml version=\"1.0\"?>"); |
|
1296 |
out.println("<port>"); |
|
1297 |
out.print(port); |
|
1298 |
out.print("</port>"); |
|
1299 |
|
|
1509 | 1300 |
} |
1510 |
zout.finish(); //terminate the zip file |
|
1511 |
zout.close(); //close the stream. |
|
1512 |
util.returnConnection(conn); //return the connection to the pool |
|
1301 |
catch (Exception e) |
|
1302 |
{ |
|
1303 |
System.out.println("error in MetacatServlet.handleGetDataPortAction: " + |
|
1304 |
e.getMessage()); |
|
1305 |
} |
|
1513 | 1306 |
} |
1514 |
catch(Exception e) |
|
1515 |
{ |
|
1516 |
System.out.println("Error creating zip file from " + |
|
1517 |
"MetacatServlet.handleGetDataDocumentAction: " + |
|
1518 |
e.getMessage()); |
|
1519 |
e.printStackTrace(System.out); |
|
1520 |
} |
|
1521 |
|
|
1522 |
/* |
|
1523 |
//////////old code using a shell script///////////////////////////////// |
|
1524 |
|
|
1525 |
boolean error_flag = false; |
|
1526 |
String error_message = ""; |
|
1527 |
// Get the document indicated |
|
1528 |
String[] datadoc = (String[])params.get("datadoc"); |
|
1529 |
// defaultdatapath = "C:\\Temp\\"; // for testing only!!! |
|
1530 |
// executescript = "test.bat"; // for testing only!!! |
|
1531 |
|
|
1532 |
// set content type and other response header fields first |
|
1533 |
response.setContentType("application/octet-stream"); |
|
1534 |
if (defaultdatapath!=null) { |
|
1535 |
if(!defaultdatapath.endsWith(System.getProperty("file.separator"))) { |
|
1536 |
defaultdatapath=defaultdatapath+System.getProperty("file.separator"); |
|
1537 |
} |
|
1538 |
System.out.println("Path= "+defaultdatapath+datadoc[0]); |
|
1539 |
if (executescript!=null) { |
|
1540 |
String command = null; |
|
1541 |
File scriptfile = new File(executescript); |
|
1542 |
if (scriptfile.exists()) { |
|
1543 |
command=executescript+" "+datadoc[0]; // script includes path |
|
1544 |
} else { // look in defaultdatapath |
|
1545 |
// on Win98 one MUST include the .bat extender |
|
1546 |
command = defaultdatapath+executescript+" "+datadoc[0]; |
|
1547 |
} |
|
1548 |
System.out.println(command); |
|
1549 |
try { |
|
1550 |
Process proc = Runtime.getRuntime().exec(command); |
|
1551 |
proc.waitFor(); |
|
1552 |
} |
|
1553 |
catch (Exception eee) { |
|
1554 |
System.out.println("Error running process!"); |
|
1555 |
error_flag = true; |
|
1556 |
error_message = "Error running process!";} |
|
1557 |
} // end executescript not null if |
|
1558 |
File datafile = new File(defaultdatapath+datadoc[0]); |
|
1559 |
try { |
|
1560 |
FileInputStream fw = new FileInputStream(datafile); |
|
1561 |
int x; |
|
1562 |
while ((x = fw.read())!=-1) { |
|
1563 |
out.write(x); } |
|
1564 |
fw.close(); |
|
1565 |
} catch (Exception e) { |
|
1566 |
System.out.println("Error in returning file\n"+e.getMessage()); |
|
1567 |
error_flag=true; |
|
1568 |
error_message = error_message+"\nError in returning file\n"+ |
|
1569 |
e.getMessage(); |
|
1570 |
} |
|
1571 |
} // end defaultdatapath not null if |
|
1572 |
*/ |
|
1573 |
} |
|
1574 | 1307 |
|
1575 | 1308 |
/** |
1576 | 1309 |
* Handle "getaccesscontrol" action. |
... | ... | |
1603 | 1336 |
} |
1604 | 1337 |
|
1605 | 1338 |
/** |
1339 |
* Handle the "getprincipals" action. |
|
1340 |
* Read all principals from authentication scheme in XML format |
|
1341 |
*/ |
|
1342 |
private void handleGetPrincipalsAction(PrintWriter out, String user, |
|
1343 |
String password) { |
|
1344 |
|
|
1345 |
Connection conn = null; |
|
1346 |
|
|
1347 |
try { |
|
1348 |
|
|
1349 |
// get connection from the pool |
|
1350 |
AuthSession auth = new AuthSession(); |
|
1351 |
String principals = auth.getPrincipals(user, password); |
|
1352 |
out.println(principals); |
|
1353 |
|
|
1354 |
} catch (Exception e) { |
|
1355 |
out.println("<?xml version=\"1.0\"?>"); |
|
1356 |
out.println("<error>"); |
|
1357 |
out.println(e.getMessage()); |
|
1358 |
out.println("</error>"); |
|
1359 |
} finally { |
|
1360 |
util.returnConnection(conn); |
|
1361 |
} |
|
1362 |
|
|
1363 |
} |
|
1364 |
|
|
1365 |
/** |
|
1606 | 1366 |
* Handle "getdoctypes" action. |
1607 | 1367 |
* Read all doctypes from db connection in XML format |
1608 | 1368 |
*/ |
... | ... | |
1702 | 1462 |
|
1703 | 1463 |
} |
1704 | 1464 |
|
1705 |
/** |
|
1706 |
* Handle the "getprincipals" action. |
|
1707 |
* Read all principals from authentication scheme in XML format |
|
1708 |
*/ |
|
1709 |
private void handleGetPrincipalsAction(PrintWriter out, String user, |
|
1710 |
String password) { |
|
1711 |
|
|
1712 |
Connection conn = null; |
|
1713 |
|
|
1714 |
try { |
|
1715 |
|
|
1716 |
// get connection from the pool |
|
1717 |
AuthSession auth = new AuthSession(); |
|
1718 |
String principals = auth.getPrincipals(user, password); |
|
1719 |
out.println(principals); |
|
1720 |
|
|
1721 |
} catch (Exception e) { |
|
1722 |
out.println("<?xml version=\"1.0\"?>"); |
|
1723 |
out.println("<error>"); |
|
1724 |
out.println(e.getMessage()); |
|
1725 |
out.println("</error>"); |
|
1726 |
} finally { |
|
1727 |
util.returnConnection(conn); |
|
1728 |
} |
|
1729 |
|
|
1730 |
} |
|
1731 |
|
|
1732 | 1465 |
} |
Also available in: Unified diff
- merged "getabstract" and "getdatadoc" actions to "read" action;
- put MIME types everywhere for the returned data;
- zip when more than one doc/files were requested or when specified by qformat="zip" parameter;
- get rid of "relation" parameter, instead use "docid" parameter name only - when more that one docid - zip them;
- get abstract for a metacat doc (docid) when "abstractpath" parameter is specified
- small fix in MetaCatUtil when parsing a query string of URL
So now "read" action can read xml metadata or any data files from Metacat or any file thru HTTP URLConnection
using these parameters and their combinations:
action=read
docid=nceas.1
docid=http://...?docid=nceas.2
docid=metacat://...?docid=nceas.3
docid=http://.../filename
qformat=html
qformat=xml(default for metacat metadata)
qformat=zip(default when more than one docids requested)
abstractpath