1 22 23 package org.xquark.servlet.fusion; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.net.MalformedURLException ; 29 import java.util.HashMap ; 30 31 import javax.servlet.ServletConfig ; 32 import javax.servlet.ServletContext ; 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServlet ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.xml.parsers.SAXParserFactory ; 38 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.helpers.AttributesImpl ; 41 import org.xquark.mediator.Mediator; 42 import org.xquark.serialize.XMLSerializer; 43 import org.xquark.xml.xdbc.*; 44 45 public class XQFusionServlet extends HttpServlet { 46 47 private static final String JNDI_SOURCE = "java:comp/env/jdbc/TestDB"; 48 private Mediator mediator; 49 private SAXParserFactory factory; 50 private HashMap mappings; 51 private ServletContext context = null; 52 53 public void init(ServletConfig config) throws ServletException { 54 super.init(config); 55 context = config.getServletContext(); 56 factory = SAXParserFactory.newInstance(); 57 factory.setNamespaceAware(true); 58 File configFile = new File (config.getServletContext().getRealPath("WEB-INF/queries/main.xml")); 59 try { 60 mediator = new Mediator(configFile.toURL().toString()); 61 } catch (MalformedURLException e) { 62 throw new ServletException ("Could not create mediator", e); 63 } catch (XMLDBCException e) { 64 throw new ServletException ("Could not create mediator", e); 65 } 66 } 67 68 public void doGet(HttpServletRequest request, HttpServletResponse response) 69 throws ServletException , IOException 70 { 71 long t0 = System.currentTimeMillis(); 72 PrintWriter out = response.getWriter(); 73 XMLConnection conn = null; 74 try { 75 String query = request.getParameter("exec"); 76 if (query != null) { 77 conn = mediator.getConnection(); 78 execQuery(conn, query, out); 79 } 80 else { 81 reportError( 82 response, 83 "Unrecognized command: <B>" 84 + request.getContextPath() 85 + request.getServletPath() 86 + request.getQueryString() 87 + "</B>.", 88 "query.jsp"); 89 return; 90 } 91 } 92 catch (XMLDBCException e) { 93 reportException(response, e, "query.jsp"); 94 } 95 finally { 96 out.close(); 97 if (conn != null) 98 try { 99 conn.close(); 100 } catch (XMLDBCException e) { 101 } 103 context.log("Request processed in "+(System.currentTimeMillis()-t0)+" ms."); 104 } 105 } 106 107 private void execQuery(XMLConnection conn, String query, PrintWriter out) 108 throws ServletException , XMLDBCException { 109 PreparedXMLStatement stmt = null; 110 XMLResultSet xrs = null; 111 try { 112 stmt = conn.prepareStatement(query); 113 xrs = stmt.executeQuery(); 114 XMLSerializer serializer = new XMLSerializer(out, "iso-8859-1"); 115 xrs.setContentHandler(serializer); 116 xrs.setLexicalHandler(serializer); 117 serializer.startDocument(); 118 serializer.startElement( 120 "", 121 "results", 122 "results", 123 new AttributesImpl ()); 124 while (xrs.hasNext()) 125 xrs.nextAsSAX(); 126 serializer.endElement("", "results", "results"); 127 serializer.endDocument(); 128 } catch (SAXException e) { 129 throw new ServletException ("Error while producing document result", e); 130 } 131 finally { 132 if (xrs != null) 133 xrs.close(); 134 if (stmt != null) 135 stmt.close(); 136 } 137 } 138 139 private void reportMessage( 140 HttpServletResponse response, 141 String message, 142 String url) 143 throws IOException { 144 response.sendRedirect( 145 response.encodeRedirectURL( 146 "info.jsp?url=" + url + "&message=" + message)); 147 } 148 149 private void reportError( 150 HttpServletResponse response, 151 String error, 152 String url) 153 throws IOException { 154 response.sendRedirect( 155 response.encodeRedirectURL( 156 "error.jsp?url=" + url + "&message=" + error)); 157 } 158 159 private void reportException( 160 HttpServletResponse response, 161 Exception e, 162 String url) 163 throws IOException { 164 170 PrintWriter out = response.getWriter(); 171 out.println("<html><body><pre>"); 172 e.printStackTrace(out); 173 out.println("</pre></body></html>"); 174 } 175 } 176 | Popular Tags |