I am calling HttpServletResponse.getOutputStream, and I get a stack trace like this:

Caused by: java.lang.IllegalStateException: WRITER
at org.mortbay.jetty.Response.getOutputStream(Response.java:594)
at com.springsource.sts.roo.rap.RAPDownloader$1.run(RAPDownloader.java:62)
at org.eclipse.rwt.internal.lifecycle.FakeContextUtil.runNonUIThreadWithFakeContext(FakeContextUtil.java:60)
at org.eclipse.rwt.lifecycle.UICallBack.runNonUIThreadWithFakeContext(UICallBack.java:48)
at com.foo.Downloader.performDownload(Downloader.java:46)
...

I am trying to download a file that was generated on the server. I am using UICallBack.runNonUIThreadWithFakeContext as other posts have suggested, but this does not seem to help.

Thanks for your help. That was the problem. Somehow, the response writer was being called first and so the output stream was not available.

However, this raises a new problem since I need to send a bunch of bytes to the output stream and there doesn't seem to be an easy way of doing this through the writer. When I try to cast the bytes to chars, then I get the following error on the web page:

On 7/25/2011 6:47 PM, Andrew Eisenberg wrote:
> Thanks for your help. That was the problem. Somehow, the response
> writer was being called first and so the output stream was not available.
>
> However, this raises a new problem since I need to send a bunch of
> bytes to the output stream and there doesn't seem to be an easy way of
> doing this through the writer. When I try to cast the bytes to chars,
> then I get the following error on the web page:
>
>
> Could not evaluate javascript response:
>
> Error: SyntaxError: Parse error
>
>
> And then the contents of the zip file I am trying to download is
> displayed on the page. Do you want me to attach the exact code
> snippet I am using to write the data?

Is using a Service handler the only way to accomplish this? If so, then it would break my intended workflow since the zip file cannot be generated through the selection listener as I show above, but only through the service handler. Apologies if I am misunderstanding something fundamental here. I have lots of experiences with RCP, but little/no experience with web-apps.

I would like to write to the response through the SelectionListener as I show above, but is this not possible?

Hi Andrew,
it's not possible to use the standard RAP Ajax request for download
purpose. Using a service handler is the way to go.
Best,
Ivan

On 7/25/2011 8:22 PM, Andrew Eisenberg wrote:
> Thanks for your response. I did have a look at the the FAQ, but it is
> not really what I am trying to do. I do not want to create a link for
> downloading.
>
> The workflow is supposed to be like this:
>
> 1. User clicks on a SWT/RWT button widget
> 2. Application generates a zip file
> 3. Zip file is downloaded to user's machine.
>
> Is using a Service handler the only way to accomplish this? If so,
> then it would break my intended workflow since the zip file cannot be
> generated through the selection listener as I show above, but only
> through the service handler. Apologies if I am misunderstanding
> something fundamental here. I have lots of experiences with RCP, but
> little/no experience with web-apps.
>
> I would like to write to the response through the SelectionListener as
> I show above, but is this not possible?

Adding a note like you sugested to the FAQ is certainly a good thing. I
also added a paragraph to the JavaDoc of RWT#getResponse() which now reads:

Returns the <code>HttpServletResponse</code> that is mapped to the
currently processed request.

Typical application code <em>never</em> needs to call this method. It is
meant only for service handlers to be able to write output and control
other aspects of the response.
Calling this method from a life cycle request (e.g. in an SWT event
listener) is almost certainly an error.

What do you think?

Regards,
Rüdiger

On 26.07.2011 17:33, Andrew Eisenberg wrote:
> I was thinking something like adding a short section at the end of the
> FAQ for downloading:
> http://wiki.eclipse.org/RAP/FAQ#How_to_provide_download_link.3F
>
> Something like this would have helped a lot:
>
> Please Note
>
> If you want to send a file to download, you must use a ServiceHandler.
> Responding to a standard SWT event will not work. Here's why...