Core J2EE Patterns: Intercepting Filter Pattern
You want to intercept and manipulate a request and a response before and after the request is processed.
Preprocessing and postprocessing of a request refer to actions taken before and after the core processing of that request. Some of these actions determine whether processing will continue, while others manipulate the incoming or outgoing data stream into a form suitable for further processing. For example:
- Does the client have a valid session?
- Does the request path violate any constraints?
- Do you support the browser type of the client?
- What encoding does the client use to send the data?
- Is the request stream encrypted or compressed?
A common approach to handling these and other types of processing requests is to implement a series of conditional checks, typically with nested if/then/else statements to control the flow of execution. However, since you do similar types of things in each of these processing statements, you have a great deal of duplicated code. Handling preprocessing and postprocessing in this way leads to code fragility and a copy-and-paste style of programming because the flow of the control and the specific processing behavior is compiled into the application.
Additionally, this approach leads to increased coupling between the pre and postprocessing components and the core application processing code.