Servlet Filter Plugin Module

Available:

Servlet Filter plugin modules are available in JIRA 4.0 and later.

Purpose of this Module Type

Servlet Filter plugin modules allow you to deploy Java Servlet filters as a part of your plugin, specifying the location and ordering of your filter. This allows you to build filters that can tackle tasks like profiling and monitoring as well as content generation.

Configuration

The root element for the Servlet Filter plugin module is servlet-filter. It allows the following attributes and child elements for configuration:

Attributes

Name class
Required  
Description The class which implements this plugin module. The class you need to provide depends on the module type. For example, Confluence theme, layout and colour-scheme modules can use classes already provided in Confluence. So you can write a theme-plugin without any Java code. But for macro and listener modules you need to write your own implementing class and include it in your plugin. See the plugin framework guide to creating plugin module instances. The servlet filter Java class must implement javax.servlet.Filter.
Default  

Name state
Required  
Description Indicate whether the plugin module should be disabled by default (value='disabled') or enabled by default (value='enabled').
Default  enabled

Name i18n-name-key
Required  
Description The localisation key for the human-readable name of the plugin module.
Default  

Name key
Required  Yes
Description The unique identifier of the plugin module. You refer to this key to use the resource from other contexts in your plugin, such as from the plugin Java code or JavaScript resources.
<component-import key="appProps" interface="com.atlassian.sal.api.ApplicationProperties"/>

In the example, appProps is the key for this particular module declaration, for component-import, in this case.

I.e. the identifier of the servlet filter.
Default N/A

Name location
Required  
Description

The position of the filter in the application's filter chain. If two plugins provide filters at the same position, the 'weight' attribute (see below) is evaluated.

  • after-encoding - Near the very top of the filter chain in the application, but after any filters which ensure the integrity of the request.
  • before-login - Before the filter that logs in the user with any authentication information included in the request.
  • before-decoration - Before the filter which does decoration of the response, typically with Sitemesh.
  • before-dispatch - At the end of the filter chain, before any servlet or filter which handles the request by default.
Default before-dispatch

Name name
Required  
Description

The human-readable name of the plugin module. I.e. the human-readable name of the filter.

Default The plugin key

Name system
Required  
Description

Indicates whether this plugin module is a system plugin module (value='true') or not (value='false'). Only available for non-OSGi plugins.

Default false

Name weight
Required  
Description

The weight of the filter, used to decide which order to place the filter in the chain for filters which have specified the same 'location' attribute (see above). The higher weight, the lower the filter's position.

Default 100

Elements

Name description
Required  
Description

The description of the plugin module. The 'key' attribute can be specified to declare a localisation key for the value instead of text in the element body. I.e. the description of the filter.

Default  

Name init-param
Required  
Description

Initialisation parameters for the filter, specified using param-name and param-value sub-elements, just as in web.xml. This element and its child elements may be repeated.

Default N/A

Name resource
Required  
Description

A resource for this plugin module. This element may be repeated. A 'resource' is a non-Java file that a plugin may need in order to operate. Refer to Adding Resources to your Project for details on defining a resource.

Default N/A

Name url-pattern
Required Yes
Description

The pattern of the URL to match. This element may be repeated.

The URL pattern format is used in Atlassian plugin types to map them to URLs. On the whole, the pattern rules are consistent with those defined in the Servlet 2.3 API. The following wildcards are supported:

  • * matches zero or many characters, including directory slashes
  • ? matches zero or one character
Examples
  • /mydir/* matches /mydir/myfile.xml
  • /*/admin/*.??ml matches /mydir/otherdir/admin/myfile.html

Default N/A

Name dispatcher
Required  
Description

Determines when the filter is triggered. You can include multiple dispatcher elements.
If this element is present, its content must be one of the following, corresponding to the filter dispatcher options defined in the Java Servlet 2.4 specification:

  • REQUEST: the filter applies to requests that came directly from the client
  • INCLUDE: the filter applies to server-side includes done via RequestDispatcher.include()
  • FORWARD: the filter applies to requests that are forwarded via RequestDispatcher.forward()
  • ERROR: the filter applies to requests that are handled by an error page
    Note: This element is only available in Plugin Framework 2.5 and later.
    If this element is not present, the default is REQUEST. (This is also the behaviour for Plugin Framework releases earlier than 2.5.)
Default Filter will be triggered only for requests from the client

Example

Here is an example atlassian-plugin.xml file containing a single servlet filter:

<atlassian-plugin name="Hello World Filter" key="example.plugin.helloworld" plugins-version="2">
    <plugin-info>
        <description>A basic Servlet filter module test - says "Hello World!</description>
        <vendor name="Atlassian Software Systems" url="http://www.atlassian.com"/>
        <version>1.0</version>
    </plugin-info>

    <servlet-filter name="Hello World Servlet" key="helloWorld" class="com.example.myplugins.helloworld.HelloWorldFilter" location="before-dispatch" weight="200">
        <description>Says Hello World, Australia or your name.</description>
        <url-pattern>/helloworld</url-pattern>
        <init-param>
            <param-name>defaultName</param-name>
            <param-value>Australia</param-value>
        </init-param>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </servlet-filter>
</atlassian-plugin>

Accessing your Servlet Filter

Your servlet will be accessible within the Atlassian web application via each url-pattern you specify, but unlike the Servlet Plugin Module, the url-pattern is relative to the root of the web application.

For example, if you specify a url-pattern of /helloworld as above, and your Atlassian application was deployed at http://yourserver/jira — then your servlet filter would be accessed at http://yourserver/jira/helloworld .

Notes

Some information to be aware of when developing or configuring a Servlet Filter plugin module:

  • Your servlet filter's init() method will not be called on web application startup, as for a normal filter. Instead, this method will be called the first time your filter is accessed after each time it is enabled. This means that if you disable a plugin containing a filter or a single servlet filter module, and re-enable it again, the filter will be re-created and its init() method will be called again.
  • Because servlet filters are deployed beneath root, be careful when choosing each url-pattern under which your filter is deployed. If you plan to handle the request in the filter, it is recommended to use a value that will always be unique to the world!
  • Some application servers, like WebSphere 6.1, won't call servlet filters if there is no underlying servlet to match the URL. On these systems, you will only be able to create a filter to handle normal application URLs.

RELATED TOPICS

Information sourced from Plugin Framework documentation

Was this page helpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport