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