Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Howto needed: Velocity 2.0 Tools with SmartGWT 4.1

    Hi Isomorphic,

    I'm using your mail feature inside a data binding. It works like a charm, but sends out unescaped characters when used with text fields from the database.
    I defined all my type="text"-fields with escapeHTML="true", but the javadocs state that this only affects databound components.

    So I have to escape my output some other way and found the Velocity Escape Tool. I included all the dependencies for velocity-tools-generic-2.0.jar (and the jar itself) in my buildpath (most likely unneeded) and in my war/WEB-INF/lib.

    Unfortunately this is apparently not enough to use the tools. Is there something missing? From the velocity docs I get that I might need an

    Code:
    <tool>
      <key>esc</key>
      <scope>application</scope>
      <class>org.apache.velocity.tools.generic.EscapeTool</class>
    </tool>
    but I don't know where / in which file.
    Is there some way to configure the Velocity instance SmartGWT uses? This is the output from the server log regarding Velocity on startup:
    Code:
    === 2014-08-25 22:56:55,882 [c-13] DEBUG Velocity - SimpleLog4JLogSystem using category 'org.apache.Velocity'
    === 2014-08-25 22:56:55,882 [c-13] DEBUG Velocity - Initializing Velocity, Calling init()...
    === 2014-08-25 22:56:55,882 [c-13] DEBUG Velocity - *******************************************************************
    === 2014-08-25 22:56:55,882 [c-13] DEBUG Velocity - Starting Apache Velocity v1.7 (compiled: 2010-11-19 12:14:37)
    === 2014-08-25 22:56:55,882 [c-13] DEBUG Velocity - RuntimeInstance initializing.
    === 2014-08-25 22:56:55,882 [c-13] DEBUG Velocity - Default Properties File: org\apache\velocity\runtime\defaults\velocity.properties
    === 2014-08-25 22:56:55,882 [c-13] DEBUG Velocity - Trying to use logger class org.apache.velocity.runtime.log.SimpleLog4JLogSystem
    === 2014-08-25 22:56:55,882 [c-13] DEBUG Velocity - LogSystem has been deprecated. Please use a LogChute implementation.
    === 2014-08-25 22:56:55,887 [c-13] DEBUG Velocity - Default ResourceManager initializing. (class org.apache.velocity.runtime.resource.ResourceManagerImpl)
    === 2014-08-25 22:56:55,894 [c-13] DEBUG Velocity - ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.FileResourceLoader
    === 2014-08-25 22:56:55,897 [c-13] DEBUG Velocity - FileResourceLoader : initialization starting.
    === 2014-08-25 22:56:55,897 [c-13] DEBUG Velocity - Do unicode file recognition:  false
    === 2014-08-25 22:56:55,897 [c-13] DEBUG Velocity - FileResourceLoader : adding path ''
    === 2014-08-25 22:56:55,897 [c-13] DEBUG Velocity - FileResourceLoader : initialization complete.
    === 2014-08-25 22:56:55,909 [c-13] DEBUG Velocity - ResourceCache: initialized (class org.apache.velocity.runtime.resource.ResourceCacheImpl) with class java.util.Collections$SynchronizedMap cache map.
    === 2014-08-25 22:56:55,909 [c-13] DEBUG Velocity - Default ResourceManager initialization complete.
    === 2014-08-25 22:56:55,912 [c-13] DEBUG Velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Stop
    === 2014-08-25 22:56:55,915 [c-13] DEBUG Velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Define
    === 2014-08-25 22:56:55,916 [c-13] DEBUG Velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Break
    === 2014-08-25 22:56:55,918 [c-13] DEBUG Velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Evaluate
    === 2014-08-25 22:56:55,920 [c-13] DEBUG Velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Literal
    === 2014-08-25 22:56:55,922 [c-13] DEBUG Velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Macro
    === 2014-08-25 22:56:55,926 [c-13] DEBUG Velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Parse
    === 2014-08-25 22:56:55,929 [c-13] DEBUG Velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Include
    === 2014-08-25 22:56:55,931 [c-13] DEBUG Velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
    === 2014-08-25 22:56:55,998 [c-13] DEBUG Velocity - Created '20' parsers.
    === 2014-08-25 22:56:56,015 [c-13] DEBUG Velocity - Velocimacro : initialization starting.
    === 2014-08-25 22:56:56,015 [c-13] DEBUG Velocity - Velocimacro : "velocimacro.library" is not set.  Trying default library: VM_global_library.vm
    === 2014-08-25 22:56:56,015 [c-13] DEBUG Velocity - Velocimacro : Default library not found.
    === 2014-08-25 22:56:56,016 [c-13] DEBUG Velocity - Velocimacro : allowInline = true : VMs can be defined inline in templates
    === 2014-08-25 22:56:56,016 [c-13] DEBUG Velocity - Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions
    === 2014-08-25 22:56:56,016 [c-13] DEBUG Velocity - Velocimacro : allowInlineLocal = false : VMs defined inline will be global in scope if allowed.
    === 2014-08-25 22:56:56,016 [c-13] DEBUG Velocity - Velocimacro : autoload off : VM system will not automatically reload global library macros
    === 2014-08-25 22:56:56,016 [c-13] DEBUG Velocity - Velocimacro : Velocimacro : initialization complete.
    === 2014-08-25 22:56:56,016 [c-13] DEBUG Velocity - RuntimeInstance successfully initialized.
    I only found one older unanswered related thread in the forums.

    Thank you,
    Blama
    Last edited by Blama; 26 Aug 2014, 02:09. Reason: typos

    #2
    Hello all,

    the configuration file that produced the output shown in the 1st post is directly from the velocity-1.7.jar-file.
    See velocity-1.7.jar:org/apache/velocity/runtime/defaults/velocity.properties. You can change it (unpack-change-zip-rename-cycle), but there has to be a better way.

    @Isomorphic: Any hints?
    Also, this would not solve the integration of velocity-tools.

    Best regards,
    Blama

    Comment


      #3
      At the moment, we do not provide a mechanism for altering the default configuration of the VelocityEngine. We will likely add this as a feature in a future release.

      However, what you can do is alter the default velocity configuration to include your custom properties. As described in this section of the Velocity docs https://click.apache.org/docs/veloci...eys_and_Values, the default config file is wrapped in the Velocity jar and lives in org/apache/velocity/runtime/defaults/velocity.defaults.

      So what you need to do is unjar the velocity jar, modify this file to include the config you need (which will have to be in .properties format instead of xml, but this is well documented here: http://velocity.apache.org/tools/rel...roperties.html - they show a sophisticated config in both XML and .properties format). Then you will need to re-jar and use this modified jar for your app.

      I realize this is sub-optimal, but it will at least solve the problem in the short term. Longer term, we will likely provide a cleaner mechanism for this sort of thing.

      Hope this helps!

      Comment


        #4
        Hi Isomorphic,

        thanks for the hint to the jar'd velocity.properties file. It is not optimal, but considering this is a one-time configuration, it doesn't matter.

        I've been trying the whole day to get the Velocity Tools / Escape Tool to work - it just did not work out. Before I start debugging into the Velocity startup, could you show with a simple example how you use Velocity Tools?

        I'd need the entry for velocity.properties, most likely something easy like
        Code:
        ...old file contents...
        tools.toolbox = application
        tools.data.version = 2.0
        tools.data.version.type = number
        tools.application.date = org.apache.velocity.tools.generic.DateTool
        tools.application.esc = org.apache.velocity.tools.generic.EscapeTool
        an a hint on how to use it. Most likely $esc.html($NOTNULLFIELDNAME).
        Additionally I'd need the names of the JARs you dropped in the webapp (velocity-1.7.jar was already there, additionally velocity-tools-generic-2.0.jar or velocity-tools-2.0.jar) and (perhaps) some more apache-commons-jars (see linked tools dependencies above).

        It shouldn't be too difficult, but it just doesn't work. So any How-To (velocity.properties, template usage, deployed files) is greatly appreciated.

        Unfortunately Velocity isn't printing out much more than the initial startup log entries for me and it is not easy to google help on this topic as the primary usage of Velocity seems to be in a servlet / xml-configuration context, that does not apply to SmartGWT, so I had no success that way.

        Thank you & Best regards,
        Blama

        Comment


          #5
          It turns out that there's no way to make Velocity Tools available in a VelocityContext by making configuration changes to the VelocityEngine class. Instead, we would need to use a ToolManager http://velocity.apache.org/tools/rel...ml#ToolManager to create a custom VelocityContext and then use that VelocityContext to parse the template. This ToolManager is what would consume the configuration that we were both looking at (http://velocity.apache.org/tools/rel...roperties.html).

          I think that it would be helpful to enable side-loading of arbitrary tools into the VelocityContext that SC/SGWT creates and we will add this support in a future release.

          For your immediate needs, you should be able to make the Escape tool available as an attribute on the request and then access it via the $requestAttributes handle as described here: http://www.smartclient.com/smartgwte...tySupport.html

          To do this globally, you will need to implement a trivial subclass of IDACall http://www.smartclient.com/smartgwte...t/IDACall.html. Simply provide a custom implementation of the processRequest method in your subclass that does the following:

          Code:
          request.setAttribute("esc", new org.apache.velocity.tools.generic.EscapeTool());
          super.processRequest(request, response)
          Then, modify your web.xml to map the IDACall to your subclass. Then, you should be able to access this escape tool in all your velocity templates as:

          Code:
          $requestAttributes.esc.escapeHTML(foo)

          Comment


            #6
            Hi Isomorphic,

            thank you very much for your fast answer and for the findings included.

            With your help I could solve the problem. I added this code to my SQLDataSource-subcalls (as I need the EscapeTool in every (fetch-)request, not only every client-request):

            Code:
            @Override
            public DSResponse executeFetch(DSRequest request) throws Exception {
            	[B]request.addToTemplateContext("esc", new EscapeTool());[/B]
            	...my old code...
            	return super.executeFetch(request);
            }
            With this, I can escape my mailtext-tokens in my <mail>-tag in my fetch-Operationbinding:
            Code:
            <tr>
            	<td width="160" bgcolor="#666666"><b>Comment:</b></td>
            	<td width="500" bgcolor="#DDDDDD">[B]$esc.html($STATUS_STATUSCOMMENT)[/B]</td>
            </tr>
            I hope this helps everyone else finding this thread as well. I added only velocity-tools-generic-2.0.jar and no other (additional) apache-commons libs.

            If it becomes possible to configure Velocity more in the future, this is great; but from my current point of view everything is working now.

            Best regards,
            Blama

            Comment


              #7
              That's even better - nicely done.

              We will still support the standard tools.xml way of configuring additional velocity tool extensions because most people will just expect that to work.

              Comment

              Working...
              X