You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by MoritzGilsdorf <ma...@moritz-gilsdorf.de> on 2008/02/21 14:17:40 UTC

Stream Text File with StreamResponse

Hello,

I'm trying to stream a text file I created with Velocity very similar to the
way it is done 
http://wiki.apache.org/tapestry/Tapestry5HowToCreateADynamicPDF here  but it
doesn't work. The text is stored in a OutputStreamWriter object. 
Is it possible to stream something else than binary data with tapestry?

Thanks,

Moritz
-- 
View this message in context: http://www.nabble.com/Stream-Text-File-with-StreamResponse-tp15611200p15611200.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: T5: Stream Text File with StreamResponse

Posted by MoritzGilsdorf <ma...@moritz-gilsdorf.de>.
Thanks a lot! There's just a little typo:

OutputStreamWriter osw = new OutputStreamWriter(out);


Davor Hrg wrote:
> 
>         ByteArrayOutputStream out = new ByteArrayOutputStream();
>         OutputStreamWriter osw = new OutputStreamWriter(System.out);
>         velocityContext.put("desc", this.description);
>         template.merge(velocityContext, osw);
>         osw.flush();
>         osw.close();
>         return new ByteArrayInputStream(out.toByteArray());
> 
> 
> 
> On Thu, Feb 21, 2008 at 2:55 PM, MoritzGilsdorf <ma...@moritz-gilsdorf.de>
> wrote:
>>
>>  The thing is that I don't know how to get my OutputStreamWriter into
>>  InputStream so that StreamResponse is able to stream it to the user.
>> This is
>>  probably a very basic problem but I'm not very experienced with that.
>>  Right now there is nothing streaming but writing to System.out. But I
>> want
>>  to do something like this:
>>
>>  StreamResponse onSubmitFromDescription() {
>>         InputMaker iMaker = new InputMaker(description);
>>         InputStream is = iMaker.makeDescription();
>>         return TextStreamResponse(is,"Description");
>>  }
>>
>>  Then, this method should return, of course, an InputStream:
>>
>>  public void makeDescription(){
>>           try {
>>                   configure(engine);
>>           } catch (Exception e) {}
>>           try {
>>                   Template template =
>>  Velocity.getTemplate("src/main/resources/templates/description.vm");
>>                   VelocityContext velocityContext = new
>> VelocityContext();
>>                   OutputStreamWriter osw = new
>>  OutputStreamWriter(System.out);
>>                   velocityContext.put("desc", this.description);
>>                   template.merge(velocityContext, osw);
>>                   osw.flush();
>>                   osw.close();
>>           } catch (Exception e) {}
>>  }
>>
>>  And, to be complete, my TextStreamResponse Class:
>>
>>  public class TextStreamResponse implements StreamResponse {
>>         private InputStream is;
>>         private String filename = "default";
>>
>>         public TextStreamResponse(InputStream is, String filename){
>>                 this.is = is;
>>                 this.filename = filename;
>>         }
>>         public String getContentType() {
>>                 return "text/plain";
>>         }
>>         public InputStream getStream() throws IOException {
>>                 return is;
>>         }
>>         public void prepareResponse(Response arg0) {
>>                 arg0.setHeader("Content-Disposition", "attachment;
>> filename=" + filename +
>>  ".txt");
>>         }
>>  }
>>
>>  Thanks a lot for helping me with that!
>>
>>  Moritz
>>
>>
>>
>>
>>  Christian Gorbach wrote:
>>  >
>>  > sure. if you post your streaming snippet we can take a look...
>>  >> Hello,
>>  >>
>>  >> I'm trying to stream a text file I created with Velocity very similar
>> to
>>  >> the
>>  >> way it is done
>>  >> http://wiki.apache.org/tapestry/Tapestry5HowToCreateADynamicPDF here 
>> but
>>  >> it
>>  >> doesn't work. The text is stored in a OutputStreamWriter object.
>>  >> Is it possible to stream something else than binary data with
>> tapestry?
>>  >>
>>  >> Thanks,
>>  >>
>>  >> Moritz
>>  >>
>>  >
>>  >
>>  > ---------------------------------------------------------------------
>>  > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>  > For additional commands, e-mail: users-help@tapestry.apache.org
>>  >
>>  >
>>  >
>>
>>  --
>>  View this message in context:
>> http://www.nabble.com/T5%3A-Stream-Text-File-with-StreamResponse-tp15611200p15611835.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
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/T5%3A-Stream-Text-File-with-StreamResponse-tp15611200p15612489.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: T5: Stream Text File with StreamResponse

Posted by Davor Hrg <hr...@gmail.com>.
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(System.out);
        velocityContext.put("desc", this.description);
        template.merge(velocityContext, osw);
        osw.flush();
        osw.close();
        return new ByteArrayInputStream(out.toByteArray());



On Thu, Feb 21, 2008 at 2:55 PM, MoritzGilsdorf <ma...@moritz-gilsdorf.de> wrote:
>
>  The thing is that I don't know how to get my OutputStreamWriter into
>  InputStream so that StreamResponse is able to stream it to the user. This is
>  probably a very basic problem but I'm not very experienced with that.
>  Right now there is nothing streaming but writing to System.out. But I want
>  to do something like this:
>
>  StreamResponse onSubmitFromDescription() {
>         InputMaker iMaker = new InputMaker(description);
>         InputStream is = iMaker.makeDescription();
>         return TextStreamResponse(is,"Description");
>  }
>
>  Then, this method should return, of course, an InputStream:
>
>  public void makeDescription(){
>           try {
>                   configure(engine);
>           } catch (Exception e) {}
>           try {
>                   Template template =
>  Velocity.getTemplate("src/main/resources/templates/description.vm");
>                   VelocityContext velocityContext = new VelocityContext();
>                   OutputStreamWriter osw = new
>  OutputStreamWriter(System.out);
>                   velocityContext.put("desc", this.description);
>                   template.merge(velocityContext, osw);
>                   osw.flush();
>                   osw.close();
>           } catch (Exception e) {}
>  }
>
>  And, to be complete, my TextStreamResponse Class:
>
>  public class TextStreamResponse implements StreamResponse {
>         private InputStream is;
>         private String filename = "default";
>
>         public TextStreamResponse(InputStream is, String filename){
>                 this.is = is;
>                 this.filename = filename;
>         }
>         public String getContentType() {
>                 return "text/plain";
>         }
>         public InputStream getStream() throws IOException {
>                 return is;
>         }
>         public void prepareResponse(Response arg0) {
>                 arg0.setHeader("Content-Disposition", "attachment; filename=" + filename +
>  ".txt");
>         }
>  }
>
>  Thanks a lot for helping me with that!
>
>  Moritz
>
>
>
>
>  Christian Gorbach wrote:
>  >
>  > sure. if you post your streaming snippet we can take a look...
>  >> Hello,
>  >>
>  >> I'm trying to stream a text file I created with Velocity very similar to
>  >> the
>  >> way it is done
>  >> http://wiki.apache.org/tapestry/Tapestry5HowToCreateADynamicPDF here  but
>  >> it
>  >> doesn't work. The text is stored in a OutputStreamWriter object.
>  >> Is it possible to stream something else than binary data with tapestry?
>  >>
>  >> Thanks,
>  >>
>  >> Moritz
>  >>
>  >
>  >
>  > ---------------------------------------------------------------------
>  > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>  > For additional commands, e-mail: users-help@tapestry.apache.org
>  >
>  >
>  >
>
>  --
>  View this message in context: http://www.nabble.com/T5%3A-Stream-Text-File-with-StreamResponse-tp15611200p15611835.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
>
>

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


Re: T5: Stream Text File with StreamResponse

Posted by Christian Gorbach <go...@gmx.at>.
ok,
what you will need is a datapump which converts an output stream to an 
input stream.
See: http://ostermiller.org/convert_java_outputstream_inputstream.html
Method 1 is the simplest: let velocity write to abytearrayoutputstream 
and use the byte array to construct an input stream object. voila.
regards
c)hristian


> The thing is that I don't know how to get my OutputStreamWriter into
> InputStream so that StreamResponse is able to stream it to the user. This is
> probably a very basic problem but I'm not very experienced with that.
> Right now there is nothing streaming but writing to System.out. But I want
> to do something like this:
>
> StreamResponse onSubmitFromDescription() {
> 	InputMaker iMaker = new InputMaker(description);
> 	InputStream is = iMaker.makeDescription();
> 	return TextStreamResponse(is,"Description");
> }
>
> Then, this method should return, of course, an InputStream:
>
> public void makeDescription(){
>           try {
>                   configure(engine);                     
>           } catch (Exception e) {}
>           try {
>                   Template template =
> Velocity.getTemplate("src/main/resources/templates/description.vm");
>                   VelocityContext velocityContext = new VelocityContext();
>                   OutputStreamWriter osw = new
> OutputStreamWriter(System.out);
>                   velocityContext.put("desc", this.description);
>                   template.merge(velocityContext, osw);                   
>                   osw.flush();
>                   osw.close();
>           } catch (Exception e) {}               
> }
>
> And, to be complete, my TextStreamResponse Class:
>
> public class TextStreamResponse implements StreamResponse {
> 	private InputStream is;
> 	private String filename = "default";
> 	
> 	public TextStreamResponse(InputStream is, String filename){
> 		this.is = is;
> 		this.filename = filename;
> 	}
> 	public String getContentType() {
> 		return "text/plain";
> 	}
> 	public InputStream getStream() throws IOException {
> 		return is;
> 	}
> 	public void prepareResponse(Response arg0) {
> 		arg0.setHeader("Content-Disposition", "attachment; filename=" + filename +
> ".txt");
> 	}
> }
>
> Thanks a lot for helping me with that!
>
> Moritz
>
>
> Christian Gorbach wrote:
>   
>> sure. if you post your streaming snippet we can take a look...
>>     
>>> Hello,
>>>
>>> I'm trying to stream a text file I created with Velocity very similar to
>>> the
>>> way it is done 
>>> http://wiki.apache.org/tapestry/Tapestry5HowToCreateADynamicPDF here  but
>>> it
>>> doesn't work. The text is stored in a OutputStreamWriter object. 
>>> Is it possible to stream something else than binary data with tapestry?
>>>
>>> Thanks,
>>>
>>> Moritz
>>>   
>>>       
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>>
>>     
>
>   


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


Re: T5: Stream Text File with StreamResponse

Posted by MoritzGilsdorf <ma...@moritz-gilsdorf.de>.
The thing is that I don't know how to get my OutputStreamWriter into
InputStream so that StreamResponse is able to stream it to the user. This is
probably a very basic problem but I'm not very experienced with that.
Right now there is nothing streaming but writing to System.out. But I want
to do something like this:

StreamResponse onSubmitFromDescription() {
	InputMaker iMaker = new InputMaker(description);
	InputStream is = iMaker.makeDescription();
	return TextStreamResponse(is,"Description");
}

Then, this method should return, of course, an InputStream:

public void makeDescription(){
          try {
                  configure(engine);                     
          } catch (Exception e) {}
          try {
                  Template template =
Velocity.getTemplate("src/main/resources/templates/description.vm");
                  VelocityContext velocityContext = new VelocityContext();
                  OutputStreamWriter osw = new
OutputStreamWriter(System.out);
                  velocityContext.put("desc", this.description);
                  template.merge(velocityContext, osw);                   
                  osw.flush();
                  osw.close();
          } catch (Exception e) {}               
}

And, to be complete, my TextStreamResponse Class:

public class TextStreamResponse implements StreamResponse {
	private InputStream is;
	private String filename = "default";
	
	public TextStreamResponse(InputStream is, String filename){
		this.is = is;
		this.filename = filename;
	}
	public String getContentType() {
		return "text/plain";
	}
	public InputStream getStream() throws IOException {
		return is;
	}
	public void prepareResponse(Response arg0) {
		arg0.setHeader("Content-Disposition", "attachment; filename=" + filename +
".txt");
	}
}

Thanks a lot for helping me with that!

Moritz


Christian Gorbach wrote:
> 
> sure. if you post your streaming snippet we can take a look...
>> Hello,
>>
>> I'm trying to stream a text file I created with Velocity very similar to
>> the
>> way it is done 
>> http://wiki.apache.org/tapestry/Tapestry5HowToCreateADynamicPDF here  but
>> it
>> doesn't work. The text is stored in a OutputStreamWriter object. 
>> Is it possible to stream something else than binary data with tapestry?
>>
>> Thanks,
>>
>> Moritz
>>   
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/T5%3A-Stream-Text-File-with-StreamResponse-tp15611200p15611835.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: T5: Stream Text File with StreamResponse

Posted by Christian Gorbach <go...@gmx.at>.
sure. if you post your streaming snippet we can take a look...
> Hello,
>
> I'm trying to stream a text file I created with Velocity very similar to the
> way it is done 
> http://wiki.apache.org/tapestry/Tapestry5HowToCreateADynamicPDF here  but it
> doesn't work. The text is stored in a OutputStreamWriter object. 
> Is it possible to stream something else than binary data with tapestry?
>
> Thanks,
>
> Moritz
>   


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