/**
   * Initializes this object with a given parameter map.
   *
   * @param parameters the parameter map to use
   */
  @Override
  public void initialize(Map<String, Object> parameters) {
    m_nodeDao = BeanUtils.getBean("daoContext", "nodeDao", NodeDao.class);

    if (m_nodeDao == null) {
      logger.error("Node dao should be a non-null value.");
    }
  }
  /**
   * Constructor for creating a instance for a given server. Checks whether credentials are
   * available in the Vmware config file.
   *
   * @param hostname the vCenter's hostname
   * @throws MarshalException
   * @throws ValidationException
   * @throws IOException
   */
  public VmwareViJavaAccess(String hostname)
      throws MarshalException, ValidationException, IOException {
    if (m_vmwareConfigDao == null) {
      m_vmwareConfigDao = BeanUtils.getBean("daoContext", "vmwareConfigDao", VmwareConfigDao.class);
    }

    this.m_hostname = hostname;

    if (m_vmwareConfigDao == null) {
      logger.error("vmwareConfigDao should be a non-null value.");
    } else {
      Map<String, VmwareServer> serverMap = m_vmwareConfigDao.getServerMap();
      if (serverMap == null) {
        logger.error("Error getting vmware-config.xml's server map.");
      } else {
        VmwareServer vmwareServer = serverMap.get(m_hostname);

        if (vmwareServer == null) {
          logger.error("Error getting credentials for VMware management server '{}'.", m_hostname);
        } else {
          this.m_username = vmwareServer.getUsername();
          this.m_password = vmwareServer.getPassword();
        }
      }
    }

    if (this.m_username == null) {
      logger.error("Error getting username for VMware management server '{}'.", m_hostname);
      this.m_username = "";
    }

    if (this.m_password == null) {
      logger.error("Error getting password for VMware management server '{}'.", m_hostname);
      this.m_password = "";
    }
  }
  /** {@inheritDoc} */
  @Override
  protected ModelAndView handleRequestInternal(
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    String fileName = request.getParameter("fileName");

    m_reportdConfigurationDao =
        BeanUtils.getBean("reportdContext", "reportdConfigDao", ReportdConfigurationDao.class);
    final File storageDirectory = new File(m_reportdConfigurationDao.getStorageDirectory());

    if (fileName != null) {
      final File requestedFile = new File(fileName);
      if (!requestedFile
          .getParentFile()
          .getCanonicalFile()
          .equals(storageDirectory.getCanonicalFile())) {
        LogUtils.warnf(
            this,
            "User attempted to retrieve file %s but was restricted to %s",
            requestedFile,
            storageDirectory);
        throw new IllegalArgumentException(
            "Cannot retrieve reports from outside Reportd storage directory");
      }

      if (fileName.toLowerCase().endsWith(".pdf")) {
        response.setContentType("application/pdf;charset=UTF-8");
      }
      if (fileName.toLowerCase().endsWith(".csv")) {
        response.setContentType("text/csv;charset=UTF-8");
      }
      response.setHeader("Content-disposition", "inline; filename=" + fileName);
      response.setHeader("Pragma", "public");
      response.setHeader("Cache-Control", "cache");
      response.setHeader("Cache-Control", "must-revalidate");
      StreamUtils.copy(new FileInputStream(new File(fileName)), response.getOutputStream());
      return null;
    }

    String[] requiredParameters = new String[] {"locatorId", "format"};

    for (String requiredParameter : requiredParameters) {
      if (request.getParameter(requiredParameter) == null) {
        throw new MissingParameterException(requiredParameter, requiredParameters);
      }
    }

    try {
      Integer reportCatalogEntryId =
          Integer.valueOf(WebSecurityUtils.safeParseInt(request.getParameter("locatorId")));

      String requestFormat = new String(request.getParameter("format"));

      if ((ReportFormat.PDF == ReportFormat.valueOf(requestFormat))
          || (ReportFormat.SVG == ReportFormat.valueOf(requestFormat))) {
        response.setContentType("application/pdf;charset=UTF-8");
        response.setHeader(
            "Content-disposition", "inline; filename=" + reportCatalogEntryId.toString() + ".pdf");
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "cache");
        response.setHeader("Cache-Control", "must-revalidate");
      }
      if (ReportFormat.CSV == ReportFormat.valueOf(requestFormat)) {
        response.setContentType("text/csv;charset=UTF-8");
        response.setHeader(
            "Content-disposition", "inline; filename=" + reportCatalogEntryId.toString() + ".csv");
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "cache");
        response.setHeader("Cache-Control", "must-revalidate");
      }
      m_reportStoreService.render(
          reportCatalogEntryId,
          ReportFormat.valueOf(requestFormat),
          (OutputStream) response.getOutputStream());
    } catch (NumberFormatException e) {
      // TODO something useful here.
    }

    return null;
  }