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