You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by hese <10...@gmail.com> on 2011/09/01 19:45:03 UTC

StreamResponse onSuccess does not stream back data

Hi,

I have a form with a file upload control.  When the user clicks submit, the
file is posted. I read the file, process it and return results back in a
file.  I am facing a problem streaming back the results.  No file is getting
streamed back, instead the page reloads again (I see onActivate of my class
getting called and the page going back to initial state). When i see in
firebug, the response for the post call is - "Reload the page to get source
for: http://localhost:8080/urlanalysis.urlanalysis"

Here is my code:

(cut it down just to the main part)

    @Log
    StreamResponse onSuccess() {
    	
    		try {
			
			// create a unique temp file name
			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");
	}
}



--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4759347.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by hese <10...@gmail.com>.
These are the response headers of the POST...seeing 302 Moved status.  Any
idea why this is happening?


HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename=test1.txt_scored.csv
Location: http://localhost:8080/urlanalysis
Content-Type: text/csv;charset=UTF-8
Content-Length: 0
Date: Thu, 01 Sep 2011 18:02:21 GMT



--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4759421.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by cqasker <cq...@yahoo.com>.
sorry, had some weird formatting issue, here is whats between the li:

                        csv


--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4760322.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by hese <10...@gmail.com>.
Thanks, I see it now.  Yeah, I think it is not my code, because i tried
using the LinkSubmit control, exactly as you did and I am still getting the
same 302 error.

Ok, i'll try to debug my post headers.  thanks for the responses!



--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4763412.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by cqasker <cq...@yahoo.com>.
Doh I guess my  email program doesn't  looks like it doesn't like this tag:

<li>
  --a t:id="link"--CSV--/a--
</li>

just replace the "--" with > and < as appropriate.

--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4763368.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by cqasker <cq...@yahoo.com>.
Sorry hese:
The missing tml is just simply:
<li>
   CSV 
</li>

Yeah I don't think it's your code -- i just prefer annotations over
convention. You could try something like
https://addons.mozilla.org/en-US/firefox/addon/tamper-data/ for your browser

you will be able to follow the each request/response -- perhaps its
something to do with redirect-after-post when your link is clicked.


--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4763360.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by hese <10...@gmail.com>.
I just see the string "CSV" within <li> no link or anything else within it.

The different in the java class between yours and mine is you've used
@OnEvent and captured submit, and I've used onSuccess()...i tried changing
it to your style, but still i get the 302 Moved Temporarily error.

Not able to see your tml... would appreciate if you can post it again.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4763316.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by cqasker <cq...@yahoo.com>.
CSV


Sorry had some formatting issues, this is the tml missing in my [revious
post. Just a simple link.

--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4760327.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by cqasker <cq...@yahoo.com>.
Sure:

<li>
                  
                      
                        CSV
                      
                                   
</li>

   @SuppressWarnings("unused")
   @Component(parameters = {"event=clicky", "defer=false"})
   private LinkSubmit link;
   
   @OnEvent(value="submit")
   Object doExport() throws Exception
{
       ByteArrayOutputStream baos = //get the csv
       return new CsvStreamResponse(baos, other params);
}

      private CsvStreamResponse(ByteArrayOutputStream baos, other params)
      {
         this.inputStream = new ByteArrayInputStream(os.toByteArray());
         //other initiialization
      }

      @Override
      public void prepareResponse(Response response)
      {
         try
         {
            response.setHeader("Content-Disposition", "attachment;
filename=" + this.fname + ".csv");
            response.setContentLength(this.inputStream.available());
         } 
         catch (IOException e)
         {
         }
      }

      @Override
      public String getContentType()
      {return "application/csv";}

      @Override
      public InputStream getStream() throws IOException
      {return this.inputStream;}

--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4760318.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by hese <10...@gmail.com>.
Thanks, I tried that and now the content length in the return headers shows
the file size, but still I get the '302 Moved Temporarily' error :(

Can u show me a sample of your tml file? Wonder if i am doing anything wrong
there.

Here is mine:
<form t:type="form" t:id="analysis">
	 	
Upload URL File

	 	<div>(Please upload only plain text files, with one ULR per line.) </div>
		<div class="formRow"> 
			<label class='stacked strong'>
				
					(.csv,.txt)
				
				
			</label> 
			<div id="file-uploader"> 
				<t:errors/>
	            <t:upload t:id="urlFile" class="marginRight"
validate="required"/>
	            <t:submit class="marginRight white button medium"
value="${message:button.upload}" t:id="upload"/>
	            <t:eventlink t:id="cancel">
	                <input type="button" class="white button medium"
value="${message:button.cancel}"/>
	            </t:eventlink>
	        </div>
	    </div>
	</form>



--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4760154.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: StreamResponse onSuccess does not stream back data

Posted by cqasker <cq...@yahoo.com>.
I don't know about the weird response code but...your stream response code
looks pretty much the same as mine except I set the content length ie:


arg0.setContentLength(getStream.available());

in your prepareResponse method

--
View this message in context: http://tapestry.1045711.n5.nabble.com/StreamResponse-onSuccess-does-not-stream-back-data-tp4759347p4760037.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org