You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Net Dawg <ne...@yahoo.com.INVALID> on 2014/07/01 20:58:17 UTC

Importing Images on FileSystem in Page Class

How can I import an image into page class (without advance knowledge of its name)?  I would like to use it to stream PDF dynamically. 
 
According to Assets in Component Classes(http://tapestry.apache.org/assets.html), components learn about assets via injection. The @Inject annotation allows Assets to be injected into components as read-only 
properties. The path to the resource is specified using the Path annotation:
 
@Inject
@Path("context:images/tapestry_banner.gif")
private Asset banner;

Assets are located within domains; these domains are identified by the prefix on the @Path annotation's value.
 
QUESTION:  What if I do not know filename (tapestry_banner.gif) in advance and it is fed to me from database?  In my case, I have several hundred such assets...from which I have to select the one I need based on what is tagged in the database...it would not be realistic to inject every one of them into page (or is it?).  Say all my images are in exactly the same directory called images under webapp\layout\images and I get the filename say image001.gif, how could I do something like this: 
 
Asset image = createImage([what?]image001.gif);

Re: Importing Images on FileSystem in Page Class

Posted by Net Dawg <ne...@yahoo.com.INVALID>.
OK - figured it out.  Thanks, Chris, for the tip.  Here is the example (using itext for pdf): 
 
            if (StringUtils.isNotBlank(logoFile))
            {
             try
             {
              Asset logoAss = assetSource.getClasspathAsset("context:layout/images/" + StringUtils.trimToEmpty(logoFile));
                     java.awt.Image logoImage = Toolkit.getDefaultToolkit().createImage(logoAss.getResource().toURL());
                     if (logoImage != null)
                     {
                      com.itextpdf.text.Image logo2 = com.itextpdf.text.Image.getInstance(logoImage, null); 
                      if (logo2 != null) 
                      {
                     table.addCell(new PdfPCell(new Phrase("Logo")));
                     table.addCell(logo2);
                      }
                     }
             }
             catch (Exception e)
             {
              logger.info("Unable to process logo file " + logoFile + " while rendering PDF of Orgs List");
              logger.info(e);;
             }
            }
 


On Tuesday, July 1, 2014 10:21 AM, Net Dawg <ne...@yahoo.com.INVALID> wrote:
  


Is there an example of this?  Meanwhile I found this nugget in SO: 
 
http://stackoverflow.com/questions/19301034/how-to-get-the-asset-path-in-tapestry-5



On Tuesday, July 1, 2014 9:25 AM, Chris Poulsen <ma...@nesluop.dk> wrote:
  


Inject AssetSource and set up things programmatically ?

-- 
Chris



On Tue, Jul 1, 2014 at 8:58 PM, Net Dawg <ne...@yahoo.com.invalid> wrote:

> How can I import an image into page class (without advance knowledge of
> its name)?  I would like to use it to stream PDF dynamically.
>
> According to Assets in Component Classes(
> http://tapestry.apache.org/assets.html), components learn about assets
> via injection. The @Inject annotation allows Assets to be injected into
> components as read-only
> properties. The path to the resource is specified using the Path
> annotation:
>
> @Inject
> @Path("context:images/tapestry_banner.gif")
> private Asset banner;
>
> Assets are located within domains; these domains are identified by the
> prefix on the @Path annotation's value.
>
> QUESTION:  What if I do not know filename (tapestry_banner.gif) in advance
> and it is fed to me from database?  In my case, I have several hundred such
> assets...from which I have to select the one I need based on what is tagged
> in the database...it would not be realistic to inject every one of them
> into page (or is it?).  Say all my images are in exactly the same directory
> called images under webapp\layout\images and I get the filename say
> image001.gif, how could I do something like this:
>
> Asset image = createImage([what?]image001.gif);

Re: Importing Images on FileSystem in Page Class

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Tue, 01 Jul 2014 17:16:57 -0300, Net Dawg <ne...@yahoo.com.invalid>  
wrote:

> Is there an example of this?  Meanwhile I found this nugget in SO:
>   
> http://stackoverflow.com/questions/19301034/how-to-get-the-asset-path-in-tapestry-5

The best approach I've seen (but not used, because I hate putting assets  
in the filesystem outside the webapp) is this:  
http://apache-tapestry-mailing-list-archives.1045711.n5.nabble.com/T5-How-to-load-image-Asset-from-filesystem-tp2430256p2430271.html.  
You should be very careful about security, though: your implementation  
should limit which folders should be accessible. Otherwise, people can  
manipulate it to download any file from your file system.

-- 
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

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


Re: Importing Images on FileSystem in Page Class

Posted by Net Dawg <ne...@yahoo.com.INVALID>.
Is there an example of this?  Meanwhile I found this nugget in SO: 
 
http://stackoverflow.com/questions/19301034/how-to-get-the-asset-path-in-tapestry-5 


On Tuesday, July 1, 2014 9:25 AM, Chris Poulsen <ma...@nesluop.dk> wrote:
  


Inject AssetSource and set up things programmatically ?

-- 
Chris



On Tue, Jul 1, 2014 at 8:58 PM, Net Dawg <ne...@yahoo.com.invalid> wrote:

> How can I import an image into page class (without advance knowledge of
> its name)?  I would like to use it to stream PDF dynamically.
>
> According to Assets in Component Classes(
> http://tapestry.apache.org/assets.html), components learn about assets
> via injection. The @Inject annotation allows Assets to be injected into
> components as read-only
> properties. The path to the resource is specified using the Path
> annotation:
>
> @Inject
> @Path("context:images/tapestry_banner.gif")
> private Asset banner;
>
> Assets are located within domains; these domains are identified by the
> prefix on the @Path annotation's value.
>
> QUESTION:  What if I do not know filename (tapestry_banner.gif) in advance
> and it is fed to me from database?  In my case, I have several hundred such
> assets...from which I have to select the one I need based on what is tagged
> in the database...it would not be realistic to inject every one of them
> into page (or is it?).  Say all my images are in exactly the same directory
> called images under webapp\layout\images and I get the filename say
> image001.gif, how could I do something like this:
>
> Asset image = createImage([what?]image001.gif);

Re: Importing Images on FileSystem in Page Class

Posted by Chris Poulsen <ma...@nesluop.dk>.
Inject AssetSource and set up things programmatically ?

-- 
Chris


On Tue, Jul 1, 2014 at 8:58 PM, Net Dawg <ne...@yahoo.com.invalid> wrote:

> How can I import an image into page class (without advance knowledge of
> its name)?  I would like to use it to stream PDF dynamically.
>
> According to Assets in Component Classes(
> http://tapestry.apache.org/assets.html), components learn about assets
> via injection. The @Inject annotation allows Assets to be injected into
> components as read-only
> properties. The path to the resource is specified using the Path
> annotation:
>
> @Inject
> @Path("context:images/tapestry_banner.gif")
> private Asset banner;
>
> Assets are located within domains; these domains are identified by the
> prefix on the @Path annotation's value.
>
> QUESTION:  What if I do not know filename (tapestry_banner.gif) in advance
> and it is fed to me from database?  In my case, I have several hundred such
> assets...from which I have to select the one I need based on what is tagged
> in the database...it would not be realistic to inject every one of them
> into page (or is it?).  Say all my images are in exactly the same directory
> called images under webapp\layout\images and I get the filename say
> image001.gif, how could I do something like this:
>
> Asset image = createImage([what?]image001.gif);