Magnolia 5.4 reached end of life on November 15, 2018. This branch is no longer supported, see End-of-life policy.
This page explains how to create and configure a custom Filter.
Please read about Filters and request processing which explains some filters used by Magnolia.
To ensure your filter is properly registered using Magnolia configuration, your filter should implement
Example:
public class KlackerFilter extends AbstractMgnlFilter{ @Override public void doFilter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws IOException, ServletException { httpServletResponse.setHeader("X-Clacks-Overhead", "GNU Terry Pratchett" ); filterChain.doFilter(httpServletRequest, httpServletResponse); } }
The example above is a dummy example inspired by GNU Terry Pratchett HTTP-Header.
Filters in a Java webapp using servlets 2.5 must be registered in the web.xml
. However:
When adding a filter to Magnolia - you should not register it in web.xml but on Magnolia configuration. If the configuration is done properly - filters are registered dynamically by Magnolia.
You can configure filters implementing
As with any other configuration data - bootstrap the configuration within your module with a bootstrap file or create an installation task to ensure configuration is set correctly when your project must be reinstalled or deployed.
/server/filters
To configure a filter add a node to /server/filters:
Node name | |
---|---|
server | |
filters | |
klacker | |
class | info.magnolia.documentation.examples.templates.KlackerFilter |
enabled | true |
class | required The fully qualified class name of your custom filter (which must implement $webResourceManager.requireResource("info.magnolia.sys.confluence.artifact-info-plugin:javadoc-resource-macro-resources")
MgnlFilter
). |
enabled | optional, default is Setting to false disables the filter. |
mappings | optional Add mappings. |
If you have a filter which can not implement
Node name | |
---|---|
filters | |
anotherFilter | |
class | info.magnolia.cms.filters.FilterDecorator |
decoratedFilter | |
class | info.magnolia.documentation.examples.templates.AnotherFilter |
config | |
myInitParam | some value |
class | required In this case class must be info.magnolia.cms.filters.FilterDecorator. |
| required |
| required The fully qualified class name of your custom filter (which does not implement $webResourceManager.requireResource("info.magnolia.sys.confluence.artifact-info-plugin:javadoc-resource-macro-resources")
AbstractMgnlFilter
). |
| optional A map to add filter init parameters. You may add as many parameters as required. The init parameters can be read in the init method of the filter (see below). |
javax.servlet.Filter has an #init method which is called once when the filter is initialized.
When using filters implementing
If you are using FilterDecorator for custom filters not implementing
Filters are executed in the order they are in the chain, from top to bottom. If you do not care about execution order, you can skip this section, otherwise make sure the filter is in the right position in the chain.
Magnolia provides task classes to adapt the order of the filter via module version handler. Use FilterOrderingTask or OrderFilterBeforeTask. The code example below is using the FilterOrderingTask
.
When installing a module, you can define the position with ModuleVersionHandler#getExtraInstallTasks
.
public class MyModuleVersionHandler extends DefaultModuleVersionHandler { @Override protected List<Task> getExtraInstallTasks(InstallContext installContext) { List<Task> extraInstallTasks = new ArrayList<Task>(super.getExtraInstallTasks(installContext)); tasks.extraInstallTasks(new FilterOrderingTask("klacker", new String[]{"contentType"})); return extraInstallTasks; } }
The code above would ensure to have the filter "klacker" right after the filter "contentType".