Uploaded image for project: 'Velocity'
  1. Velocity
  2. VELOCITY-734

Add name of missing resource to Velocity context for the IncludeNotFound event handler implementation

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.7
    • Component/s: Engine
    • Labels:
      None

      Description

      When a resource is not found, the IncludeNotFound event handler will insert a "notfound.vm" template into the stream. This provides a robust way of finishing the rendering process. It would be very helpful if the notfound template could also inform the user of the full name of the missing resource.

      The following patch fixes this problem. I've made IncludeNotFound implement the ContextAware interface. It then addes the name of the missing resource to the context so it can be referenced from the notfound template. I suppose it might also be nice to make the name of the reference configurable . . . .

      Index: /home/readyportal/Desktop/machineOneWorkspaces/bipSpace/velocity/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
      ===================================================================
      — /home/readyportal/Desktop/machineOneWorkspaces/bipSpace/velocity/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java (revision 809033)
      +++ /home/readyportal/Desktop/machineOneWorkspaces/bipSpace/velocity/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java (working copy)
      @@ -20,7 +20,9 @@
      */

      import org.apache.velocity.app.event.IncludeEventHandler;
      +import org.apache.velocity.context.Context;
      import org.apache.velocity.runtime.RuntimeServices;
      +import org.apache.velocity.util.ContextAware;
      import org.apache.velocity.util.RuntimeServicesAware;
      import org.apache.velocity.util.StringUtils;

      @@ -36,6 +38,13 @@

      • eventhandler.include.notfound = error.vm
      • </PRE>
      • </code>
        + *
        + * <p>
        + * The name of the missing resource is put into the Velocity context, under the
        + * key "missingResource", so that the notfound.vm template can report the missing
        + * resource with a Velocity reference, like:
        + * <code>$missingResource</code>
        + * </p>
        *
      • @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
      • @version $Id$
        @@ -41,7 +50,7 @@
      • @version $Id$
      • @since 1.5
        */
        -public class IncludeNotFound implements IncludeEventHandler,RuntimeServicesAware {
        +public class IncludeNotFound implements IncludeEventHandler,RuntimeServicesAware, ContextAware {

      private static final String DEFAULT_NOT_FOUND = "notfound.vm";
      private static final String PROPERTY_NOT_FOUND = "eventhandler.include.notfound";
      @@ -47,6 +56,7 @@
      private static final String PROPERTY_NOT_FOUND = "eventhandler.include.notfound";
      private RuntimeServices rs = null;
      String notfound;
      + Context context;

      /**

      • Chseck to see if included file exists, and display "not found" page if it
        @@ -70,6 +80,11 @@
        boolean exists = (rs.getLoaderNameForResource(includeResourcePath) != null);
        if (!exists)
        {
        + /**
        + * Put the missingResource name in the velocity context so that the notfound.vm can report the info to the user.
        + */
        + context.put("missingResource", includeResourcePath );
        +
        if (rs.getLoaderNameForResource(notfound) != null) { return notfound; @@ -99,4 +114,12 @@ notfound = StringUtils.nullTrim(rs.getString(PROPERTY_NOT_FOUND, DEFAULT_NOT_FOUND)); }

      +
      + /**
      + * Part of the ContextAware interface.
      + */
      + public void setContext(Context context)

      { + this.context = context; + }

      +
      }

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              chadmichael chad davis
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 0.25h
                0.25h
                Remaining:
                Remaining Estimate - 0.25h
                0.25h
                Logged:
                Time Spent - Not Specified
                Not Specified