Create a Custom Valve
Overview
This page describes how to implement a custom valve and add it to the Bloomreach Content delivery tier's request processing pipeline using Spring configuration.
When to Use
Use a custom valve when you need to add project-specific request processing logic to the HST request pipeline. You can position your custom valve at any point in the pipeline.
Prerequisites
- A Bloomreach Content project with a working delivery tier.
- Familiarity with Java and Spring configuration.
- Access to the
site/componentsmodule in your project.
Request Processing Pipeline
In Bloomreach Content, the delivery framework (HST) processes incoming requests through a sequence of valves. These valves are assembled into a pipeline. You can inject custom valves into the default pipeline to modify or extend request handling.
The default pipeline is defined as DefaultSitePipeline in
classpath:/org/hippoecm/hst/site/container/SpringComponentManager-pipelines.xml.
Implementation Steps
1. Implement a Custom Valve
Create a Java class that extends org.hippoecm.hst.container.valves.AbstractOrderableValve. Override the invoke method to add your processing logic. Always call context.invokeNext() to continue the pipeline.
Example:
site/components/src/main/java/org/example/container/MyCustomValve.java
package org.example.container; import org.hippoecm.hst.container.valves.AbstractOrderableValve; import org.hippoecm.hst.core.container.ContainerException; import org.hippoecm.hst.core.container.ValveContext; public class MyCustomValve extends AbstractOrderableValve { @Override public void invoke(ValveContext context) throws ContainerException { try { // perform custom processing here } finally { context.invokeNext(); } } }
2. Configure the Custom Valve in Spring
Define your custom valve as a Spring bean. Set the valveName property, and use beforeValves and afterValves to control its position in the pipeline.
Best practice: Set valveName to match the bean id.
You can specify multiple valves in beforeValves or afterValves as a comma-separated list.
Example:
site/components/src/main/resources/META-INF/hst-assembly/overrides/custom-valves.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myCustomValve" class="org.example.container.MyCustomValve"> <property name="valveName" value="myCustomValve" /> <property name="afterValves" value="securityValve" /> <property name="beforeValves" value="pageCachingValve" /> </bean> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="org.hippoecm.hst.core.container.Pipelines" /> <property name="targetMethod" value="getPipeline" /> <property name="arguments"> <value>DefaultSitePipeline</value> </property> </bean> </property> <property name="targetMethod" value="addProcessingValve" /> <property name="arguments"> <ref bean="myCustomValve" /> </property> </bean> </beans>
This configuration injects the custom valve into the DefaultSitePipeline after the securityValve and before the pageCachingValve.
3. Rebuild and Restart
Rebuild your project and restart the application to apply the changes.
For instructions, see Rebuilding and Restarting a Project.
Verification
Request a page from your site. The custom valve should be invoked as part of the request processing pipeline. Add logging or breakpoints in your valve to confirm execution.