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 51 2000-04-17 23:06:07Z 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.util.Enumeration;
|
18
|
import java.util.Hashtable;
|
19
|
import java.net.URL;
|
20
|
import java.net.MalformedURLException;
|
21
|
import java.sql.Connection;
|
22
|
|
23
|
import javax.servlet.ServletConfig;
|
24
|
import javax.servlet.ServletContext;
|
25
|
import javax.servlet.ServletException;
|
26
|
import javax.servlet.ServletInputStream;
|
27
|
import javax.servlet.http.HttpServlet;
|
28
|
import javax.servlet.http.HttpServletRequest;
|
29
|
import javax.servlet.http.HttpServletResponse;
|
30
|
import javax.servlet.http.HttpUtils;
|
31
|
|
32
|
import oracle.xml.parser.v2.XSLStylesheet;
|
33
|
import oracle.xml.parser.v2.XSLException;
|
34
|
import oracle.xml.parser.v2.XMLDocumentFragment;
|
35
|
import oracle.xml.parser.v2.XSLProcessor;
|
36
|
|
37
|
/**
|
38
|
* A metadata catalog server implemented as a Java Servlet
|
39
|
*
|
40
|
* <p>Valid parameters are:<br>
|
41
|
* action=query -- query the values of all elements and attributes
|
42
|
* and return a result set of nodes<br>
|
43
|
* action=getdocument -- display an XML document in XML or HTML<br>
|
44
|
* qformat=xml -- display resultset from query in XML<br>
|
45
|
* qformat=html -- display resultset from query in HTML<br>
|
46
|
* action=getdocument -- display an XML document in XML or HTML<br>
|
47
|
* docid=34 -- display the document with the document ID number 34<br>
|
48
|
*/
|
49
|
public class MetaCatServlet extends HttpServlet {
|
50
|
|
51
|
private ServletConfig config = null;
|
52
|
private ServletContext context = null;
|
53
|
DBSimpleQuery queryobj = null;
|
54
|
DBReader docreader = null;
|
55
|
static String user = MetaCatUtil.user;
|
56
|
static String password = MetaCatUtil.password;
|
57
|
static String defaultDB = MetaCatUtil.defaultDB;
|
58
|
static String resultStyleURL = "file:///home/httpd/html/xmltodb/resultset.xsl";
|
59
|
|
60
|
/**
|
61
|
* Initialize the servlet by creating appropriate database connections
|
62
|
*/
|
63
|
public void init( ServletConfig config ) throws ServletException {
|
64
|
try {
|
65
|
super.init( config );
|
66
|
this.config = config;
|
67
|
this.context = config.getServletContext();
|
68
|
|
69
|
try {
|
70
|
// Open a connection to the database
|
71
|
Connection dbconn = MetaCatUtil.openDBConnection(
|
72
|
"oracle.jdbc.driver.OracleDriver",
|
73
|
defaultDB, user, password);
|
74
|
|
75
|
queryobj = new DBSimpleQuery(dbconn);
|
76
|
docreader = new DBReader(dbconn);
|
77
|
} catch (Exception e) {
|
78
|
}
|
79
|
} catch ( ServletException ex ) {
|
80
|
throw ex;
|
81
|
}
|
82
|
}
|
83
|
|
84
|
/** Handle "GET" method requests from HTTP clients */
|
85
|
public void doGet (HttpServletRequest request, HttpServletResponse response)
|
86
|
throws ServletException, IOException {
|
87
|
|
88
|
// Get the parameters from the form
|
89
|
String querystring = request.getQueryString();
|
90
|
Hashtable params = HttpUtils.parseQueryString(querystring);
|
91
|
|
92
|
// Process the data and send back the response
|
93
|
handleGetOrPost(response, params);
|
94
|
}
|
95
|
|
96
|
/** Handle "POST" method requests from HTTP clients */
|
97
|
public void doPost( HttpServletRequest request, HttpServletResponse response)
|
98
|
throws ServletException, IOException {
|
99
|
|
100
|
// Get the input data from the client
|
101
|
ServletInputStream in = request.getInputStream();
|
102
|
int len = request.getContentLength();
|
103
|
|
104
|
// Parse the input data into a Hashtable
|
105
|
Hashtable params = HttpUtils.parsePostData(len, in);
|
106
|
|
107
|
// Process the data and send back the response
|
108
|
handleGetOrPost(response, params);
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Control servlet response depending on the action parameter specified
|
113
|
*/
|
114
|
private void handleGetOrPost(HttpServletResponse response, Hashtable params)
|
115
|
throws ServletException, IOException {
|
116
|
|
117
|
// Get a handle to the output stream back to the client
|
118
|
PrintWriter out = response.getWriter();
|
119
|
|
120
|
String action = ((String[])params.get("action"))[0];
|
121
|
|
122
|
if (action.equals("query")) {
|
123
|
handleQueryAction(out, params, response);
|
124
|
} else if (action.equals("getdocument")) {
|
125
|
handleGetDocumentAction(out, params, response);
|
126
|
} else {
|
127
|
out.println("Error: action not registered. Please report this error.");
|
128
|
}
|
129
|
|
130
|
// Close the stream to the client
|
131
|
out.close();
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* Handle the database query request and return a result set, possibly
|
136
|
* transformed from XML into HTML
|
137
|
*/
|
138
|
private void handleQueryAction(PrintWriter out, Hashtable params,
|
139
|
HttpServletResponse response) {
|
140
|
// Run the query
|
141
|
String query = ((String[])params.get("query"))[0];
|
142
|
Hashtable nodelist = queryobj.findRootNodes(query);
|
143
|
|
144
|
// Create a buffer to hold the xml result
|
145
|
StringBuffer resultset = new StringBuffer();
|
146
|
|
147
|
// Print the resulting root nodes
|
148
|
long nodeid;
|
149
|
resultset.append("<?xml version=\"1.0\"?>\n");
|
150
|
resultset.append("<resultset>\n");
|
151
|
resultset.append(" <query>" + query + "</query>");
|
152
|
Enumeration rootlist = nodelist.keys();
|
153
|
while (rootlist.hasMoreElements()) {
|
154
|
nodeid = ((Long)rootlist.nextElement()).longValue();
|
155
|
resultset.append(" <nodeid>" + nodeid + "</nodeid>");
|
156
|
}
|
157
|
resultset.append("</resultset>");
|
158
|
|
159
|
String qformat = ((String[])params.get("qformat"))[0];
|
160
|
if (qformat.equals("xml")) {
|
161
|
// set content type and other response header fields first
|
162
|
response.setContentType("text/xml");
|
163
|
out.println(resultset.toString());
|
164
|
} else if (qformat.equals("html")) {
|
165
|
// set content type and other response header fields first
|
166
|
response.setContentType("text/html");
|
167
|
//out.println("Converting to HTML...");
|
168
|
XMLDocumentFragment htmldoc = null;
|
169
|
try {
|
170
|
XSLStylesheet style = new XSLStylesheet(new URL(resultStyleURL), null);
|
171
|
htmldoc = (new XSLProcessor()).processXSL(style,
|
172
|
(Reader)(new StringReader(resultset.toString())),null);
|
173
|
htmldoc.print(out);
|
174
|
} catch (Exception e) {
|
175
|
out.println("Error transforming document:\n" + e.getMessage());
|
176
|
}
|
177
|
}
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Handle the database getdocument request and return a XML document,
|
182
|
* possibly transformed from XML into HTML
|
183
|
*/
|
184
|
private void handleGetDocumentAction(PrintWriter out, Hashtable params,
|
185
|
HttpServletResponse response) {
|
186
|
// Get the document indicated
|
187
|
String docid = ((String[])params.get("docid"))[0];
|
188
|
String doc = docreader.readXMLDocument((new Long(docid)).longValue());
|
189
|
|
190
|
// set content type and other response header fields first
|
191
|
response.setContentType("text/xml");
|
192
|
|
193
|
out.println(doc);
|
194
|
}
|
195
|
}
|