1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements a metadata catalog as a java Servlet
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Matt Jones, Dan Higgins, Jivka Bojilova, Chad Berkley
|
7
|
* Release: @release@
|
8
|
*
|
9
|
* '$Author: berkley $'
|
10
|
* '$Date: 2000-08-17 09:04:20 -0700 (Thu, 17 Aug 2000) $'
|
11
|
* '$Revision: 373 $'
|
12
|
*/
|
13
|
|
14
|
package edu.ucsb.nceas.metacat;
|
15
|
|
16
|
import java.io.PrintWriter;
|
17
|
import java.io.IOException;
|
18
|
import java.io.Reader;
|
19
|
import java.io.StringReader;
|
20
|
import java.io.BufferedReader;
|
21
|
import java.io.File;
|
22
|
import java.io.FileInputStream;
|
23
|
import java.util.Enumeration;
|
24
|
import java.util.Hashtable;
|
25
|
import java.util.ResourceBundle;
|
26
|
import java.util.PropertyResourceBundle;
|
27
|
import java.net.URL;
|
28
|
import java.net.MalformedURLException;
|
29
|
import java.sql.PreparedStatement;
|
30
|
import java.sql.ResultSet;
|
31
|
import java.sql.Connection;
|
32
|
import java.sql.SQLException;
|
33
|
import java.lang.reflect.*;
|
34
|
|
35
|
import javax.servlet.ServletConfig;
|
36
|
import javax.servlet.ServletContext;
|
37
|
import javax.servlet.ServletException;
|
38
|
import javax.servlet.ServletInputStream;
|
39
|
import javax.servlet.http.HttpServlet;
|
40
|
import javax.servlet.http.HttpServletRequest;
|
41
|
import javax.servlet.http.HttpServletResponse;
|
42
|
import javax.servlet.http.HttpSession;
|
43
|
import javax.servlet.http.HttpUtils;
|
44
|
|
45
|
import oracle.xml.parser.v2.XSLStylesheet;
|
46
|
import oracle.xml.parser.v2.XSLException;
|
47
|
import oracle.xml.parser.v2.XMLDocumentFragment;
|
48
|
import oracle.xml.parser.v2.XSLProcessor;
|
49
|
|
50
|
import org.xml.sax.SAXException;
|
51
|
|
52
|
/**
|
53
|
* A metadata catalog server implemented as a Java Servlet
|
54
|
*
|
55
|
* <p>Valid parameters are:<br>
|
56
|
* action=query -- query the values of all elements and attributes
|
57
|
* and return a result set of nodes<br>
|
58
|
* action=squery -- structured query (see pathquery.dtd)<br>
|
59
|
* action=insert -- insert an XML document into the database store<br>
|
60
|
* action=update -- update an XML document that is in the database store<br>
|
61
|
* action=delete -- delete an XML document from the database store<br>
|
62
|
* action=validate -- vallidate the xml contained in valtext<br>
|
63
|
* action=getdocument -- display an XML document in XML or HTML<br>
|
64
|
* doctype -- document type list returned by the query (publicID)<br>
|
65
|
* qformat=xml -- display resultset from query in XML<br>
|
66
|
* qformat=html -- display resultset from query in HTML<br>
|
67
|
* docid=34 -- display the document with the document ID number 34<br>
|
68
|
* doctext -- XML text of the document to load into the database<br>
|
69
|
* query -- actual query text (to go with 'action=query' or 'action=squery')<br>
|
70
|
* valtext -- XML text to be validated<br>
|
71
|
* action=getdatadoc -- retreive a stored datadocument<br>
|
72
|
* action=getdoctypes -- retreive all doctypes (publicID)<br>
|
73
|
* action=getdataguide -- retreive a Data Guide<br>
|
74
|
* datadoc -- data document name (id)<br>
|
75
|
* <p>
|
76
|
* The particular combination of parameters that are valid for each
|
77
|
* particular action value is quite specific. This documentation
|
78
|
* will be reorganized to reflect this information.
|
79
|
*/
|
80
|
public class MetaCatServlet extends HttpServlet {
|
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;
|
91
|
// path to directory where data files
|
92
|
// that can be downloaded will be stored
|
93
|
private String executescript = null;
|
94
|
// script to get data file and put it
|
95
|
// in defaultdocpath dir
|
96
|
private PropertyResourceBundle options = null;
|
97
|
|
98
|
private MetaCatUtil util = null;
|
99
|
|
100
|
/**
|
101
|
* Initialize the servlet by creating appropriate database connections
|
102
|
*/
|
103
|
public void init( ServletConfig config ) throws ServletException {
|
104
|
try {
|
105
|
super.init( config );
|
106
|
this.config = config;
|
107
|
this.context = config.getServletContext();
|
108
|
System.out.println("MetaCatServlet Initialize");
|
109
|
|
110
|
util = new MetaCatUtil();
|
111
|
|
112
|
// Get the configuration file information
|
113
|
resultStyleURL = util.getOption("resultStyleURL");
|
114
|
xmlcatalogfile = util.getOption("xmlcatalogfile");
|
115
|
saxparser = util.getOption("saxparser");
|
116
|
defaultdatapath = util.getOption("defaultdatapath");
|
117
|
executescript = util.getOption("executescript");
|
118
|
servletpath = util.getOption("servletpath");
|
119
|
htmlpath = util.getOption("htmlpath");
|
120
|
|
121
|
try {
|
122
|
// Open a pool of db connections
|
123
|
connectionPool = util.getConnectionPool();
|
124
|
} catch (Exception e) {
|
125
|
System.err.println("Error creating pool of database connections");
|
126
|
System.err.println(e.getMessage());
|
127
|
}
|
128
|
} catch ( ServletException ex ) {
|
129
|
throw ex;
|
130
|
}
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Close all db connections from the pool
|
135
|
*/
|
136
|
public void destroy() {
|
137
|
|
138
|
if (util != null) {
|
139
|
util.closeConnections();
|
140
|
}
|
141
|
}
|
142
|
|
143
|
/** Handle "GET" method requests from HTTP clients */
|
144
|
public void doGet (HttpServletRequest request, HttpServletResponse response)
|
145
|
throws ServletException, IOException {
|
146
|
|
147
|
// Process the data and send back the response
|
148
|
handleGetOrPost(request, response);
|
149
|
}
|
150
|
|
151
|
/** Handle "POST" method requests from HTTP clients */
|
152
|
public void doPost( HttpServletRequest request, HttpServletResponse response)
|
153
|
throws ServletException, IOException {
|
154
|
|
155
|
// Process the data and send back the response
|
156
|
handleGetOrPost(request, response);
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Control servlet response depending on the action parameter specified
|
161
|
*/
|
162
|
private void handleGetOrPost(HttpServletRequest request,
|
163
|
HttpServletResponse response)
|
164
|
throws ServletException, IOException
|
165
|
{
|
166
|
|
167
|
if ( util == null ) {
|
168
|
util = new MetaCatUtil();
|
169
|
}
|
170
|
if ( connectionPool == null ) {
|
171
|
try {
|
172
|
// Open a pool of db connections
|
173
|
connectionPool = util.getConnectionPool();
|
174
|
} catch (Exception e) {
|
175
|
System.err.println("Error creating pool of database connections");
|
176
|
System.err.println(e.getMessage());
|
177
|
}
|
178
|
}
|
179
|
// Get a handle to the output stream back to the client
|
180
|
PrintWriter out = response.getWriter();
|
181
|
//response.setContentType("text/html");
|
182
|
|
183
|
String name = null;
|
184
|
String[] value = null;
|
185
|
String[] docid = new String[3];
|
186
|
Hashtable params = new Hashtable();
|
187
|
Enumeration paramlist = request.getParameterNames();
|
188
|
while (paramlist.hasMoreElements()) {
|
189
|
name = (String)paramlist.nextElement();
|
190
|
value = request.getParameterValues(name);
|
191
|
|
192
|
// Decode the docid and mouse click information
|
193
|
if (name.endsWith(".y")) {
|
194
|
docid[0] = name.substring(0,name.length()-2);
|
195
|
//out.println("docid => " + docid[0]);
|
196
|
params.put("docid", docid);
|
197
|
name = "ypos";
|
198
|
}
|
199
|
if (name.endsWith(".x")) {
|
200
|
name = "xpos";
|
201
|
}
|
202
|
|
203
|
//out.println(name + " => " + value[0]);
|
204
|
params.put(name,value);
|
205
|
}
|
206
|
|
207
|
//if the user clicked on the input images, decode which image
|
208
|
//was clicked then set the action.
|
209
|
String action = decodeMouseAction(params);
|
210
|
if(action.equals("error"))
|
211
|
{
|
212
|
action = ((String[])params.get("action"))[0];
|
213
|
}
|
214
|
|
215
|
// Jivka added
|
216
|
// handle login action
|
217
|
if (action.equals("Login") || action.equals("Login Client")) {
|
218
|
handleLoginAction(out, params, request, response);
|
219
|
// handle logout action
|
220
|
} else if (action.equals("Logout") || action.equals("Logout Client")) {
|
221
|
HttpSession sess = request.getSession(false);
|
222
|
if (sess != null) { sess.invalidate(); }
|
223
|
if (action.equals("Logout Client")) {
|
224
|
out.println("<?xml version=\"1.0\"?>");
|
225
|
out.println("<success>");
|
226
|
out.println("User logout.");
|
227
|
out.println("</success>");
|
228
|
return;
|
229
|
}
|
230
|
|
231
|
response.sendRedirect(htmlpath + "/index.html");
|
232
|
|
233
|
// aware of session expiration on every request
|
234
|
} else {
|
235
|
HttpSession sess = request.getSession(true);
|
236
|
if (sess.isNew()) {
|
237
|
// session expired or has not been stored b/w user requests
|
238
|
// redirect to default page for query only access
|
239
|
|
240
|
// response.sendRedirect(htmlpath + "/sexpire.html");
|
241
|
|
242
|
}
|
243
|
}
|
244
|
// End of Jivka added
|
245
|
|
246
|
if(action.equals("query"))
|
247
|
{
|
248
|
handleQuery(out, params, response);
|
249
|
}
|
250
|
else if(action.equals("squery"))
|
251
|
{
|
252
|
if(params.containsKey("query"))
|
253
|
{
|
254
|
handleSQuery(((String[])params.get("query"))[0]);
|
255
|
}
|
256
|
else
|
257
|
{
|
258
|
out.println("Illegal action squery without \"query\" parameter");
|
259
|
}
|
260
|
}
|
261
|
else if (action.equals("getdocument")) {
|
262
|
try {
|
263
|
handleGetDocumentAction(out, params, response);
|
264
|
} catch (ClassNotFoundException e) {
|
265
|
out.println(e.getMessage());
|
266
|
} catch (SQLException se) {
|
267
|
out.println(se.getMessage());
|
268
|
}
|
269
|
} else if (action.equals("insert") || action.equals("update")) {
|
270
|
handleInsertOrUpdateAction(out, params, response);
|
271
|
} else if (action.equals("delete")) {
|
272
|
handleDeleteAction(out, params, response);
|
273
|
} else if (action.equals("validate")) {
|
274
|
handleValidateAction(out, params, response);
|
275
|
} else if (action.equals("getdatadoc")) {
|
276
|
handleGetDataDocumentAction(out, params, response);
|
277
|
} else if (action.equals("getdoctypes")) {
|
278
|
handleGetDoctypesAction(out, params, response);
|
279
|
} else if (action.equals("getdataguide")) {
|
280
|
handleGetDataGuideAction(out, params, response);
|
281
|
} else if (action.equals("Login") || action.equals("Login Client")) {
|
282
|
} else {
|
283
|
out.println("Error: action not registered. Please report this error.");
|
284
|
}
|
285
|
|
286
|
// Close the stream to the client
|
287
|
out.close();
|
288
|
}
|
289
|
|
290
|
/**
|
291
|
* decodes the mouse click information coming from the client.
|
292
|
* This function may be overwritten to provide specific functionality
|
293
|
* for different applications.
|
294
|
* @param params the parameters from the CGI
|
295
|
* @return action the action to be performed or "error" if an error was
|
296
|
* generated
|
297
|
*/
|
298
|
private String decodeMouseAction(Hashtable params)
|
299
|
{
|
300
|
// Determine what type of request the user made
|
301
|
// if the action parameter is set, use it as a default
|
302
|
// but if the ypos param is set, calculate the action needed
|
303
|
String action=null;
|
304
|
long ypos = 0;
|
305
|
try {
|
306
|
ypos = (new Long(((String[])params.get("ypos"))[0]).longValue());
|
307
|
//out.println("<P>YPOS IS " + ypos);
|
308
|
if (ypos <= 13) {
|
309
|
action = "getdocument";
|
310
|
} else if (ypos > 13 && ypos <= 27) {
|
311
|
action = "validate";
|
312
|
} else if (ypos > 27) {
|
313
|
action = "transform";
|
314
|
}
|
315
|
return action;
|
316
|
} catch (Exception npe) {
|
317
|
//out.println("<P>Caught exception looking for Y value.");
|
318
|
return "error";
|
319
|
}
|
320
|
}
|
321
|
|
322
|
// Jivka added
|
323
|
/**
|
324
|
* Handle the Login request. Create a new session object.
|
325
|
* Make a user authentication through SRB RMI Connection.
|
326
|
*/
|
327
|
|
328
|
private void handleLoginAction(PrintWriter out, Hashtable params,
|
329
|
HttpServletRequest request, HttpServletResponse response) {
|
330
|
|
331
|
MetaCatSession sess = null;
|
332
|
String un = ((String[])params.get("username"))[0];
|
333
|
String pw = ((String[])params.get("password"))[0];
|
334
|
String action = ((String[])params.get("action"))[0];
|
335
|
|
336
|
try {
|
337
|
sess = new MetaCatSession(request, un, pw);
|
338
|
} catch (Exception e) {
|
339
|
out.println(e.getMessage());
|
340
|
}
|
341
|
|
342
|
if ( un.equals("anonymous") ) {
|
343
|
try {
|
344
|
if (action.equals("Login Client")) {
|
345
|
out.println("<?xml version=\"1.0\"?>");
|
346
|
out.println("<success>");
|
347
|
out.println("User Authentication successful.");
|
348
|
out.println("</success>");
|
349
|
return;
|
350
|
} else {
|
351
|
response.sendRedirect(
|
352
|
response.encodeRedirectUrl(htmlpath + "/index.html"));
|
353
|
}
|
354
|
} catch ( java.io.IOException ioe) {
|
355
|
sess.disconnect();
|
356
|
out.println("<?xml version=\"1.0\"?>");
|
357
|
out.println("<error>");
|
358
|
out.println("MetaCatServlet.handleLoginAction() - " +
|
359
|
"Error on redirect of HttpServletResponse: " +
|
360
|
ioe.getMessage());
|
361
|
out.println("</error>");
|
362
|
return;
|
363
|
}
|
364
|
}
|
365
|
|
366
|
try {
|
367
|
if (sess.userAuth(un, pw)) {
|
368
|
try {
|
369
|
if (action.equals("Login Client")) {
|
370
|
out.println("<?xml version=\"1.0\"?>");
|
371
|
out.println("<success>");
|
372
|
out.println("User Authentication successful.");
|
373
|
out.println("</success>");
|
374
|
} else {
|
375
|
response.sendRedirect(
|
376
|
|
377
|
response.encodeRedirectUrl(htmlpath + "/metacat.html"));
|
378
|
}
|
379
|
} catch ( java.io.IOException ioe) {
|
380
|
sess.disconnect();
|
381
|
out.println("<?xml version=\"1.0\"?>");
|
382
|
out.println("<error>");
|
383
|
out.println("MetaCatServlet.handleLoginAction() - " +
|
384
|
"Error on redirect of HttpServletResponse: " +
|
385
|
ioe.getMessage());
|
386
|
out.println("</error>");
|
387
|
}
|
388
|
|
389
|
} else {
|
390
|
sess.disconnect();
|
391
|
out.println("<?xml version=\"1.0\"?>");
|
392
|
out.println("<error>");
|
393
|
out.println("SRB Connection failed. " +
|
394
|
"SRB RMI Server is not running now or " +
|
395
|
"user " + un +
|
396
|
" has not been authenticated to use the system.");
|
397
|
out.println("</error>");
|
398
|
}
|
399
|
} catch ( java.rmi.RemoteException re) {
|
400
|
sess.disconnect();
|
401
|
out.println("<?xml version=\"1.0\"?>");
|
402
|
out.println("<error>");
|
403
|
out.println("SRB Connection failed. " + re.getMessage());
|
404
|
out.println("</error>");
|
405
|
}
|
406
|
}
|
407
|
|
408
|
/**
|
409
|
*Create the squery xml, execute it and display it
|
410
|
* @param out is the output stream to the client
|
411
|
* @param params is the Hashtable of parameters that should be included
|
412
|
* in the squery.
|
413
|
* @param response is the response object linked to the client
|
414
|
* @param conn the database connection
|
415
|
*/
|
416
|
private String handleSQuery(String xmlquery)
|
417
|
{
|
418
|
Hashtable doclist = runQuery(xmlquery);
|
419
|
String resultdoc = createResultDocument(doclist, xmlquery);
|
420
|
return resultdoc;
|
421
|
}
|
422
|
|
423
|
/**
|
424
|
*Create the xml query, execute it and display the results.
|
425
|
* @param out is the output stream to the client
|
426
|
* @param params is the Hashtable of parameters that should be included
|
427
|
* in the squery.
|
428
|
* @param response is the response object linked to the client
|
429
|
*/
|
430
|
private void handleQuery(PrintWriter out, Hashtable params,
|
431
|
HttpServletResponse response)
|
432
|
{
|
433
|
|
434
|
|
435
|
//create the query and run it
|
436
|
String xmlquery = DBQuery.createSQuery(params);
|
437
|
Hashtable doclist = runQuery(xmlquery);
|
438
|
String qformat = ((String[])params.get("qformat"))[0];
|
439
|
String resultdoc = createResultDocument(doclist, xmlquery);
|
440
|
//format and transform the results
|
441
|
if(qformat.equals("html"))
|
442
|
{
|
443
|
transformResultset(resultdoc, response, out);
|
444
|
}
|
445
|
else if(qformat.equals("xml"))
|
446
|
{
|
447
|
response.setContentType("text/xml");
|
448
|
out.println(resultdoc);
|
449
|
}
|
450
|
else
|
451
|
{
|
452
|
out.println("invalid qformat: " + qformat);
|
453
|
}
|
454
|
}
|
455
|
|
456
|
/**
|
457
|
* Run the query and return a hashtable of results.
|
458
|
* @param xmlquery the query to run
|
459
|
*/
|
460
|
private Hashtable runQuery(String xmlquery)
|
461
|
{
|
462
|
Hashtable doclist=null;
|
463
|
Connection conn = null;
|
464
|
try
|
465
|
{
|
466
|
conn = util.getConnection();
|
467
|
DBQuery queryobj = new DBQuery(conn, saxparser);
|
468
|
doclist = queryobj.findDocuments(new StringReader(xmlquery));
|
469
|
util.returnConnection(conn);
|
470
|
return doclist;
|
471
|
}
|
472
|
catch (Exception e)
|
473
|
{
|
474
|
if (conn != null)
|
475
|
{
|
476
|
util.returnConnection(conn);
|
477
|
}
|
478
|
util.debugMessage("Error in runQuery: " + e.getMessage());
|
479
|
doclist = null;
|
480
|
return doclist;
|
481
|
}
|
482
|
}
|
483
|
|
484
|
/**
|
485
|
* Transorms an xml resultset document to html and sends it to the browser
|
486
|
* @param resultdoc the string representation of the document that needs
|
487
|
* to be transformed.
|
488
|
* @param response the HttpServletResponse object bound to the client.
|
489
|
* @param out the output stream to the client
|
490
|
*/
|
491
|
private void transformResultset(String resultdoc,
|
492
|
HttpServletResponse response,
|
493
|
PrintWriter out)
|
494
|
{
|
495
|
Connection conn = null;
|
496
|
try
|
497
|
{
|
498
|
conn = util.getConnection();
|
499
|
DBTransform trans = new DBTransform(conn);
|
500
|
response.setContentType("text/html");
|
501
|
trans.transformXMLDocument(resultdoc, "-//NCEAS//resultset//EN",
|
502
|
"-//W3C//HTML//EN", out);
|
503
|
}
|
504
|
catch(Exception e)
|
505
|
{
|
506
|
if (conn != null)
|
507
|
{
|
508
|
util.returnConnection(conn);
|
509
|
}
|
510
|
}
|
511
|
}
|
512
|
|
513
|
/**
|
514
|
* Transforms a hashtable of documents to an xml or html result.
|
515
|
* @param doclist- the hashtable to transform
|
516
|
* @param xmlquery- the query that returned the dolist result
|
517
|
*/
|
518
|
private String createResultDocument(Hashtable doclist, String xmlquery)
|
519
|
{
|
520
|
// Create a buffer to hold the xml result
|
521
|
StringBuffer resultset = new StringBuffer();
|
522
|
|
523
|
// Print the resulting root nodes
|
524
|
String docid = null;
|
525
|
String document = null;
|
526
|
resultset.append("<?xml version=\"1.0\"?>\n");
|
527
|
resultset.append("<resultset>\n");
|
528
|
//the following should work but it doesn't because the parser doesn't
|
529
|
//like the <?xml?> tag inside the <query> tag. This is supposed to be
|
530
|
//the way that the query itself is sent back to the client.
|
531
|
//resultset.append(" <query>" + xmlquery + "</query>");
|
532
|
Enumeration doclistkeys = doclist.keys();
|
533
|
while (doclistkeys.hasMoreElements())
|
534
|
{
|
535
|
docid = (String)doclistkeys.nextElement();
|
536
|
document = (String)doclist.get(docid);
|
537
|
resultset.append(" <document>" + document + "</document>");
|
538
|
}
|
539
|
resultset.append("</resultset>");
|
540
|
return resultset.toString();
|
541
|
}
|
542
|
|
543
|
/**
|
544
|
* Handle the database getdocument request and return a XML document,
|
545
|
* possibly transformed from XML into HTML
|
546
|
*/
|
547
|
private void handleGetDocumentAction(PrintWriter out, Hashtable params,
|
548
|
HttpServletResponse response)
|
549
|
throws ClassNotFoundException, IOException, SQLException {
|
550
|
String docidstr = null;
|
551
|
String docid = null;
|
552
|
String doc = null;
|
553
|
Connection conn = null;
|
554
|
|
555
|
try {
|
556
|
// Find the document id number
|
557
|
docidstr = ((String[])params.get("docid"))[0];
|
558
|
//docid = (new Long(docidstr)).longValue();
|
559
|
docid = docidstr;
|
560
|
|
561
|
conn = util.getConnection();
|
562
|
DBReader docreader = new DBReader(conn);
|
563
|
DBTransform dbt = new DBTransform(conn);
|
564
|
|
565
|
// Get the document indicated fromthe db
|
566
|
doc = docreader.readXMLDocument(docid);
|
567
|
|
568
|
// Return the document in XML or HTML format
|
569
|
String qformat = ((String[])params.get("qformat"))[0];
|
570
|
if (qformat.equals("xml")) {
|
571
|
// set content type and other response header fields first
|
572
|
response.setContentType("text/xml");
|
573
|
out.println(doc);
|
574
|
} else if (qformat.equals("html")) {
|
575
|
// set content type and other response header fields first
|
576
|
response.setContentType("text/html");
|
577
|
|
578
|
// Look up the document type
|
579
|
String sourcetype = docreader.getDoctypeInfo(docid).getDoctype();
|
580
|
|
581
|
// Transform the document to the new doctype
|
582
|
dbt.transformXMLDocument(doc, sourcetype, "-//W3C//HTML//EN", out);
|
583
|
}
|
584
|
//} catch (NullPointerException npe) {
|
585
|
//response.setContentType("text/html");
|
586
|
//out.println("Error getting document ID: " + docidstr +" (" + docid + ")");
|
587
|
} catch (McdbException e) {
|
588
|
response.setContentType("text/xml");
|
589
|
e.toXml(out);
|
590
|
} catch (Throwable t) {
|
591
|
response.setContentType("text/html");
|
592
|
out.println(t.getMessage());
|
593
|
} finally {
|
594
|
util.returnConnection(conn);
|
595
|
}
|
596
|
|
597
|
}
|
598
|
|
599
|
/**
|
600
|
* Handle the database putdocument request and write an XML document
|
601
|
* to the database connection
|
602
|
*/
|
603
|
private void handleInsertOrUpdateAction(PrintWriter out, Hashtable params,
|
604
|
HttpServletResponse response) {
|
605
|
|
606
|
Connection conn = null;
|
607
|
|
608
|
try {
|
609
|
// Get the document indicated
|
610
|
String[] doctext = (String[])params.get("doctext");
|
611
|
StringReader xml = null;
|
612
|
try {
|
613
|
xml = new StringReader(doctext[0]);
|
614
|
|
615
|
String[] action = (String[])params.get("action");
|
616
|
String[] docid = (String[])params.get("docid");
|
617
|
String newdocid = null;
|
618
|
|
619
|
String doAction = null;
|
620
|
if (action[0].equals("insert")) {
|
621
|
doAction = "INSERT";
|
622
|
} else if (action[0].equals("update")) {
|
623
|
doAction = "UPDATE";
|
624
|
}
|
625
|
|
626
|
try {
|
627
|
// get a connection from the pool
|
628
|
conn = util.getConnection();
|
629
|
// write the document to the database
|
630
|
DBWriter dbw = new DBWriter(conn, saxparser);
|
631
|
|
632
|
try {
|
633
|
String accNumber = docid[0];
|
634
|
if (accNumber.equals("")) {
|
635
|
accNumber = null;
|
636
|
}
|
637
|
newdocid = dbw.write(xml, doAction, accNumber);
|
638
|
} catch (NullPointerException npe) {
|
639
|
newdocid = dbw.write(xml, doAction, null);
|
640
|
}
|
641
|
} catch (Exception e) {
|
642
|
response.setContentType("text/html");
|
643
|
out.println(e.getMessage());
|
644
|
} finally {
|
645
|
util.returnConnection(conn);
|
646
|
}
|
647
|
|
648
|
// set content type and other response header fields first
|
649
|
response.setContentType("text/xml");
|
650
|
out.println("<?xml version=\"1.0\"?>");
|
651
|
out.println("<success>");
|
652
|
out.println("<docid>" + newdocid + "</docid>");
|
653
|
out.println("</success>");
|
654
|
|
655
|
} catch (NullPointerException npe) {
|
656
|
response.setContentType("text/xml");
|
657
|
out.println("<?xml version=\"1.0\"?>");
|
658
|
out.println("<error>");
|
659
|
out.println(npe.getMessage());
|
660
|
out.println("</error>");
|
661
|
}
|
662
|
} catch (Exception e) {
|
663
|
response.setContentType("text/xml");
|
664
|
out.println("<?xml version=\"1.0\"?>");
|
665
|
out.println("<error>");
|
666
|
out.println(e.getMessage());
|
667
|
if (e instanceof SAXException) {
|
668
|
Exception e2 = ((SAXException)e).getException();
|
669
|
out.println("<error>");
|
670
|
out.println(e2.getMessage());
|
671
|
out.println("</error>");
|
672
|
}
|
673
|
//e.printStackTrace(out);
|
674
|
out.println("</error>");
|
675
|
}
|
676
|
}
|
677
|
|
678
|
/**
|
679
|
* Handle the database delete request and delete an XML document
|
680
|
* from the database connection
|
681
|
*/
|
682
|
private void handleDeleteAction(PrintWriter out, Hashtable params,
|
683
|
HttpServletResponse response) {
|
684
|
|
685
|
String[] docid = (String[])params.get("docid");
|
686
|
Connection conn = null;
|
687
|
|
688
|
// delete the document from the database
|
689
|
try {
|
690
|
// get a connection from the pool
|
691
|
conn = util.getConnection();
|
692
|
DBWriter dbw = new DBWriter(conn, saxparser);
|
693
|
// NOTE -- NEED TO TEST HERE
|
694
|
// FOR EXISTENCE OF PARAM
|
695
|
// BEFORE ACCESSING ARRAY
|
696
|
try {
|
697
|
dbw.delete(docid[0]);
|
698
|
response.setContentType("text/xml");
|
699
|
out.println("<?xml version=\"1.0\"?>");
|
700
|
out.println("<success>");
|
701
|
out.println("Document deleted.");
|
702
|
out.println("</success>");
|
703
|
} catch (AccessionNumberException ane) {
|
704
|
response.setContentType("text/xml");
|
705
|
out.println("<?xml version=\"1.0\"?>");
|
706
|
out.println("<error>");
|
707
|
out.println("Error deleting document!!!");
|
708
|
out.println(ane.getMessage());
|
709
|
out.println("</error>");
|
710
|
}
|
711
|
} catch (Exception e) {
|
712
|
response.setContentType("text/xml");
|
713
|
out.println("<?xml version=\"1.0\"?>");
|
714
|
out.println("<error>");
|
715
|
out.println(e.getMessage());
|
716
|
out.println("</error>");
|
717
|
} finally {
|
718
|
util.returnConnection(conn);
|
719
|
}
|
720
|
}
|
721
|
|
722
|
/**
|
723
|
* Handle the validtion request and return the results to the requestor
|
724
|
*/
|
725
|
private void handleValidateAction(PrintWriter out, Hashtable params,
|
726
|
HttpServletResponse response) {
|
727
|
|
728
|
// Get the document indicated
|
729
|
String valtext = null;
|
730
|
|
731
|
try {
|
732
|
valtext = ((String[])params.get("valtext"))[0];
|
733
|
} catch (Exception nullpe) {
|
734
|
|
735
|
Connection conn = null;
|
736
|
String docid = null;
|
737
|
try {
|
738
|
// Find the document id number
|
739
|
docid = ((String[])params.get("docid"))[0];
|
740
|
|
741
|
// get a connection from the pool
|
742
|
conn = util.getConnection();
|
743
|
DBReader docreader = new DBReader(conn);
|
744
|
// Get the document indicated from the db
|
745
|
valtext = docreader.readXMLDocument(docid);
|
746
|
|
747
|
} catch (NullPointerException npe) {
|
748
|
response.setContentType("text/xml");
|
749
|
out.println("<error>Error getting document ID: " + docid + "</error>");
|
750
|
if ( conn != null ) { util.returnConnection(conn); }
|
751
|
return; // Jivka added
|
752
|
} catch (Exception e) {
|
753
|
response.setContentType("text/html");
|
754
|
out.println(e.getMessage());
|
755
|
} finally {
|
756
|
util.returnConnection(conn);
|
757
|
}
|
758
|
}
|
759
|
|
760
|
Connection conn = null;
|
761
|
try {
|
762
|
// get a connection from the pool
|
763
|
conn = util.getConnection();
|
764
|
DBValidate valobj = new DBValidate(saxparser,conn);
|
765
|
boolean valid = valobj.validateString(valtext);
|
766
|
|
767
|
// set content type and other response header fields first
|
768
|
response.setContentType("text/xml");
|
769
|
out.println(valobj.returnErrors());
|
770
|
|
771
|
} catch (NullPointerException npe2) {
|
772
|
// set content type and other response header fields first
|
773
|
response.setContentType("text/xml");
|
774
|
out.println("<error>Error validating document.</error>");
|
775
|
} catch (Exception e) {
|
776
|
response.setContentType("text/html");
|
777
|
out.println(e.getMessage());
|
778
|
} finally {
|
779
|
util.returnConnection(conn);
|
780
|
}
|
781
|
}
|
782
|
|
783
|
/**
|
784
|
* Handle the document request and return the results
|
785
|
* to the requestor
|
786
|
*/
|
787
|
private void handleGetDataDocumentAction(PrintWriter out, Hashtable params,
|
788
|
HttpServletResponse response) {
|
789
|
boolean error_flag = false;
|
790
|
String error_message = "";
|
791
|
// Get the document indicated
|
792
|
String[] datadoc = (String[])params.get("datadoc");
|
793
|
// defaultdatapath = "C:\\Temp\\"; // for testing only!!!
|
794
|
// executescript = "test.bat"; // for testing only!!!
|
795
|
|
796
|
// set content type and other response header fields first
|
797
|
response.setContentType("application/octet-stream");
|
798
|
if (defaultdatapath!=null) {
|
799
|
if(!defaultdatapath.endsWith(System.getProperty("file.separator"))) {
|
800
|
defaultdatapath=defaultdatapath+System.getProperty("file.separator");
|
801
|
}
|
802
|
System.out.println("Path= "+defaultdatapath+datadoc[0]);
|
803
|
if (executescript!=null) {
|
804
|
String command = null;
|
805
|
File scriptfile = new File(executescript);
|
806
|
if (scriptfile.exists()) {
|
807
|
command=executescript+" "+datadoc[0]; // script includes path
|
808
|
} else { // look in defaultdatapath
|
809
|
// on Win98 one MUST include the .bat extender
|
810
|
command = defaultdatapath+executescript+" "+datadoc[0];
|
811
|
}
|
812
|
System.out.println(command);
|
813
|
try {
|
814
|
Process proc = Runtime.getRuntime().exec(command);
|
815
|
proc.waitFor();
|
816
|
}
|
817
|
catch (Exception eee) {
|
818
|
System.out.println("Error running process!");
|
819
|
error_flag = true;
|
820
|
error_message = "Error running process!";}
|
821
|
} // end executescript not null if
|
822
|
File datafile = new File(defaultdatapath+datadoc[0]);
|
823
|
try {
|
824
|
FileInputStream fw = new FileInputStream(datafile);
|
825
|
int x;
|
826
|
while ((x = fw.read())!=-1) {
|
827
|
out.write(x); }
|
828
|
fw.close();
|
829
|
} catch (Exception e) {
|
830
|
System.out.println("Error in returning file\n"+e.getMessage());
|
831
|
error_flag=true;
|
832
|
error_message = error_message+"\nError in returning file\n"+
|
833
|
e.getMessage();
|
834
|
}
|
835
|
} // end defaultdatapath not null if
|
836
|
}
|
837
|
|
838
|
/**
|
839
|
* Handle the getdoctypes Action.
|
840
|
* Read all doctypes from db connection in XML format
|
841
|
*/
|
842
|
|
843
|
private void handleGetDoctypesAction(PrintWriter out, Hashtable params,
|
844
|
HttpServletResponse response) {
|
845
|
|
846
|
Connection conn = null;
|
847
|
|
848
|
try {
|
849
|
|
850
|
// get connection from the pool
|
851
|
conn = util.getConnection();
|
852
|
DBUtil dbutil = new DBUtil(conn);
|
853
|
String doctypes = dbutil.readDoctypes();
|
854
|
out.println(doctypes);
|
855
|
|
856
|
} catch (Exception e) {
|
857
|
out.println("<?xml version=\"1.0\"?>");
|
858
|
out.println("<error>");
|
859
|
out.println(e.getMessage());
|
860
|
out.println("</error>");
|
861
|
} finally {
|
862
|
util.returnConnection(conn);
|
863
|
}
|
864
|
|
865
|
}
|
866
|
|
867
|
/**
|
868
|
* Handle the getdataguide Action.
|
869
|
* Read Data Guide for a given doctype from db connection in XML format
|
870
|
*/
|
871
|
|
872
|
private void handleGetDataGuideAction(PrintWriter out, Hashtable params,
|
873
|
HttpServletResponse response) {
|
874
|
|
875
|
Connection conn = null;
|
876
|
String doctype = null;
|
877
|
String[] doctypeArr = (String[])params.get("doctype");
|
878
|
|
879
|
// get only the first doctype specified in the list of doctypes
|
880
|
// it could be done for all doctypes in that list
|
881
|
if (doctypeArr != null) {
|
882
|
doctype = ((String[])params.get("doctype"))[0];
|
883
|
}
|
884
|
|
885
|
try {
|
886
|
|
887
|
// get connection from the pool
|
888
|
conn = util.getConnection();
|
889
|
DBUtil dbutil = new DBUtil(conn);
|
890
|
String dataguide = dbutil.readDataGuide(doctype);
|
891
|
out.println(dataguide);
|
892
|
|
893
|
} catch (Exception e) {
|
894
|
out.println("<?xml version=\"1.0\"?>");
|
895
|
out.println("<error>");
|
896
|
out.println(e.getMessage());
|
897
|
out.println("</error>");
|
898
|
} finally {
|
899
|
util.returnConnection(conn);
|
900
|
}
|
901
|
|
902
|
}
|
903
|
|
904
|
}
|
905
|
|
906
|
/**
|
907
|
* '$Log$
|
908
|
* 'Revision 1.73 2000/08/16 20:08:20 berkley
|
909
|
* 'fixed bug with handleSQuery() that kept DMan from access the squery functionality
|
910
|
* '
|
911
|
* 'Revision 1.72 2000/08/16 18:48:57 berkley
|
912
|
* '- created transformResultset() which transforms an xml resultset document and displays it to the client useing DBTransform
|
913
|
* '- renamed transformDocument() to createResultDocument() and modified its functionality to only return a restultset xml document
|
914
|
* '- changed handleSQuery() and handleQuery() to use the new methods
|
915
|
* '
|
916
|
* 'Revision 1.71 2000/08/15 20:48:05 berkley
|
917
|
* 'remove handleQueryAction() in favor of directly calling handleQuery() and handleSQuery() from doGetOrPost()
|
918
|
* '
|
919
|
* 'Revision 1.70 2000/08/15 20:02:15 bojilova
|
920
|
* 'Cleared hardcoded paths for the location of .html files and use
|
921
|
* 'the new "htmlpath" property from metacat.properties file
|
922
|
* '
|
923
|
* 'Revision 1.69 2000/08/15 15:58:03 berkley
|
924
|
* 'Added decodeMouseAction(Hashtable) to decode the mouse click action outside of handleGetOrPost to allow for easy modification of images in a different application.
|
925
|
* '
|
926
|
* 'Revision 1.68 2000/08/14 21:28:54 berkley
|
927
|
* 'Broke up handleQueryAction into handleQuery, handleSQuery, runQuery and transformDocument. handleQueryAction is now a base function which makes calls to each of these functions to create, run and transform a query from CGI parameters.
|
928
|
* '
|
929
|
* 'Revision 1.67 2000/08/14 20:43:27 jones
|
930
|
* 'Updated build process to now use a copy of the source files so that keyword
|
931
|
* 'substitution can ocur before the build. This allows for substitution of
|
932
|
* 'hardcoded values into the source before the compile. Currently, I am
|
933
|
* 'using this feature to do the following:
|
934
|
* '
|
935
|
* ' 1) Substitute a "Release" number into the code
|
936
|
* ' 2) Substitute a hardcoded servlet path into the code and html files
|
937
|
* '
|
938
|
* 'By changing the value of "servlet-path" and "installdir" properties in
|
939
|
* 'build.xml, one can now easily install a new version of the servlet in a
|
940
|
* 'different location by simply using "ant install".
|
941
|
* '
|
942
|
* 'Revision 1.66 2000/08/14 18:27:37 bojilova
|
943
|
* 'added Logout handling
|
944
|
* '
|
945
|
* 'Revision 1.64 2000/08/11 22:20:04 jones
|
946
|
* 'Changed exception handling mechanisms for DBReader
|
947
|
* '
|
948
|
* 'Revision 1.63 2000/08/11 18:25:26 berkley
|
949
|
* 'broke up handleQueryAction into handleQuery, handleSQuery, runQuery and transformDocument
|
950
|
* '
|
951
|
* 'Revision 1.61 2000/08/10 18:56:48 bojilova
|
952
|
* 'added "anonymous" user connection
|
953
|
* '
|
954
|
* 'Revision 1.60 2000/08/09 00:39:47 jones
|
955
|
* '-Reorganized xmltodb module to support new install process for the new
|
956
|
* 'linux server (dev.nceas.ucsb.edu). Added "build.sh" shell script that
|
957
|
* 'calls ant withthe proper umask set for installation. Use:
|
958
|
* '
|
959
|
* ' ./build.sh install
|
960
|
* '
|
961
|
* 'to post a new copy of the servlet and its supporting files to the install
|
962
|
* 'directory defined in build.xml.
|
963
|
* '
|
964
|
* '-Updated the servlet to use a new servlet prefix that we'll use with the
|
965
|
* 'Tomcat servlet engine.
|
966
|
* '
|
967
|
* '-Update bin dir shell scripts to reflect new locations of relevant jar files.
|
968
|
* '
|
969
|
* 'Revision 1.59 2000/08/08 00:31:20 bojilova
|
970
|
* 'rearrange html pages for login and metacat access
|
971
|
* '
|
972
|
* 'Revision 1.58 2000/08/04 23:34:09 bojilova
|
973
|
* 'more precise handling of the Connection Pool
|
974
|
* '
|
975
|
* 'Revision 1.57 2000/08/03 23:20:31 bojilova
|
976
|
* 'Changes related to "getdataguide" action
|
977
|
* '
|
978
|
* 'Revision 1.55 2000/08/01 18:26:50 bojilova
|
979
|
* 'added Pool of Connections
|
980
|
* 'DBQuery, DBReader, DBTransform, DBUtil are created on every request and use the connections from the Pool
|
981
|
* 'same with DBWriter and DBValidate
|
982
|
* '
|
983
|
* 'Revision 1.54 2000/07/27 23:12:21 bojilova
|
984
|
* 'Added "getdoctypes" and "getdataguide" action handlers
|
985
|
* '
|
986
|
* 'Revision 1.53 2000/07/26 20:48:29 bojilova
|
987
|
* 'Added "Login Client" action for login from the Desktop Client
|
988
|
* '
|
989
|
* 'Revision 1.52 2000/07/26 20:38:40 higgins
|
990
|
* 'no message
|
991
|
* '
|
992
|
* 'Revision 1.51 2000/07/01 01:09:44 jones
|
993
|
* 'MetaCatServlet.java
|
994
|
* '
|
995
|
* 'Revision 1.50 2000/06/30 23:42:33 bojilova
|
996
|
* 'finished user auth & session tracking
|
997
|
* '
|
998
|
* 'Revision 1.49 2000/06/29 23:27:08 jones
|
999
|
* 'Fixed bug in DBEntityResolver so that it now properly delegates to
|
1000
|
* 'the system id found inthe database.
|
1001
|
* 'Changed DBValidate to use DBEntityResolver, rather than the OASIS
|
1002
|
* 'catalog, and to return validation results in XML format.
|
1003
|
* '
|
1004
|
* 'Revision 1.48 2000/06/29 20:04:51 bojilova
|
1005
|
* 'testing login
|
1006
|
* '
|
1007
|
* 'Revision 1.36 2000/06/28 02:36:26 jones
|
1008
|
* 'Added feature to now ouput COMMENTs and PIs when the document is
|
1009
|
* 'read from the database with DBReader.
|
1010
|
* '
|
1011
|
* 'Revision 1.35 2000/06/28 00:00:47 bojilova
|
1012
|
* 'changed to
|
1013
|
* 'response.sendRedirect(response.encodeRedirectUrl("/xmltodb/lib/index.html"));
|
1014
|
* '
|
1015
|
* 'Revision 1.33 2000/06/27 04:50:33 jones
|
1016
|
* 'Updated javadoc documentation.
|
1017
|
* '
|
1018
|
* 'Revision 1.32 2000/06/27 04:31:07 jones
|
1019
|
* 'Fixed bugs associated with the new UPDATE and DELETE functions of
|
1020
|
* 'DBWriter. There were problematic interactions between some static
|
1021
|
* 'variables used in DBEntityResolver and the way in which the
|
1022
|
* 'Servlet objects are re-used across multiple client invocations.
|
1023
|
* '
|
1024
|
* 'Generally cleaned up error reporting. Now all errors and success
|
1025
|
* 'results are reported as XML documents from MetaCatServlet. Need
|
1026
|
* 'to make the command line tools do the same.
|
1027
|
* '
|
1028
|
* 'Revision 1.31 2000/06/26 10:35:05 jones
|
1029
|
* 'Merged in substantial changes to DBWriter and associated classes and to
|
1030
|
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
|
1031
|
* 'functions. The command line tools and the parameters for the
|
1032
|
* 'servlet have changed substantially.
|
1033
|
* '
|
1034
|
* 'Revision 1.30.2.6 2000/06/26 10:18:06 jones
|
1035
|
* 'Partial fix for MetaCatServlet INSERT?UPDATE bug. Only will work on
|
1036
|
* 'the first call to the servlet. Subsequent calls fail. Seems to be
|
1037
|
* 'related to exception handling. Multiple successive DELETE actions
|
1038
|
* 'work fine.
|
1039
|
* '
|
1040
|
* 'Revision 1.30.2.5 2000/06/26 09:09:53 jones
|
1041
|
* 'Modified MetaCatServlet and associated files to handle the UPDATE
|
1042
|
* 'and DELETE actions for DBWriter.
|
1043
|
* '
|
1044
|
* 'Revision 1.30.2.4 2000/06/26 00:51:06 jones
|
1045
|
* 'If docid passed to DBWriter.write() is not unique, classes now generate
|
1046
|
* 'an AccessionNumberException containing the new docid generated as a
|
1047
|
* 'replacement. The docid is then extracted from the exception and
|
1048
|
* 'returned to the calling application for user feedback or client processing.
|
1049
|
* '
|
1050
|
* 'Revision 1.30.2.3 2000/06/25 23:38:17 jones
|
1051
|
* 'Added RCSfile keyword
|
1052
|
* '
|
1053
|
* 'Revision 1.30.2.2 2000/06/25 23:34:18 jones
|
1054
|
* 'Changed documentation formatting, added log entries at bottom of source files
|
1055
|
* ''
|
1056
|
*/
|