Intercepting Filter Design Pattern
Intercepting Filter Design Pattern
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 1
JAVA Means DURGA SOFT
Intercepting Filter
Problem: When a request enters a Web application, it often must pass several entrance tests
prior to the main processing stage. For example,
Different type of processing is required for the different type of request received by the
presentation tier request handler. Some requests are simply forwarded to the appropriate
handler component, while other requests must be processed being further processed. We
require Preprocessing and post-processing of a client Web request and response.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 2
JAVA Means DURGA SOFT
Most applications have some requirements, such as security and logging that are
applicable across all application requests. To add such functionality separately to each
application service would be time-consuming, error-prone, and difficult to maintain. Even
implementing these services within a front controller would still require code changes to add
and remove services. The sequence diagram in Figure 1 below shows how each Web resource is
responsible for calling such services individually.
Solution: Create pluggable filters (Intercepting Filter) to process common services in a standard
manner without requiring changes to core request processing code. The filters intercept (traps)
incoming requests and outgoing responses, allowing preprocessing and post-processing. We
are able to add and remove these filters unobtrusively, without requiring changes to our
existing code.
Def: A pluggable component design to intercept incoming requests and outgoing responses,
provide common services in a standard manner (independently) without changing core
processing code.
The Intercepting Filter, as the name implies, intercepts the requests and helps separate
all the tasks that need to be done on the server into reusable components. The actions/filters
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 3
JAVA Means DURGA SOFT
can be assigned to specific requests, chained in any order, and added or removed within the
application configuration. After the filters complete, they pass the request to the intended
recipient, in this case Servlet or JSP. After the server is finished, a response is sent to the client.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 4
JAVA Means DURGA SOFT
In the above diagram, Filter traps both request and response; steps (1-7) talks about how
Servlet Filter trapping the request and response of Servlet1 and steps (i-vii) talks about how
Servlet Filter takes Http request from client and process that request to normal java class.
Note A Front controller can trap only request of other web resource programs like servlet, JSP
programs and it cannot trap response of them. But an Intecepting filter can trap both request &
response of other web resource programs like servlet, JSP programs.
The Servlet Filter interface in the Java 2 Platform, Enterprise Edition (J2EE) platform is a
direct implementation of the Intercepting Filter pattern.
In Struts 2.x environment a predefined servlet filter program i.e. org.apache.struts2.
dispatcher.FilterDispatcher is given as controller.
Advantages:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 5
JAVA Means DURGA SOFT
Intecepting filters centralizes control with Loosely Coupled Handlers, Filters provide a
central place for handling processing across multiple requests, as does a controller. Filters are
better suited to massaging requests and responses for ultimate handling by a target resource,
such as a controller. Additionally, a controller often ties together the management of numerous
unrelated common services, such as authentication, logging, encryption, and so forth, while
filtering allows for much more loosely coupled handlers, which can be combined in various
combinations.
But sharing information between filters can be inefficient, since by definition each filter
is loosely coupled. If large amounts of information must be shared between filters, then this
approach may prove to be costly.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 6
JAVA Means DURGA SOFT
The Intercepting Filter pattern wraps existing application resources with a filter that
intercepts the reception of a request and the transmission of a response. An intercepting filter
can pre-process or redirect application requests, and can post-process or replace the content of
application responses. Intercepting filters can also be stacked one on top of the other to add a
chain of separate, declaratively-deployable services to existing Web resources with no changes
to source code. Figure 2 below shows a chain of two intercepting filters intercepting requests to
two Web resources that they wrap.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 7
JAVA Means DURGA SOFT
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 8