public void setMimeType(final String mimeType) {
   response.setContentType(mimeType);
   response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
   //
   // Since this is an HTTP thing, allow users to override default behavior by allowing them
   // the mime object as an attachment. Forcing everything non-html to be an attachment
   // means that users can't have their excel reports appear in their IE browser, it makes
   // SVG not work unless you have an external SVG viewer, it makes PDF files no longer
   // be viewable in the browser. Please see PLATFORM-1018.
   // also see: http://support.microsoft.com/kb/260519 for info on content-disposition header
   //
   String forceAttachment = request.getParameter("forceAttachment"); // $NON-NLS-1$
   String fileName = title;
   if (fileName == null) {
     fileName = "default"; // $NON-NLS-1$
   }
   fileName = fileName.replace(' ', '_');
   fileName += MimeHelper.getExtension(mimeType);
   if ((forceAttachment != null) && ("true".equalsIgnoreCase(forceAttachment))) { // $NON-NLS-1$
     response.setHeader(
         "content-disposition", "attachment;filename=" + fileName); // $NON-NLS-1$ //$NON-NLS-2$
   } else {
     response.setHeader(
         "content-disposition", "inline;filename=" + fileName); // $NON-NLS-1$ //$NON-NLS-2$
   }
 }
  public OutputStream getOutputStream() throws Exception {
    String tempOutputFilePath = outputFilePath;
    String extension = RepositoryFilenameUtils.getExtension(tempOutputFilePath);
    if ("*".equals(extension)) { // $NON-NLS-1$
      tempOutputFilePath = tempOutputFilePath.substring(0, tempOutputFilePath.lastIndexOf('.'));
      if (streamingAction != null) {
        String mimeType = streamingAction.getMimeType(null);
        if (mimeType != null && MimeHelper.getExtension(mimeType) != null) {
          tempOutputFilePath += MimeHelper.getExtension(mimeType);
        }
      }
    }

    RepositoryFileOutputStream outputStream =
        new RepositoryFileOutputStream(tempOutputFilePath, autoCreateUniqueFilename, true);
    outputStream.addListener(this);
    return outputStream;
  }
 public String getMimeType() {
   String mimeType = null;
   if (streamingAction != null) {
     mimeType = streamingAction.getMimeType(null);
   }
   if (mimeType == null) {
     mimeType = MimeHelper.getMimeTypeFromFileName(outputFilePath);
   }
   if (mimeType == null) {
     mimeType = "binary/octet-stream";
   }
   return mimeType;
 }
  public void createReportContent(
      final OutputStream outputStream,
      final Serializable fileId,
      final String path,
      final boolean forceDefaultOutputTarget)
      throws Exception {
    final long start = System.currentTimeMillis();
    final Map<String, Object> inputs = contentGenerator.createInputs();

    AuditHelper.audit(
        userSession.getId(),
        userSession.getName(),
        path,
        contentGenerator.getObjectName(),
        getClass().getName(),
        MessageTypes.INSTANCE_START,
        contentGenerator.getInstanceId(),
        "",
        0,
        contentGenerator); //$NON-NLS-1$

    String result = MessageTypes.INSTANCE_END;
    StagingHandler reportStagingHandler = null;
    try {
      final Object rawSessionId = inputs.get(ParameterXmlContentHandler.SYS_PARAM_SESSION_ID);
      if ((rawSessionId instanceof String) == false || "".equals(rawSessionId)) {
        inputs.put(ParameterXmlContentHandler.SYS_PARAM_SESSION_ID, UUIDUtil.getUUIDAsString());
      }

      // produce rendered report
      final SimpleReportingComponent reportComponent = new SimpleReportingComponent();
      reportComponent.setReportFileId(fileId);
      reportComponent.setPaginateOutput(true);
      reportComponent.setForceDefaultOutputTarget(forceDefaultOutputTarget);
      reportComponent.setDefaultOutputTarget(HtmlTableModule.TABLE_HTML_PAGE_EXPORT_TYPE);
      if (path.endsWith(".prpti")) {
        reportComponent.setForceUnlockPreferredOutput(true);
      }
      reportComponent.setInputs(inputs);

      final MasterReport report = reportComponent.getReport();
      final StagingMode stagingMode = getStagingMode(inputs, report);
      reportStagingHandler = new StagingHandler(outputStream, stagingMode, this.userSession);

      if (reportStagingHandler.isFullyBuffered()) {
        // it is safe to disable the buffered writing for the report now that we have a
        // extra buffering in place.
        report.getReportConfiguration().setConfigProperty(FORCED_BUFFERED_WRITING, "false");
      }

      reportComponent.setOutputStream(reportStagingHandler.getStagingOutputStream());

      // the requested mime type can be null, in that case the report-component will resolve the
      // desired
      // type from the output-target.
      // Hoever, the report-component will inspect the inputs independently from the mimetype here.

      final IUnifiedRepository repository =
          PentahoSystem.get(IUnifiedRepository.class, userSession);
      final RepositoryFile file = repository.getFileById(fileId);

      // add all inputs (request parameters) to report component
      final String mimeType = reportComponent.getMimeType();

      // If we haven't set an accepted page, -1 will be the default, which will give us a report
      // with no pages. This default is used so that when we do our parameter interaction with the
      // engine we can spend as little time as possible rendering unused pages, making it no pages.
      // We are going to intentionally reset the accepted page to the first page, 0, at this point,
      // if the accepted page is -1.
      final String outputTarget = reportComponent.getComputedOutputTarget();
      if (HtmlTableModule.TABLE_HTML_PAGE_EXPORT_TYPE.equals(outputTarget)
          && reportComponent.getAcceptedPage() < 0) {
        reportComponent.setAcceptedPage(0);
      }

      if (logger.isDebugEnabled()) {
        logger.debug(
            Messages.getInstance()
                .getString(
                    "ReportPlugin.logStartGenerateContent",
                    mimeType, //$NON-NLS-1$
                    outputTarget,
                    String.valueOf(reportComponent.getAcceptedPage())));
      }

      HttpServletResponse response = null;
      boolean streamToBrowser = false;
      final IParameterProvider pathProviders = contentGenerator.getParameterProviders().get("path");
      if (pathProviders != null) {
        final Object httpResponse = pathProviders.getParameter("httpresponse");
        if (httpResponse
            instanceof HttpServletResponse) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
          response = (HttpServletResponse) httpResponse; // $NON-NLS-1$ //$NON-NLS-2$
          if (reportStagingHandler.getStagingMode() == StagingMode.THRU) {
            // Direct back - check output stream...
            final OutputStream respOutputStream = response.getOutputStream();
            if (respOutputStream == outputStream) {
              //
              // Massive assumption here -
              // Assume the container returns the same object on successive calls to
              // response.getOutputStream()
              streamToBrowser = true;
            }
          }
        }
      }

      final String extension = MimeHelper.getExtension(mimeType);
      String filename = file.getName();
      if (filename.lastIndexOf(".") != -1) { // $NON-NLS-1$
        filename = filename.substring(0, filename.lastIndexOf(".")); // $NON-NLS-1$
      }
      String disposition =
          "inline; filename*=UTF-8''"
              + RepositoryPathEncoder.encode(
                  RepositoryPathEncoder.encodeRepositoryPath(filename + extension));

      final boolean validates = reportComponent.validate();
      if (!validates) {
        sendErrorResponse(response, outputStream, reportStagingHandler);
      } else {
        if (response != null) {
          // Send headers before we begin execution
          response.setHeader("Content-Disposition", disposition);
          response.setHeader("Content-Description", file.getName()); // $NON-NLS-1$
          response.setHeader("Cache-Control", "private, max-age=0, must-revalidate");
        }
        if (reportComponent.execute()) {
          if (response != null) {
            if (reportStagingHandler.canSendHeaders()) {
              response.setHeader("Content-Disposition", disposition);
              response.setHeader("Content-Description", file.getName()); // $NON-NLS-1$
              response.setHeader("Cache-Control", "private, max-age=0, must-revalidate");
              response.setContentLength(reportStagingHandler.getWrittenByteCount());
            }
          }
          if (logger.isDebugEnabled()) {
            logger.debug(
                Messages.getInstance()
                    .getString(
                        "ReportPlugin.logEndGenerateContent",
                        String.valueOf(reportStagingHandler.getWrittenByteCount()))); // $NON-NLS-1$
          }
          reportStagingHandler.complete(); // will copy bytes to final destination...

        } else { // failed execution
          sendErrorResponse(response, outputStream, reportStagingHandler);
        }
      }
    } catch (Exception ex) {
      result = MessageTypes.INSTANCE_FAILED;
      throw ex;
    } finally {
      if (reportStagingHandler != null) {
        reportStagingHandler.close();
      }
      final long end = System.currentTimeMillis();
      AuditHelper.audit(
          userSession.getId(),
          userSession.getName(),
          path,
          contentGenerator.getObjectName(),
          getClass().getName(),
          result,
          contentGenerator.getInstanceId(),
          "",
          ((float) (end - start) / 1000),
          contentGenerator); //$NON-NLS-1$
    }
  }