Quantcast

redirect-after-post messes with my StreamResponse

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

redirect-after-post messes with my StreamResponse

hese

I've started a new thread because my older post regarding this issue lead me to think on different lines, than my subject line and i didnt want to re-edit it and mess with all the following threads. (http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-td4759347.html)

So here i go,

I have a form using which a user can upload a file.  The problem is - due to redirect-after-post (thanks to cqasker) instead of my response file being streamed back, the page is getting redirected to reload my page once again.  So when the user clicks submit, instead of receiving the file, the page just reloads.  I checked all other form submits in my application and this seems to the default form submit behavior (a 302 redirect), which isnt a problem in the other pages, because the page reloads with the new data.  But in this case I want it to just stream back my file.

There is no problem with my streaming code.  I confirmed this by doing a GET on that url (in onActivate I am just streaming back the file) and the file downloads.

Any idea how to make the streaming work with a POST operation?







Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

hese

Please help tapestry gurus! Thiago?  Taha?? Anyone?


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

hese

I worked around the problem like this: storing the name of the return file in a property (FLASH, so it stays just for a single refresh), and returning the contents of the file in onActivate, so when the page reloads the file will be streamed back.  But this seems ugly :(  Still looking for a solution....

public Object onActivate(EventContext ec) {
    if(null != resultFile) {
    File file = new File(resultFile);
    if(file.exists()) {
    try {
                                        return new CsvStreamResponse(new FileInputStream(file.getAbsolutePath()), file.getName());
                                } catch (FileNotFoundException e) {
                                        e.printStackTrace();
                                }
    }
    }
   
    return null;
    }


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

Thiago H de Paula Figueiredo
In reply to this post by hese
On Fri, 02 Sep 2011 17:28:30 -0300, hese <[hidden email]> wrote:

> I worked around the problem like this: storing the name of the return  
> file in a property (FLASH, so it stays just for a single refresh), and  
> returning the contents of the file in onActivate, so when the page  
> reloads the file
> will be streamed back.  But this seems ugly :(  Still looking for a
> solution....

It wouldn't be ugly if you had a separate page just for streaming CSV  
files. ;) That's the approach I use most of the time when using  
StreamResponse.

--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

lprimak
Oh you are here Thiago!
How about my Grid/Sessino invalidation session question?

Thanks!


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

hese

(lol...looks like people are clamoring for you Thiago :) )

I dont mind having a separate page for streaming the csv, but i would like to know why my current onSuccess StreamResponse is not streaming back the file.  I checked if it is null or something by printing it out in my onSuccess method before returning it and it prints the contents well.  I see other posts taking about onSuccess returning StreamResponse without problem, and I wonder why mine is not working :(

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

Martin Strand-4
In reply to this post by hese
I am doing something similar for a reporting form and it works as expected  
(in tap 5.2)
1. user fills out a few fields
2. user submits the form
3. onSuccess() returns a StreamResponse with a text file
4. a download dialog pops up in the browser

Although I haven't looked closely into the matter, it sounds like your  
solution ought to work just fine, so there might be something else going  
on.
Is the form perhaps submitted through AJAX?
Are there perhaps other event listeners involved?
What if you create a minimal example to demonstrate the problem?


On Fri, 02 Sep 2011 21:51:05 +0200, hese <[hidden email]> wrote:

>
> I've started a new thread because my older post regarding this issue  
> lead me
> to think on different lines, than my subject line and i didnt want to
> re-edit it and mess with all the following threads.
> (http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-td4759347.html)
>
> So here i go,
>
> I have a form using which a user can upload a file.  The problem is -  
> due to
> redirect-after-post (thanks to cqasker) instead of my response file being
> streamed back, the page is getting redirected to reload my page once  
> again.
> So when the user clicks submit, instead of receiving the file, the page  
> just
> reloads.  I checked all other form submits in my application and this  
> seems
> to the default form submit behavior (a 302 redirect), which isnt a  
> problem
> in the other pages, because the page reloads with the new data.  But in  
> this
> case I want it to just stream back my file.
>
> There is no problem with my streaming code.  I confirmed this by doing a  
> GET
> on that url (in onActivate I am just streaming back the file) and the  
> file
> downloads.
>
> Any idea how to make the streaming work with a POST operation?

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

Thiago H de Paula Figueiredo
In reply to this post by hese
On Fri, 02 Sep 2011 18:03:25 -0300, hese <[hidden email]> wrote:

> (lol...looks like people are clamoring for you Thiago :) )

:P

> I dont mind having a separate page for streaming the csv, but i would  
> like to know why my current onSuccess StreamResponse is not streaming  
> back the
> file.

That's weird.

> I checked if it is null or something by printing it out in my
> onSuccess method before returning it and it prints the contents well.  I  
> see other posts taking about onSuccess returning StreamResponse without  
> problem, and I wonder why mine is not working :(

If you're returning a non-null StreamResponse in onSuccess() and it  
doesn't get streamed, then I'd say it's a Tapestry bug. Which version are  
you using?

--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

Thiago H de Paula Figueiredo
In reply to this post by hese
On Fri, 02 Sep 2011 17:43:42 -0300, Lenny Primak <[hidden email]>  
wrote:

> Oh you are here Thiago!

I'm always here, but I don't know the answers for all questions nor the  
time to investigate them. (I wish I had).

> How about my Grid/Sessino invalidation session question?

I have no idea . . .

--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

lprimak
I figured that was a simple one.  Should not have thought so.
all my questions are complicated, otherwise I would have not asked :)

On Sep 2, 2011, at 5:11 PM, Thiago H. de Paula Figueiredo wrote:

> On Fri, 02 Sep 2011 17:43:42 -0300, Lenny Primak <[hidden email]> wrote:
>
>> Oh you are here Thiago!
>
> I'm always here, but I don't know the answers for all questions nor the time to investigate them. (I wish I had).
>
>> How about my Grid/Sessino invalidation session question?
>
> I have no idea . . .
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

cqasker
In reply to this post by hese
That's pretty smart -- but yeah does seem hacky. I will await the responses from the gurus as I am curious myself.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

hese

@Thiago - I am using Tapestry 5.1.0.5.

@Martin - here is my code.  This code works when called from onActivate() and returns the file.

JAVA

@Log
StreamResponse onSuccess() {

try {

    File tmpFile = File.createTempFile(urlFile.getFileName(), null);
    BufferedWriter br = new BufferedWriter(new FileWriter(tmpFile));
    br.append("something to test\nAnother line to test");
    br.flush();
    br.close();

    return new CsvStreamResponse(new FileInputStream(tmpFile.getAbsolutePath()), "results_file");
 } catch (IOException e) {
     e.printStackTrace();
     addError("An Error occured while streaming back the results.");
 }

   return null;
}

public class CsvStreamResponse implements StreamResponse {
    private InputStream is;
    private String filename;

    public CsvStreamResponse(InputStream is, String filename) {
            this.is = is;
            this.filename = filename;
    }

    public String getContentType() {
            return "text/csv";
    }

    public InputStream getStream() throws IOException {
            return is;
    }

    public void prepareResponse(Response arg0) {
            arg0.setHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
            //also tried setting content length after this - arg0.setContentLength(is.available());
    }
}



TML
<form t:type="form" t:id="analysis">
    <t:upload t:id="urlFile" class="marginRight" validate="required"/>
    <t:submit class="marginRight white button medium" value="${message:button.upload}" t:id="upload"/>
</form>


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

hese

I upgraded to Tap 5.2.6 and now I see an interesting error message...

Notice that just before the exception, I print the contents of the StreamResponse and it does print my test lines.  Any idea?

DEBUG [http-8080-6] UrlAnalysis._$advised$onSubmit(122) | Streaming back the results.
DEBUG [http-8080-6] UrlAnalysis._$advised$onSubmit(123) | something to testAnother line to test
DEBUG [http-8080-6] UrlAnalysis.exit(153) | [ EXIT] onSubmit [com.adsafe.fcc.webapp.components.CsvStreamResponse@5143dd30]
ERROR [http-8080-6] RequestExceptionHandler.handleRequestException(63) | Processing of request failed with uncaught exception: Sanity check - neither a stream response nor a redirect response was generated for this action request.
java.lang.IllegalStateException: Sanity check - neither a stream response nor a redirect response was generated for this action request.
        at org.apache.tapestry5.internal.services.ImmediateActionRenderResponseFilter.handle(ImmediateActionRenderResponseFilter.java:63)
        at $ComponentEventRequestHandler_1322ca8cb11.handle($ComponentEventRequestHandler_1322ca8cb11.java)
        at org.apache.tapestry5.internal.services.AjaxFilter.handle(AjaxFilter.java:42)
        at $ComponentEventRequestHandler_1322ca8cb11.handle($ComponentEventRequestHandler_1322ca8cb11.java)
        at org.apache.tapestry5.upload.internal.services.UploadExceptionFilter.handle(UploadExceptionFilter.java:75)
        at $ComponentEventRequestHandler_1322ca8cb11.handle($ComponentEventRequestHandler_1322ca8cb11.java)
        at org.apache.tapestry5.services.TapestryModule$39.handle(TapestryModule.java:2583)
        at $ComponentEventRequestHandler_1322ca8cb11.handle($ComponentEventRequestHandler_1322ca8cb11.java)
        at $ComponentEventRequestHandler_1322ca8ca0c.handle($ComponentEventRequestHandler_1322ca8ca0c.java)
        at org.apache.tapestry5.internal.services.ComponentRequestHandlerTerminator.handleComponentEvent(ComponentRequestHandlerTerminator.java:43)
        at org.apache.tapestry5.services.InitializeActivePageName.handleComponentEvent(InitializeActivePageName.java:39)
        at $ComponentRequestHandler_1322ca8ca0e.handleComponentEvent($ComponentRequestHandler_1322ca8ca0e.java)
        at $ComponentRequestHandler_1322ca8c9f3.handleComponentEvent($ComponentRequestHandler_1322ca8c9f3.java)
        at org.apache.tapestry5.internal.services.ComponentEventDispatcher.dispatch(ComponentEventDispatcher.java:46)
        at $Dispatcher_1322ca8c9f5.dispatch($Dispatcher_1322ca8c9f5.java)
        at $Dispatcher_1322ca8c9ee.dispatch($Dispatcher_1322ca8c9ee.java)
        at org.apache.tapestry5.services.TapestryModule$RequestHandlerTerminator.service(TapestryModule.java:321)
        at org.apache.tapestry5.internal.services.RequestErrorFilter.service(RequestErrorFilter.java:26)
        at $RequestHandler_1322ca8c9ef.service($RequestHandler_1322ca8c9ef.java)
        at org.apache.tapestry5.services.TapestryModule$4.service(TapestryModule.java:984)
        at $RequestHandler_1322ca8c9ef.service($RequestHandler_1322ca8c9ef.java)
        at org.apache.tapestry5.services.TapestryModule$3.service(TapestryModule.java:974)
        at $RequestHandler_1322ca8c9ef.service($RequestHandler_1322ca8c9ef.java)
        at org.apache.tapestry5.internal.services.StaticFilesFilter.service(StaticFilesFilter.java:90)
        at $RequestHandler_1322ca8c9ef.service($RequestHandler_1322ca8c9ef.java)
        at org.apache.tapestry5.internal.services.CheckForUpdatesFilter$2.invoke(CheckForUpdatesFilter.java:90)
        at org.apache.tapestry5.internal.services.CheckForUpdatesFilter$2.invoke(CheckForUpdatesFilter.java:80)
        at org.apache.tapestry5.ioc.internal.util.ConcurrentBarrier.withRead(ConcurrentBarrier.java:85)
        at org.apache.tapestry5.internal.services.CheckForUpdatesFilter.service(CheckForUpdatesFilter.java:103)
        at $RequestHandler_1322ca8c9ef.service($RequestHandler_1322ca8c9ef.java)
        at $RequestHandler_1322ca8c9e7.service($RequestHandler_1322ca8c9e7.java)
        at org.apache.tapestry5.services.TapestryModule$HttpServletRequestHandlerTerminator.service(TapestryModule.java:272)
        at org.apache.tapestry5.upload.internal.services.MultipartServletRequestFilter.service(MultipartServletRequestFilter.java:44)
        at $HttpServletRequestHandler_1322ca8c9e9.service($HttpServletRequestHandler_1322ca8c9e9.java)
        at org.apache.tapestry5.internal.services.IgnoredPathsFilter.service(IgnoredPathsFilter.java:62)
        at $HttpServletRequestFilter_1322ca8c9e6.service($HttpServletRequestFilter_1322ca8c9e6.java)
        at $HttpServletRequestHandler_1322ca8c9e9.service($HttpServletRequestHandler_1322ca8c9e9.java)
        at org.apache.tapestry5.services.TapestryModule$2.service(TapestryModule.java:928)
        at $HttpServletRequestHandler_1322ca8c9e9.service($HttpServletRequestHandler_1322ca8c9e9.java)



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

Taha Hafeez
In reply to this post by hese
Hi

I just copied your code and it works for me. I have tried it on 5.2.6.

Are you sure your onSubmit action is getting called. Can you put a
break point or simply a log message so see if it is getting called

On Sat, Sep 3, 2011 at 4:04 AM, hese <[hidden email]> wrote:

>
> @Thiago - I am using Tapestry 5.1.0.5.
>
> @Martin - here is my code.  This code works when called from onActivate()
> and returns the file.
>
> JAVA
>
> @Log
> StreamResponse onSuccess() {
>
> try {
>
>    File tmpFile = File.createTempFile(urlFile.getFileName(), null);
>    BufferedWriter br = new BufferedWriter(new FileWriter(tmpFile));
>    br.append("something to test\nAnother line to test");
>    br.flush();
>    br.close();
>
>    return new CsvStreamResponse(new
> FileInputStream(tmpFile.getAbsolutePath()), "results_file");
>  } catch (IOException e) {
>     e.printStackTrace();
>     addError("An Error occured while streaming back the results.");
>  }
>
>   return null;
> }
>
> public class CsvStreamResponse implements StreamResponse {
>    private InputStream is;
>    private String filename;
>
>    public CsvStreamResponse(InputStream is, String filename) {
>            this.is = is;
>            this.filename = filename;
>    }
>
>    public String getContentType() {
>            return "text/csv";
>    }
>
>    public InputStream getStream() throws IOException {
>            return is;
>    }
>
>    public void prepareResponse(Response arg0) {
>            arg0.setHeader("Content-Disposition", "attachment; filename=" +
> filename + ".csv");
>            //also tried setting content length after this -
> arg0.setContentLength(is.available());
>    }
> }
>
>
>
> TML
> <form t:type="form" t:id="analysis">
>    <t:upload t:id="urlFile" class="marginRight" validate="required"/>
>    <t:submit class="marginRight white button medium"
> value="${message:button.upload}" t:id="upload"/>
> </form>
>
>
>
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/redirect-after-post-messes-with-my-StreamResponse-tp4763647p4764159.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>



--
Regards

Taha Hafeez Siddiqi (tawus)
http://tawus.wordpress.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

hese
In reply to this post by hese

Hey Taha, there you are!  

Yes, it is getting called.  The log message "Streaming back the results." is from within onSubmit.  Also I debugged it and saw the method is being called.

It works for you?  Then i guess it is something to do with my env then..some jars not playing well together or something like that.



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

Thiago H de Paula Figueiredo
In reply to this post by hese
On Fri, 02 Sep 2011 21:25:20 -0300, hese <[hidden email]> wrote:

> DEBUG [http-8080-6] UrlAnalysis._$advised$onSubmit(122)

What is this UrlAnalysis thing?

--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

Martin Strand-4
In reply to this post by hese
The class name of that StreamResponse suggests it is in the "components"  
package, where it will be transformed by Tapestry's classloading magic and  
treated as a "component".
You need to move that class to another package to be able to use it as  
anything else (avoid packages named "pages", "components", "base" and  
"mixins")


On Sat, 03 Sep 2011 02:25:20 +0200, hese <[hidden email]> wrote:

>
> I upgraded to Tap 5.2.6 and now I see an interesting error message...
>
> Notice that just before the exception, I print the contents of the
> StreamResponse and it does print my test lines.  Any idea?
>
> DEBUG [http-8080-6] UrlAnalysis._$advised$onSubmit(122) | Streaming back  
> the results.
> DEBUG [http-8080-6] UrlAnalysis._$advised$onSubmit(123) | something to  
> testAnother line to test
> DEBUG [http-8080-6] UrlAnalysis.exit(153) | [ EXIT] onSubmit  
> [com.adsafe.fcc.webapp.components.CsvStreamResponse@5143dd30]

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

hese
In reply to this post by Thiago H de Paula Figueiredo

We do content rating of web pages.  So if you give a domain/url, we give you a safety rating (0-1000, 0 being low safety & 1000 being high safety) for that page in different categories, like Adult, Alcohol, Drugs, Torrents, Offensive Language, etc.  This is used by ad agencies and networks
- in realtime to prevent ads from landing on an offensive site (like say porn), and to protect an advertisers "brand safety"....so blocking ads by high risk content, country etc.
- post campaign metrics, like how many high risk pages their publishers served, the position of the ads served by each publisher, which countries their ads were served etc.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

hese
In reply to this post by Martin Strand-4
@Martin - wow, thats a great thought.  Let me try it now.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: redirect-after-post messes with my StreamResponse

hese

nope, i moved the class to another package, (util) and tried and that didn't work too.  Same error.  I guess I'll just go with returning the file during the reload.

Thank you all for your help!!  Really appreciate it.

12
Loading...