How to URL encode a string

I am trying to url encode a string to be used in a link in a template. I have found that there is a velocity tool that can do this, but it doesn’t look like it is supported in Percussion. The tool that I want to use is:
http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/EscapeTool.html#url(java.lang.Object)

It says that it was added in VelocityTools 1.3. When I try to type it in Workbench, it doesn’t show up in help menu when you type in a $tools.esc. I don’t see the url method, but do see others such as http, xml, and so on. When I try to use it, it just outputs the code directly and isn’t interpreted during assembly. Does Percussion not support this?

Thanks,

-Fernando

Fernando,

Your correct that our implementation of Velocity tools is a little behind, although Velocity like some of the other technologies we use, is a bit of a moving target.

This simplest path is to write your own JEXL function. This takes only a few minutes to do, and doesn’t even require you to restart the server.

As of Java 5, the java.net.URLEncoder class now works properly across all characters: just make sure you specify UTF-8 as the character encoding.

Hi David,

I’ve been trying to find some example java source in the rx tree, but can’t seem to find anything. I did find some document that stated I would need to extend the interface IPSJexlExpression to write my own function extension. Also, I read where to register the extension in Workbench. Can you provide any example source that could provide me with a skeleton to work with?

Thanks,
-Fernando

Here’s a small fragment from the PSOToolkit

First, your class must implement the correct Interface…


public class PSOBase64Codec extends PSJexlUtilBase implements IPSJexlExpression

Then you need a method which returns a value. It has to be annotated with
@IPSJexlMethod and @IPSJexlParam annotations.


   @IPSJexlMethod(description="Encode a binary property", 
         params={
        @IPSJexlParam(name="source", description="binary property to encode")})
   public String encode(Property jcrProperty) throws ValueFormatException, RepositoryException
   {
      if(jcrProperty == null)
      {
         log.debug("Property is null, no base64 encoding is possible" ); 
         return null; 
      }
      return encode(jcrProperty.getStream()); 
   }


If you’re not familiar with Java 5 annotations, this can be a little confusing. The @IPSJexlParams are an Array, so they have to be enclosed in curly braces {} and separated with commas.

I hope this helps.

Dave

Once you have your class, the next thing is to register it.

This you do in the “System View” of the Workbench. Open the Extensions section and find the JEXL extensions. Right click and create a new Extension.

If you placed your class in a JAR, you can specify the JAR during the extension registration which means that you will not need to restart the server.

Thanks Dave! I tried it out and it worked. :smiley:

Would it be possible for Fernando or someone from Rhythmyx to post the code necessary to enable URL Encoding? I’m not a Java programmer :o and need to replace spaces with the %20 character.
If there’s another solution that would be appreciated. We’re running Rx6.1.
Thanks,
Geoffrey

This week, I was building something that needed URL encoding, and I decided that I’d try upgrading the Velocity Tools.

Caution: I’ve only done very minimal testing. Proceed at your own risk.

Step 1) Download Velocity Tools 1.4 from the Velocity web site: http://velocity.apache.org/download.cgi#Tools

Step 2) Shut down the server

Step 3) In the folder C:\Rhythmyx\AppServer\server\rx\deploy\rxapp.ear\rxapp.war\WEB-INF\lib, remove the velocity-tools-1.2.jar (keep a backup copy) and replace it with the velocity-tools-1.4.jar that you downloaded. (It would seem that velocity-tools-view-1.4.jar might also work, but I didn’t try it).

Step 4) In the folder C:\Rhythmyx\AppServer\server\rx\deploy\rxapp.ear\rxapp.war\WEB-INF\config\velocity edit the tools.xml file. Add these 4 lines to the bottom of the file, just before </toolbox>

<tool>
  <key>esc</key>
  <scope>application</scope>
  <class>org.apache.velocity.tools.generic.EscapeTool</class>
</tool>

Note that this file will get overwritten when you run the installer, so keep a copy someplace safe for future reference.

Step 5) Restart the server. In the workbench, you should see $tools.esc in the “code completion” window.

It’s also possible that some of the other new tools (e.g. the DateComparison tool) might also work, but I haven’t tried them yet.

The current plan is to replace the Velocity 1.4 and Velocity Tools 1.2 with Velocity 1.5 and Velocity Tools 1.4 in the next release of Rhythmyx (tentatively Rhythmyx 6.6)

Dave

Thanks Dave,
I’ll give it a go. Looks like it might be useful to a lot of people.
Cheers,
Geoffrey