title: Sending a BIRT file via HttpServletResponse through a Wicket Link, causes IllegalStateException
body: <p>Somewhere within my Wicket Link, that calls a Utility, which serves a report file,
  generated by BIRT, I am getting an IllegalStateException.</p>
  
  <p>From the Wicket page:</p>
  
  <pre><code>Link&lt;Void&gt; downloadLink = new Link&lt;Void&gt;("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);
  </code></pre>
  
  <p>From the ReportUtility.java:</p>
  
  <pre><code>public void serveFile(String reportDesignFile, String extType,
          HttpServletResponse response, Map&lt;String, Object&gt; 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
          }
      }
  }
  </code></pre>
  
  <p>If it matters, the createReportFileName method appends today's date and the proper file extension to
  the basename of the report design file, i.e. "myFileName.rptdesign" becomes "myFileName_04_24_2012.pdf"</p>
  
  <p>Here is the statck trace:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>It is important to state that this does not effect the operation: the user clicks, the open/save/cancel appears,
  the file comes, and looks very pretty.  After ward, however, whatever the user tries to do next, sends to
  our error page with a StalePageException.  After that, everything returns to normal again.</p>
  
  <p>I suspect this involves the HttpServletResponse, or perhaps how I am getting it from Wicket.  However, that portion of
  my code that adds the file to the response with the header is copied almost exactly from BIRT's tutorials.  (Maybe BIRT and Wicket
  just don't like each other.)</p>
  
  <p>I note that none of my own code shows in the stack trace.  Also, I have tried to "catch" the IllegalStateException in
  a handful of places, including the onClick, the serveFile, and even my Wicket Application, all without
  success.  Of course, even if I could catch it, I would prefer to have never caused it in the first place.</p>