URL Escape in Template bindings

It looks like JEXL syntax $tools.esc.url() is not working for version 1.4 of org.apache.velocity.tools.generic.EscapeTool.
I was thinking of using String.replaceAll() to escape offending strings one by one, but it may miss something that has not been identified.
Does anyone have any other ideas to escape URL in the JEXL bindings?
A lot of on-line references point to java.net.URLEncoder class, and it does have a nice “encode” method, but how to use this kind of class inside of Rhythmyx bindings to work with JEXL expression?

Thank you.
-Laura

Which version of CM System are you using? This function and the updated velocity tools is available in the current 6.7. I think it was the base 6.7 did not have the updated tools in the Workbench deployment without a patch which caused the syntax checker to fail even though this would work when previewing or publishing. If using an older version of percussion I have dealt with this by using a replaceAll as you suggest, or create your own jexl function to do this for you.

If all else fails, use the java URLEncoder class.

$sys.getClass().forName('java.net.URLEncoder').getDeclaredMethod('encode').invoke($null,$yourVariable,'UTF8')

We are using CM System 6.5.
Have you created a JEXL function to do this? Any more details?

Thanks.

I tried the code excitedly since it was something I was looking for, but I got the following error:
Unexpected exception while assembling one or more items: Problem when evaluating expression “$sys.getClass().forName(‘java.net.URLEncoder’).getDeclaredMethod(‘encode’).invoke($null,$row.title,‘UTF8’);” for variable “$row.contenturl”: Could not find method getDeclaredMethod for object [class java.net.URLEncoder] and arguments [encode]

So you know why this didn’t work?
Any documentation for $sys.getClass().forName(‘java.net.URLEncoder’).getDeclaredMethod(‘encode’).invoke($null,$youVariable,‘UTF8’)? I couldn’t find any document for $sys.getClass().forName().

Thanks.

ah… sorry… I got needlessly complex.

$sys.getClass().forName('java.net.URLEncoder').encode($row.title,'UTF8');

Not only had I left out the classes for the arguments to “encode”, I was having you use reflection when you didn’t need to. Java apparently supports calling static methods directly on the Class object… who knew, right?

Same error as before:
Unexpected exception while assembling one or more items: Problem when evaluating expression “$sys.getClass().forName(‘java.net.URLEncoder’).getDeclaredMethod(‘encode’, $sys.getClass().forName(‘java.lang.String’), $sys.getClass().forName(‘java.lang.String’)).invoke($null,$row.title.String,‘UTF8’);” for variable “$row.contenturl”: Could not find method getDeclaredMethod for object [class java.net.URLEncoder] and arguments [encode,class java.lang.String,class java.lang.String]

It seems the parameters in getDeclaredMethod are having some problems. The 3rd parameter is supposed to be some kind of parameter array, based on the java doc.
Any idea? Thanks.

Yeah… you read my post before I finished editing it… try the simpler version:

$sys.getClass().forName('java.net.URLEncoder').encode($row.title.String,'UTF8');

This works, but it doesn’t do the url encoding as expected, somehow it encodes space to +, but and encodes “®” as %C2%AE with an extra %C2 in the front.
For example:
“Another Press Release for /Testing Simulink®” is the original string.
The binding will encode it into:
“Another+Press+Release+for+%2FTesting+Simulink%C2%AE”.

It is a little weird.