1
|
/**
|
2
|
* Name: MetaCatServlet.java
|
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
|
7
|
*
|
8
|
* Version: '$Id: MetaCatServlet.java 88 2000-05-09 01:59:58Z jones $'
|
9
|
*/
|
10
|
|
11
|
package edu.ucsb.nceas.metacat;
|
12
|
|
13
|
import java.io.PrintWriter;
|
14
|
import java.io.IOException;
|
15
|
import java.io.Reader;
|
16
|
import java.io.StringReader;
|
17
|
import java.io.BufferedReader;
|
18
|
import java.util.Enumeration;
|
19
|
import java.util.Hashtable;
|
20
|
import java.util.ResourceBundle;
|
21
|
import java.util.PropertyResourceBundle;
|
22
|
import java.net.URL;
|
23
|
import java.net.MalformedURLException;
|
24
|
import java.sql.PreparedStatement;
|
25
|
import java.sql.ResultSet;
|
26
|
import java.sql.Connection;
|
27
|
import java.sql.SQLException;
|
28
|
|
29
|
import javax.servlet.ServletConfig;
|
30
|
import javax.servlet.ServletContext;
|
31
|
import javax.servlet.ServletException;
|
32
|
import javax.servlet.ServletInputStream;
|
33
|
import javax.servlet.http.HttpServlet;
|
34
|
import javax.servlet.http.HttpServletRequest;
|
35
|
import javax.servlet.http.HttpServletResponse;
|
36
|
import javax.servlet.http.HttpUtils;
|
37
|
|
38
|
import oracle.xml.parser.v2.XSLStylesheet;
|
39
|
import oracle.xml.parser.v2.XSLException;
|
40
|
import oracle.xml.parser.v2.XMLDocumentFragment;
|
41
|
import oracle.xml.parser.v2.XSLProcessor;
|
42
|
import oracle.xml.parser.v2.*; //Oracle parser - DFH
|
43
|
|
44
|
/**
|
45
|
* A metadata catalog server implemented as a Java Servlet
|
46
|
*
|
47
|
* <p>Valid parameters are:<br>
|
48
|
* action=query -- query the values of all elements and attributes
|
49
|
* and return a result set of nodes<br>
|
50
|
* action=getdocument -- display an XML document in XML or HTML<br>
|
51
|
* qformat=xml -- display resultset from query in XML<br>
|
52
|
* qformat=html -- display resultset from query in HTML<br>
|
53
|
* action=getdocument -- display an XML document in XML or HTML<br>
|
54
|
* docid=34 -- display the document with the document ID number 34<br>
|
55
|
* action=putdocument -- load an XML document into the database store<br>
|
56
|
* doctext -- XML text ofthe document to load into the database<br>
|
57
|
* query -- actual query text (to go with 'action=query')<br>
|
58
|
* action=validate -- vallidate the xml contained in validatetext<br>
|
59
|
* valtext -- XML text to be validated
|
60
|
*/
|
61
|
public class MetaCatServlet extends HttpServlet {
|
62
|
|
63
|
private ServletConfig config = null;
|
64
|
private ServletContext context = null;
|
65
|
Connection conn = null;
|
66
|
DBSimpleQuery queryobj = null;
|
67
|
DBReader docreader = null;
|
68
|
DBTransform dbt = null;
|
69
|
String user = null;
|
70
|
String password = null;
|
71
|
String defaultDB = null;
|
72
|
String resultStyleURL = null;
|
73
|
String xmlcatalogfile = null;
|
74
|
PropertyResourceBundle options = null;
|
75
|
|
76
|
/**
|
77
|
* Initialize the servlet by creating appropriate database connections
|
78
|
*/
|
79
|
public void init( ServletConfig config ) throws ServletException {
|
80
|
try {
|
81
|
super.init( config );
|
82
|
this.config = config;
|
83
|
this.context = config.getServletContext();
|
84
|
System.out.println("Servlet Initialize");
|
85
|
|
86
|
// Get the configuration file information
|
87
|
options = (PropertyResourceBundle)PropertyResourceBundle.getBundle("edu.ucsb.nceas.metacat.metacat");
|
88
|
user = (String)options.handleGetObject("user");
|
89
|
password = (String)options.handleGetObject("password");
|
90
|
defaultDB = (String)options.handleGetObject("defaultDB");
|
91
|
resultStyleURL = (String)options.handleGetObject("resultStyleURL");
|
92
|
xmlcatalogfile = (String)options.handleGetObject("xmlcatalogfile");
|
93
|
|
94
|
try {
|
95
|
// Open a connection to the database
|
96
|
conn = MetaCatUtil.openDBConnection(
|
97
|
"oracle.jdbc.driver.OracleDriver",
|
98
|
defaultDB, user, password);
|
99
|
|
100
|
queryobj = new DBSimpleQuery(conn);
|
101
|
docreader = new DBReader(conn);
|
102
|
dbt = new DBTransform(conn);
|
103
|
|
104
|
} catch (Exception e) {
|
105
|
}
|
106
|
} catch ( ServletException ex ) {
|
107
|
throw ex;
|
108
|
}
|
109
|
}
|
110
|
|
111
|
/** Handle "GET" method requests from HTTP clients */
|
112
|
public void doGet (HttpServletRequest request, HttpServletResponse response)
|
113
|
throws ServletException, IOException {
|
114
|
|
115
|
// Process the data and send back the response
|
116
|
handleGetOrPost(request, response);
|
117
|
}
|
118
|
|
119
|
/** Handle "POST" method requests from HTTP clients */
|
120
|
public void doPost( HttpServletRequest request, HttpServletResponse response)
|
121
|
throws ServletException, IOException {
|
122
|
|
123
|
// Process the data and send back the response
|
124
|
handleGetOrPost(request, response);
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Control servlet response depending on the action parameter specified
|
129
|
*/
|
130
|
private void handleGetOrPost(HttpServletRequest request,
|
131
|
HttpServletResponse response)
|
132
|
throws ServletException, IOException {
|
133
|
|
134
|
// Get a handle to the output stream back to the client
|
135
|
PrintWriter out = response.getWriter();
|
136
|
|
137
|
String name = null;
|
138
|
String[] value = null;
|
139
|
Hashtable params = new Hashtable();
|
140
|
Enumeration paramlist = request.getParameterNames();
|
141
|
while (paramlist.hasMoreElements()) {
|
142
|
name = (String)paramlist.nextElement();
|
143
|
value = request.getParameterValues(name);
|
144
|
params.put(name,value);
|
145
|
}
|
146
|
|
147
|
String action = ((String[])params.get("action"))[0];
|
148
|
|
149
|
if (action.equals("query")) {
|
150
|
handleQueryAction(out, params, response);
|
151
|
} else if (action.equals("getdocument")) {
|
152
|
try {
|
153
|
handleGetDocumentAction(out, params, response);
|
154
|
} catch (ClassNotFoundException e) {
|
155
|
System.out.println(e.getMessage());
|
156
|
} catch (SQLException se) {
|
157
|
System.out.println(se.getMessage());
|
158
|
}
|
159
|
} else if (action.equals("putdocument")) {
|
160
|
handlePutDocumentAction(out, params, response);
|
161
|
} else if (action.equals("validate")) {
|
162
|
handleValidateAction(out, params, response);
|
163
|
} else {
|
164
|
out.println("Error: action not registered. Please report this error.");
|
165
|
}
|
166
|
|
167
|
// Close the stream to the client
|
168
|
out.close();
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* Handle the database query request and return a result set, possibly
|
173
|
* transformed from XML into HTML
|
174
|
*/
|
175
|
private void handleQueryAction(PrintWriter out, Hashtable params,
|
176
|
HttpServletResponse response) {
|
177
|
// Run the query
|
178
|
Hashtable nodelist = null;
|
179
|
String query = ((String[])params.get("query"))[0];
|
180
|
if (queryobj != null) {
|
181
|
nodelist = queryobj.findDocuments(query);
|
182
|
} else {
|
183
|
out.println("Query Object Init failed.");
|
184
|
/*
|
185
|
out.println(user);
|
186
|
out.println(defaultDB);
|
187
|
out.println(xmlcatalogfile);
|
188
|
*/
|
189
|
return;
|
190
|
}
|
191
|
|
192
|
// Create a buffer to hold the xml result
|
193
|
StringBuffer resultset = new StringBuffer();
|
194
|
|
195
|
// Print the resulting root nodes
|
196
|
long nodeid;
|
197
|
resultset.append("<?xml version=\"1.0\"?>\n");
|
198
|
//resultset.append("<!DOCTYPE resultset PUBLIC " +
|
199
|
// "\"-//NCEAS//resultset//EN\" \"resultset.dtd\">\n");
|
200
|
resultset.append("<resultset>\n");
|
201
|
resultset.append(" <query>" + query + "</query>");
|
202
|
Enumeration rootlist = nodelist.keys();
|
203
|
while (rootlist.hasMoreElements()) {
|
204
|
nodeid = ((Long)rootlist.nextElement()).longValue();
|
205
|
resultset.append(" <docid>" + nodeid + "</docid>");
|
206
|
}
|
207
|
resultset.append("</resultset>");
|
208
|
|
209
|
String qformat = ((String[])params.get("qformat"))[0];
|
210
|
if (qformat.equals("xml")) {
|
211
|
// set content type and other response header fields first
|
212
|
response.setContentType("text/xml");
|
213
|
out.println(resultset.toString());
|
214
|
} else if (qformat.equals("html")) {
|
215
|
// set content type and other response header fields first
|
216
|
response.setContentType("text/html");
|
217
|
//out.println("Converting to HTML...");
|
218
|
XMLDocumentFragment htmldoc = null;
|
219
|
try {
|
220
|
XSLStylesheet style = new XSLStylesheet(new URL(resultStyleURL), null);
|
221
|
htmldoc = (new XSLProcessor()).processXSL(style,
|
222
|
(Reader)(new StringReader(resultset.toString())),null);
|
223
|
htmldoc.print(out);
|
224
|
} catch (Exception e) {
|
225
|
out.println("Error transforming document:\n" + e.getMessage());
|
226
|
}
|
227
|
}
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Handle the database getdocument request and return a XML document,
|
232
|
* possibly transformed from XML into HTML
|
233
|
*/
|
234
|
private void handleGetDocumentAction(PrintWriter out, Hashtable params,
|
235
|
HttpServletResponse response)
|
236
|
throws ClassNotFoundException, IOException, SQLException {
|
237
|
// Find the document id number
|
238
|
String docidstr = ((String[])params.get("docid"))[0];
|
239
|
long docid = (new Long(docidstr)).longValue();
|
240
|
|
241
|
// Get the document indicated fromthe db
|
242
|
String doc = docreader.readXMLDocument(docid);
|
243
|
|
244
|
|
245
|
// Return the document in XML or HTML format
|
246
|
String qformat = ((String[])params.get("qformat"))[0];
|
247
|
if (qformat.equals("xml")) {
|
248
|
// set content type and other response header fields first
|
249
|
response.setContentType("text/xml");
|
250
|
out.println(doc);
|
251
|
} else if (qformat.equals("html")) {
|
252
|
// set content type and other response header fields first
|
253
|
response.setContentType("text/html");
|
254
|
|
255
|
// Look up the document type
|
256
|
String sourcetype = getDoctype(docid);
|
257
|
|
258
|
// Transform the document to the new doctype
|
259
|
dbt.transformXMLDocument(doc, sourcetype, "-//W3C//HTML//EN", out);
|
260
|
}
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* Handle the database putdocument request and write an XML document
|
265
|
* to the database connection
|
266
|
*/
|
267
|
private void handlePutDocumentAction(PrintWriter out, Hashtable params,
|
268
|
HttpServletResponse response) {
|
269
|
|
270
|
// Get the document indicated
|
271
|
String[] doctext = (String[])params.get("doctext");
|
272
|
StringReader xml = new StringReader(doctext[0]);
|
273
|
|
274
|
// write the document to the database
|
275
|
try {
|
276
|
DBSAXWriter dbw = new DBSAXWriter(xml, conn);
|
277
|
} catch (SQLException e1) {
|
278
|
out.println("Error 1 loading document:<p>\n" + e1.getMessage());
|
279
|
}catch (IOException e2) {
|
280
|
out.println("Error 2 loading document:<p>\n" + e2.getMessage());
|
281
|
}catch (ClassNotFoundException e3) {
|
282
|
out.println("Error 3 loading document:<p>\n" + e3.getMessage());
|
283
|
}
|
284
|
|
285
|
// set content type and other response header fields first
|
286
|
response.setContentType("text/xml");
|
287
|
|
288
|
out.println(doctext[0]);
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* Handle the validtion request and return the results
|
293
|
* to the requestor - DFH
|
294
|
*/
|
295
|
private void handleValidateAction(PrintWriter out, Hashtable params, HttpServletResponse response) {
|
296
|
|
297
|
// Get the document indicated
|
298
|
String[] valtext = (String[])params.get("valtext");
|
299
|
|
300
|
|
301
|
SAXParser parser = new SAXParser(); // works for both Xerces and Oracle
|
302
|
parser.setValidationMode(true); // Oracle
|
303
|
GenericXMLValidate gxv = new GenericXMLValidate(parser, xmlcatalogfile);
|
304
|
boolean valid = gxv.validateString(valtext[0]);
|
305
|
|
306
|
// set content type and other response header fields first
|
307
|
response.setContentType("text/plain");
|
308
|
|
309
|
if (valid) {
|
310
|
out.println("The input XML is VALID!");
|
311
|
}
|
312
|
else {
|
313
|
out.println("The input XML is NOT VALID\n" + gxv.returnErrors());
|
314
|
}
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* Look up the document type from the database
|
319
|
*
|
320
|
*/
|
321
|
private String getDoctype(long docid) {
|
322
|
// Look up the System ID of the XSL sheet
|
323
|
PreparedStatement pstmt;
|
324
|
String doctype = null;
|
325
|
|
326
|
try {
|
327
|
pstmt =
|
328
|
conn.prepareStatement("SELECT doctype " +
|
329
|
"FROM xml_documents " +
|
330
|
"WHERE docid = ?");
|
331
|
// Bind the values to the query
|
332
|
pstmt.setLong(1, new Long(docid).longValue());
|
333
|
|
334
|
pstmt.execute();
|
335
|
try {
|
336
|
ResultSet rs = pstmt.getResultSet();
|
337
|
try {
|
338
|
boolean tableHasRows = rs.next();
|
339
|
if (tableHasRows) {
|
340
|
try {
|
341
|
doctype = rs.getString(1);
|
342
|
} catch (SQLException e) {
|
343
|
System.out.println("Error with getString: " + e.getMessage());
|
344
|
}
|
345
|
}
|
346
|
} catch (SQLException e) {
|
347
|
System.out.println("Error with next: " + e.getMessage());
|
348
|
}
|
349
|
} catch (SQLException e) {
|
350
|
System.out.println("Error with getrset: " + e.getMessage());
|
351
|
}
|
352
|
pstmt.close();
|
353
|
} catch (SQLException e) {
|
354
|
System.out.println("Error getting id: " + e.getMessage());
|
355
|
}
|
356
|
|
357
|
return doctype;
|
358
|
}
|
359
|
}
|