1 |
51
|
jones
|
/**
|
2 |
203
|
jones
|
* '$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
|
7 |
154
|
jones
|
*
|
8 |
203
|
jones
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
51
|
jones
|
*/
|
12 |
|
|
|
13 |
|
|
package edu.ucsb.nceas.metacat;
|
14 |
|
|
|
15 |
46
|
jones
|
import java.io.PrintWriter;
|
16 |
|
|
import java.io.IOException;
|
17 |
50
|
jones
|
import java.io.Reader;
|
18 |
|
|
import java.io.StringReader;
|
19 |
59
|
jones
|
import java.io.BufferedReader;
|
20 |
185
|
jones
|
import java.io.File;
|
21 |
|
|
import java.io.FileInputStream;
|
22 |
46
|
jones
|
import java.util.Enumeration;
|
23 |
|
|
import java.util.Hashtable;
|
24 |
82
|
jones
|
import java.util.ResourceBundle;
|
25 |
|
|
import java.util.PropertyResourceBundle;
|
26 |
50
|
jones
|
import java.net.URL;
|
27 |
|
|
import java.net.MalformedURLException;
|
28 |
85
|
jones
|
import java.sql.PreparedStatement;
|
29 |
|
|
import java.sql.ResultSet;
|
30 |
50
|
jones
|
import java.sql.Connection;
|
31 |
55
|
jones
|
import java.sql.SQLException;
|
32 |
46
|
jones
|
|
33 |
|
|
import javax.servlet.ServletConfig;
|
34 |
|
|
import javax.servlet.ServletContext;
|
35 |
|
|
import javax.servlet.ServletException;
|
36 |
48
|
jones
|
import javax.servlet.ServletInputStream;
|
37 |
46
|
jones
|
import javax.servlet.http.HttpServlet;
|
38 |
|
|
import javax.servlet.http.HttpServletRequest;
|
39 |
|
|
import javax.servlet.http.HttpServletResponse;
|
40 |
210
|
bojilova
|
import javax.servlet.http.HttpSession;
|
41 |
47
|
jones
|
import javax.servlet.http.HttpUtils;
|
42 |
46
|
jones
|
|
43 |
50
|
jones
|
import oracle.xml.parser.v2.XSLStylesheet;
|
44 |
|
|
import oracle.xml.parser.v2.XSLException;
|
45 |
|
|
import oracle.xml.parser.v2.XMLDocumentFragment;
|
46 |
|
|
import oracle.xml.parser.v2.XSLProcessor;
|
47 |
|
|
|
48 |
204
|
jones
|
import org.xml.sax.SAXException;
|
49 |
|
|
|
50 |
46
|
jones
|
/**
|
51 |
|
|
* A metadata catalog server implemented as a Java Servlet
|
52 |
154
|
jones
|
*
|
53 |
|
|
* <p>Valid parameters are:<br>
|
54 |
|
|
* action=query -- query the values of all elements and attributes
|
55 |
|
|
* and return a result set of nodes<br>
|
56 |
205
|
jones
|
* action=squery -- structured query (see pathquery.dtd)<br>
|
57 |
|
|
* action=insert -- insert an XML document into the database store<br>
|
58 |
|
|
* action=update -- update an XML document that is in the database store<br>
|
59 |
|
|
* action=delete -- delete an XML document from the database store<br>
|
60 |
|
|
* action=validate -- vallidate the xml contained in valtext<br>
|
61 |
|
|
* action=getdocument -- display an XML document in XML or HTML<br>
|
62 |
|
|
* doctype -- document type list returned by the query (publicID)<br>
|
63 |
154
|
jones
|
* qformat=xml -- display resultset from query in XML<br>
|
64 |
|
|
* qformat=html -- display resultset from query in HTML<br>
|
65 |
|
|
* docid=34 -- display the document with the document ID number 34<br>
|
66 |
205
|
jones
|
* doctext -- XML text of the document to load into the database<br>
|
67 |
183
|
jones
|
* query -- actual query text (to go with 'action=query' or 'action=squery')<br>
|
68 |
205
|
jones
|
* valtext -- XML text to be validated<br>
|
69 |
|
|
* action=getdatadoc -- retreive a stored datadocument<br>
|
70 |
|
|
* datadoc -- data document name (id)<br>
|
71 |
|
|
* <p>
|
72 |
|
|
* The particular combination of parameters that are valid for each
|
73 |
|
|
* particular action value is quite specific. This documentation
|
74 |
|
|
* will be reorganized to reflect this information.
|
75 |
46
|
jones
|
*/
|
76 |
|
|
public class MetaCatServlet extends HttpServlet {
|
77 |
|
|
|
78 |
|
|
private ServletConfig config = null;
|
79 |
|
|
private ServletContext context = null;
|
80 |
181
|
jones
|
private Connection conn = null;
|
81 |
|
|
private DBQuery queryobj = null;
|
82 |
|
|
private DBReader docreader = null;
|
83 |
|
|
private DBTransform dbt = null;
|
84 |
|
|
private String resultStyleURL = null;
|
85 |
|
|
private String xmlcatalogfile = null;
|
86 |
|
|
private String saxparser = null;
|
87 |
|
|
private String defaultdatapath = null;
|
88 |
|
|
// path to directory where data files
|
89 |
|
|
// that can be downloaded will be stored
|
90 |
|
|
private String executescript = null;
|
91 |
|
|
// script to get data file and put it
|
92 |
|
|
// in defaultdocpath dir
|
93 |
|
|
private PropertyResourceBundle options = null;
|
94 |
46
|
jones
|
|
95 |
184
|
jones
|
private MetaCatUtil util = null;
|
96 |
|
|
|
97 |
50
|
jones
|
/**
|
98 |
|
|
* Initialize the servlet by creating appropriate database connections
|
99 |
|
|
*/
|
100 |
46
|
jones
|
public void init( ServletConfig config ) throws ServletException {
|
101 |
|
|
try {
|
102 |
|
|
super.init( config );
|
103 |
|
|
this.config = config;
|
104 |
|
|
this.context = config.getServletContext();
|
105 |
184
|
jones
|
System.out.println("MetaCatServlet Initialize");
|
106 |
82
|
jones
|
|
107 |
184
|
jones
|
util = new MetaCatUtil();
|
108 |
|
|
|
109 |
83
|
jones
|
// Get the configuration file information
|
110 |
184
|
jones
|
resultStyleURL = util.getOption("resultStyleURL");
|
111 |
|
|
xmlcatalogfile = util.getOption("xmlcatalogfile");
|
112 |
|
|
saxparser = util.getOption("saxparser");
|
113 |
|
|
defaultdatapath = util.getOption("defaultdatapath");
|
114 |
|
|
executescript = util.getOption("executescript");
|
115 |
82
|
jones
|
|
116 |
46
|
jones
|
try {
|
117 |
50
|
jones
|
// Open a connection to the database
|
118 |
184
|
jones
|
conn = util.openDBConnection();
|
119 |
50
|
jones
|
|
120 |
181
|
jones
|
queryobj = new DBQuery(conn,saxparser);
|
121 |
55
|
jones
|
docreader = new DBReader(conn);
|
122 |
87
|
jones
|
dbt = new DBTransform(conn);
|
123 |
68
|
higgins
|
|
124 |
46
|
jones
|
} catch (Exception e) {
|
125 |
100
|
jones
|
System.err.println("Error opening database connection");
|
126 |
46
|
jones
|
}
|
127 |
|
|
} catch ( ServletException ex ) {
|
128 |
|
|
throw ex;
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
|
132 |
50
|
jones
|
/** Handle "GET" method requests from HTTP clients */
|
133 |
46
|
jones
|
public void doGet (HttpServletRequest request, HttpServletResponse response)
|
134 |
|
|
throws ServletException, IOException {
|
135 |
|
|
|
136 |
48
|
jones
|
// Process the data and send back the response
|
137 |
59
|
jones
|
handleGetOrPost(request, response);
|
138 |
48
|
jones
|
}
|
139 |
|
|
|
140 |
50
|
jones
|
/** Handle "POST" method requests from HTTP clients */
|
141 |
48
|
jones
|
public void doPost( HttpServletRequest request, HttpServletResponse response)
|
142 |
|
|
throws ServletException, IOException {
|
143 |
|
|
|
144 |
|
|
// Process the data and send back the response
|
145 |
59
|
jones
|
handleGetOrPost(request, response);
|
146 |
48
|
jones
|
}
|
147 |
|
|
|
148 |
49
|
jones
|
/**
|
149 |
50
|
jones
|
* Control servlet response depending on the action parameter specified
|
150 |
49
|
jones
|
*/
|
151 |
59
|
jones
|
private void handleGetOrPost(HttpServletRequest request,
|
152 |
|
|
HttpServletResponse response)
|
153 |
48
|
jones
|
throws ServletException, IOException {
|
154 |
|
|
|
155 |
100
|
jones
|
if (conn == null) {
|
156 |
|
|
System.err.println("Connection to database lost. Reopening...");
|
157 |
|
|
try {
|
158 |
|
|
// Open a connection to the database
|
159 |
184
|
jones
|
conn = util.openDBConnection();
|
160 |
100
|
jones
|
|
161 |
181
|
jones
|
queryobj = new DBQuery(conn, saxparser);
|
162 |
100
|
jones
|
docreader = new DBReader(conn);
|
163 |
|
|
dbt = new DBTransform(conn);
|
164 |
|
|
|
165 |
|
|
} catch (Exception e) {
|
166 |
|
|
System.err.println("Error opening database connection");
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
|
170 |
49
|
jones
|
// Get a handle to the output stream back to the client
|
171 |
48
|
jones
|
PrintWriter out = response.getWriter();
|
172 |
102
|
jones
|
//response.setContentType("text/html");
|
173 |
49
|
jones
|
|
174 |
59
|
jones
|
String name = null;
|
175 |
|
|
String[] value = null;
|
176 |
103
|
jones
|
String[] docid = new String[3];
|
177 |
59
|
jones
|
Hashtable params = new Hashtable();
|
178 |
|
|
Enumeration paramlist = request.getParameterNames();
|
179 |
|
|
while (paramlist.hasMoreElements()) {
|
180 |
|
|
name = (String)paramlist.nextElement();
|
181 |
|
|
value = request.getParameterValues(name);
|
182 |
103
|
jones
|
|
183 |
|
|
// Decode the docid and mouse click information
|
184 |
|
|
if (name.endsWith(".y")) {
|
185 |
|
|
docid[0] = name.substring(0,name.length()-2);
|
186 |
|
|
//out.println("docid => " + docid[0]);
|
187 |
|
|
params.put("docid", docid);
|
188 |
|
|
name = "ypos";
|
189 |
|
|
}
|
190 |
|
|
if (name.endsWith(".x")) {
|
191 |
|
|
name = "xpos";
|
192 |
|
|
}
|
193 |
|
|
|
194 |
102
|
jones
|
//out.println(name + " => " + value[0]);
|
195 |
59
|
jones
|
params.put(name,value);
|
196 |
|
|
}
|
197 |
|
|
|
198 |
103
|
jones
|
// Determine what type of request the user made
|
199 |
|
|
// if the action parameter is set, use it as a default
|
200 |
|
|
// but if the ypos param is set, calculate the action needed
|
201 |
49
|
jones
|
String action = ((String[])params.get("action"))[0];
|
202 |
103
|
jones
|
long ypos = 0;
|
203 |
|
|
try {
|
204 |
|
|
ypos = (new Long(((String[])params.get("ypos"))[0]).longValue());
|
205 |
|
|
//out.println("<P>YPOS IS " + ypos);
|
206 |
|
|
if (ypos <= 13) {
|
207 |
|
|
action = "getdocument";
|
208 |
|
|
} else if (ypos > 13 && ypos <= 27) {
|
209 |
|
|
action = "validate";
|
210 |
|
|
} else if (ypos > 27) {
|
211 |
|
|
action = "transform";
|
212 |
|
|
} else {
|
213 |
|
|
action = "";
|
214 |
|
|
}
|
215 |
|
|
} catch (Exception npe) {
|
216 |
|
|
//out.println("<P>Caught exception looking for Y value.");
|
217 |
|
|
}
|
218 |
210
|
bojilova
|
// Jivka added
|
219 |
|
|
// handle login action
|
220 |
|
|
if (action.equals("Login")) {
|
221 |
|
|
handleLoginAction(out, params, request, response);
|
222 |
|
|
// handle logout action
|
223 |
|
|
} else if (action.equals("Logout")) {
|
224 |
|
|
HttpSession sess = request.getSession(false);
|
225 |
|
|
if (sess != null) {
|
226 |
|
|
sess.invalidate();
|
227 |
|
|
response.sendRedirect("/xmltodb/lib/login.html");
|
228 |
|
|
}
|
229 |
|
|
// aware of session expiration on every request
|
230 |
|
|
} else {
|
231 |
|
|
HttpSession sess = request.getSession(false);
|
232 |
|
|
if (sess == null) {
|
233 |
|
|
out.println("Session expired. You will be redirected to Login page again.");
|
234 |
|
|
response.sendRedirect("/xmltodb/lib/login.html");
|
235 |
|
|
}
|
236 |
|
|
}
|
237 |
|
|
// End of Jivka added
|
238 |
181
|
jones
|
if (action.equals("query") || action.equals("squery")) {
|
239 |
49
|
jones
|
handleQueryAction(out, params, response);
|
240 |
|
|
} else if (action.equals("getdocument")) {
|
241 |
87
|
jones
|
try {
|
242 |
|
|
handleGetDocumentAction(out, params, response);
|
243 |
|
|
} catch (ClassNotFoundException e) {
|
244 |
103
|
jones
|
out.println(e.getMessage());
|
245 |
87
|
jones
|
} catch (SQLException se) {
|
246 |
103
|
jones
|
out.println(se.getMessage());
|
247 |
87
|
jones
|
}
|
248 |
203
|
jones
|
} else if (action.equals("insert") || action.equals("update")) {
|
249 |
|
|
handleInsertOrUpdateAction(out, params, response);
|
250 |
|
|
} else if (action.equals("delete")) {
|
251 |
|
|
handleDeleteAction(out, params, response);
|
252 |
68
|
higgins
|
} else if (action.equals("validate")) {
|
253 |
|
|
handleValidateAction(out, params, response);
|
254 |
91
|
higgins
|
} else if (action.equals("getdatadoc")) {
|
255 |
|
|
handleGetDataDocumentAction(out, params, response);
|
256 |
50
|
jones
|
} else {
|
257 |
|
|
out.println("Error: action not registered. Please report this error.");
|
258 |
46
|
jones
|
}
|
259 |
|
|
|
260 |
49
|
jones
|
// Close the stream to the client
|
261 |
46
|
jones
|
out.close();
|
262 |
|
|
}
|
263 |
49
|
jones
|
|
264 |
210
|
bojilova
|
// Jivka added
|
265 |
50
|
jones
|
/**
|
266 |
210
|
bojilova
|
* Handle the Login request. Create a new session object.
|
267 |
|
|
* Make a user authentication through SRB RMI Connection.
|
268 |
|
|
*/
|
269 |
|
|
private void handleLoginAction(PrintWriter out, Hashtable params,
|
270 |
|
|
HttpServletRequest request, HttpServletResponse response) {
|
271 |
|
|
String un = (String)params.get("username");
|
272 |
|
|
String pw = (String)params.get("password");
|
273 |
|
|
|
274 |
|
|
MetaCatSession sess = new MetaCatSession(request, un, pw);
|
275 |
|
|
try {
|
276 |
|
|
if (sess.userAuth(pw)) {
|
277 |
|
|
try {
|
278 |
|
|
response.sendRedirect("/xmltodb/lib/index.html");
|
279 |
|
|
} catch ( java.io.IOException ioe) {
|
280 |
|
|
sess.disconnect();
|
281 |
|
|
out.println("MetaCatServlet.handleLoginAction() - " +
|
282 |
|
|
"Error on redirect of HttpServletResponse: " +
|
283 |
|
|
ioe.getMessage());
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
} else {
|
287 |
|
|
sess.disconnect();
|
288 |
|
|
out.println("SRB Connection failed. " +
|
289 |
|
|
"SRB RMI Server is not running now or " +
|
290 |
|
|
"user " + un +
|
291 |
|
|
" has not been authenticated to use the system");
|
292 |
|
|
}
|
293 |
|
|
} catch ( java.rmi.RemoteException re) {
|
294 |
|
|
sess.disconnect();
|
295 |
|
|
out.println("SRB Connection failed. " + re.getMessage());
|
296 |
|
|
}
|
297 |
|
|
}
|
298 |
|
|
/**
|
299 |
50
|
jones
|
* Handle the database query request and return a result set, possibly
|
300 |
|
|
* transformed from XML into HTML
|
301 |
|
|
*/
|
302 |
49
|
jones
|
private void handleQueryAction(PrintWriter out, Hashtable params,
|
303 |
|
|
HttpServletResponse response) {
|
304 |
181
|
jones
|
String action = ((String[])params.get("action"))[0];
|
305 |
|
|
String query = ((String[])params.get("query"))[0];
|
306 |
167
|
jones
|
Hashtable doclist = null;
|
307 |
181
|
jones
|
String[] doctypeArr = null;
|
308 |
154
|
jones
|
String doctype = null;
|
309 |
181
|
jones
|
Reader xmlquery = null;
|
310 |
|
|
|
311 |
|
|
// Run the query if it is a structured query
|
312 |
|
|
// or, if it is a free-text, simple query,
|
313 |
|
|
// format it first as a structured query and then run it
|
314 |
|
|
if (action.equals("query")) {
|
315 |
|
|
doctypeArr = (String[])params.get("doctype");
|
316 |
|
|
doctype = null;
|
317 |
|
|
if (doctypeArr != null) {
|
318 |
|
|
doctype = ((String[])params.get("doctype"))[0];
|
319 |
|
|
}
|
320 |
|
|
|
321 |
154
|
jones
|
if (doctype != null) {
|
322 |
181
|
jones
|
xmlquery = new StringReader(DBQuery.createQuery(query,doctype));
|
323 |
154
|
jones
|
} else {
|
324 |
181
|
jones
|
xmlquery = new StringReader(DBQuery.createQuery(query));
|
325 |
154
|
jones
|
}
|
326 |
181
|
jones
|
} else if (action.equals("squery")) {
|
327 |
|
|
xmlquery = new StringReader(query);
|
328 |
82
|
jones
|
} else {
|
329 |
181
|
jones
|
System.err.println("Error handling query -- illegal action value");
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
if (queryobj != null) {
|
333 |
|
|
doclist = queryobj.findDocuments(xmlquery);
|
334 |
|
|
} else {
|
335 |
82
|
jones
|
out.println("Query Object Init failed.");
|
336 |
83
|
jones
|
return;
|
337 |
82
|
jones
|
}
|
338 |
50
|
jones
|
|
339 |
|
|
// Create a buffer to hold the xml result
|
340 |
|
|
StringBuffer resultset = new StringBuffer();
|
341 |
|
|
|
342 |
49
|
jones
|
// Print the resulting root nodes
|
343 |
167
|
jones
|
String docid;
|
344 |
98
|
jones
|
String document = null;
|
345 |
50
|
jones
|
resultset.append("<?xml version=\"1.0\"?>\n");
|
346 |
87
|
jones
|
//resultset.append("<!DOCTYPE resultset PUBLIC " +
|
347 |
|
|
// "\"-//NCEAS//resultset//EN\" \"resultset.dtd\">\n");
|
348 |
50
|
jones
|
resultset.append("<resultset>\n");
|
349 |
|
|
resultset.append(" <query>" + query + "</query>");
|
350 |
167
|
jones
|
Enumeration doclistkeys = doclist.keys();
|
351 |
|
|
while (doclistkeys.hasMoreElements()) {
|
352 |
|
|
docid = (String)doclistkeys.nextElement();
|
353 |
|
|
document = (String)doclist.get(docid);
|
354 |
98
|
jones
|
resultset.append(" <document>" + document + "</document>");
|
355 |
49
|
jones
|
}
|
356 |
50
|
jones
|
resultset.append("</resultset>");
|
357 |
|
|
|
358 |
|
|
String qformat = ((String[])params.get("qformat"))[0];
|
359 |
|
|
if (qformat.equals("xml")) {
|
360 |
|
|
// set content type and other response header fields first
|
361 |
|
|
response.setContentType("text/xml");
|
362 |
|
|
out.println(resultset.toString());
|
363 |
|
|
} else if (qformat.equals("html")) {
|
364 |
|
|
// set content type and other response header fields first
|
365 |
|
|
response.setContentType("text/html");
|
366 |
|
|
//out.println("Converting to HTML...");
|
367 |
|
|
XMLDocumentFragment htmldoc = null;
|
368 |
|
|
try {
|
369 |
185
|
jones
|
XSLStylesheet style = new XSLStylesheet(
|
370 |
|
|
new URL(resultStyleURL), null);
|
371 |
50
|
jones
|
htmldoc = (new XSLProcessor()).processXSL(style,
|
372 |
|
|
(Reader)(new StringReader(resultset.toString())),null);
|
373 |
|
|
htmldoc.print(out);
|
374 |
|
|
} catch (Exception e) {
|
375 |
|
|
out.println("Error transforming document:\n" + e.getMessage());
|
376 |
|
|
}
|
377 |
|
|
}
|
378 |
49
|
jones
|
}
|
379 |
|
|
|
380 |
50
|
jones
|
/**
|
381 |
|
|
* Handle the database getdocument request and return a XML document,
|
382 |
|
|
* possibly transformed from XML into HTML
|
383 |
|
|
*/
|
384 |
49
|
jones
|
private void handleGetDocumentAction(PrintWriter out, Hashtable params,
|
385 |
87
|
jones
|
HttpServletResponse response)
|
386 |
|
|
throws ClassNotFoundException, IOException, SQLException {
|
387 |
102
|
jones
|
String docidstr = null;
|
388 |
162
|
bojilova
|
String docid = null;
|
389 |
102
|
jones
|
String doc = null;
|
390 |
|
|
try {
|
391 |
87
|
jones
|
// Find the document id number
|
392 |
102
|
jones
|
docidstr = ((String[])params.get("docid"))[0];
|
393 |
162
|
bojilova
|
//docid = (new Long(docidstr)).longValue();
|
394 |
|
|
docid = docidstr;
|
395 |
49
|
jones
|
|
396 |
87
|
jones
|
// Get the document indicated fromthe db
|
397 |
102
|
jones
|
doc = docreader.readXMLDocument(docid);
|
398 |
|
|
} catch (NullPointerException npe) {
|
399 |
|
|
response.setContentType("text/html");
|
400 |
|
|
out.println("Error getting document ID: " + docidstr +" (" + docid + ")");
|
401 |
|
|
}
|
402 |
85
|
jones
|
|
403 |
87
|
jones
|
// Return the document in XML or HTML format
|
404 |
85
|
jones
|
String qformat = ((String[])params.get("qformat"))[0];
|
405 |
|
|
if (qformat.equals("xml")) {
|
406 |
|
|
// set content type and other response header fields first
|
407 |
|
|
response.setContentType("text/xml");
|
408 |
|
|
out.println(doc);
|
409 |
|
|
} else if (qformat.equals("html")) {
|
410 |
|
|
// set content type and other response header fields first
|
411 |
|
|
response.setContentType("text/html");
|
412 |
|
|
|
413 |
87
|
jones
|
// Look up the document type
|
414 |
103
|
jones
|
String sourcetype = docreader.getDoctypeInfo(docid).getDoctype();
|
415 |
86
|
jones
|
|
416 |
87
|
jones
|
// Transform the document to the new doctype
|
417 |
|
|
dbt.transformXMLDocument(doc, sourcetype, "-//W3C//HTML//EN", out);
|
418 |
85
|
jones
|
}
|
419 |
49
|
jones
|
}
|
420 |
55
|
jones
|
|
421 |
|
|
/**
|
422 |
|
|
* Handle the database putdocument request and write an XML document
|
423 |
|
|
* to the database connection
|
424 |
|
|
*/
|
425 |
203
|
jones
|
private void handleInsertOrUpdateAction(PrintWriter out, Hashtable params,
|
426 |
55
|
jones
|
HttpServletResponse response) {
|
427 |
59
|
jones
|
|
428 |
204
|
jones
|
try {
|
429 |
|
|
// Get the document indicated
|
430 |
|
|
String[] doctext = (String[])params.get("doctext");
|
431 |
|
|
StringReader xml = null;
|
432 |
|
|
try {
|
433 |
|
|
xml = new StringReader(doctext[0]);
|
434 |
59
|
jones
|
|
435 |
204
|
jones
|
String[] action = (String[])params.get("action");
|
436 |
|
|
String[] docid = (String[])params.get("docid");
|
437 |
|
|
String newdocid = null;
|
438 |
203
|
jones
|
|
439 |
204
|
jones
|
String doAction = null;
|
440 |
|
|
if (action[0].equals("insert")) {
|
441 |
|
|
doAction = "INSERT";
|
442 |
|
|
} else if (action[0].equals("update")) {
|
443 |
|
|
doAction = "UPDATE";
|
444 |
|
|
}
|
445 |
203
|
jones
|
|
446 |
204
|
jones
|
// write the document to the database
|
447 |
|
|
DBWriter dbw = new DBWriter(conn, saxparser);
|
448 |
|
|
|
449 |
|
|
try {
|
450 |
|
|
String accNumber = docid[0];
|
451 |
|
|
if (accNumber.equals("")) {
|
452 |
|
|
accNumber = null;
|
453 |
|
|
}
|
454 |
|
|
newdocid = dbw.write(xml, doAction, accNumber);
|
455 |
|
|
} catch (NullPointerException npe) {
|
456 |
|
|
newdocid = dbw.write(xml, doAction, null);
|
457 |
203
|
jones
|
}
|
458 |
204
|
jones
|
|
459 |
|
|
// set content type and other response header fields first
|
460 |
|
|
response.setContentType("text/xml");
|
461 |
|
|
out.println("<?xml version=\"1.0\"?>");
|
462 |
|
|
out.println("<success>");
|
463 |
|
|
out.println("<docid>" + newdocid + "</docid>");
|
464 |
|
|
out.println("</success>");
|
465 |
|
|
|
466 |
203
|
jones
|
} catch (NullPointerException npe) {
|
467 |
204
|
jones
|
response.setContentType("text/xml");
|
468 |
|
|
out.println("<?xml version=\"1.0\"?>");
|
469 |
|
|
out.println("<error>");
|
470 |
|
|
out.println(npe.getMessage());
|
471 |
|
|
out.println("</error>");
|
472 |
55
|
jones
|
}
|
473 |
204
|
jones
|
} catch (Exception e) {
|
474 |
|
|
response.setContentType("text/xml");
|
475 |
|
|
out.println("<?xml version=\"1.0\"?>");
|
476 |
|
|
out.println("<error>");
|
477 |
|
|
out.println(e.getMessage());
|
478 |
|
|
if (e instanceof SAXException) {
|
479 |
|
|
Exception e2 = ((SAXException)e).getException();
|
480 |
|
|
out.println("<error>");
|
481 |
|
|
out.println(e2.getMessage());
|
482 |
|
|
out.println("</error>");
|
483 |
|
|
}
|
484 |
|
|
//e.printStackTrace(out);
|
485 |
|
|
out.println("</error>");
|
486 |
203
|
jones
|
}
|
487 |
55
|
jones
|
}
|
488 |
203
|
jones
|
|
489 |
|
|
/**
|
490 |
|
|
* Handle the database delete request and delete an XML document
|
491 |
|
|
* from the database connection
|
492 |
|
|
*/
|
493 |
|
|
private void handleDeleteAction(PrintWriter out, Hashtable params,
|
494 |
|
|
HttpServletResponse response) {
|
495 |
|
|
|
496 |
|
|
String[] docid = (String[])params.get("docid");
|
497 |
|
|
|
498 |
|
|
// delete the document from the database
|
499 |
|
|
try {
|
500 |
|
|
DBWriter dbw = new DBWriter(conn, saxparser);
|
501 |
|
|
// NOTE -- NEED TO TEST HERE
|
502 |
|
|
// FOR EXISTENCE OF PARAM
|
503 |
|
|
// BEFORE ACCESSING ARRAY
|
504 |
|
|
try {
|
505 |
|
|
dbw.delete(docid[0]);
|
506 |
204
|
jones
|
response.setContentType("text/xml");
|
507 |
|
|
out.println("<?xml version=\"1.0\"?>");
|
508 |
|
|
out.println("<success>");
|
509 |
203
|
jones
|
out.println("Document deleted.");
|
510 |
204
|
jones
|
out.println("</success>");
|
511 |
203
|
jones
|
} catch (AccessionNumberException ane) {
|
512 |
204
|
jones
|
response.setContentType("text/xml");
|
513 |
|
|
out.println("<?xml version=\"1.0\"?>");
|
514 |
|
|
out.println("<error>");
|
515 |
|
|
out.println("Error deleting document!!!");
|
516 |
203
|
jones
|
out.println(ane.getMessage());
|
517 |
204
|
jones
|
out.println("</error>");
|
518 |
203
|
jones
|
}
|
519 |
204
|
jones
|
} catch (Exception e) {
|
520 |
|
|
response.setContentType("text/xml");
|
521 |
|
|
out.println("<?xml version=\"1.0\"?>");
|
522 |
|
|
out.println("<error>");
|
523 |
|
|
out.println(e.getMessage());
|
524 |
|
|
out.println("</error>");
|
525 |
203
|
jones
|
}
|
526 |
|
|
}
|
527 |
68
|
higgins
|
|
528 |
|
|
/**
|
529 |
185
|
jones
|
* Handle the validtion request and return the results to the requestor
|
530 |
68
|
higgins
|
*/
|
531 |
185
|
jones
|
private void handleValidateAction(PrintWriter out, Hashtable params,
|
532 |
|
|
HttpServletResponse response) {
|
533 |
68
|
higgins
|
|
534 |
103
|
jones
|
// Get the document indicated
|
535 |
|
|
String valtext = null;
|
536 |
|
|
try {
|
537 |
|
|
valtext = ((String[])params.get("valtext"))[0];
|
538 |
|
|
} catch (Exception nullpe) {
|
539 |
68
|
higgins
|
|
540 |
162
|
bojilova
|
String docid = null;
|
541 |
103
|
jones
|
try {
|
542 |
|
|
// Find the document id number
|
543 |
185
|
jones
|
docid = ((String[])params.get("docid"))[0];
|
544 |
103
|
jones
|
|
545 |
|
|
// Get the document indicated fromthe db
|
546 |
|
|
valtext = docreader.readXMLDocument(docid);
|
547 |
185
|
jones
|
|
548 |
103
|
jones
|
} catch (NullPointerException npe) {
|
549 |
|
|
response.setContentType("text/html");
|
550 |
185
|
jones
|
out.println("Error getting document ID: " + docid );
|
551 |
103
|
jones
|
}
|
552 |
|
|
}
|
553 |
68
|
higgins
|
|
554 |
103
|
jones
|
try {
|
555 |
185
|
jones
|
DBValidate valobj = new DBValidate(saxparser,xmlcatalogfile);
|
556 |
|
|
boolean valid = valobj.validateString(valtext);
|
557 |
68
|
higgins
|
|
558 |
|
|
// set content type and other response header fields first
|
559 |
105
|
jones
|
response.setContentType("text/html");
|
560 |
|
|
out.println("<html>");
|
561 |
|
|
out.println("<head><link rel=\"stylesheet\" type=\"text/css\" " +
|
562 |
|
|
"href=\"/xmltodb/rowcol.css\" /></head>");
|
563 |
|
|
out.println("<body class=\"emlbody\">");
|
564 |
68
|
higgins
|
|
565 |
|
|
if (valid) {
|
566 |
|
|
out.println("The input XML is VALID!");
|
567 |
103
|
jones
|
} else {
|
568 |
105
|
jones
|
out.println("The input XML is NOT VALID<br />\n<pre>\n"
|
569 |
185
|
jones
|
+ valobj.returnErrors() + "\n</pre>\n");
|
570 |
68
|
higgins
|
}
|
571 |
105
|
jones
|
out.println("</body></html>");
|
572 |
103
|
jones
|
} catch (NullPointerException npe2) {
|
573 |
|
|
// set content type and other response header fields first
|
574 |
|
|
response.setContentType("text/html");
|
575 |
|
|
out.println("Error validating document.");
|
576 |
68
|
higgins
|
}
|
577 |
103
|
jones
|
}
|
578 |
87
|
jones
|
|
579 |
|
|
/**
|
580 |
91
|
higgins
|
* Handle the document request and return the results
|
581 |
154
|
jones
|
* to the requestor
|
582 |
91
|
higgins
|
*/
|
583 |
185
|
jones
|
private void handleGetDataDocumentAction(PrintWriter out, Hashtable params,
|
584 |
|
|
HttpServletResponse response) {
|
585 |
91
|
higgins
|
boolean error_flag = false;
|
586 |
|
|
String error_message = "";
|
587 |
|
|
// Get the document indicated
|
588 |
|
|
String[] datadoc = (String[])params.get("datadoc");
|
589 |
185
|
jones
|
// defaultdatapath = "C:\\Temp\\"; // for testing only!!!
|
590 |
|
|
// executescript = "test.bat"; // for testing only!!!
|
591 |
91
|
higgins
|
|
592 |
|
|
// set content type and other response header fields first
|
593 |
|
|
response.setContentType("application/octet-stream");
|
594 |
185
|
jones
|
if (defaultdatapath!=null) {
|
595 |
|
|
if(!defaultdatapath.endsWith(System.getProperty("file.separator"))) {
|
596 |
|
|
defaultdatapath=defaultdatapath+System.getProperty("file.separator");
|
597 |
91
|
higgins
|
}
|
598 |
185
|
jones
|
System.out.println("Path= "+defaultdatapath+datadoc[0]);
|
599 |
|
|
if (executescript!=null) {
|
600 |
|
|
String command = null;
|
601 |
|
|
File scriptfile = new File(executescript);
|
602 |
|
|
if (scriptfile.exists()) {
|
603 |
|
|
command=executescript+" "+datadoc[0]; // script includes path
|
604 |
|
|
} else { // look in defaultdatapath
|
605 |
|
|
// on Win98 one MUST include the .bat extender
|
606 |
|
|
command = defaultdatapath+executescript+" "+datadoc[0];
|
607 |
|
|
}
|
608 |
91
|
higgins
|
System.out.println(command);
|
609 |
|
|
try {
|
610 |
|
|
Process proc = Runtime.getRuntime().exec(command);
|
611 |
|
|
proc.waitFor();
|
612 |
|
|
}
|
613 |
|
|
catch (Exception eee) {
|
614 |
|
|
System.out.println("Error running process!");
|
615 |
|
|
error_flag = true;
|
616 |
|
|
error_message = "Error running process!";}
|
617 |
|
|
} // end executescript not null if
|
618 |
|
|
File datafile = new File(defaultdatapath+datadoc[0]);
|
619 |
|
|
try {
|
620 |
|
|
FileInputStream fw = new FileInputStream(datafile);
|
621 |
|
|
int x;
|
622 |
185
|
jones
|
while ((x = fw.read())!=-1) {
|
623 |
91
|
higgins
|
out.write(x); }
|
624 |
185
|
jones
|
fw.close();
|
625 |
|
|
} catch (Exception e) {
|
626 |
91
|
higgins
|
System.out.println("Error in returning file\n"+e.getMessage());
|
627 |
|
|
error_flag=true;
|
628 |
185
|
jones
|
error_message = error_message+"\nError in returning file\n"+
|
629 |
|
|
e.getMessage();
|
630 |
|
|
}
|
631 |
|
|
} // end defaultdatapath not null if
|
632 |
91
|
higgins
|
}
|
633 |
46
|
jones
|
}
|
634 |
203
|
jones
|
|
635 |
|
|
/**
|
636 |
|
|
* '$Log$
|
637 |
210
|
bojilova
|
* 'Revision 1.33 2000/06/27 04:50:33 jones
|
638 |
|
|
* 'Updated javadoc documentation.
|
639 |
|
|
* '
|
640 |
205
|
jones
|
* 'Revision 1.32 2000/06/27 04:31:07 jones
|
641 |
|
|
* 'Fixed bugs associated with the new UPDATE and DELETE functions of
|
642 |
|
|
* 'DBWriter. There were problematic interactions between some static
|
643 |
|
|
* 'variables used in DBEntityResolver and the way in which the
|
644 |
|
|
* 'Servlet objects are re-used across multiple client invocations.
|
645 |
|
|
* '
|
646 |
|
|
* 'Generally cleaned up error reporting. Now all errors and success
|
647 |
|
|
* 'results are reported as XML documents from MetaCatServlet. Need
|
648 |
|
|
* 'to make the command line tools do the same.
|
649 |
|
|
* '
|
650 |
204
|
jones
|
* 'Revision 1.31 2000/06/26 10:35:05 jones
|
651 |
|
|
* 'Merged in substantial changes to DBWriter and associated classes and to
|
652 |
|
|
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
|
653 |
|
|
* 'functions. The command line tools and the parameters for the
|
654 |
|
|
* 'servlet have changed substantially.
|
655 |
|
|
* '
|
656 |
203
|
jones
|
* 'Revision 1.30.2.6 2000/06/26 10:18:06 jones
|
657 |
|
|
* 'Partial fix for MetaCatServlet INSERT?UPDATE bug. Only will work on
|
658 |
|
|
* 'the first call to the servlet. Subsequent calls fail. Seems to be
|
659 |
|
|
* 'related to exception handling. Multiple successive DELETE actions
|
660 |
|
|
* 'work fine.
|
661 |
|
|
* '
|
662 |
|
|
* 'Revision 1.30.2.5 2000/06/26 09:09:53 jones
|
663 |
|
|
* 'Modified MetaCatServlet and associated files to handle the UPDATE
|
664 |
|
|
* 'and DELETE actions for DBWriter.
|
665 |
|
|
* '
|
666 |
|
|
* 'Revision 1.30.2.4 2000/06/26 00:51:06 jones
|
667 |
|
|
* 'If docid passed to DBWriter.write() is not unique, classes now generate
|
668 |
|
|
* 'an AccessionNumberException containing the new docid generated as a
|
669 |
|
|
* 'replacement. The docid is then extracted from the exception and
|
670 |
|
|
* 'returned to the calling application for user feedback or client processing.
|
671 |
|
|
* '
|
672 |
|
|
* 'Revision 1.30.2.3 2000/06/25 23:38:17 jones
|
673 |
|
|
* 'Added RCSfile keyword
|
674 |
|
|
* '
|
675 |
|
|
* 'Revision 1.30.2.2 2000/06/25 23:34:18 jones
|
676 |
|
|
* 'Changed documentation formatting, added log entries at bottom of source files
|
677 |
|
|
* ''
|
678 |
|
|
*/
|