Revision 49
Added by Matt Jones over 24 years ago
MetaCatServlet.java | ||
---|---|---|
20 | 20 |
private ServletConfig config = null; |
21 | 21 |
private ServletContext context = null; |
22 | 22 |
DBSimpleQuery queryobj = null; |
23 |
DBReader docreader = null; |
|
23 | 24 |
static String user = "jones"; |
24 | 25 |
static String password = "your-pw-goes-here"; |
25 | 26 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test"; |
... | ... | |
32 | 33 |
|
33 | 34 |
try { |
34 | 35 |
queryobj = new DBSimpleQuery(user, password, defaultDB); |
36 |
docreader = new DBReader(user, password, defaultDB); |
|
35 | 37 |
} catch (Exception e) { |
36 | 38 |
} |
37 | 39 |
} catch ( ServletException ex ) { |
... | ... | |
64 | 66 |
handleGetOrPost(response, params); |
65 | 67 |
} |
66 | 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 |
*/ |
|
67 | 76 |
private void handleGetOrPost(HttpServletResponse response, Hashtable params) |
68 | 77 |
throws ServletException, IOException { |
69 | 78 |
|
70 |
// Run the query |
|
71 |
String[] query = (String[])params.get("query"); |
|
72 |
Hashtable nodelist = queryobj.findRootNodes(query[0]); |
|
73 |
|
|
74 |
// set content type and other response header fields first |
|
75 |
response.setContentType("text/xml"); |
|
76 |
|
|
77 |
// then write the data of the response |
|
79 |
// Get a handle to the output stream back to the client |
|
78 | 80 |
PrintWriter out = response.getWriter(); |
81 |
|
|
82 |
String action = ((String[])params.get("action"))[0]; |
|
79 | 83 |
|
80 |
// Print the reulting root nodes |
|
81 |
long nodeid; |
|
82 |
out.println("<?xml version=\"1.0\"?>\n"); |
|
83 |
out.println("<resultset>\n"); |
|
84 |
out.println(" <query>" + query[0] + "</query>"); |
|
85 |
Enumeration rootlist = nodelist.keys(); |
|
86 |
while (rootlist.hasMoreElements()) { |
|
87 |
nodeid = ((Long)rootlist.nextElement()).longValue(); |
|
88 |
out.println(" <nodeid>" + nodeid + "</nodeid>"); |
|
84 |
if (action.equals("query")) { |
|
85 |
handleQueryAction(out, params, response); |
|
86 |
} else if (action.equals("getdocument")) { |
|
87 |
handleGetDocumentAction(out, params, response); |
|
89 | 88 |
} |
90 |
out.println("</resultset>"); |
|
91 | 89 |
|
90 |
// Close the stream to the client |
|
92 | 91 |
out.close(); |
93 | 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 |
} |
|
94 | 131 |
} |
Also available in: Unified diff
added document display function to servlet