SAP Commerce(Hybris) Platform Filters for Web Applications
Hybris Platform Filters is a solution that provides a set of filters used for Web applications. Filter chain which is the representation of these filters can be modified according to our needs.
Every Web application requires Platform Filters to ensure that Platform works properly. All available filters are a part of Platform, but not all of them are necessary for every web application.
Filters which are needed for proper handling of Catalog versions in current session are:
- SimpleCatalogVersionActivationFilter: It assures that configured names of catalog versions are set as session catalog versions.
- DynamicCatalogVersionActivationFilter: It takes care for activating the catalog versions at runtime. It allows to activate them also with proper URL parameter.
Other filters available with different use are: SessionFilter, ProfileFilter, Log4jFilter, DataSourceSwitchingFilter, SecureMediaFilter, WebAppMediaFilter.
How to use filters?
Every HTTP request triggers the PlatformFilterChain. Before the request hits our controller, it goes through this filter chain. All the filters that are triggered are configured as the part of PlatformFilterChain in the spring application context. The default set of filters that are required by almost all the web applications are configured in a file core-filter-spring.xml.
In core-filter-spring.xml we can see some of the default filters that are used by all the web applications in SAP Commerce(Hybris).
However, every Web application in addition can instantiate other filters in their own Spring application context file. We can add as many filters to our web extension as we want.
Every web extension needs its own filter chain based upon ‘PlatformFilterChain’ bean.
We can configure our own filter chain definition for our “custom” extension:
1. In filter-config-spring.xml file, we can define our filter chain using the PlatformFilterChain bean and also define our custom filters that can be included in our custom filter chain.
2. We should activate the filter chain in the web.xml of the extensions:
3. And we should set the mapping correctly in the web.xml file:
Creating custom Filter
For creating our own filter, we need to create a java class in our web extension that would extend from ‘GenericFilterBean’. When creating a custom filter we need to implement the doFilter() method as without it the filter chain is going to break.
- Everything we put before the filterChain.doFilter() method is executed before the request is finished.
- Everything after that method is executed after the request is finished.
After we implement our logic for this custom filter, we can follow the same steps shown above to configure the filter.
More you can learn here: https://help.sap.com/viewer/d0224eca81e249cb821f2cdf45a82ace/1808/en-US/8be9c15b866910149c1bcbec75a42bbf.html
Make sure you give this post a clap and follow my blog if you find it helpful.