1 12 13 package org.xquark.servlet.bridge; 14 15 import java.io.*; 16 import java.net.MalformedURLException ; 17 import java.util.HashMap ; 18 19 import javax.servlet.ServletConfig ; 20 import javax.servlet.ServletException ; 21 import javax.servlet.http.HttpServlet ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.xml.parsers.SAXParserFactory ; 25 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.helpers.AttributesImpl ; 28 import org.xquark.bridge.Mapper; 29 import org.xquark.bridge.Mapping; 30 import org.xquark.bridge.XQBridge; 31 import org.xquark.serialize.XMLSerializer; 32 import org.xquark.servlet.util.FileExtensionFilter; 33 import org.xquark.servlet.util.PostDataParser; 34 import org.xquark.xml.xdbc.*; 35 36 public class XQBridgeServlet extends HttpServlet { 37 38 private static final String JNDI_SOURCE = "java:comp/env/jdbc/TestDB"; 39 40 private XQBridge bridge; 41 42 private SAXParserFactory factory; 43 44 private HashMap mappings; 45 46 public void init(ServletConfig config) throws ServletException { 47 super.init(config); 48 factory = SAXParserFactory.newInstance(); 49 factory.setNamespaceAware(true); 50 bridge = new XQBridge("jndi:" + JNDI_SOURCE); 51 mappings = loadMappingsFromFiles(config.getServletContext().getRealPath("/WEB-INF/mappings")); 52 } 53 54 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 55 PrintWriter out = response.getWriter(); 56 XMLConnection conn = null; 57 try { 58 String query = request.getParameter("exec"); 59 if (query != null) { 60 conn = bridge.getXMLConnection(); 61 execQuery(conn, query, out); 62 } else { 63 reportError(response, "Unrecognized command: <B>" + request.getContextPath() + request.getServletPath() + request.getQueryString() + "</B>.", "query.jsp"); 64 return; 65 } 66 } catch (XMLDBCException e) { 67 reportException(response, e, "query.jsp"); 68 } finally { 69 out.close(); 70 if (conn != null) 71 try { 72 conn.close(); 73 } catch (XMLDBCException e) { 74 } 76 } 77 } 78 79 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 80 PrintWriter out = response.getWriter(); 81 String mapping = null; 82 String file = null; 83 if (request.getContentType().startsWith("multipart/form-data;")) { 84 PostDataParser poster = new PostDataParser(request); 85 mapping = poster.getParameter("mapping"); 86 file = poster.getFileContent("file"); 87 } else if (request.getContentType().equals("application/x-www-form-urlencoded")) { 88 mapping = request.getParameter("mapping"); 89 file = request.getParameter("file"); 90 } else { 91 reportError(response, "Unknown content type: " + request.getContentType(), "insert.jsp"); 92 return; 93 } 94 if (mapping == null || file == null) { 95 reportError(response, "Missing parameters for file insertion", "insert.jsp"); 96 } else { 97 Mapping map = null; 98 Mapper mapper = null; 99 try { 100 String mapFile = (String ) mappings.get(mapping); 101 map = bridge.getMapping(mapFile); 102 mapper = map.getMapper(); 103 mapper.setDocumentBase("file:/c:/"); 104 mapper.insertDocument(file); 105 reportMessage(response, "Document inserted successfully.", "insert.jsp"); 106 } catch (SAXException ex) { 107 if (ex.getException() != null) 108 reportException(response, ex.getException(), "insert.jsp"); 109 else 110 reportException(response, ex, "insert.jsp"); 111 } catch (Exception ex) { 112 reportException(response, ex, "insert.jsp"); 113 } finally { 114 try { 115 if (mapper != null) 116 mapper.close(); 117 } catch (XMLDBCException e) { 118 } 119 try { 120 if (map != null) 121 map.close(); 122 } catch (XMLDBCException e) { 123 } 124 } 125 } 126 } 127 128 private HashMap loadMappingsFromFiles(String realPath) { 129 HashMap result = new HashMap (); 130 FileFilter mapFilter = new FileExtensionFilter(".map"); 131 File mappingsDir = new File(realPath); 132 File[] mapDirs = mappingsDir.listFiles(); 133 for (int i = 0; i < mapDirs.length; i++) { 134 File mapDir = mapDirs[i]; 135 if (mapDir.isDirectory()) { 136 File[] mapFiles = mapDir.listFiles(mapFilter); 137 if (mapFiles.length > 0) { 138 String mapFile = null; 139 try { 140 mapFile = mapFiles[0].toURL().toString(); 141 Mapping mapping = bridge.getMapping(mapFile); 142 result.put(mapDir.getName(), mapFile); 143 } catch (MalformedURLException ex) { 144 log("Error while loading mapping file: " + mapFile, ex); 145 } catch (XMLDBCException ex) { 146 log("Error while loading mapping file: " + mapFile, ex); 147 } 148 } 149 } 150 } 151 return result; 152 } 153 154 private void execQuery(XMLConnection conn, String query, PrintWriter out) throws ServletException , XMLDBCException { 155 XMLStatement stmt = null; 156 XMLResultSet xrs = null; 157 try { 158 stmt = conn.createStatement(); 159 xrs = stmt.executeQuery(query); 160 XMLSerializer serializer = new XMLSerializer(out, "iso-8859-1"); 161 xrs.setContentHandler(serializer); 162 xrs.setLexicalHandler(serializer); 163 serializer.startDocument(); 164 serializer.startElement("", "results", "results", new AttributesImpl ()); 166 while (xrs.hasNext()) 167 xrs.nextAsSAX(); 168 serializer.endElement("", "results", "results"); 169 serializer.endDocument(); 170 } catch (SAXException e) { 171 throw new ServletException ("Error while producing document result", e); 172 } finally { 173 if (xrs != null) 174 xrs.close(); 175 if (stmt != null) 176 stmt.close(); 177 } 178 } 179 180 private void reportMessage(HttpServletResponse response, String message, String url) throws IOException { 181 response.sendRedirect(response.encodeRedirectURL("info.jsp?url=" + url + "&message=" + message)); 182 } 183 184 private void reportError(HttpServletResponse response, String error, String url) throws IOException { 185 response.sendRedirect(response.encodeRedirectURL("error.jsp?url=" + url + "&message=" + error)); 186 } 187 188 private void reportException(HttpServletResponse response, Exception e, String url) throws IOException { 189 192 PrintWriter out = response.getWriter(); 193 out.println("<html><body><pre>"); 194 e.printStackTrace(out); 195 out.println("</pre></body></html>"); 196 } 197 } | Popular Tags |