/** * 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); } } }
public void close() throws IOException { if (closed) { throw new IOException("This output stream has already been closed"); } gzipstream.finish(); byte[] bytes = baos.toByteArray(); response.addHeader("Content-Length", Integer.toString(bytes.length)); response.addHeader("Content-Encoding", "gzip"); output.write(bytes); output.flush(); output.close(); closed = true; }
/** * return OutputStream of JasperReport object, this page could only be viewed from localhost for * security concern. parameter can be (id), or (table and type) * * @param id - report id, or * @param table - table name * @param type - reporttype "s","l","o", case insensitive * @param client(*) - client domain * @param version - version number, default to -1 */ public void process(HttpServletRequest request, HttpServletResponse response) throws Exception { String clientName = request.getParameter("client"); int objectId = ParamUtils.getIntAttributeOrParameter(request, "id", -1); if (objectId == -1) { // try using table and type objectId = getReportId(clientName, request.getParameter("table"), request.getParameter("type")); } if (objectId == -1) { logger.error("report not found, request is:" + Tools.toString(request)); throw new NDSException("report not found"); } int version = ParamUtils.getIntAttributeOrParameter(request, "version", -1); File reportXMLFile = new File(ReportTools.getReportFile(objectId, clientName)); if (reportXMLFile.exists()) { // generate jasperreport if file not exists or not newer String reportName = reportXMLFile.getName().substring(0, reportXMLFile.getName().lastIndexOf(".")); File reportJasperFile = new File(reportXMLFile.getParent(), reportName + ".jasper"); if (!reportJasperFile.exists() || reportJasperFile.lastModified() < reportXMLFile.lastModified()) { JasperCompileManager.compileReportToFile( reportXMLFile.getAbsolutePath(), reportJasperFile.getAbsolutePath()); } InputStream is = new FileInputStream(reportJasperFile); response.setContentType("application/octetstream;"); response.setContentLength((int) reportJasperFile.length()); // response.setHeader("Content-Disposition","inline;filename=\""+reportJasperFile.getName()+"\""); ServletOutputStream os = response.getOutputStream(); byte[] b = new byte[8192]; int bInt; while ((bInt = is.read(b, 0, b.length)) != -1) { os.write(b, 0, bInt); } is.close(); os.flush(); os.close(); } else { throw new NDSException("Not found report template"); } }
/** * 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); } } }