+

Search Tips   |   Advanced Search

Create a content URL generation filter class

A content URL generation filter is used to customize the URLs generated by a web content viewer. By creating a plug-in that implements a content URL generation filter, we can tailor the URLs to content items. The web content viewer generates a content URL whenever there is a URL to web content within content the viewer is displaying. The content URL generation filter chain is a chain of filters based on the Intercepting Filter design pattern. This design pattern provides a mechanism for intercepting a request and manipulating the request and its response. When used with requests for content URL generation, the content URL generation filter chain has default filters that perform the following actions:

  • Process any request to generate a URL that target a web content item.
  • Create a URL based on the configuration of the web content viewer and any parameters set on the URL generation tags, such as the URLCmpnt tag.

The default filters occur at the end of the filter chain.

We can customize the content URL generation filter chain by creating custom filters. These filters are registered with the portal through the Eclipse plug-in framework with the extension ID com.ibm.workplace.wcm.api.ContentUrlGenerationFilter. The sequence of filters in the filter chain is specified by a weight value associated with each filter plug-in. To insert custom filters into the filter chain before the default filters, use the weight attribute in the plugin.xml file. If the weight attribute is not present, filter sequence is determined by the getFilterChainWeight method of each custom filter factory.

Custom content URL generation filters can perform various actions:

  • Modify parameters before calling the default URL generation filters.
  • Modify the URL generated by the default filters.
  • Handle exceptions generated by the default filters.
  • Determine whether the default filters are invoked.
  • Modify the content path used as input for the default URL generation filters.
  • Generate any type of URL without using the default URL generation filters.

To use a content URL generation filter, create two classes:

  • A content URL generation filter used to create URLs.
  • A content URL generation filter factory used to create new instances of the filter.

We must deploy both classes on the server and register the filter factory using a plugin.xml file.

  1. Create a Java class that implements the interface com.ibm.workplace.wcm.api.extensions.url.ContentUrlGenerationFilterFactory. This class implement the following methods:

      public ContentUrlGenerationFilter getFilter(RenderRequest portletRequest, RenderResponse portletResponse) throws ContentUrlFilterInstantiationException

      This method is called by the content URL generation chain once for each content item rendered. The method creates an instance of a content URL generation filter, which generates all URLs that appear in the rendered web content.

      public int getFilterChainWeight() {}

      This method returns the weight applied to the content URL generation filter elements in the filter chain. The less it is weighted, the earlier the filter is inserted into the chain. If the weight parameter is defined in the plugin.xml file, that value overrides the value returned by this method.

    See the Javadoc documentation for further information. The Javadoc files for WCM are located in the PORTAL_HOME/doc/Javadoc/spi_docs/com/ibm/workplace/wcm directory.

  2. Create a Java class that implements the interface com.ibm.workplace.wcm.api.extensions.url.ContentUrlGenerationFilter. This class implement the following methods:

      public void writeURL(ContentUrlGenerationRequest request, ContentUrlGenerationResponse response, ContentUrlGenerationFilterChain chain) throws ContentUrlGenerationException, IOException

      This method is invoked during content URL generation processing and is invoked once per content URL. The response parameter enables us to write to the URL. The request parameter contains the following information:

      • Information about the item for which the URL is generated.
      • Any web content viewer configuration and related information that might affect the generation of the URL.

      The filter chain contains the subsequent filters that can be invoked when needed.

      public void dispose()

      This method is invoked by the filter chain after all URLs that appear in the rendered content items are written. The method enables filters to perform a cleanup of their internal state.

    For more information about this class, see the Javadoc documentation.

  3. A plugin.xml file is needed whether the deployment is done using a WAR or EAR file, or using a JAR file. If we deploy with an application in a WAR or EAR file, include the plugin.xml file in the "WEB-INF" folder. When using a JAR file, include the plugin.xml in the root of the JAR file.
    <?xml version="1.0" encoding="UTF-8"?>
    <plugin 
           name="Sample content URL generation filter"
           version="1.0.0"
           provider-name="IBM">
             <extension point="com.ibm.workplace.wcm.api.ContentUrlGenerationFilter">
        <factory class="com.example.SampleContentUrlGenerationFilterFactory" weight="1"/>
     </extension>
     </plugin>

    When creating plug-ins, note the following characteristics:

    • Each plug-in is represented by a single <extension> tag.
    • The value of the extension point attribute must be com.ibm.workplace.wcm.api.ContentUrlGenerationFilter.
    • Provide an ID value of the choice.
    • Specify the filter factory class for the plug-in.
    • The weight parameter overrides the value of the getFilterChainWeight method

    Naming conventions:

    If we create a new plug-in application with the same names and IDs as an existing plug-in, the new plug-in might override the first. When creating plug-in applications ensure the following elements are unique across your system:

    • The plug-in ID, plug-in name, and extension ID of the plugin.xml file.
    • The fully qualified class name and path of all classes within the application.
    • The file path of any files within the application.


Parent Create custom plug-ins

Related concepts:

Friendly URLs and Web Content Viewers