You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by ph...@free.fr on 2015/04/09 14:58:17 UTC

File exists

Hello,

does anyone know how to check if a file exists in Velocity?

I have tried:

#set ($exists = new File().exists("/mnt/xxxparnex01_pdf/PDF/IHT/${pdf_year_mon_v}/${pdf_day_v}/${pdf_name_page_number}_${pdf_name_page_type}_VIG.jpg") )

to no avail.

Cheers,

Philippe

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: File exists

Posted by ph...@free.fr.
Many thanks Sergiu.

This is exactly what I need.

BTW, only the second version of <object works with anchors:

For example,

<a href="...">
<object data="/path/to/image.png">
  <object data="/path/to/fallback.png">
    <p>Image not found</p>
  </object>
</object>
</a>





----- Mail original -----
De: "Sergiu Dumitriu" <se...@gmail.com>
À: "Velocity Users List" <us...@velocity.apache.org>
Envoyé: Vendredi 10 Avril 2015 08:47:06
Objet: Re: File exists

There's a simple HTML-only solution for that:

<object data="/path/to/image.png">
  <object data="/path/to/fallback.png"/>
</object>


Or even better:

<object data="/path/to/image.png">
  <object data="/path/to/fallback.png">
    <p>Image not found</p>
  </object>
</object>

On 04/09/2015 10:17 AM, phiroc@free.fr wrote:
> Hi Erik,
> 
> my application's SOLR database has a field called "pdf_img", whose value is equal to the path of an image which may or may not exist, for all sorts of reasons (network issues, etc.)
> 
> Before displaying the image with Velocity, I would like to check whether the file exists or not, or is accessible or not. If it doesn't exist or it doesn't exist, I will display a "image not available" picture.
> 
> Philippe
> 
> 
> 
> ----- Mail original -----
> De: "Erik Hatcher" <er...@gmail.com>
> À: "Velocity Users List" <us...@velocity.apache.org>
> Envoyé: Jeudi 9 Avril 2015 15:55:18
> Objet: Re: File exists
> 
> You’re intentionally constrained in Solr to a specific set of helpers, otherwise a template could cause a security concern.  See context objects table here <https://cwiki.apache.org/confluence/display/solr/Response+Writers#ResponseWriters-VelocityResponseWriter <https://cwiki.apache.org/confluence/display/solr/Response+Writers#ResponseWriters-VelocityResponseWriter>>
> 
> If you’re up for some custom Java coding in Solr, you can implement a SearchComponent that you can put into any search request handler that would put something into the response object, say a boolean that tells you if the file exists.  If you need to actually read or write to that file, you could do that in the SearchComponent as well.  
> 
> Of course the “FileHelper” idea would work if you hacked the VelocityResponseWriter code (there is a TODO for me in there to make helpers pluggable, but this type of request scares me away from making it too easy! :), but the SearchComponent is a way to get custom data into the response which can then be leveraged by the Velocity template.  You can also send in custom request parameters that become part of the request context object as well, in case that helps here.
> 
> But I’m curious - what’s the need here?    It’s a very unusual request for this type of thing, so I’m concerned you’re trying something that should be done a different way instead.
> 
> 
> —
> Erik Hatcher, Senior Solutions Architect
> http://www.lucidworks.com <http://www.lucidworks.com/>
> 
> 
> 
> 
>> On Apr 9, 2015, at 9:18 AM, phiroc@free.fr wrote:
>>
>> Unfortunately, I am using SOLR's implementation of Velocity, and don't know how to create FileHelpers in that context.
>>
>>
>> ----- Mail original -----
>> De: "Will Glass-Husain" <wg...@gmail.com>
>> À: "Velocity Users List" <us...@velocity.apache.org>
>> Envoyé: Jeudi 9 Avril 2015 15:06:47
>> Objet: Re: File exists
>>
>> You can't instantiate objects in Velocity for an arbitrary class.
>> remember, Velocity is intended to be a templating, not a scripting
>> language.
>>
>> Create a new class FileHelper which has a method "fileExists(string)".
>> Pass it into the context in your java code as a reference "fileHelper".
>> Then in your template call do
>>
>> $fileHelper.fileExists("/mnt/etc/etc/etc/")
>>
>> WILL
>>
>> On Thu, Apr 9, 2015 at 5:58 AM, <ph...@free.fr> wrote:
>>
>>> Hello,
>>>
>>> does anyone know how to check if a file exists in Velocity?
>>>
>>> I have tried:
>>>
>>> #set ($exists = new
>>> File().exists("/mnt/xxxparnex01_pdf/PDF/IHT/${pdf_year_mon_v}/${pdf_day_v}/${pdf_name_page_number}_${pdf_name_page_type}_VIG.jpg")
>>> )
>>>
>>> to no avail.
>>>
>>> Cheers,
>>>
>>> Philippe
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>>> For additional commands, e-mail: user-help@velocity.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 


-- 
Sergiu Dumitriu
http://purl.org/net/sergiu/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: File exists

Posted by Sergiu Dumitriu <se...@gmail.com>.
There's a simple HTML-only solution for that:

<object data="/path/to/image.png">
  <object data="/path/to/fallback.png"/>
</object>


Or even better:

<object data="/path/to/image.png">
  <object data="/path/to/fallback.png">
    <p>Image not found</p>
  </object>
</object>

On 04/09/2015 10:17 AM, phiroc@free.fr wrote:
> Hi Erik,
> 
> my application's SOLR database has a field called "pdf_img", whose value is equal to the path of an image which may or may not exist, for all sorts of reasons (network issues, etc.)
> 
> Before displaying the image with Velocity, I would like to check whether the file exists or not, or is accessible or not. If it doesn't exist or it doesn't exist, I will display a "image not available" picture.
> 
> Philippe
> 
> 
> 
> ----- Mail original -----
> De: "Erik Hatcher" <er...@gmail.com>
> À: "Velocity Users List" <us...@velocity.apache.org>
> Envoyé: Jeudi 9 Avril 2015 15:55:18
> Objet: Re: File exists
> 
> You’re intentionally constrained in Solr to a specific set of helpers, otherwise a template could cause a security concern.  See context objects table here <https://cwiki.apache.org/confluence/display/solr/Response+Writers#ResponseWriters-VelocityResponseWriter <https://cwiki.apache.org/confluence/display/solr/Response+Writers#ResponseWriters-VelocityResponseWriter>>
> 
> If you’re up for some custom Java coding in Solr, you can implement a SearchComponent that you can put into any search request handler that would put something into the response object, say a boolean that tells you if the file exists.  If you need to actually read or write to that file, you could do that in the SearchComponent as well.  
> 
> Of course the “FileHelper” idea would work if you hacked the VelocityResponseWriter code (there is a TODO for me in there to make helpers pluggable, but this type of request scares me away from making it too easy! :), but the SearchComponent is a way to get custom data into the response which can then be leveraged by the Velocity template.  You can also send in custom request parameters that become part of the request context object as well, in case that helps here.
> 
> But I’m curious - what’s the need here?    It’s a very unusual request for this type of thing, so I’m concerned you’re trying something that should be done a different way instead.
> 
> 
> —
> Erik Hatcher, Senior Solutions Architect
> http://www.lucidworks.com <http://www.lucidworks.com/>
> 
> 
> 
> 
>> On Apr 9, 2015, at 9:18 AM, phiroc@free.fr wrote:
>>
>> Unfortunately, I am using SOLR's implementation of Velocity, and don't know how to create FileHelpers in that context.
>>
>>
>> ----- Mail original -----
>> De: "Will Glass-Husain" <wg...@gmail.com>
>> À: "Velocity Users List" <us...@velocity.apache.org>
>> Envoyé: Jeudi 9 Avril 2015 15:06:47
>> Objet: Re: File exists
>>
>> You can't instantiate objects in Velocity for an arbitrary class.
>> remember, Velocity is intended to be a templating, not a scripting
>> language.
>>
>> Create a new class FileHelper which has a method "fileExists(string)".
>> Pass it into the context in your java code as a reference "fileHelper".
>> Then in your template call do
>>
>> $fileHelper.fileExists("/mnt/etc/etc/etc/")
>>
>> WILL
>>
>> On Thu, Apr 9, 2015 at 5:58 AM, <ph...@free.fr> wrote:
>>
>>> Hello,
>>>
>>> does anyone know how to check if a file exists in Velocity?
>>>
>>> I have tried:
>>>
>>> #set ($exists = new
>>> File().exists("/mnt/xxxparnex01_pdf/PDF/IHT/${pdf_year_mon_v}/${pdf_day_v}/${pdf_name_page_number}_${pdf_name_page_type}_VIG.jpg")
>>> )
>>>
>>> to no avail.
>>>
>>> Cheers,
>>>
>>> Philippe
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>>> For additional commands, e-mail: user-help@velocity.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 


-- 
Sergiu Dumitriu
http://purl.org/net/sergiu/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: File exists

Posted by ph...@free.fr.
Hi Erik,

my application's SOLR database has a field called "pdf_img", whose value is equal to the path of an image which may or may not exist, for all sorts of reasons (network issues, etc.)

Before displaying the image with Velocity, I would like to check whether the file exists or not, or is accessible or not. If it doesn't exist or it doesn't exist, I will display a "image not available" picture.

Philippe



----- Mail original -----
De: "Erik Hatcher" <er...@gmail.com>
À: "Velocity Users List" <us...@velocity.apache.org>
Envoyé: Jeudi 9 Avril 2015 15:55:18
Objet: Re: File exists

You’re intentionally constrained in Solr to a specific set of helpers, otherwise a template could cause a security concern.  See context objects table here <https://cwiki.apache.org/confluence/display/solr/Response+Writers#ResponseWriters-VelocityResponseWriter <https://cwiki.apache.org/confluence/display/solr/Response+Writers#ResponseWriters-VelocityResponseWriter>>

If you’re up for some custom Java coding in Solr, you can implement a SearchComponent that you can put into any search request handler that would put something into the response object, say a boolean that tells you if the file exists.  If you need to actually read or write to that file, you could do that in the SearchComponent as well.  

Of course the “FileHelper” idea would work if you hacked the VelocityResponseWriter code (there is a TODO for me in there to make helpers pluggable, but this type of request scares me away from making it too easy! :), but the SearchComponent is a way to get custom data into the response which can then be leveraged by the Velocity template.  You can also send in custom request parameters that become part of the request context object as well, in case that helps here.

But I’m curious - what’s the need here?    It’s a very unusual request for this type of thing, so I’m concerned you’re trying something that should be done a different way instead.


—
Erik Hatcher, Senior Solutions Architect
http://www.lucidworks.com <http://www.lucidworks.com/>




> On Apr 9, 2015, at 9:18 AM, phiroc@free.fr wrote:
> 
> Unfortunately, I am using SOLR's implementation of Velocity, and don't know how to create FileHelpers in that context.
> 
> 
> ----- Mail original -----
> De: "Will Glass-Husain" <wg...@gmail.com>
> À: "Velocity Users List" <us...@velocity.apache.org>
> Envoyé: Jeudi 9 Avril 2015 15:06:47
> Objet: Re: File exists
> 
> You can't instantiate objects in Velocity for an arbitrary class.
> remember, Velocity is intended to be a templating, not a scripting
> language.
> 
> Create a new class FileHelper which has a method "fileExists(string)".
> Pass it into the context in your java code as a reference "fileHelper".
> Then in your template call do
> 
> $fileHelper.fileExists("/mnt/etc/etc/etc/")
> 
> WILL
> 
> On Thu, Apr 9, 2015 at 5:58 AM, <ph...@free.fr> wrote:
> 
>> Hello,
>> 
>> does anyone know how to check if a file exists in Velocity?
>> 
>> I have tried:
>> 
>> #set ($exists = new
>> File().exists("/mnt/xxxparnex01_pdf/PDF/IHT/${pdf_year_mon_v}/${pdf_day_v}/${pdf_name_page_number}_${pdf_name_page_type}_VIG.jpg")
>> )
>> 
>> to no avail.
>> 
>> Cheers,
>> 
>> Philippe
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>> 
>> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: File exists

Posted by Erik Hatcher <er...@gmail.com>.
You’re intentionally constrained in Solr to a specific set of helpers, otherwise a template could cause a security concern.  See context objects table here <https://cwiki.apache.org/confluence/display/solr/Response+Writers#ResponseWriters-VelocityResponseWriter <https://cwiki.apache.org/confluence/display/solr/Response+Writers#ResponseWriters-VelocityResponseWriter>>

If you’re up for some custom Java coding in Solr, you can implement a SearchComponent that you can put into any search request handler that would put something into the response object, say a boolean that tells you if the file exists.  If you need to actually read or write to that file, you could do that in the SearchComponent as well.  

Of course the “FileHelper” idea would work if you hacked the VelocityResponseWriter code (there is a TODO for me in there to make helpers pluggable, but this type of request scares me away from making it too easy! :), but the SearchComponent is a way to get custom data into the response which can then be leveraged by the Velocity template.  You can also send in custom request parameters that become part of the request context object as well, in case that helps here.

But I’m curious - what’s the need here?    It’s a very unusual request for this type of thing, so I’m concerned you’re trying something that should be done a different way instead.


—
Erik Hatcher, Senior Solutions Architect
http://www.lucidworks.com <http://www.lucidworks.com/>




> On Apr 9, 2015, at 9:18 AM, phiroc@free.fr wrote:
> 
> Unfortunately, I am using SOLR's implementation of Velocity, and don't know how to create FileHelpers in that context.
> 
> 
> ----- Mail original -----
> De: "Will Glass-Husain" <wg...@gmail.com>
> À: "Velocity Users List" <us...@velocity.apache.org>
> Envoyé: Jeudi 9 Avril 2015 15:06:47
> Objet: Re: File exists
> 
> You can't instantiate objects in Velocity for an arbitrary class.
> remember, Velocity is intended to be a templating, not a scripting
> language.
> 
> Create a new class FileHelper which has a method "fileExists(string)".
> Pass it into the context in your java code as a reference "fileHelper".
> Then in your template call do
> 
> $fileHelper.fileExists("/mnt/etc/etc/etc/")
> 
> WILL
> 
> On Thu, Apr 9, 2015 at 5:58 AM, <ph...@free.fr> wrote:
> 
>> Hello,
>> 
>> does anyone know how to check if a file exists in Velocity?
>> 
>> I have tried:
>> 
>> #set ($exists = new
>> File().exists("/mnt/xxxparnex01_pdf/PDF/IHT/${pdf_year_mon_v}/${pdf_day_v}/${pdf_name_page_number}_${pdf_name_page_type}_VIG.jpg")
>> )
>> 
>> to no avail.
>> 
>> Cheers,
>> 
>> Philippe
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>> 
>> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 


Re: File exists

Posted by ph...@free.fr.
Unfortunately, I am using SOLR's implementation of Velocity, and don't know how to create FileHelpers in that context.


----- Mail original -----
De: "Will Glass-Husain" <wg...@gmail.com>
À: "Velocity Users List" <us...@velocity.apache.org>
Envoyé: Jeudi 9 Avril 2015 15:06:47
Objet: Re: File exists

You can't instantiate objects in Velocity for an arbitrary class.
 remember, Velocity is intended to be a templating, not a scripting
language.

Create a new class FileHelper which has a method "fileExists(string)".
Pass it into the context in your java code as a reference "fileHelper".
Then in your template call do

$fileHelper.fileExists("/mnt/etc/etc/etc/")

WILL

On Thu, Apr 9, 2015 at 5:58 AM, <ph...@free.fr> wrote:

> Hello,
>
> does anyone know how to check if a file exists in Velocity?
>
> I have tried:
>
> #set ($exists = new
> File().exists("/mnt/xxxparnex01_pdf/PDF/IHT/${pdf_year_mon_v}/${pdf_day_v}/${pdf_name_page_number}_${pdf_name_page_type}_VIG.jpg")
> )
>
> to no avail.
>
> Cheers,
>
> Philippe
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: File exists

Posted by Will Glass-Husain <wg...@gmail.com>.
You can't instantiate objects in Velocity for an arbitrary class.
 remember, Velocity is intended to be a templating, not a scripting
language.

Create a new class FileHelper which has a method "fileExists(string)".
Pass it into the context in your java code as a reference "fileHelper".
Then in your template call do

$fileHelper.fileExists("/mnt/etc/etc/etc/")

WILL

On Thu, Apr 9, 2015 at 5:58 AM, <ph...@free.fr> wrote:

> Hello,
>
> does anyone know how to check if a file exists in Velocity?
>
> I have tried:
>
> #set ($exists = new
> File().exists("/mnt/xxxparnex01_pdf/PDF/IHT/${pdf_year_mon_v}/${pdf_day_v}/${pdf_name_page_number}_${pdf_name_page_type}_VIG.jpg")
> )
>
> to no avail.
>
> Cheers,
>
> Philippe
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>