title: Velocity integration with Jersey, ResourceTool does not initialize properly
body: <p>I'm integrating Velocity with Jersey and I'm facing a problem with ResourceTool for i18n.</p>
  
  <p>The integration is done as follow:</p>
  
  <p><strong>web.xml</strong> (striped, keeping with relevant parts):</p>
  
  <pre><code>&lt;!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2_3.dtd" &gt;
  &lt;web-app&gt;
     &lt;context-param&gt;
        &lt;param-name&gt;contextConfiguration&lt;/param-name&gt;
        &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
     &lt;context-param&gt;
     &lt;filter&gt;
        &lt;filter-name&gt;velocity-filter&lt;/filter-name&gt;
        &lt;filter-class&gt;org.apache.velocity.tools.view.VelocityViewFilter&lt;/filter-class&gt;
     &lt;/filter&gt;
     &lt;filter-mapping&gt;
        &lt;filter-name&gt;velocity-filter&lt;/filter-name&gt;
        &lt;servlet-name&gt;rest-servlet&lt;/servlet-name&gt;
     &lt;/filter-mapping&gt;
     &lt;listener&gt;
        &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
     &lt;/listener&gt;
     &lt;servlet&gt;
        &lt;servlet-name&gt;rest-servlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;com.sun.jersey.spi.spring.container.servlet.SpringServlet&lt;/servlet-class&gt;
        &lt;init-param&gt;
           &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
           &lt;param-value&gt;my.package.demo&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
     &lt;/servlet&gt;
     &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;rest-servlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
     &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Now, my <strong>tools.xml</strong> (in WEB-INF folder):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;                                               
  &lt;tools&gt;                                                                              
      &lt;data type="boolean" key="STRUTS_TOOLS_AVAILABLE" value="false"/&gt;                
      &lt;toolbox scope="request"&gt;                                                        
          &lt;tool class="org.apache.velocity.tools.generic.ResourceTool"                 
              bundles="my.package.demo.Resource" /&gt;                               
      &lt;/toolbox&gt;                                                                       
  &lt;/tools&gt;                                                                             
  </code></pre>
  
  <p>I have then created a basic <strong>VelocityTemplateProcessor</strong></p>
  
  <pre><code>package my.package.demo;
  
  import static com.google.common.base.Strings.isNullOrEmpty;
  
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.StringWriter;
  import java.io.Writer;
  import java.util.HashMap;
  import java.util.Map;
  
  import javax.servlet.ServletContext;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.ext.Provider;
  
  import org.apache.velocity.Template;
  import org.apache.velocity.exception.ResourceNotFoundException;
  import org.apache.velocity.tools.ToolContext;
  import org.apache.velocity.tools.view.ServletUtils;
  import org.apache.velocity.tools.view.VelocityView;
  
  import com.sun.jersey.api.core.HttpContext;
  import com.sun.jersey.api.view.Viewable;
  import com.sun.jersey.spi.template.ViewProcessor;
  
  @Provider
  public class VelocityTemplateProcessor implements ViewProcessor&lt;Template&gt; {
  
      private static Map&lt;MediaType, String&gt; fileExtension;
  
      static {
          fileExtension = new HashMap&lt;&gt;();
          fileExtension.put(MediaType.TEXT_HTML_TYPE, ".html.vm");
          fileExtension.put(MediaType.APPLICATION_JSON_TYPE, ".json.vm");
          fileExtension.put(MediaType.APPLICATION_XML_TYPE, ".xml.vm");
      }
  
      @Context
      private HttpContext httpContext;
  
      private final VelocityView velocityView;
  
      public VelocityTemplateProcessor(@Context final ServletContext servletContext) {
          velocityView = ServletUtils.getVelocityView(servletContext);
      }
  
      @Override
      public Template resolve(final String name) {
          if (isNullOrEmpty(name)) {
              return null;
          }
          final MediaType mediaType = httpContext.getResponse().getMediaType();
          final String extension = fileExtension.get(mediaType);
          if (isNullOrEmpty(extension)) {
              return null;
          }
          try {
              return velocityView.getTemplate(name + extension);
          } catch (final ResourceNotFoundException e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public void writeTo(final Template t, final Viewable viewable, final OutputStream out) throws IOException {
          final ToolContext context = velocityView.createContext();
          context.put("data", viewable.getModel());
          final Writer writer = new StringWriter();
          t.merge(context, writer);
          out.write(writer.toString().getBytes());
      }
  
  }
  </code></pre>
  
  <p>This works well, except for the ResourceTool. It seems properly loaded with the requested configuration (at least, that's how it appears in the ToolContext), however, when I try to call the <code>$text.my.property</code> inside a template, it seems that it uses a ResourceTool with the default configuration (with bundle named "resources"), and then fail to find my key.<br>
  It works when I explicitly mention <code>$text.bundle('my.package.demo.Resource').my.property</code> but I would like to make it more transparent for the peoples that will manage the templates.</p>