1
|
import java.io.PrintWriter;
|
2
|
import java.io.IOException;
|
3
|
import java.util.Enumeration;
|
4
|
import java.util.Hashtable;
|
5
|
|
6
|
import javax.servlet.ServletConfig;
|
7
|
import javax.servlet.ServletContext;
|
8
|
import javax.servlet.ServletException;
|
9
|
import javax.servlet.ServletInputStream;
|
10
|
import javax.servlet.http.HttpServlet;
|
11
|
import javax.servlet.http.HttpServletRequest;
|
12
|
import javax.servlet.http.HttpServletResponse;
|
13
|
import javax.servlet.http.HttpUtils;
|
14
|
|
15
|
/**
|
16
|
* A metadata catalog server implemented as a Java Servlet
|
17
|
*/
|
18
|
public class MetaCatServlet extends HttpServlet {
|
19
|
|
20
|
private ServletConfig config = null;
|
21
|
private ServletContext context = null;
|
22
|
DBSimpleQuery queryobj = null;
|
23
|
DBReader docreader = null;
|
24
|
static String user = "jones";
|
25
|
static String password = "your-pw-goes-here";
|
26
|
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
27
|
|
28
|
public void init( ServletConfig config ) throws ServletException {
|
29
|
try {
|
30
|
super.init( config );
|
31
|
this.config = config;
|
32
|
this.context = config.getServletContext();
|
33
|
|
34
|
try {
|
35
|
queryobj = new DBSimpleQuery(user, password, defaultDB);
|
36
|
docreader = new DBReader(user, password, defaultDB);
|
37
|
} catch (Exception e) {
|
38
|
}
|
39
|
} catch ( ServletException ex ) {
|
40
|
throw ex;
|
41
|
}
|
42
|
}
|
43
|
|
44
|
public void doGet (HttpServletRequest request, HttpServletResponse response)
|
45
|
throws ServletException, IOException {
|
46
|
|
47
|
// Get the parameters from the form
|
48
|
String querystring = request.getQueryString();
|
49
|
Hashtable params = HttpUtils.parseQueryString(querystring);
|
50
|
|
51
|
// Process the data and send back the response
|
52
|
handleGetOrPost(response, params);
|
53
|
}
|
54
|
|
55
|
public void doPost( HttpServletRequest request, HttpServletResponse response)
|
56
|
throws ServletException, IOException {
|
57
|
|
58
|
// Get the input data from the client
|
59
|
ServletInputStream in = request.getInputStream();
|
60
|
int len = request.getContentLength();
|
61
|
|
62
|
// Parse the input data into a Hashtable
|
63
|
Hashtable params = HttpUtils.parsePostData(len, in);
|
64
|
|
65
|
// Process the data and send back the response
|
66
|
handleGetOrPost(response, params);
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* routine to control servlet response depending on the action requested
|
71
|
* Valid "action" parameters are:
|
72
|
* query -- query the text values of all elements and attributes
|
73
|
* and return a result set of nodes
|
74
|
* getdocument -- display an XML document in XML or HTML given a document ID
|
75
|
*/
|
76
|
private void handleGetOrPost(HttpServletResponse response, Hashtable params)
|
77
|
throws ServletException, IOException {
|
78
|
|
79
|
// Get a handle to the output stream back to the client
|
80
|
PrintWriter out = response.getWriter();
|
81
|
|
82
|
String action = ((String[])params.get("action"))[0];
|
83
|
|
84
|
if (action.equals("query")) {
|
85
|
handleQueryAction(out, params, response);
|
86
|
} else if (action.equals("getdocument")) {
|
87
|
handleGetDocumentAction(out, params, response);
|
88
|
}
|
89
|
|
90
|
// Close the stream to the client
|
91
|
out.close();
|
92
|
}
|
93
|
|
94
|
private void handleQueryAction(PrintWriter out, Hashtable params,
|
95
|
HttpServletResponse response) {
|
96
|
// Run the query
|
97
|
String query = ((String[])params.get("query"))[0];
|
98
|
Hashtable nodelist = queryobj.findRootNodes(query);
|
99
|
|
100
|
// set content type and other response header fields first
|
101
|
response.setContentType("text/xml");
|
102
|
|
103
|
// Print the resulting root nodes
|
104
|
long nodeid;
|
105
|
out.println("<?xml version=\"1.0\"?>\n");
|
106
|
out.println("<resultset>\n");
|
107
|
out.println(" <query>" + query + "</query>");
|
108
|
Enumeration rootlist = nodelist.keys();
|
109
|
while (rootlist.hasMoreElements()) {
|
110
|
nodeid = ((Long)rootlist.nextElement()).longValue();
|
111
|
out.println(" <nodeid>" + nodeid + "</nodeid>");
|
112
|
}
|
113
|
out.println("</resultset>");
|
114
|
}
|
115
|
|
116
|
private void handleGetDocumentAction(PrintWriter out, Hashtable params,
|
117
|
HttpServletResponse response) {
|
118
|
// Get the document indicated
|
119
|
String docid = ((String[])params.get("docid"))[0];
|
120
|
String doc = docreader.readXMLDocument((new Long(docid)).longValue());
|
121
|
|
122
|
// set content type and other response header fields first
|
123
|
response.setContentType("text/xml");
|
124
|
|
125
|
out.println(doc);
|
126
|
|
127
|
// Print the resulting root nodes
|
128
|
//out.println("<?xml version=\"1.0\"?>\n");
|
129
|
//out.println("<docid>" + docid + "</docid>");
|
130
|
}
|
131
|
}
|