0

在我的 Wicket Link 中的某个地方,它调用了一个实用程序,该实用程序提供了一个由 BIRT 生成的报告文件,我得到了一个 IllegalStateException。

从 Wicket 页面:

Link<Void> downloadLink = new Link<Void>("download") {
    private static final long serialVersionUID = 1L;

    @Override
    public void onClick() {

        HttpServletResponse response =
                (HttpServletResponse)((WebResponse)getResponse()).getContainerResponse();
        ReportUtility.getInstance().serveFile("myFileName.rptdesign", "pdf", response, null);

    }

};
add (downloadLink);

从 ReportUtility.java:

public void serveFile(String reportDesignFile, String extType,
        HttpServletResponse response, Map<String, Object> paramMap) {

    InputStream is = null;
    ServletOutputStream os = null;
    try {
        is = ReportUtility.class.getResourceAsStream(reportDesignFile);
        os = response.getOutputStream();

        IReportRunnable irr = engine.openReportDesign(is);
        IRunAndRenderTask task = engine.createRunAndRenderTask(irr);

        HTMLRenderOption options = new HTMLRenderOption();
        options.setOutputFormat(extType);
        options.closeOutputStreamOnExit(true);
        options.setOutputStream(os);

        // to force open/save/cancel with our specified filename
        String outputFileName = createReportFileName(reportDesignFile, extType);
        response.addHeader("Content-Disposition", "attachment;filename=" + outputFileName);

        if (paramMap != null) {
            task.setParameterValues(paramMap);
        }
        task.setRenderOption(options);      
        task.run();

    }
    catch (EngineException ex) {
        logger.error("Engine Exception while serving file", ex);
        throw new RuntimeException(ex);
    }
    catch (IOException ioex) {
        logger.error("IO Exception while openning response output stream", ioex);
        throw new RuntimeException(ioex);
    }

    finally {
        try {
            if (os != null)
                os.close();
            if (is != null)
                is.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

如果重要,createReportFileName 方法会将今天的日期和正确的文件扩展名附加到报告设计文件的基本名称,即“myFileName.rptdesign”变为“myFileName_04_24_2012.pdf”

这是 statck 跟踪:

Apr 24, 2012 9:35:04 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet default threw exception
    java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
at org.apache.wicket.protocol.http.servlet.ServletWebResponse.sendRedirect(ServletWebResponse.java:230)
at org.apache.wicket.protocol.http.BufferedWebResponse$SendRedirectAction.invoke(BufferedWebResponse.java:392)
at org.apache.wicket.protocol.http.BufferedWebResponse.writeTo(BufferedWebResponse.java:580)
at org.apache.wicket.protocol.http.HeaderBufferingWebResponse.flush(HeaderBufferingWebResponse.java:89)
at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:195)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:241)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

重要的是要说明这不会影响操作:用户单击,打开/保存/取消出现,文件来了,看起来很漂亮。然而,在 ward 之后,无论用户接下来尝试做什么,都会向我们的错误页面发送 StalePageException。之后,一切又恢复正常。

我怀疑这涉及到 HttpServletResponse,或者我是如何从 Wicket 获取它的。但是,我将文件添加到带有标题的响应的那部分代码几乎完全是从 BIRT 的教程中复制的。(也许 BIRT 和 Wicket 只是不喜欢对方。)

我注意到我自己的代码都没有显示在堆栈跟踪中。此外,我尝试在少数几个地方“捕获” IllegalStateException,包括 onClick、serveFile 甚至我的 Wicket 应用程序,但均未成功。当然,即使我能抓住它,我也宁愿一开始就没有引起它。

4

1 回答 1

0

提供报告的更好方法是实现您自己的 ResourceStream (...implements IResourceStream)。当用户单击时,您的实现会返回生成的报告。

以下是一些示例,如何添加将报告流式传输到客户端的检票口链接:

add(new Link("reportAsPdfButton") {
    @Override
    public void onClick() {
        BirtReportResourceStream brrs = new BirtReportResourceStream();
        getRequestCycle().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(brrs, "your_report_name.pdf"));
    }
});

这是一个简短的示例,用于实现 IResourceStream:

public class BirtReportResourceStream implements IResourceStream {
    private byte[] content = null;

    ...

    public BirtReportResourceStream() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EngineConfig config = new EngineConfig();
        config.setEngineHome(basePath + "/WEB-INF/ext_lib/");

        try {
            Platform.startup(config);
            IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
            IReportEngine engine = factory.createReportEngine(config);
            IReportRunnable report = engine.openReportDesign("path/to/your/report-definiton.rpt");
            IRunAndRenderTask task = engine.createRunAndRenderTask(report);

            if (null != getParameter()) {
                task.setParameterValues(getParameter());
            }  

            PDFRenderOption options = new PDFRenderOption();
            options.setOutputFormat(format.toString().toLowerCase());
            options.setOutputStream(baos);
            task.setRenderOption(options);
            task.run();
            task.close();
        } catch (BirtException e) {
            // handle the exception...
        }

        content = baos.toByteArray();
    }

    public InputStream getInputStream() throws ResourceStreamNotFoundException {
        return new ByteArrayInputStream(content);
    }

    ...
}

还有一些方法可以实现,但它们很简单。最复杂的部分是找出如何创建报告并将其流式传输到字节数组以及如何将其流式传输到客户端。这两种方法如上所示。

希望能帮助到你。

于 2012-04-24T14:46:37.453 回答