You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Major Péter <ma...@sch.bme.hu> on 2009/08/07 21:06:04 UTC
How to use DynamicWebResource?
Hi all,
I have a page, and I need to generate a PDF file: after the user presses
a button, then the downloading is being started.
For the PDF-generation I would like to use iText, but I have no idea,
how to create a link to the downloadable content.
I tried to follow UploadDownload wiki page, but got lost by spring
beans. So far I created a DynamicWebResource subclass:
public class PdfFileResource extends DynamicWebResource {
@Override
protected ResourceState getResourceState() {
return new PdfFileResourceState();
}
class PdfFileResourceState extends ResourceState {
@Override
public String getContentType() {
return "application/pdf";
}
@Override
public byte[] getData() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
I guess in the PdfFileResource's constructor I need to set the data for
ResourceState, am I right?
The wiki sais also, that in Application#init I need to add the new
SharedResource, so I did that too.
but after that how to use it?
I guessing the follows:
In the Button#onSubmit I should create first the outputstream
(bytearrayoutputstream) for the pdf, after that I should create a new
pdfFileResource with the outputstream.
- But after that, how do I get URL for the newly created resource and
how do I redirect to it?
- While creating the URL for the resource, does it matters which
urlcodingstrategy am I using? (I'm currently using BPRTUCS).
- what does the mountSharedResource do at all? (Is it for only static
resources?)
Any help would be appreciated.
//I looked the archives first, but am still confused, please help
Best Regards,
Peter
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Martin Makundi <ma...@koodaripalvelut.com>.
> Sorry for the newbie questions..
I'm a newbie too, welcome to the club :)
> /*
> But if I do the follow in a Page's constructor:
> getRequestCycle().setRequestTarget(...);
> return;
> And the page is itself bookmarkable, maybe this would be working.(?)
> */
Exactly, this should work.
**
Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Major Péter <ma...@sch.bme.hu>.
I was meaning about bookmarkable, that the resourcetarget has an own
link like:
path/to/dynamic/resource.pdf
Sorry for the newbie questions..
/*
But if I do the follow in a Page's constructor:
getRequestCycle().setRequestTarget(...);
return;
And the page is itself bookmarkable, maybe this would be working.(?)
*/
Peter
2009-08-08 13:58 keltezéssel, Martin Makundi írta:
> You can have it on a bookmarkable page.. not?
>
> **
> Martin
>
> 2009/8/8 Major Péter<ma...@sch.bme.hu>:
>> I was only guessing, that the DWR would be the solution to my problem (the
>> idea was from uploaddownload wiki page).
>>
>> The link you provided is exactly the same solution which I linked before
>> (It's just on a different archive-browser.) :)
>> //And that one is working perfectly for me.
>>
>> So if I have a custom DWR, do I gonna need a ResourceStreamRequestTarget to
>> making it downloadable (or bookmarkable)?
>> And if I have a RequestTarget how can I make it bookmarkable?
>>
>> Thanks
>>
>> Peter
>>
>> 2009-08-08 13:40 keltezéssel, Martin Makundi írta:
>>>> - After creating a DWR, how do I get an URL for the newly created
>>>> resource
>>>> and how do I redirect to it?
>>> I would assume DWR is just a resource. It does not have an URL on its
>>> own. HEre is an example where it is attached to a link:
>>>
>>> http://mail-archives.apache.org/mod_mbox/wicket-users/200708.mbox/%3C46D80834.1040209@jayway.dk%3E
>>>
>>> Use for example ResourceStreamRequestTarget
>>>
>>>> - While creating the URL for the resource, does it matters which
>>>> urlcodingstrategy am I using? (I'm currently using BPRTUCS).
>>> Don't know how/why it would matter.
>>>
>>>> - what does the mountSharedResource do at all? (Is it for only static
>>>> resources?)
>>> Don't know that either. Hopefully someone else can help you with these.
>>>
>>> **
>>> Martin
>>>
>>>
>>>> 2009-08-08 13:23 keltezéssel, Martin Makundi írta:
>>>>>> I'm using iText for generating Pdf, this is only a PoC code, Later I
>>>>>> will
>>>>>> add a business object to the class, so I can provide custom datas for
>>>>>> the
>>>>>> pdf generating part. You can also see, that this code will generate the
>>>>>> pdf
>>>>>> on-demand.
>>>>> Can you elaborate the problem you still have?
>>>>>
>>>>> **
>>>>> Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Martin Makundi <ma...@koodaripalvelut.com>.
You can have it on a bookmarkable page.. not?
**
Martin
2009/8/8 Major Péter <ma...@sch.bme.hu>:
> I was only guessing, that the DWR would be the solution to my problem (the
> idea was from uploaddownload wiki page).
>
> The link you provided is exactly the same solution which I linked before
> (It's just on a different archive-browser.) :)
> //And that one is working perfectly for me.
>
> So if I have a custom DWR, do I gonna need a ResourceStreamRequestTarget to
> making it downloadable (or bookmarkable)?
> And if I have a RequestTarget how can I make it bookmarkable?
>
> Thanks
>
> Peter
>
> 2009-08-08 13:40 keltezéssel, Martin Makundi írta:
>>>
>>> - After creating a DWR, how do I get an URL for the newly created
>>> resource
>>> and how do I redirect to it?
>>
>> I would assume DWR is just a resource. It does not have an URL on its
>> own. HEre is an example where it is attached to a link:
>>
>> http://mail-archives.apache.org/mod_mbox/wicket-users/200708.mbox/%3C46D80834.1040209@jayway.dk%3E
>>
>> Use for example ResourceStreamRequestTarget
>>
>>> - While creating the URL for the resource, does it matters which
>>> urlcodingstrategy am I using? (I'm currently using BPRTUCS).
>>
>> Don't know how/why it would matter.
>>
>>> - what does the mountSharedResource do at all? (Is it for only static
>>> resources?)
>>
>> Don't know that either. Hopefully someone else can help you with these.
>>
>> **
>> Martin
>>
>>
>>> 2009-08-08 13:23 keltezéssel, Martin Makundi írta:
>>>>>
>>>>> I'm using iText for generating Pdf, this is only a PoC code, Later I
>>>>> will
>>>>> add a business object to the class, so I can provide custom datas for
>>>>> the
>>>>> pdf generating part. You can also see, that this code will generate the
>>>>> pdf
>>>>> on-demand.
>>>>
>>>> Can you elaborate the problem you still have?
>>>>
>>>> **
>>>> Martin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Major Péter <ma...@sch.bme.hu>.
Now I have a basic understanding about Resources.
Thanks guys for helping me.
Regards,
Peter
2009-08-08 15:50 keltezéssel, Bas Gooren írta:
> The idea is as follows, for as far as I know:
> - you either mount your resource (dynamic, static) as a shared resource
> (1), or
> - you set it as the request target when the user clicks a link (2)
>
> Now let me explain both:
>
> 1) Basically, what you do is mount your DWR through
> Application.get().getSharedResources().add() (als take a look at
> SharedResources.putClassAlias for prettier URLs)
> Then you create a ResourceReference to it;
> The link between the mounted DWR and the ResourceReference is the scope
> (either Application or one of your own classes) and a static name (String).
>
> Linking to it is then merely a matter of calling component.urlFor(
> ResourceReference ) (or RequestCycle.get().urlFor in case you are
> outside of a component). The URL returned here can be used as the href
> for a link, for example.
>
> 2) As mentioned earlier, you use
> getRequestCycle().setRequestTarget( new ResourceStreamRequestTarget(...) )
> and supply it with an IResourceStream implementation which wraps your
> resource.
>
> (2) is automatically secure (url depends on session), (1) is accessible
> by anyone unless you add checks yourself.
> You can also add pageparameters to (1) to make it not only depend on e.g
> the Session but also on external parameters.
>
> Hope this helps.
>
> Bas
>
> ----- Original Message ----- From: "Major Péter" <ma...@sch.bme.hu>
> To: <us...@wicket.apache.org>
> Sent: Saturday, August 08, 2009 1:52 PM
> Subject: Re: How to use DynamicWebResource?
>
>
>> I was only guessing, that the DWR would be the solution to my problem
>> (the idea was from uploaddownload wiki page).
>>
>> The link you provided is exactly the same solution which I linked
>> before (It's just on a different archive-browser.) :)
>> //And that one is working perfectly for me.
>>
>> So if I have a custom DWR, do I gonna need a
>> ResourceStreamRequestTarget to making it downloadable (or bookmarkable)?
>> And if I have a RequestTarget how can I make it bookmarkable?
>>
>> Thanks
>>
>> Peter
>>
>> 2009-08-08 13:40 keltezéssel, Martin Makundi írta:
>>>> - After creating a DWR, how do I get an URL for the newly created
>>>> resource
>>>> and how do I redirect to it?
>>>
>>> I would assume DWR is just a resource. It does not have an URL on its
>>> own. HEre is an example where it is attached to a link:
>>> http://mail-archives.apache.org/mod_mbox/wicket-users/200708.mbox/%3C46D80834.1040209@jayway.dk%3E
>>>
>>>
>>> Use for example ResourceStreamRequestTarget
>>>
>>>> - While creating the URL for the resource, does it matters which
>>>> urlcodingstrategy am I using? (I'm currently using BPRTUCS).
>>>
>>> Don't know how/why it would matter.
>>>
>>>> - what does the mountSharedResource do at all? (Is it for only static
>>>> resources?)
>>>
>>> Don't know that either. Hopefully someone else can help you with these.
>>>
>>> **
>>> Martin
>>>
>>>
>>>> 2009-08-08 13:23 keltezéssel, Martin Makundi írta:
>>>>>> I'm using iText for generating Pdf, this is only a PoC code, Later
>>>>>> I will
>>>>>> add a business object to the class, so I can provide custom datas
>>>>>> for the
>>>>>> pdf generating part. You can also see, that this code will
>>>>>> generate the
>>>>>> pdf
>>>>>> on-demand.
>>>>> Can you elaborate the problem you still have?
>>>>>
>>>>> **
>>>>> Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Bas Gooren <ba...@iswd.nl>.
The idea is as follows, for as far as I know:
- you either mount your resource (dynamic, static) as a shared resource (1),
or
- you set it as the request target when the user clicks a link (2)
Now let me explain both:
1) Basically, what you do is mount your DWR through
Application.get().getSharedResources().add() (als take a look at
SharedResources.putClassAlias for prettier URLs)
Then you create a ResourceReference to it;
The link between the mounted DWR and the ResourceReference is the scope
(either Application or one of your own classes) and a static name (String).
Linking to it is then merely a matter of calling component.urlFor(
ResourceReference ) (or RequestCycle.get().urlFor in case you are outside of
a component). The URL returned here can be used as the href for a link, for
example.
2) As mentioned earlier, you use
getRequestCycle().setRequestTarget( new ResourceStreamRequestTarget(...) )
and supply it with an IResourceStream implementation which wraps your
resource.
(2) is automatically secure (url depends on session), (1) is accessible by
anyone unless you add checks yourself.
You can also add pageparameters to (1) to make it not only depend on e.g the
Session but also on external parameters.
Hope this helps.
Bas
----- Original Message -----
From: "Major Péter" <ma...@sch.bme.hu>
To: <us...@wicket.apache.org>
Sent: Saturday, August 08, 2009 1:52 PM
Subject: Re: How to use DynamicWebResource?
>I was only guessing, that the DWR would be the solution to my problem (the
>idea was from uploaddownload wiki page).
>
> The link you provided is exactly the same solution which I linked before
> (It's just on a different archive-browser.) :)
> //And that one is working perfectly for me.
>
> So if I have a custom DWR, do I gonna need a ResourceStreamRequestTarget
> to making it downloadable (or bookmarkable)?
> And if I have a RequestTarget how can I make it bookmarkable?
>
> Thanks
>
> Peter
>
> 2009-08-08 13:40 keltezéssel, Martin Makundi írta:
>>> - After creating a DWR, how do I get an URL for the newly created
>>> resource
>>> and how do I redirect to it?
>>
>> I would assume DWR is just a resource. It does not have an URL on its
>> own. HEre is an example where it is attached to a link:
>> http://mail-archives.apache.org/mod_mbox/wicket-users/200708.mbox/%3C46D80834.1040209@jayway.dk%3E
>>
>> Use for example ResourceStreamRequestTarget
>>
>>> - While creating the URL for the resource, does it matters which
>>> urlcodingstrategy am I using? (I'm currently using BPRTUCS).
>>
>> Don't know how/why it would matter.
>>
>>> - what does the mountSharedResource do at all? (Is it for only static
>>> resources?)
>>
>> Don't know that either. Hopefully someone else can help you with these.
>>
>> **
>> Martin
>>
>>
>>> 2009-08-08 13:23 keltezéssel, Martin Makundi írta:
>>>>> I'm using iText for generating Pdf, this is only a PoC code, Later I
>>>>> will
>>>>> add a business object to the class, so I can provide custom datas for
>>>>> the
>>>>> pdf generating part. You can also see, that this code will generate
>>>>> the
>>>>> pdf
>>>>> on-demand.
>>>> Can you elaborate the problem you still have?
>>>>
>>>> **
>>>> Martin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Major Péter <ma...@sch.bme.hu>.
I was only guessing, that the DWR would be the solution to my problem
(the idea was from uploaddownload wiki page).
The link you provided is exactly the same solution which I linked before
(It's just on a different archive-browser.) :)
//And that one is working perfectly for me.
So if I have a custom DWR, do I gonna need a ResourceStreamRequestTarget
to making it downloadable (or bookmarkable)?
And if I have a RequestTarget how can I make it bookmarkable?
Thanks
Peter
2009-08-08 13:40 keltezéssel, Martin Makundi írta:
>> - After creating a DWR, how do I get an URL for the newly created resource
>> and how do I redirect to it?
>
> I would assume DWR is just a resource. It does not have an URL on its
> own. HEre is an example where it is attached to a link:
> http://mail-archives.apache.org/mod_mbox/wicket-users/200708.mbox/%3C46D80834.1040209@jayway.dk%3E
>
> Use for example ResourceStreamRequestTarget
>
>> - While creating the URL for the resource, does it matters which
>> urlcodingstrategy am I using? (I'm currently using BPRTUCS).
>
> Don't know how/why it would matter.
>
>> - what does the mountSharedResource do at all? (Is it for only static
>> resources?)
>
> Don't know that either. Hopefully someone else can help you with these.
>
> **
> Martin
>
>
>> 2009-08-08 13:23 keltezéssel, Martin Makundi írta:
>>>> I'm using iText for generating Pdf, this is only a PoC code, Later I will
>>>> add a business object to the class, so I can provide custom datas for the
>>>> pdf generating part. You can also see, that this code will generate the
>>>> pdf
>>>> on-demand.
>>> Can you elaborate the problem you still have?
>>>
>>> **
>>> Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Martin Makundi <ma...@koodaripalvelut.com>.
> - After creating a DWR, how do I get an URL for the newly created resource
> and how do I redirect to it?
I would assume DWR is just a resource. It does not have an URL on its
own. HEre is an example where it is attached to a link:
http://mail-archives.apache.org/mod_mbox/wicket-users/200708.mbox/%3C46D80834.1040209@jayway.dk%3E
Use for example ResourceStreamRequestTarget
> - While creating the URL for the resource, does it matters which
> urlcodingstrategy am I using? (I'm currently using BPRTUCS).
Don't know how/why it would matter.
> - what does the mountSharedResource do at all? (Is it for only static
> resources?)
Don't know that either. Hopefully someone else can help you with these.
**
Martin
>
> 2009-08-08 13:23 keltezéssel, Martin Makundi írta:
>>>
>>> I'm using iText for generating Pdf, this is only a PoC code, Later I will
>>> add a business object to the class, so I can provide custom datas for the
>>> pdf generating part. You can also see, that this code will generate the
>>> pdf
>>> on-demand.
>>
>> Can you elaborate the problem you still have?
>>
>> **
>> Martin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Major Péter <ma...@sch.bme.hu>.
The problem is, that I don't understand DynamicWebResource well.
In the first e-mail I wrote some questions:
- After creating a DWR, how do I get an URL for the newly created
resource and how do I redirect to it?
- While creating the URL for the resource, does it matters which
urlcodingstrategy am I using? (I'm currently using BPRTUCS).
- what does the mountSharedResource do at all? (Is it for only static
resources?)
These questions are only for helping me to better understand the framework.
Thanks
Peter
2009-08-08 13:23 keltezéssel, Martin Makundi írta:
>> I'm using iText for generating Pdf, this is only a PoC code, Later I will
>> add a business object to the class, so I can provide custom datas for the
>> pdf generating part. You can also see, that this code will generate the pdf
>> on-demand.
>
> Can you elaborate the problem you still have?
>
> **
> Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Martin Makundi <ma...@koodaripalvelut.com>.
> I'm using iText for generating Pdf, this is only a PoC code, Later I will
> add a business object to the class, so I can provide custom datas for the
> pdf generating part. You can also see, that this code will generate the pdf
> on-demand.
Can you elaborate the problem you still have?
**
Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Major Péter <ma...@sch.bme.hu>.
In my PdfLink.java I do the follow:
public class PdfLink extends Link {
private static final long serialVersionUID = 1L;
public PdfLink(String id) {
super(id);
}
public void onClick() {
try {
IResourceStream resourceStream = new ByteArrayResourceStream(
((ByteArrayOutputStream) generatePdf()).toByteArray(),
"application/pdf");
getRequestCycle().setRequestTarget(new
ResourceStreamRequestTarget(resourceStream) {
@Override
public String getFileName() {
return ("export.pdf");
}
});
} catch (Exception ex) {
error("Error while creating the PDF file!");
ex.printStackTrace();
}
}
private OutputStream generatePdf() throws DocumentException,
IOException {
Document document = new Document(PageSize.A4);
final OutputStream os = new ByteArrayOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
document.open();
document.add(new Paragraph("some sample text"));
document.add(Image.getInstance(new URL("some.URL.to.pic")));
document.close();
return os;
}
}
I'm using iText for generating Pdf, this is only a PoC code, Later I
will add a business object to the class, so I can provide custom datas
for the pdf generating part. You can also see, that this code will
generate the pdf on-demand.
Thanks for the link anyway, I'm looking into it.
Regards,
Peter
2009-08-08 12:58 keltezéssel, Martin Makundi írta:
> Hi!
>
> Maybe this might help:
> http://cwiki.apache.org/WICKET/showing-a-remote-image-on-wicket-site.html
>
> ? It's about fetching a remote image.. I did not quite figure out how
> you provide your pdf, as stream or otherwise.
>
> **
> Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Martin Makundi <ma...@koodaripalvelut.com>.
Hi!
Maybe this might help:
http://cwiki.apache.org/WICKET/showing-a-remote-image-on-wicket-site.html
? It's about fetching a remote image.. I did not quite figure out how
you provide your pdf, as stream or otherwise.
**
Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How to use DynamicWebResource?
Posted by Major Péter <ma...@sch.bme.hu>.
I was able to solve my issue with this:
http://markmail.org/message/66le4n6vmpcfv3tq#query:wicket%20dynamicwebresource%20example+page:1+mid:yv5hunvthfqrb5bv+state:results
But still, I would be happy if somebody answers my questions. ;)
Regards,
Peter
2009-08-07 21:06 keltezéssel, Major Péter írta:
> Hi all,
>
> I have a page, and I need to generate a PDF file: after the user presses
> a button, then the downloading is being started.
> For the PDF-generation I would like to use iText, but I have no idea,
> how to create a link to the downloadable content.
> I tried to follow UploadDownload wiki page, but got lost by spring
> beans. So far I created a DynamicWebResource subclass:
>
> public class PdfFileResource extends DynamicWebResource {
>
> @Override
> protected ResourceState getResourceState() {
> return new PdfFileResourceState();
> }
>
> class PdfFileResourceState extends ResourceState {
>
> @Override
> public String getContentType() {
> return "application/pdf";
> }
>
> @Override
> public byte[] getData() {
> throw new UnsupportedOperationException("Not supported yet.");
> }
> }
> }
>
> I guess in the PdfFileResource's constructor I need to set the data for
> ResourceState, am I right?
>
> The wiki sais also, that in Application#init I need to add the new
> SharedResource, so I did that too.
>
> but after that how to use it?
>
> I guessing the follows:
> In the Button#onSubmit I should create first the outputstream
> (bytearrayoutputstream) for the pdf, after that I should create a new
> pdfFileResource with the outputstream.
>
> - But after that, how do I get URL for the newly created resource and
> how do I redirect to it?
> - While creating the URL for the resource, does it matters which
> urlcodingstrategy am I using? (I'm currently using BPRTUCS).
> - what does the mountSharedResource do at all? (Is it for only static
> resources?)
>
> Any help would be appreciated.
>
> //I looked the archives first, but am still confused, please help
>
> Best Regards,
> Peter
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org