diff --git a/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/JasperReport7Result.java b/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/JasperReport7Result.java index e1ef3e4828..880f17ead9 100644 --- a/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/JasperReport7Result.java +++ b/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/JasperReport7Result.java @@ -156,21 +156,13 @@ public void setNotExcludedAcceptedPatterns(NotExcludedAcceptedPatternsChecker no protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { initializeProperties(invocation); - LOG.debug("Creating JasperReport for dataSource = {}, format = {}", dataSource, format); + LOG.debug("Creating JasperReport for dataSource: {} and format: {}", dataSource, format); // Construct the data source for the report. ValueStack stack = invocation.getStack(); - ValueStackDataSource stackDataSource = null; - - Connection conn = (Connection) stack.findValue(connection); - if (conn == null) { - boolean evaluated = parsedDataSource != null && !parsedDataSource.equals(dataSource); - boolean reevaluate = !evaluated || isAcceptableExpression(parsedDataSource); - if (reevaluate) { - stackDataSource = new ValueStackDataSource(stack, parsedDataSource, wrapField); - } else { - throw new ServletException(String.format("Error building dataSource for excluded or not accepted [%s]", - parsedDataSource)); - } + Connection reportConnection = (Connection) stack.findValue(connection); + ValueStackDataSource reportDataSource = null; + if (reportConnection == null) { + reportDataSource = prepareDataSource(stack); } if (invocation.getAction() instanceof JasperReport7Aware action) { @@ -186,25 +178,17 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro applyLocale(invocation, parameters); applyTimeZone(invocation, parameters); - - // Add any report parameters from action to param map. - boolean evaluated = parsedReportParameters != null && !parsedReportParameters.equals(reportParameters); - boolean reevaluate = !evaluated || isAcceptableExpression(parsedReportParameters); - Map reportParams = reevaluate ? (Map) stack.findValue(parsedReportParameters) : null; - if (reportParams != null) { - LOG.debug("Found report parameters; adding to parameters..."); - parameters.putAll(reportParams); - } + applyCustomParameters(stack, parameters); JasperPrint jasperPrint; // Fill the report and produce a print object try { JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(systemId)); - if (conn == null) { - jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, stackDataSource); + if (reportConnection == null) { + jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, reportDataSource); } else { - jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn); + jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, reportConnection); } if (invocation.getAction() instanceof JasperReport7Aware action) { @@ -213,7 +197,7 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro action.afterReportGeneration(invocation, jasperReport); } } catch (JRException e) { - LOG.error("Error building report for uri {}", systemId, e); + LOG.error("Error building report for uri: {}", systemId, e); throw new ServletException(e.getMessage(), e); } @@ -229,9 +213,8 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro throw new ServletException(e.getMessage(), e); } finally { try { - if (conn != null) { - // avoid NPE if connection was not used for the report - conn.close(); + if (reportConnection != null) { + reportConnection.close(); } } catch (Exception e) { LOG.warn("Could not close db connection properly", e); @@ -239,6 +222,17 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro } } + protected ValueStackDataSource prepareDataSource(ValueStack stack) throws ServletException { + boolean evaluated = parsedDataSource != null && !parsedDataSource.equals(dataSource); + boolean reevaluate = !evaluated || isAcceptableExpression(parsedDataSource); + if (reevaluate) { + return new ValueStackDataSource(stack, parsedDataSource, wrapField); + } else { + throw new ServletException(String.format("Error building dataSource for excluded or not accepted [%s]", + parsedDataSource)); + } + } + protected void applyLocale(ActionInvocation invocation, Map parameters) { Locale locale = null; if (invocation.getAction() instanceof JasperReport7Aware action) { @@ -263,6 +257,16 @@ protected void applyTimeZone(ActionInvocation invocation, Map pa } } + protected void applyCustomParameters(ValueStack stack, Map parameters) { + boolean evaluated = parsedReportParameters != null && !parsedReportParameters.equals(reportParameters); + boolean reevaluate = !evaluated || isAcceptableExpression(parsedReportParameters); + Map reportParams = reevaluate ? (Map) stack.findValue(parsedReportParameters) : null; + if (reportParams != null) { + LOG.debug("Found report parameters; adding to parameters..."); + parameters.putAll(reportParams); + } + } + protected void exportReport(ActionInvocation invocation, JasperPrint jasperPrint, JasperReport7ExporterProvider exporterProvider) throws StrutsException { HttpServletResponse response = prepapreHttpServletResponse(invocation); try { diff --git a/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7CsvExporterProvider.java b/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7CsvExporterProvider.java index 50dbabbf95..112855c2c0 100644 --- a/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7CsvExporterProvider.java +++ b/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7CsvExporterProvider.java @@ -62,9 +62,10 @@ public JasperReport7CsvExporterProvider( @Override public JRCsvExporter createExporter(ActionInvocation invocation, JasperPrint jasperPrint) throws StrutsException { LOG.debug("Creating: {} exporter", this.getClass().getSimpleName()); - HttpServletResponse response = invocation.getInvocationContext().getServletResponse(); + HttpServletResponse response = invocation.getInvocationContext().getServletResponse(); response.setContentType("text/csv"); + JRCsvExporter exporter = new JRCsvExporter(); String reportDelimiter = null; diff --git a/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7PdfExporterProvider.java b/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7PdfExporterProvider.java index cda5761e94..b6f44c1fac 100644 --- a/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7PdfExporterProvider.java +++ b/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7PdfExporterProvider.java @@ -39,6 +39,7 @@ public class JasperReport7PdfExporterProvider implements JasperReport7ExporterPr @Override public JRPdfExporter createExporter(ActionInvocation invocation, JasperPrint jasperPrint) throws StrutsException { LOG.debug("Creating: {} exporter", this.getClass().getSimpleName()); + HttpServletResponse response = invocation.getInvocationContext().getServletResponse(); response.setContentType("application/pdf"); diff --git a/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7XlsxExporterProvider.java b/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7XlsxExporterProvider.java index 3fead98c3c..c2dcbfeb96 100644 --- a/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7XlsxExporterProvider.java +++ b/plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/export/JasperReport7XlsxExporterProvider.java @@ -39,8 +39,8 @@ public class JasperReport7XlsxExporterProvider implements JasperReport7ExporterP @Override public JRXlsxExporter createExporter(ActionInvocation invocation, JasperPrint jasperPrint) throws StrutsException { LOG.debug("Creating: {} exporter", this.getClass().getSimpleName()); - HttpServletResponse response = invocation.getInvocationContext().getServletResponse(); + HttpServletResponse response = invocation.getInvocationContext().getServletResponse(); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); JRXlsxExporter exporter = new JRXlsxExporter();