/**
   * Downloads a Bundle file for a given bundle id.
   *
   * @param request HttpRequest
   * @param response HttpResponse
   * @throws IOException If fails sending back to the user response information
   */
  public void downloadBundle(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    try {
      if (!APILocator.getLayoutAPI()
          .doesUserHaveAccessToPortlet("EXT_CONTENT_PUBLISHING_TOOL", getUser())) {
        response.sendError(401);
        return;
      }
    } catch (DotDataException e1) {
      Logger.error(RemotePublishAjaxAction.class, e1.getMessage(), e1);
      response.sendError(401);
      return;
    }
    Map<String, String> map = getURIParams();
    response.setContentType("application/x-tgz");

    String bid = map.get("bid");

    PublisherConfig config = new PublisherConfig();
    config.setId(bid);
    File bundleRoot = BundlerUtil.getBundleRoot(config);

    ArrayList<File> list = new ArrayList<File>(1);
    list.add(bundleRoot);
    File bundle =
        new File(bundleRoot + File.separator + ".." + File.separator + config.getId() + ".tar.gz");
    if (!bundle.exists()) {
      response.sendError(500, "No Bundle Found");
      return;
    }

    response.setHeader("Content-Disposition", "attachment; filename=" + config.getId() + ".tar.gz");
    BufferedInputStream in = null;
    try {
      in = new BufferedInputStream(new FileInputStream(bundle));
      byte[] buf = new byte[4096];
      int len;

      while ((len = in.read(buf, 0, buf.length)) != -1) {
        response.getOutputStream().write(buf, 0, len);
      }
    } catch (Exception e) {
      Logger.warn(this.getClass(), "Error Downloading Bundle.", e);
    } finally {
      try {
        in.close();
      } catch (Exception ex) {
        Logger.warn(this.getClass(), "Error Closing Stream.", ex);
      }
    }
    return;
  }
  /**
   * Generates and flush an Unpublish bundle for a given bundle id and operation (publish/unpublish)
   *
   * @param request HttpRequest
   * @param response HttpResponse
   * @throws IOException If fails sending back to the user response information
   */
  public void downloadUnpushedBundle(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    try {
      if (!APILocator.getLayoutAPI()
          .doesUserHaveAccessToPortlet("EXT_CONTENT_PUBLISHING_TOOL", getUser())) {
        response.sendError(401);
        return;
      }
    } catch (DotDataException e1) {
      Logger.error(RemotePublishAjaxAction.class, e1.getMessage(), e1);
      response.sendError(401);
      return;
    }
    // Read the parameters
    Map<String, String> map = getURIParams();
    String bundleId = map.get("bundleId");
    String paramOperation = map.get("operation");
    if (bundleId == null || bundleId.isEmpty()) {
      Logger.error(this.getClass(), "No Bundle Found with id: " + bundleId);
      response.sendError(500, "No Bundle Found with id: " + bundleId);
      return;
    }

    // What we want to do with this bundle
    PushPublisherConfig.Operation operation = PushPublisherConfig.Operation.PUBLISH;
    if (paramOperation != null && paramOperation.equalsIgnoreCase("unpublish")) {
      operation = PushPublisherConfig.Operation.UNPUBLISH;
    }

    File bundle;
    String generatedBundleId;
    try {
      // Generate the bundle file for this given operation
      Map<String, Object> bundleData = generateBundle(bundleId, operation);
      bundle = (File) bundleData.get("file");
      generatedBundleId = (String) bundleData.get("id");
    } catch (Exception e) {
      Logger.error(this.getClass(), "Error trying to generate bundle with id: " + bundleId, e);
      response.sendError(500, "Error trying to generate bundle with id: " + bundleId);
      return;
    }

    response.setContentType("application/x-tgz");
    response.setHeader("Content-Disposition", "attachment; filename=" + bundle.getName());
    BufferedInputStream in = null;
    try {
      in = new BufferedInputStream(new FileInputStream(bundle));
      byte[] buf = new byte[4096];
      int len;

      while ((len = in.read(buf, 0, buf.length)) != -1) {
        response.getOutputStream().write(buf, 0, len);
      }
    } catch (Exception e) {
      Logger.error(this.getClass(), "Error reading bundle stream for bundle id: " + bundleId, e);
      response.sendError(500, "Error reading bundle stream for bundle id: " + bundleId);
    } finally {
      try {
        in.close();
      } catch (Exception ex) {
        Logger.error(this.getClass(), "Error closing Stream for bundle: " + bundleId, ex);
      }

      // Clean the just created bundle because on each download we will generate a new bundle file
      // with a new id in order to avoid conflicts with ids
      File bundleRoot = BundlerUtil.getBundleRoot(generatedBundleId);
      File compressedBundle =
          new File(ConfigUtils.getBundlePath() + File.separator + generatedBundleId + ".tar.gz");
      if (compressedBundle.exists()) {
        compressedBundle.delete();
        if (bundleRoot.exists()) {
          com.liferay.util.FileUtil.deltree(bundleRoot);
        }
      }
    }
  }