protected void doExecute()

in plugins/jasperreports7/src/main/java/org/apache/struts2/views/jasperreports7/JasperReport7Result.java [156:223]


    protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
        initializeProperties(invocation);

        LOG.debug("Creating JasperReport for dataSource: {} and format: {}", dataSource, format);
        // Construct the data source for the report.
        ValueStack stack = invocation.getStack();
        Connection reportConnection = (Connection) stack.findValue(connection);
        ValueStackDataSource reportDataSource = null;
        if (reportConnection == null) {
            reportDataSource = prepareDataSource(stack);
        }

        if (invocation.getAction() instanceof JasperReport7Aware action) {
            LOG.debug("Passing control to action: {} before generating report", invocation.getInvocationContext().getActionName());
            action.beforeReportGeneration(invocation);
        }

        ServletContext servletContext = invocation.getInvocationContext().getServletContext();
        String systemId = servletContext.getRealPath(finalLocation);
        Map<String, Object> parameters = new ValueStackShadowMap(stack);
        File directory = new File(systemId.substring(0, systemId.lastIndexOf(File.separator)));
        parameters.put("reportDirectory", directory);

        applyLocale(invocation, parameters);
        applyTimeZone(invocation, parameters);
        applyCustomParameters(stack, parameters);

        JasperPrint jasperPrint;

        // Fill the report and produce a print object
        try {
            JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(systemId));
            if (reportConnection == null) {
                jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, reportDataSource);
            } else {
                jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, reportConnection);
            }

            if (invocation.getAction() instanceof JasperReport7Aware action) {
                LOG.debug("Passing control to action: {} after generating report: {}",
                        invocation.getInvocationContext().getActionName(), jasperReport.getName());
                action.afterReportGeneration(invocation, jasperReport);
            }
        } catch (JRException e) {
            LOG.error("Error building report for uri: {}", systemId, e);
            throw new ServletException(e.getMessage(), e);
        }

        try {
            LOG.debug("Export the print object to the desired output format: {}", format);
            JasperReport7ExporterProvider<?> exporterProvider = invocation.getInvocationContext().getContainer().getInstance(JasperReport7ExporterProvider.class, format);
            if (exporterProvider == null) {
                throw new StrutsException("No exporter found for format: " + format);
            }
            exportReport(invocation, jasperPrint, exporterProvider);
        } catch (StrutsException e) {
            LOG.error("Error producing: {} report for uri: {}", format, systemId, e);
            throw new ServletException(e.getMessage(), e);
        } finally {
            try {
                if (reportConnection != null) {
                    reportConnection.close();
                }
            } catch (Exception e) {
                LOG.warn("Could not close db connection properly", e);
            }
        }
    }