Пример #1
0
  /** Processes the HTTP request, */
  protected void processRequest(
      HttpServletRequest request, HttpServletResponse response, String request_type)
      throws ServletException, IOException {

    try {

      // Is this a dispatch?
      Object consume_status_key = request.getAttribute(AsyncServletProcessUtil.CONSUME_STATUS_KEY);

      // Make a process client,
      PlatformContext ctx = PlatformContextFactory.getPlatformContext();

      ProcessInputMessage result;
      ProcessResult result_ob;

      // Is this the continuation init?
      if (consume_status_key == null) {

        Cookie pid_cookie = null;
        // The process id cookie,
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
          for (Cookie c : cookies) {
            String cname = c.getName();
            if (cname.equals("pid")) {
              pid_cookie = c;
            }
          }
        }

        if (pid_cookie == null) {
          // Give a friendlier error message here?
          response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Session cookie not found");
          return;
        }

        // Get the cookie's value,
        String process_id_str = pid_cookie.getValue();

        AppServiceProcessClient process_client = ctx.getAppServiceProcessClient();
        ProcessId process_id = ProcessId.fromString(process_id_str);

        String ip_addr = request.getRemoteAddr();

        // Check if the process is valid,
        ProcessMessage msg_to_send = ByteArrayProcessMessage.encodeArgs("?", null, null, ip_addr);

        // Invoke the function on the process id,
        result_ob = process_client.invokeFunction(process_id, msg_to_send, true);
        request.setAttribute(getClass().getName(), result_ob);

        result = result_ob.getResult(AsyncServletProcessUtil.createNotifier(request));
        if (result == null) {
          // Set timeout to 30 seconds,
          request.getAsyncContext().setTimeout(30 * 1000);
          return;
        }

      }
      // Not initial,
      else {
        result_ob = (ProcessResult) request.getAttribute(getClass().getName());
        result = result_ob.getResult();
      }

      // This would be a timeout,
      if (result == null) {
        response.sendError(
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Timed out waiting for response");
        return;
      }
      if (result.getType() == ProcessInputMessage.Type.RETURN_EXCEPTION) {
        throw new ServletException(result.getError());
      }

      // Ok, we now have our result from the process,
      Object[] args = ByteArrayProcessMessage.decodeArgsList(result.getMessage(), 0);
      String resp = (String) args[0];
      if (!resp.equals("OK")) {
        throw new RuntimeException("Invalid response from process: " + resp);
      }

      // The user of the process,
      String username = (String) args[1];

      // Ok, everything looks good to go,

      String location;
      String html_body;

      // Context info,
      String context_path = request.getContextPath();
      if (context_path == null) context_path = "";
      if (context_path.equals("/")) context_path = "";
      String servlet_path = request.getServletPath();
      String path_info = request.getPathInfo();
      if (path_info == null) path_info = "/";

      // Create the response,

      DDBResourceAccess mckoi_access = ctx.getDDBResourceAccess();

      // Parse the link,

      StringBuilder request_link = new StringBuilder();
      request_link.append(context_path);
      request_link.append(servlet_path);
      request_link.append(path_info);

      // Sub-part,
      int delim = path_info.lastIndexOf("/");
      String path_p = path_info.substring(0, delim);
      if (path_p.startsWith("/")) {
        path_p = path_p.substring(1);
      }

      // 'path_p' now contains the path we are looking at,

      // If no path_p,
      if (path_p.equals("")) {

        String link_prepend = context_path + servlet_path + "/";

        location = "Path List";
        html_body = pathListSummary(mckoi_access, link_prepend);

      } else {

        ODBTransaction t = mckoi_access.createODBTransaction(path_p);

        // Extract the location,
        delim = request_link.lastIndexOf("/");
        location = request_link.substring(delim + 1);

        String link_prepend = request_link.substring(0, delim + 1);

        // Get the 'pos' and 'size' vars if they exist,
        String pos = request.getParameter("pos");
        String size = request.getParameter("size");
        if (pos != null) {
          location = location + "?pos=" + pos + "&size=" + size;
        }

        if (location.equals("")) {

          location = "Path: " + path_p;
          html_body = ODBPathSummary(t, path_p, link_prepend);

        } else {

          // Get the formatter,
          ODBHTMLFormatter formatter = new ODBHTMLFormatter(t, link_prepend);

          // Format it,
          html_body = formatter.format(location);
        }
      }

      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();

      try {

        out.println("<html>");
        out.println("<head>");
        out.println("<title>DBBrowser - " + HTMLWriter.toHTMLEntity(location) + "</title>");

        out.println("<style type=\"text/css\">");
        out.println(" table { border: 1px solid #000000; border-collapse: collapse; }");
        out.println(
            " td { border-left: 1px solid #000000; border-right: 1px solid #000000; padding: 0px 6px 0px 6px; }");
        out.println(" th { background: #000000; color: #FFFFFF; }");
        out.println(" table.oblist { font-family:monospace; }");
        out.println(" table.data { border: none; border-collapse: collapse; }");
        out.println(
            " table.data td { border-left: none; border-right: none; padding: 2px 12px 2px 12px; }");
        out.println("</style>");

        out.println("</head>");
        out.println("<body>");
        out.println(html_body);
        out.println("</body>");
        out.println("</html>");

      } finally {
        out.close();
      }

    } catch (Throwable e) {
      // Output the error message,

      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();

      try {

        out.println("<html>");
        out.println("<head>");
        out.println("<title>DBBrowser - error when producing page.</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>" + HTMLWriter.toHTMLEntity(e.getMessage()) + "</h1>");
        out.print("<pre>");
        StringWriter str_out = new StringWriter();
        PrintWriter pout = new PrintWriter(str_out);
        e.printStackTrace(pout);
        pout.flush();
        out.print(HTMLWriter.toHTMLEntity(str_out.toString()));
        out.println("</pre>");
        out.println("</body>");
        out.println("</html>");

      } finally {
        out.close();
      }
    }
  }