コード例 #1
0
  /**
   * This method will open the sample report pdf.
   *
   * @param reportFilePath - full path of the sample report to be shown.
   * @param request - instance of HttpServletRequest
   * @param response - instance of HttpServletResponse
   * @throws ServletException - error
   * @throws IOException - error
   */
  private static void showSampleReport(
      String reportFilePath, HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    if (null != request.getSession().getAttribute(ReportServiceConstant.VIEW_SAMPLE_REPORT)
        && request
            .getSession()
            .getAttribute(ReportServiceConstant.VIEW_SAMPLE_REPORT)
            .toString()
            .equalsIgnoreCase("Y")) {
      ServletOutputStream output = null;
      try {

        FileInputStream fis = new FileInputStream(reportFilePath);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[256];
        try {
          for (int readNum; (readNum = fis.read(buf)) != -1; ) {
            baos.write(buf, 0, readNum); // no doubt here is 0
            // Writes len bytes from the specified byte array starting at offset off to this byte
            // array output stream.
          }

        } catch (IOException ex) {
          ex.printStackTrace();
        }

        if (null != baos) {

          // Init servlet response.
          response.reset();
          response.setContentType("application/pdf");
          response.setContentLength(baos.size());
          response.setHeader("Content-disposition", "inline; filename=\"" + reportFilePath);
          response.setHeader("Expires", "0");
          response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
          //                  response.setHeader("Transfer-Encoding", "identity");
          output = response.getOutputStream();

          output.write(baos.toByteArray(), 0, baos.size());

          // Finalize task.
          output.flush();
        }
      } catch (Exception exception) {
        OPPE_LOG.error("ERROR.SHOW_PDF.ERROR", exception);
      } finally {

        // Gently close streams.
        close((Closeable) output);
      }
    }
  }
コード例 #2
0
  /**
   * This method handles response of showing pdf servlet.
   *
   * @param request - instance of HttpServletRequest
   * @param response - instance of HttpServletResponse
   * @throws ServletException - error
   * @throws IOException - error
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String preview = request.getParameter(PREVIEW_REPORT_PARAMETER);
    String sampleReportParam = request.getParameter(SAMPLE_REPORT_PARAM);
    if (null != sampleReportParam) {
      String sampleReportPath = "";
      if (sampleReportParam.equalsIgnoreCase(ReportServiceConstant.PROVIDER_SUMMARY)) {
        sampleReportPath =
            WL_HOME_PATH + RESOURCES_PATH + ReportServiceConstant.PROVIDER_SUMMARY_SAMPLE_FILE;
        showSampleReport(sampleReportPath, request, response);
      } else if (sampleReportParam.equalsIgnoreCase(ReportServiceConstant.COMPARATIVE_SUMMARY)) {
        sampleReportPath =
            WL_HOME_PATH + RESOURCES_PATH + ReportServiceConstant.COMPARITIVE_SUMMARY_SAMPLE_FILE;
        showSampleReport(sampleReportPath, request, response);
      } else if (sampleReportParam.equalsIgnoreCase(ReportServiceConstant.EXECUTIVE_SUMMARY)) {
        sampleReportPath =
            WL_HOME_PATH + RESOURCES_PATH + ReportServiceConstant.EXECUTIVE_SUMMARY_SAMPLE_FILE;
        showSampleReport(sampleReportPath, request, response);
      }
    } else if (null != preview && STR_TRUE.equals(preview)) {
      // report generation using OracleBI
      ServletOutputStream output = null;
      byte[] rawBinaryFile = null;
      Map<String, String> parameterMap = null;
      PublicReportServicePortClient client = null;
      FileStream fileStream = null;
      try {

        if (null != request.getSession().getAttribute(REPORTS_PARAMETERS_MAP)) {
          parameterMap =
              (Map<String, String>) request.getSession().getAttribute(REPORTS_PARAMETERS_MAP);
          //                    request.getSession().removeAttribute(REPORTS_PARAMETERS_MAP);
        }

        if (null != parameterMap && parameterMap.containsKey("REPORT_PATH")) {
          client = new PublicReportServicePortClient();
          fileStream = client.generateReport(parameterMap);
          rawBinaryFile = fileStream.getFileContent();
        }

        if (null != rawBinaryFile) {

          // Init servlet response.
          response.reset();
          response.setContentType("application/pdf");
          response.setContentLength(rawBinaryFile.length);
          response.setHeader(
              "Content-disposition", "inline; filename=\"" + fileStream.getFileName() + ".pdf\"");
          response.setHeader("Expires", "0");
          response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
          //                  response.setHeader("Transfer-Encoding", "identity");
          output = response.getOutputStream();

          output.write(rawBinaryFile, 0, rawBinaryFile.length);

          // Finalize task.
          output.flush();
        }
      } catch (Exception exception) {
        OPPE_LOG.error("ERROR.SHOW_PDF.ERROR", exception);
      } finally {

        // Gently close streams.
        close((Closeable) output);
      }
    }
  }