Intercepting Filter Pattern
The Intercepting Filter Pattern (Intercepting Filter Pattern) is used to perform some pre-processing/post-processing of an application's requests or responses. Filters are defined and applied to the request before passing it to the actual target application. Filters can perform authentication/authorization, logging, or request tracking, and then pass the request to the appropriate handler. The following are the entities of this design pattern.
- Filter (Filter) - The filter performs certain tasks before or after the request handler executes the request.
- Filter Chain (Filter Chain) - The filter chain carries multiple filters and executes these filters on the Target in a defined order.
- Target - The Target object is the request handler.
- Filter Manager (Filter Manager) - The filter manager manages filters and filter chains.
- Client - The Client is the object that sends requests to the Target object.
Summary
Intent
Used to pre-process and post-process requests through a series of filters before the request reaches its final destination.
Main problem solved
- Solves the problem in web applications of needing to perform common operations (such as logging, security checks, transaction management, etc.) before and after request processing.
Use Cases
- When extra logic needs to be inserted into the request processing flow, and that logic is unrelated to business logic.
Implementation
- Filter chain: A series of filters executed in a specific order.
- Filter: The component that pre-processes and post-processes requests and responses.
- Target object: The final destination of request processing.
Key code
- Filter interface: Defines the methods a filter must implement, such as
before()andafter()。 - Concrete filter: Implements the filter interface and contains specific processing logic.
- Filter manager: Responsible for maintaining the filter chain and the invocation order of filters.
Application Example
- Web application server: Such as Tomcat, which uses filters to intercept requests, for example permission checks, logging, etc.
Advantages
- Separation of responsibilities: Separates common operations from business logic, improving code maintainability.
- Extensibility: Filters can be flexibly added, removed, or modified.
- Reusability: Filters can be reused across different request processing.
Disadvantages
- Performance Impact: If the filter chain is too long or the operations performed by filters are complex, performance may be affected.
Usage recommendation
- When common operations unrelated to business logic need to be inserted into the request processing flow, consider using the Intercepting Filter pattern.
Notes
- Ensure the execution order of filters meets expectations to avoid logical errors.
Main roles included
-
Filter Interface (Filter Interface):
-
Concrete Filter (Concrete Filter):
-
Filter Manager (Filter Manager):
-
Target Object (Target):
-
Client (optional):
The Intercepting Filter pattern helps keep business logic clear and focused by inserting common operations into the request processing flow.
Create a class that implements
We will create FilterChain、FilterManager、Target、Client As the various objects representing entities.AuthenticationFilter and DebugFilter Represent entity filters.
InterceptingFilterDemo Class uses Client To demonstrate the Intercepting Filter design pattern.
Step 1
Create the filter interface Filter.
Filter.java
public
interface
Filter
{
public
void
execute
(
String
request
)
;
}
Step 2
Create the entity filters.
AuthenticationFilter.java
public
class
AuthenticationFilter
implements
Filter
{
public
void
execute
(
String
request
)
{
System
.
out
.
println
(
"
Authenticating request:
"
request
)
;
}
}
DebugFilter.java
public
class
DebugFilter
implements
Filter
{
public
void
execute
(
String
request
)
{
System
.
out
.
println
(
"
request log:
"
request
)
;
}
}
Step 3
Create the Target.
Target.java
public
class
Target
{
public
void
execute
(
String
request
)
{
System
.
out
.
println
(
"
Executing request:
"
request
)
;
}
}
Step 4
Create the filter chain.
FilterChain.java
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
FilterChain
{
private
List
<
Filter
filters
=
new
ArrayList
<
Filter
(
);
private
Target
target
;
public
void
addFilter
(
Filter
filter
)
{
filters
.
add
(
filter
)
;
}
public
void
execute
(
String
request
)
{
for
(
Filter
filter
:
filters
)
{
filter
.
execute
(
request
)
;
}
target
.
execute
(
request
)
;
}
public
void
setTarget
(
Target
target
)
{
this
.
target
=
target
;
}
}
Step 5
Create the filter manager.
FilterManager.java
public
class
FilterManager
{
FilterChain
filterChain
;
public
FilterManager
(
Target
target
)
{
filterChain
=
new
FilterChain
(
)
;
filterChain
.
setTarget
(
target
)
;
}
public
void
setFilter
(
Filter
filter
)
{
filterChain
.
addFilter
(
filter
)
;
}
public
void
filterRequest
(
String
request
)
{
filterChain
.
execute
(
request
)
;
}
}
Step 6
Create the client Client.
Client.java
public
class
Client
{
FilterManager
filterManager
;
public
void
setFilterManager
(
FilterManager
filterManager
)
{
this
.
filterManager
=
filterManager
;
}
public
void
sendRequest
(
String
request
)
{
filterManager
.
filterRequest
(
request
)
;
}
}
Step 7
Use Client To demonstrate the Intercepting Filter design pattern.
InterceptingFilterDemo.java
public
class
InterceptingFilterDemo
{
public
static
void
main
(
String
[
]
args
)
{
FilterManager
filterManager
=
new
FilterManager
(
new
Target
(
)
)
;
filterManager
.
setFilter
(
new
AuthenticationFilter
(
)
)
;
filterManager
.
setFilter
(
new
DebugFilter
(
)
)
;
Client
client
=
new
Client
(
)
;
client
.
setFilterManager
(
filterManager
)
;
client
.
sendRequest
(
"
HOME
"
)
;
}
}
Step 8
Run the program, output result:
Authenticating request: HOME
request log: HOME
Executing request: HOME