kevinross 2003/07/14 09:30:13
Modified: java/src/org/apache/xindice/server XindiceServlet.java Added: java/src/org/apache/xindice/server UglyBrowser.java Log: cleanup - Separated UglyBrowser from the server bootstrapping code. Revision Changes Path 1.15 +55 -229 xml-xindice/java/src/org/apache/xindice/server/XindiceServlet.java Index: XindiceServlet.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/server/XindiceServlet.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- XindiceServlet.java 18 Jun 2003 23:03:16 -0000 1.14 +++ XindiceServlet.java 14 Jul 2003 16:30:12 -0000 1.15 @@ -63,7 +63,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.StringTokenizer; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; @@ -74,13 +73,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.xindice.core.Collection; -import org.apache.xindice.core.DBException; import org.apache.xindice.core.Database; import org.apache.xindice.server.rpc.RPCMessageInterface; import org.apache.xindice.util.Configuration; import org.apache.xindice.util.XindiceException; -import org.apache.xindice.xml.TextWriter; import org.apache.xindice.xml.dom.DOMParser; import org.apache.xmlrpc.XmlRpc; import org.apache.xmlrpc.XmlRpcServer; @@ -98,12 +94,60 @@ private static final String DEFAULT_XMLRPC_DRIVER = "xerces"; protected static Log log = LogFactory.getLog("org.apache.xindice.servlet"); - + protected Database database; protected XmlRpcServer xmlrpc; - protected Database db; private String xmlRpcDriver; + public void destroy() { + try { + // When the servlet engine goes down we need to close the + // database instance. + // TODO: make sure we're handling threads properly in this case. + if (database != null) { + database.close(); + } + if (log.isInfoEnabled()) { + log.info("Database successfully closed"); + } + } + catch (Exception e) { + log.error("Error in destroy", e); + } + } + + /** + * Delegate requests with method = GET to the UglyBrowser. + */ + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + UglyBrowser.doGet(request, response, this.database); + } + + /** + * Sends an XML query to the server and writes the output back. Currenlty + * only XML-RPC query is supported. + */ + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + byte[] result = xmlrpc.execute(request.getInputStream()); + response.setContentType("text/xml"); + response.setContentLength(result.length); + OutputStream output = response.getOutputStream(); + output.write(result); + output.flush(); + } + + /** + * Return the XML-RPC driver, a classname or shorthand string informing + * the XmlRpc package which SAX parser we wish it to use. [Created for + * unit-testing purposes.] + * + * @returns String containing the classname or shorthand name of the SAX + * parser the XmlRpc package will use. + */ + public String getXmlRpcDriver() { + return xmlRpcDriver; + } + /** * TODO: verify that if is an error occured, the database will be closed * propertly @@ -112,7 +156,7 @@ /* note: there no need to call the DatabaseManager since we already * know the database we are using: the XML-RPC one */ - db = new Database(); + database = new Database(); /* holds the database configuration */ Document configurationDocument; @@ -143,7 +187,7 @@ File dbRootDir = new File(dbRoot); if (dbRootDir.isAbsolute()) { - db.setConfig(collConf); + database.setConfig(collConf); } else { // OK, no absolute path. We have to perform some checks. @@ -184,7 +228,7 @@ } - db.setConfig(collConf); + database.setConfig(collConf); } else { @@ -212,7 +256,7 @@ if (xmlRpcDriverConfiguration != null) { xmlrpcDriver = xmlRpcDriverConfiguration.getAttribute("name", DEFAULT_XMLRPC_DRIVER); if (xmlrpcDriver == null || xmlrpcDriver.equals("")) { // this will probably never happen due to the default. - + throw new ServletException( "The xml-rpc configuration (in '" + config.getInitParameter(Xindice.PROP_XINDICE_CONFIGURATION) @@ -245,23 +289,6 @@ } } - public void destroy() { - try { - // When the servlet engine goes down we need to close the - // database instance. - // TODO: make sure we're handling threads properly in this case. - if (db != null) { - db.close(); - } - if (log.isInfoEnabled()) { - log.info("Database successfully closed"); - } - } - catch (Exception e) { - log.error("Error in destroy", e); - } - } - /** * Loads the Xindice configuration file. The file is searched in the * following locations: @@ -294,207 +321,6 @@ } return result; - } - - // Get handling to provide simple HTTP access to the Xindice instance. - public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - try { - String contextPath = request.getContextPath(); - StringBuffer sb = new StringBuffer(); - String path = request.getQueryString(); - sb.append("<html><body><center>"); - if (path == null) { - sb.append("<h2>THIS IS AN UGLY DEBUG TOOL!</h2><p>"); - sb.append("To browse the database, follow the link "); - sb.append("<a href=\"" + contextPath + "?/" + db.getName() + "\">" + db.getName() + "</a>"); - } - else { - // we have something to chew on - XPathPseudoParser parser = new XPathPseudoParser(path); - - /* building the page once we have all the information */ - sb.append("<table border=\"1\" width=\"90%\">"); - sb.append("<tr><td rowspan=\"1\" colspan=\"2\">" + getPathNavigation(parser, contextPath) + "</td></tr>"); - sb.append("<tr><td valign=\"top\">" + getHierarchy(parser, contextPath) + "</td>"); - sb.append("<td valign=\"top\">" + getDetailView(parser) + "</td></tr>"); - sb.append("</table>"); - } - sb.append("</center></body></html>"); - - response.setContentType("text/html; charset=utf-8"); - byte[] resultBytes = sb.toString().getBytes("utf-8"); - response.setContentLength(resultBytes.length); - OutputStream output = response.getOutputStream(); - output.write(resultBytes); - output.flush(); - } - catch (Exception e) { - throw new ServletException("argh!", e); - //response.setStatus(HttpServletResponse.SC_NOT_FOUND); - // TODO: need intelligent error handling. - // throw new ServletException("Error while doGet method", e); - } - } - - protected String getPathNavigation(XPathPseudoParser parser, String contextPath) { - String path = parser.getQuery(); - StringBuffer result = new StringBuffer(); - StringTokenizer st = new StringTokenizer(path, "/"); - String currentPath = "<a href=\"" + contextPath + "?"; - while (st.hasMoreTokens()) { - String token = st.nextToken(); - if (token.indexOf("&") > 0) { - token = token.substring(0, token.indexOf("&")); - } - currentPath = currentPath + "/" + token; - result.append(currentPath + "\">" + token + "</a> - "); - - } - return result.toString(); - } - - protected String getHierarchy(XPathPseudoParser parser, String contextPath) throws DBException, Exception { - StringBuffer result = new StringBuffer(); - - String path = parser.getPath(); - - Collection col; - if (!path.equals("")) { - col = db.getCollection(parser.getPath()); - } - else { - col = db; - } - - if (col == null) { - result.append("Collection not found! " + parser.getPath()); - return result.toString(); - } - - String[] cols = col.listCollections(); - for (int i = 0; i < cols.length; i++) { - result.append("<a href=\"" + contextPath + "?" + parser.getDatabase() + "/" + parser.getPath()); - result.append("/"); - result.append(cols[i]); - result.append("\">"); - result.append(cols[i]); - result.append("</a><br>"); - } - - try { - String[] docs = col.listDocuments(); - for (int i = 0; i < docs.length; i++) { - result.append("<a href=\"" + contextPath + "?" + parser.getDatabase() + "/" + parser.getPath()); - result.append("&"); - result.append(docs[i]); - result.append("\">"); - result.append(docs[i]); - result.append("</a><br>"); - } - } - catch (DBException e) { - // do nothing but this collection cannot store documents. - } - - return result.toString(); - } - - protected String getDetailView(XPathPseudoParser parser) throws Exception { - Collection col = this.db.getCollection(parser.getPath()); - - String document = parser.getDocument(); - if (document == null) { - return ""; - } - - Document doc = col.getDocument(document); - if (doc == null) { - return "Document '" + document + "' not found"; - } - - return "Document '" + document + "'<p>" + TextWriter.toString(doc); - } - - /** - * Sends an XML query to the server and writes the output back. Currenlty - * only XML-RPC query is supported. - */ - public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - byte[] result = xmlrpc.execute(request.getInputStream()); - response.setContentType("text/xml"); - response.setContentLength(result.length); - OutputStream output = response.getOutputStream(); - output.write(result); - output.flush(); - } - - /** - * Return the XML-RPC driver, a classname or shorthand string informing - * the XmlRpc package which SAX parser we wish it to use. [Created for - * unit-testing purposes.] - * - * @returns String containing the classname or shorthand name of the SAX - * parser the XmlRpc package will use. - */ - public String getXmlRpcDriver() { - return xmlRpcDriver; - } - - public static class XPathPseudoParser { - - private String query; - - public XPathPseudoParser(String query) throws Exception { - this.query = query; - } - - public String getQuery() { - return this.query; - } - - public String getDatabase() { - int max = query.indexOf("/", 1); - if (max > 1) { - return query.substring(0, max); - } - else { - return query; - } - } - - public String getPath() { - int min = query.indexOf("/", 1); - if (min <= 0) { - return ""; - } - - int max = query.indexOf("&"); - if (max > 1) { - return query.substring(min + 1, max); - } - else { - return query.substring(min + 1); - } - /* - int pos = query.indexOf("&"); - if (pos > 0) { - return query.substring(0, pos); - } else { - return query; - } - */ - } - - public String getDocument() { - int pos = query.indexOf("&"); - if (pos > 0) { - return query.substring(pos + 1); - } - else { - return null; - } - } - } } 1.1 xml-xindice/java/src/org/apache/xindice/server/UglyBrowser.java Index: UglyBrowser.java =================================================================== package org.apache.xindice.server; import java.io.IOException; import java.io.OutputStream; import java.util.StringTokenizer; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.xindice.core.Collection; import org.apache.xindice.core.DBException; import org.apache.xindice.core.Database; import org.apache.xindice.xml.TextWriter; import org.w3c.dom.Document; /** * Moved out of XindiceServlet for separation of display and server bootstrapping. * * @author kross */ public class UglyBrowser { /** * */ public UglyBrowser() { super(); } // Get handling to provide simple HTTP access to the Xindice instance. public static void doGet(HttpServletRequest request, HttpServletResponse response, Database db) throws ServletException, IOException { try { String contextPath = request.getContextPath(); StringBuffer sb = new StringBuffer(); String path = request.getQueryString(); sb.append("<html><body><center>"); if (path == null) { sb.append("<h2>THIS IS AN UGLY DEBUG TOOL!</h2><p>"); sb.append("To browse the database, follow the link "); sb.append("<a href=\"" + contextPath + "?/" + db.getName() + "\">" + db.getName() + "</a>"); } else { // we have something to chew on XPathPseudoParser parser = new XPathPseudoParser(path); /* building the page once we have all the information */ sb.append("<table border=\"1\" width=\"90%\">"); sb.append("<tr><td rowspan=\"1\" colspan=\"2\">" + getPathNavigation(parser, contextPath) + "</td></tr>"); sb.append("<tr><td valign=\"top\">" + getHierarchy(parser, contextPath, db) + "</td>"); sb.append("<td valign=\"top\">" + getDetailView(parser, db) + "</td></tr>"); sb.append("</table>"); } sb.append("</center></body></html>"); response.setContentType("text/html; charset=utf-8"); byte[] resultBytes = sb.toString().getBytes("utf-8"); response.setContentLength(resultBytes.length); OutputStream output = response.getOutputStream(); output.write(resultBytes); output.flush(); } catch (Exception e) { throw new ServletException("argh!", e); //response.setStatus(HttpServletResponse.SC_NOT_FOUND); // TODO: need intelligent error handling. // throw new ServletException("Error while doGet method", e); } } protected static String getPathNavigation(XPathPseudoParser parser, String contextPath) { String path = parser.getQuery(); StringBuffer result = new StringBuffer(); StringTokenizer st = new StringTokenizer(path, "/"); String currentPath = "<a href=\"" + contextPath + "?"; while (st.hasMoreTokens()) { String token = st.nextToken(); if (token.indexOf("&") > 0) { token = token.substring(0, token.indexOf("&")); } currentPath = currentPath + "/" + token; result.append(currentPath + "\">" + token + "</a> - "); } return result.toString(); } protected static String getHierarchy(XPathPseudoParser parser, String contextPath, Database db) throws DBException, Exception { StringBuffer result = new StringBuffer(); String path = parser.getPath(); Collection col; if (!path.equals("")) { col = db.getCollection(parser.getPath()); } else { col = db; } if (col == null) { result.append("Collection not found! " + parser.getPath()); return result.toString(); } String[] cols = col.listCollections(); for (int i = 0; i < cols.length; i++) { result.append("<a href=\"" + contextPath + "?" + parser.getDatabase() + "/" + parser.getPath()); result.append("/"); result.append(cols[i]); result.append("\">"); result.append(cols[i]); result.append("</a><br>"); } try { String[] docs = col.listDocuments(); for (int i = 0; i < docs.length; i++) { result.append("<a href=\"" + contextPath + "?" + parser.getDatabase() + "/" + parser.getPath()); result.append("&"); result.append(docs[i]); result.append("\">"); result.append(docs[i]); result.append("</a><br>"); } } catch (DBException e) { // do nothing but this collection cannot store documents. } return result.toString(); } protected static String getDetailView(XPathPseudoParser parser, Database db) throws Exception { Collection col = db.getCollection(parser.getPath()); String document = parser.getDocument(); if (document == null) { return ""; } Document doc = col.getDocument(document); if (doc == null) { return "Document '" + document + "' not found"; } return "Document '" + document + "'<p>" + TextWriter.toString(doc); } public static class XPathPseudoParser { private String query; public XPathPseudoParser(String query) throws Exception { this.query = query; } public String getQuery() { return this.query; } public String getDatabase() { int max = query.indexOf("/", 1); if (max > 1) { return query.substring(0, max); } else { return query; } } public String getPath() { int min = query.indexOf("/", 1); if (min <= 0) { return ""; } int max = query.indexOf("&"); if (max > 1) { return query.substring(min + 1, max); } else { return query.substring(min + 1); } /* int pos = query.indexOf("&"); if (pos > 0) { return query.substring(0, pos); } else { return query; } */ } public String getDocument() { int pos = query.indexOf("&"); if (pos > 0) { return query.substring(pos + 1); } else { return null; } } } }