Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » IllegalStateException when calling response.getOutputStream
IllegalStateException when calling response.getOutputStream [message #701414] Mon, 25 July 2011 04:23 Go to next message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
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.

Here is a snippet of the code I am running:

        Button doit = new Button(parent, SWT.PUSH);
        doit.setText("Execute command");
        doit.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent e) {
                
                UICallBack.runNonUIThreadWithFakeContext(text.getDisplay(), new Runnable() {
                    
                    public void run() {
                        HttpServletResponse response = RWT.getResponse();
                        try {
                            response.getOutputStream();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
           }
      }


I get the illegal state exception in the run method. Does anyone have any ideas how to avoid that?
Re: IllegalStateException when calling response.getOutputStream [message #701504 is a reply to message #701414] Mon, 25 July 2011 07:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

Andrew,

the exception is thrown because the getWriter() method has been called
on this response (see the JavaDoc of getOutputStream).

The UICallBack#runNonUIThreadWithFakeContext() is unnecessary as your
code is already executed on the UI thread.

Please see the FAQ [1] for hints on how to download files.

HTh
Rüdiger

[1] http://wiki.eclipse.org/RAP/FAQ#How_to_provide_download_link.3F

On 25.07.2011 06:24, Andrew Eisenberg wrote:
> 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.
>
> Here is a snippet of the code I am running:
>
>
> Button doit = new Button(parent, SWT.PUSH);
> doit.setText("Execute command");
> doit.addSelectionListener(new SelectionListener() {
> public void widgetSelected(SelectionEvent e) {
> UICallBack.runNonUIThreadWithFakeContext(text.getDisplay(), new
> Runnable() {
> public void run() {
> HttpServletResponse response = RWT.getResponse();
> try {
> response.getOutputStream();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> });
> }
> }
>
>
> I get the illegal state exception in the run method. Does anyone have
> any ideas how to avoid that?
>

--
Rüdiger Herrmann

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: IllegalStateException when calling response.getOutputStream [message #701796 is a reply to message #701504] Mon, 25 July 2011 15:47 Go to previous messageGo to next message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
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?
Re: IllegalStateException when calling response.getOutputStream [message #701807 is a reply to message #701796] Mon, 25 July 2011 16:02 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2426
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
Hi Andrew,
did you follow the FAQ entry that Ruediger pointed [1]? Did you create
your download service handler? From your explanation it seems that you
try to push your bytes to the response of the Ajax request.
[1] http://wiki.eclipse.org/RAP/FAQ#How_to_provide_download_link.3F
HTH,
Ivan

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?
Re: IllegalStateException when calling response.getOutputStream [message #701854 is a reply to message #701807] Mon, 25 July 2011 17:22 Go to previous messageGo to next message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
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?
Re: IllegalStateException when calling response.getOutputStream [message #701889 is a reply to message #701854] Mon, 25 July 2011 17:57 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2426
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
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?
Re: IllegalStateException when calling response.getOutputStream [message #701891 is a reply to message #701889] Mon, 25 July 2011 18:04 Go to previous messageGo to next message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
Good to know. It would be nice if this were made clear in the FAQ somewhere that service handlers must be used. Thanks.

[Updated on: Mon, 25 July 2011 18:05]

Report message to a moderator

Re: IllegalStateException when calling response.getOutputStream [message #701917 is a reply to message #701891] Mon, 25 July 2011 18:13 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2426
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
Andrew,
that's why the FAQ entry is created.
Best,
Ivan

On 7/25/2011 9:04 PM, Andrew Eisenberg wrote:
> Good to know. It would be nice if this were made clear somewhere that
> service handlers must be used. Thanks.
Re: IllegalStateException when calling response.getOutputStream [message #702618 is a reply to message #701891] Tue, 26 July 2011 15:24 Go to previous messageGo to next message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1313
Registered: July 2009
Senior Member

Hi Andrew,

> Good to know. It would be nice if this were made clear somewhere that
> service handlers must be used. Thanks.

I'm sorry that it was difficult for you to figure this out. Where would
you have expected this information? We'll happy to enhance our
documentation if you have an idea what exactly is missing.

Best regards, Ralf

--
Ralf Sternberg

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: IllegalStateException when calling response.getOutputStream [message #702622 is a reply to message #702618] Tue, 26 July 2011 15:33 Go to previous messageGo to next message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
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...
Re: IllegalStateException when calling response.getOutputStream [message #703412 is a reply to message #702622] Wed, 27 July 2011 14:16 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

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...

--
Rüdiger Herrmann

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: IllegalStateException when calling response.getOutputStream [message #703430 is a reply to message #703412] Wed, 27 July 2011 15:04 Go to previous message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
That's great. Much clearer to me now.
Previous Topic:Draw2D fill arc in CHORD
Next Topic:RAP1.4 + BIRT3.7 - java.lang.NoClassDefFoundError: org/eclipse/core/runtime/IPlatformRunnable
Goto Forum:
  


Current Time: Fri Mar 29 07:00:58 GMT 2024

Powered by FUDForum. Page generated in 0.04486 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top