public void init(ServletConfig config) throws ServletException { try { String ps = config.getServletContext().getRealPath("/WEB-INF/jiql.properties"); if (!new File(ps).exists()) { return; } NameValuePairs p = new NameValuePairs(ps); theUser = (String) p.get("user"); thePassword = (String) p.get("password"); if (p.getInt("maxUpload") > 0) mU = p.getInt("maxUpload"); } catch (Exception e) { tools.util.LogMgr.err("JiqlServlet.init " + e.toString()); e.printStackTrace(System.out); } super.init(config); }
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { boolean isMultipart = FileUpload.isMultipartContent(req); Hashtable dv = new Hashtable(); String user = null; String password = null; String sql = null; String dfo = null; String enc = null; if (isMultipart) { ServletFileUpload upload = new ServletFileUpload(); upload.setSizeMax(mU); try { FileItemIterator iter = upload.getItemIterator(req); // List<FileItem> items = upload.parseRequest(req); while (iter.hasNext()) { FileItemStream item = iter.next(); // for (int ct = 0;ct < items.size();ct++){ // FileItem item = (FileItem)items.get(ct); String name = item.getName(); // (name + " jiql UREEAD 1aay " + item.isFormField() + ":" + name.equals("directValues")); InputStream stream = item.openStream(); // InputStream stream = item.getInputStream(); //// (name + " jiql UREEAD 1 " + stream.available()); // byte[] b = StreamUtil.readBytes(stream); if (name.equals("directValues")) { // (stream.available() + " jiql UREEAD " ); // ByteArrayInputStream bout = new ByteArrayInputStream(b); ObjectInputStream dout = new ObjectInputStream(stream); // ObjectInputStream dout = new ObjectInputStream(bout); dv = (Hashtable) dout.readObject(); } } } catch (Exception e) { tools.util.LogMgr.err("JS.readDV " + e.toString()); e.printStackTrace(); } // ("$$$ DV " + dv); Hashtable pars = (Hashtable) dv.get("parameters"); if (pars != null) { Enumeration en = pars.keys(); while (en.hasMoreElements()) { String n = en.nextElement().toString(); // ("PARSMS " + n); if (n.equals("query")) sql = pars.get(n).toString(); else if (n.equals("password")) password = pars.get(n).toString(); else if (n.equals("user")) user = pars.get(n).toString(); else if (n.equals("date.format")) dfo = pars.get(n).toString(); else if (n.equals("encoding")) enc = pars.get(n).toString(); } } } if (user == null) user = req.getParameter("user"); if (password == null) password = req.getParameter("password"); if (!StringUtil.isRealString(user) || !StringUtil.isRealString(password)) { resp.sendError(403, "Invalid User or Password"); return; } if (!StringUtil.isRealString(theUser) || !StringUtil.isRealString(thePassword)) { resp.sendError(403, "Invalid User OR Password"); return; } Connection Conn = null; Hashtable h = new Hashtable(); h.put("remote", "true"); try { // NameValuePairs p = new NameValuePairs(ps); if (!user.equals(theUser) || !password.equals(thePassword)) { resp.sendError(403, "Invalid User OR Invalid Password"); return; } // throw new ServletException("Invalid User OR Password"); if (sql == null) sql = req.getParameter("query"); // ( "THE SQL " + sql); NameValuePairs nvp = new NameValuePairs(); if (dfo == null) dfo = req.getParameter("date.format"); if (dfo != null) nvp.put("date.format", dfo); if (enc == null) enc = req.getParameter("encoding"); if (enc != null) nvp.put("encoding", enc); Conn = get(nvp); org.jiql.jdbc.Statement Stmt = (org.jiql.jdbc.Statement) Conn.createStatement(); Stmt.setDirectValues(dv); Stmt.execute(sql); org.jiql.jdbc.ResultSet res = (org.jiql.jdbc.ResultSet) Stmt.getResultSet(); if (res != null) { if (res.getResults() != null) h.put("results", res.getResults()); if (res.getSQLParser() != null) h.put("sqlparser", res.getSQLParser()); } else h.put("sqlparser", Stmt.getSQLParser()); // h.put("remote","true"); resp.setContentType("binary/object"); // if (enc != null) // resp.setCharacterEncoding(enc); OutputStream fos = resp.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(h); // resp.getWriter(). ("result" + h); } catch (Exception ex) { org.jiql.util.JGUtil.olog(ex); ex.printStackTrace(); tools.util.LogMgr.err("JIQLServlet " + ex.toString()); JGException je = null; if (ex instanceof JGException) je = (JGException) ex; else je = new JGException(ex.toString()); h.put("error", je); resp.setContentType("binary/object"); OutputStream fos = resp.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(h); // throw new ServletException(ex.toString()); } finally { if (Conn != null) try { Conn.close(); } catch (Exception ex) { ex.printStackTrace(); } } }