/** Download resource to the given file */
  private boolean download(URL target, File file) {

    _log.addDebug("JarDiffHandler:  Doing download");

    boolean ret = true;
    boolean delete = false;
    // use bufferedstream for better performance
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {
      in = new BufferedInputStream(target.openStream());
      out = new BufferedOutputStream(new FileOutputStream(file));
      int read = 0;
      int totalRead = 0;
      byte[] buf = new byte[BUF_SIZE];
      while ((read = in.read(buf)) != -1) {
        out.write(buf, 0, read);
        totalRead += read;
      }

      _log.addDebug("total read: " + totalRead);
      _log.addDebug("Wrote URL " + target.toString() + " to file " + file);

    } catch (IOException ioe) {

      _log.addDebug("Got exception while downloading resource: " + ioe);

      ret = false;

      if (file != null) delete = true;

    } finally {

      try {
        in.close();
        in = null;
      } catch (IOException ioe) {
        _log.addDebug("Got exception while downloading resource: " + ioe);
      }

      try {
        out.close();
        out = null;
      } catch (IOException ioe) {
        _log.addDebug("Got exception while downloading resource: " + ioe);
      }

      if (delete) {
        file.delete();
      }
    }
    return ret;
  }
  public void _jspService(HttpServletRequest request, HttpServletResponse response)
      throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;

    try {
      response.setContentType("text/html");
      pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write('\n');
      out.write('\n');

      response.setHeader("Pragma", "no-cache"); // HTTP 1.0
      response.setDateHeader("Expires", 0);
      response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1

      String _adminid = "";
      String _adminname = "";
      String _admintype = "";
      String _admingroup = "";
      String _approval = "";
      String _adminclass = "";
      String _adminmail = "";

      try {

        _adminid = (String) session.getAttribute("adminid");

        if (_adminid == null || _adminid.length() == 0 || _adminid.equals("null")) {
          response.sendRedirect("/admin/login_first.html");
          return;
        }

        _adminname = (String) session.getAttribute("adminname");
        _admintype = (String) session.getAttribute("admintype");
        _admingroup = (String) session.getAttribute("admingroup");
        _approval = (String) session.getAttribute("approval");
        _adminclass = (String) session.getAttribute("adminclass");
        _adminmail = (String) session.getAttribute("admin_email");
        // session.setMaxInactiveInterval(60*60);

      } catch (Exception e) {
        response.sendRedirect("/admin/login_first.html");
        return;
      }

      out.write('\n');

      // response.resetBuffer();
      response.reset();

      String attachPath = request.getParameter("attachPath");
      String attachFile = request.getParameter("attachFile");
      String attachRealFile = request.getParameter("attachRealFile");

      try {

        String savePath = application.getRealPath(attachPath) + File.separator;
        File file = new File(savePath + attachRealFile);

        if (!file.exists()) {
          throw new UserDefinedException("File does not exist.");
        }

        // file read ..
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Length", "" + file.length());
        response.setHeader("Content-Disposition", "attachment; filename=" + attachFile + ";");

        byte[] buf = new byte[8192];

        int cnt = 0;

        BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream outStream = new BufferedOutputStream(response.getOutputStream());

        try {
          while ((cnt = inStream.read(buf)) != -1) {
            outStream.write(buf, 0, cnt);
          }

          outStream.flush();
        } catch (Exception e) {
          throw e;
        } finally {
          if (outStream != null) outStream.close();
          if (inStream != null) inStream.close();
        }

      } catch (Exception e) {
        out.println(
            "<html><script language='javascript'>alert('" + e.getMessage() + "');</script></html>");
      }

    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)) {
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try {
            out.clearBuffer();
          } catch (java.io.IOException e) {
          }
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }