Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin


Wiki Markup
{scrollbar}


Excerpt
hiddentrue

Adding an Annotation and a Filter to customize Tapestry's page rendering

...

Again, this could be done by having a specific base-class that included a beginRender() method, but I think you'll see that the meta-programming approach is nearly as easy and much more flexible.

...

Code Block
languagejava
titleFnordSymbols.java
package com.fnord;

import org.apache.tapestry5.http.services.BaseURLSource;

import com.fnord.annotations.ForbidFraming;

public class FnordSymbols {

  /**
   * Meta-data key; when true, MarkupRendererFilter will inject some extra
   * content into the response to enforce that the content may not be framed
   * (i.e., "stolen").
   * 
   * @see ForbidFraming
   */
  public static final String FORBID_FRAMING = "forbid-framing";

}

...

Code Block
languagejava
titleForbidFramingModule.class
package com.fnord.services.forbidframing;

import org.apache.tapestry5.ioccommons.MappedConfiguration;
import org.apache.tapestry5.ioc.annotations.Contribute;
import org.apache.tapestry5.ioc.services.FactoryDefaults;
import org.apache.tapestry5.ioc.services.SymbolProvider;

import com.fnord.FnordSymbols;

public class ForbidFramingModule {

  @Contribute(SymbolProvider.class)
  @FactoryDefaults
  public static void setupForbidFramingDefault(
      MappedConfiguration<String, String> configuration) {
    configuration.add(FnordSymbols.FORBID_FRAMING, "false");
  }
}

...

The work we ultimately want to do occurs when rendering a page. Tapestry defines a pipeline for that overall process. The point of a pipeline is that we can add filters to it. We'll add a filter that checks for the meta-data key and adds the response header and JavaScript.

...

How do you know what filters are built-in and where to add your own? The right starting point is the JavaDoc for the method of TapestryModule that contributes the base set: contributeMarkupRenderer()

Implementing the Filter

Everything comes together in the filter:

...

There's a bit going on in this short piece of code. The heart of the code is the MetaDataLocator service; given a meta-data key and a page name, it can not only extract the value, but then coerce it to a desired type, all in one go.

...

Note

The code in this example was designed for Tapestry version 5.2 and later. Some names were changed to maintain the anonymity of the client (whose project is still secret at the time of writing).