Example #1
0
  /**
   * Processes the request coming to the servlet and grabs the attributes set by the servlet and
   * uses them to fire off pre-determined methods set in the setupActionMethods function of the
   * servlet.
   *
   * @param request the http request coming from the browser.
   * @param response the http response going to the browser.
   * @throws javax.servlet.ServletException
   * @throws java.io.IOException
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    if (!actionInitialized) {
      LogController.write(this, "This dispatcher servlet is not initialized properly!");
      return;
    }

    if (actionTag == null) {
      LogController.write(this, "There is no action attribute tag name!");
      return;
    }

    HttpSession httpSession = request.getSession();
    UserSession userSession = (UserSession) httpSession.getAttribute("user_session");

    if (userSession == null) {
      LogController.write(this, "User session is no longer available in this http session.");

      userSession = new UserSession();

      // We always want a user session though...
      httpSession.setAttribute("user_session", userSession);
    }

    String action = (String) request.getAttribute(actionTag);

    try {
      if (action == null) {
        // There is no action attribute specified, check parameters.
        String external_action = (String) request.getParameter(actionTag);

        if (external_action != null) {
          Method method = externalActions.get(external_action);

          if (method != null) {
            LogController.write(this, "Performing external action: " + external_action);
            method.invoke(this, new Object[] {userSession, request, response});
          } else {
            if (defaultExternalMethod != null) {
              LogController.write(this, "Performing default external action.");
              defaultExternalMethod.invoke(this, new Object[] {userSession, request, response});
            } else {
              LogController.write(this, "Unable to perform default external action.");
            }
          }
        } else {
          if (defaultExternalMethod != null) {
            LogController.write(this, "Performing default external action.");
            defaultExternalMethod.invoke(this, new Object[] {userSession, request, response});
          } else {
            LogController.write(this, "Unable to perform default external action.");
          }
        }
      } else {
        Method method = internalActions.get(action);

        if (method != null) {
          LogController.write(this, "Performing internal action: " + action);
          method.invoke(this, new Object[] {userSession, request, response});
        } else {
          if (defaultInternalMethod != null) {
            LogController.write(this, "Performing default internal action.");
            defaultInternalMethod.invoke(this, new Object[] {userSession, request, response});
          } else {
            LogController.write(this, "Unable to perform default internal action.");
          }
        }

        request.removeAttribute("application_action");
      }
    } catch (IllegalAccessException accessEx) {
      LogController.write(this, "Exception while processing request: " + accessEx.getMessage());
    } catch (InvocationTargetException invokeEx) {
      LogController.write(this, "Exception while processing request: " + invokeEx.toString());
      invokeEx.printStackTrace();
    } catch (Exception ex) {
      LogController.write(this, "Unknown exception: " + ex.toString());
    }
  }
Example #2
0
  /**
   * Get Query By Form results throught response output stream.
   *
   * @param queryspec Name of XML file containing the Query Specification
   * @param columnlist List of comma separated column names to retrieve
   * @param where SQL WHERE clause to apply
   * @param order by SQL ORDER BY clause to apply
   * @param showas Output format. One of { "CSV" <i>(comma separated)</i>, "TSV" <i>(tabbed
   *     separated)</i>, "XLS" <i>(Excel)</i> }
   * @throws IOException
   * @throws FileNotFoundException
   * @throws ServletException
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, FileNotFoundException, ServletException {
    Class oDriver;
    Connection oConn = null;
    ServletOutputStream oOut = response.getOutputStream();
    QueryByForm oQBF;
    String sQuerySpec;
    String sColumnList;
    String sWhere;
    String sOrderBy;
    String sShowAs;
    String sStorage;

    if (DebugFile.trace) {
      DebugFile.writeln("Begin HttpQueryServlet.doGet(...)");
      DebugFile.incIdent();
    }

    sStorage = Environment.getProfileVar("hipergate", "storage");

    if (DebugFile.trace) DebugFile.writeln("storage=" + sStorage);

    try {
      oDriver = Class.forName(jdbcDriverClassName);
    } catch (ClassNotFoundException ignore) {
      oDriver = null;
      if (DebugFile.trace)
        DebugFile.writeln("Class.forName(" + jdbcDriverClassName + ") : " + ignore.getMessage());
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database driver not found");
    }

    if (null == oDriver) return;

    try {

      sQuerySpec = request.getParameter("queryspec");
      sColumnList = request.getParameter("columnlist");
      if (null == sColumnList) sColumnList = "*";
      sWhere = request.getParameter("where");
      if (null == sWhere) sWhere = "1=1";
      sOrderBy = request.getParameter("orderby");
      if (null == sOrderBy) sOrderBy = "";
      sShowAs = request.getParameter("showas");
      if (null == sShowAs) sShowAs = "CSV";

      if (DebugFile.trace)
        DebugFile.writeln("queryspec=" + sQuerySpec != null ? sQuerySpec : "null");
      if (DebugFile.trace) DebugFile.writeln("where=" + sWhere);
      if (DebugFile.trace) DebugFile.writeln("orderby=" + sOrderBy);

      if (hasSqlSignature(sColumnList)) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid Column List Syntax");
        return;
      }

      oQBF = new QueryByForm("file://" + sStorage + "/qbf/" + sQuerySpec + ".xml");

      if (DebugFile.trace) DebugFile.writeln("DriverManager.getConnection(" + jdbcURL + ",...)");
      oConn = DriverManager.getConnection(jdbcURL, dbUserName, dbUserPassword);

      // Send some basic http headers to support binary d/l.
      if (sShowAs.equalsIgnoreCase("XLS")) {
        response.setContentType("application/x-msexcel");
        response.setHeader(
            "Content-Disposition",
            "inline; filename=\"" + oQBF.getTitle(request.getLocale().getLanguage()) + " 1.csv\"");
      } else if (sShowAs.equalsIgnoreCase("CSV")) {
        response.setContentType("text/plain");
        response.setHeader(
            "Content-Disposition",
            "attachment; filename=\""
                + oQBF.getTitle(request.getLocale().getLanguage())
                + " 1.csv\"");
      } else if (sShowAs.equalsIgnoreCase("TSV")) {
        response.setContentType("text/tab-separated-values");
        response.setHeader(
            "Content-Disposition",
            "attachment; filename=\""
                + oQBF.getTitle(request.getLocale().getLanguage())
                + " 1.tsv\"");
      } else {
        response.setContentType("text/plain");
        response.setHeader(
            "Content-Disposition",
            "inline; filename=\"" + oQBF.getTitle(request.getLocale().getLanguage()) + " 1.txt\"");
      }

      if (0 == sOrderBy.length())
        oQBF.queryToStream(
            oConn, sColumnList, oQBF.getBaseFilter(request) + " " + sWhere, oOut, sShowAs);
      else
        oQBF.queryToStream(
            oConn,
            sColumnList,
            oQBF.getBaseFilter(request) + " " + sWhere + " ORDER BY " + sOrderBy,
            oOut,
            sShowAs);

      oConn.close();
      oConn = null;

      oOut.flush();
    } catch (SQLException e) {
      if (DebugFile.trace) DebugFile.writeln("SQLException " + e.getMessage());
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (ClassNotFoundException e) {
      if (DebugFile.trace) DebugFile.writeln("ClassNotFoundException " + e.getMessage());
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (IllegalAccessException e) {
      if (DebugFile.trace) DebugFile.writeln("IllegalAccessException " + e.getMessage());
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (Exception e) {
      if (DebugFile.trace) DebugFile.writeln("Exception " + e.getMessage());
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    } finally {
      try {
        if (null != oConn) if (!oConn.isClosed()) oConn.close();
      } catch (SQLException e) {
        if (DebugFile.trace) DebugFile.writeln("SQLException " + e.getMessage());
      }
    }

    if (DebugFile.trace) {
      DebugFile.decIdent();
      DebugFile.writeln("End HttpQueryServlet.doGet()");
    }
  } // doGet()