Design Pattern

Intercepting Filter Design Pattern – Core J2EE Patterns

<p>Intercepting filter Design Pattern is one of the Java EE patterns&period; It is capable of creating pluggable filters which are responsible for processing common services&period; These services are processed in such a manner that there no changes required by the core request processing code&period; These filters are set to seize the requests coming in and responses going out&comma; which allow the system pre and post processing&period; These filters can be removed or added in an unnoticeable manner which does not need the existing code to be altered&period; The intercepting filter pattern implicates the processing transparently which is also reusable after and before the standard request executes by page and front controllers&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px 16px 16px 16px&semi;">&NewLine;<h2 dir&equals;"ltr">Spring 5 Design Pattern Book<&sol;h2>&NewLine;<div dir&equals;"ltr" style&equals;"color&colon; &num;20124d&semi; font-family&colon; 'calibri' &comma; sans-serif&semi; font-size&colon; 13pt&semi; text-align&colon; justify&semi;">You could purchase my <strong>Spring 5 book<&sol;strong> that is with title name &&num;8220&semi;<strong>Spring 5 Design Patterns<&sol;strong>&&num;8220&semi;&period; This book is available on the <a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;ie&equals;UTF8&amp&semi;qid&equals;1507925340&amp&semi;sr&equals;8-1&amp&semi;keywords&equals;spring&plus;5&plus;design&plus;pattern" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Amazon<&sol;strong><&sol;a> and <a href&equals;"https&colon;&sol;&sol;www&period;packtpub&period;com&sol;application-development&sol;spring-5-design-patterns" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Packt<&sol;strong><&sol;a> publisher website&period; Learn various <strong>design patterns<&sol;strong> and <strong>best practices<&sol;strong> in Spring 5 and use them to solve common design problems&period; You could use author discount to purchase this book by using code- &&num;8220&semi;<strong>AUTHDIS40<&sol;strong>&&num;8220&semi;&period;<&sol;div>&NewLine;<div dir&equals;"ltr"><&sol;div>&NewLine;<div dir&equals;"ltr"><a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;s&equals;books&amp&semi;ie&equals;UTF8&amp&semi;qid&equals;1512607966&amp&semi;sr&equals;1-1&amp&semi;keywords&equals;dinesh&plus;rajput" target&equals;"&lowbar;blank" rel&equals;"noopener"><img class&equals;"aligncenter wp-image-3053 size-medium" title&equals;"Spring-5-Design-Pattern" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;03&sol;Spring-5-design-pattern-243x300&period;jpg" width&equals;"243" height&equals;"300" &sol;><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;<h2><strong>Intercepting filter Design Pattern<&sol;strong><&sol;h2>&NewLine;<p>The intercepting filters are applied and defined on a particular request before it passes the request to the actual aimed application&period; These filters are capable of authorizing&comma; authenticating&comma; tracking or logging the request&period; It then passes these request to their corresponding handlers&period; The structure of intercept design pattern is such that it has a filter&comma; filter chain&comma; filter manager&comma; target&comma; and client&period; Each of these handles their own part of the system&period;<&sol;p>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote>&NewLine;<p><strong>also read&colon;<&sol;strong><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;context-object-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;context-object-design-pattern&sol;">Context Object Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;front-controller-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;front-controller-design-pattern&sol;">Front Controller Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;application-controller-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;application-controller-design-pattern&sol;">Application Controller Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;view-helper-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;view-helper-design-pattern&sol;">View Helper Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;composite-view-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;composite-view-design-pattern&sol;">Composite View Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;dispatcher-view-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;dispatcher-view-design-pattern&sol;">Dispatcher View Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;service-to-worker&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;service-to-worker&sol;">Service to Worker Pattern<&sol;a><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<h3><strong>Filter <&sol;strong><&sol;h3>&NewLine;<p>The filter is responsible for performing a particular task before or after the execution of the request by its request handler&period;<&sol;p>&NewLine;<h3><strong>Target<&sol;strong><&sol;h3>&NewLine;<p>The target is&comma; in fact&comma; the target object- a request handler&period;<&sol;p>&NewLine;<h3><strong>Filter chain<&sol;strong><&sol;h3>&NewLine;<p>Filter chain carries a chain of filters which are to be implemented on the target&comma; in different and defined order&period;<&sol;p>&NewLine;<h3><strong>Filter manager<&sol;strong><&sol;h3>&NewLine;<p>Filter manager is responsible for managing the filters and filter chain&period;<&sol;p>&NewLine;<h3><strong>Client<&sol;strong><&sol;h3>&NewLine;<p>The client is the object which sends the requests to the Target object&period;<br &sol;>&NewLine;<img class&equals;"aligncenter wp-image-3590 size-full" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;12&sol;Intercepting&lowbar;filter&lowbar;pattern&lowbar;class&lowbar;diagram&period;gif" alt&equals;"Intercepting filter design pattern class diagram" width&equals;"401" height&equals;"248" &sol;><&sol;p>&NewLine;<h2><strong>Implementation and Use cases of Intercepting Filter Pattern<&sol;strong><&sol;h2>&NewLine;<p>When an intercepting filter is sent a particular task to perform&comma; it&comma; after receiving the request&comma; perform certain tasks on it&period; It passes the data to another filter object so that it filters it&period; That object&comma; then&comma; sends the filtered data back to the filter and the normal execution is continued&period; After&comma; all this process&comma; a requested output is produced&period;<&sol;p>&NewLine;<h3>Use Cases<&sol;h3>&NewLine;<p>These filters are required when the user need to perform logging or authentication&period; It enhances the security of the system&period; There are certain conditions&comma; in which the user might need to implement the intercepting filter pattern&period; These conditions include&semi; when an additional function is required to be added to the current web application when the main process is needed to be decorated&comma; when the user needs to debug or when the preprocessing and post-processing is required for a particular client&period; It uncompresses the incoming request&period; The filter can be added or removed transparently and it can be triggered automatically&period; It gives the benefit of improved re-usability&period; It composes the deployment time&period; The intercepting filter pattern centralizes the control of the system&period; In the filter chain&comma; every filter is coupled loosely and are&comma; therefore&comma; not efficient for sharing the information&period; The pattern offers improved flexibility within the system- the users are able to add or remove the common components flexibly and declaratively&period; The unnecessarily long filter chains with a large number of interceptors or filters can cause the performance efficiency to be reduced&period;<&sol;p>&NewLine;<h3>Implementation Filter strategies<&sol;h3>&NewLine;<p>The process is the intercepting filter pattern is such that&comma; the client invokes a request and sends it to the filter manager&period; The filter manager is the creator of both&comma; the filter chain and the filter object and it manages both as well&period; There is an exchange of processed and the processed data between filter object and filter chain&period; Filter chain then invokes the processed request back to the target &lpar;request handler&rpar;&period;<&sol;p>&NewLine;<p>There are certain strategies for implementing an intercepting filter&period; Some of them are&semi; <strong><em>standard filter strategy<&sol;em><&sol;strong>&comma;<em><strong> base filter strategy<&sol;strong><&sol;em>&comma; <em><strong>template filter strategy<&sol;strong><&sol;em>&comma; <em><strong>web services<&sol;strong> <strong>and<&sol;strong> <strong>message handling strategies<&sol;strong><&sol;em> and <em><strong>custom filter strategies<&sol;strong><&sol;em>&period;<&sol;p>&NewLine;<h2><strong>Sample Implementation<&sol;strong><&sol;h2>&NewLine;<p>We are going to create a FilterChain&comma; FilterManager&comma; Target&comma; Client as various objects representing our entities&period; AuthenticationFilter and DebugFilter represent concrete filters&period;<&sol;p>&NewLine;<h3>Step 1 &colon; Create Filter interface&period;<br &sol;>&NewLine;Filter&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">public interface Filter &lbrace; &NewLine; public void execute&lpar;ServletRequest request&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3>Step 2 &colon; Create Filter&&num;8217&semi;s implementation classes&period;<br &sol;>&NewLine;DebuggingFilter&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">public class DebuggingFilter implements Filter&lbrace; &NewLine; public void execute&lpar;ServletRequest request&rpar; &lbrace; &NewLine; &sol;&sol;Do some filter processing here&comma; such as &NewLine; &sol;&sol; displaying request parameters &NewLine; System&period;out&period;println&lpar;"request log&colon; " &plus; request&rpar;&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<h3>AuthenticationFilter&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">public class AuthenticationFilter implements Filter &lbrace; &NewLine; public void execute&lpar;ServletRequest request&rpar;&lbrace; &NewLine; &sol;&sol;Do some filter processing here&comma; such as &NewLine; &sol;&sol; displaying request parameters &NewLine; System&period;out&period;println&lpar;"Authenticating request&colon; " &plus; request&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3>Step 3 &colon; Create Target class&period;<br &sol;>&NewLine;Target&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">public class Target &lbrace; &NewLine; public void execute&lpar;ServletRequest request&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;"Executing request&colon; " &plus; request&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3>Step 4&colon; Create Filter Chain class<br &sol;>&NewLine;FilterChain&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">public class FilterChain &lbrace; &NewLine; private List filters &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; private Target target&semi; &NewLine; &NewLine; public void addFilter&lpar;Filter filter&rpar;&lbrace; &NewLine; filters&period;add&lpar;filter&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; public void execute&lpar;ServletRequest request&rpar;&lbrace; &NewLine; for &lpar;Filter filter &colon; filters&rpar; &lbrace; &NewLine; filter&period;execute&lpar;request&rpar;&semi; &NewLine; &rcub; &NewLine; target&period;execute&lpar;request&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setTarget&lpar;Target target&rpar;&lbrace; &NewLine; this&period;target &equals; target&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3>Step 5&colon; Create Filter Manager class&period;<br &sol;>&NewLine;FilterManager&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">public class FilterManager &lbrace; &NewLine; FilterChain filterChain&semi; &NewLine; &NewLine; public FilterManager&lpar;Target target&rpar;&lbrace; &NewLine; filterChain &equals; new FilterChain&lpar;&rpar;&semi; &NewLine; filterChain&period;setTarget&lpar;target&rpar;&semi; &NewLine; &rcub; &NewLine; public void setFilter&lpar;Filter filter&rpar;&lbrace; &NewLine; filterChain&period;addFilter&lpar;filter&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; public void filterRequest&lpar;ServletRequest request&rpar;&lbrace; &NewLine; filterChain&period;execute&lpar;request&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3>Step 6&colon; Create client class<br &sol;>&NewLine;Client&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">public class Client &lbrace; &NewLine; FilterManager filterManager&semi; &NewLine; &NewLine; public void setFilterManager&lpar;FilterManager filterManager&rpar;&lbrace; &NewLine; this&period;filterManager &equals; filterManager&semi; &NewLine; &rcub; &NewLine; &NewLine; public void sendRequest&lpar;ServletRequest request&rpar;&lbrace; &NewLine; filterManager&period;filterRequest&lpar;request&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3>Create 7&colon; Create a demo class<br &sol;>&NewLine;InterceptingFilterDemo&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">public class InterceptingFilterDemo &lbrace; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; ServletRequest request &equals; &sol;&sol;request object&period; &NewLine; FilterManager filterManager &equals; new FilterManager&lpar;new Target&lpar;&rpar;&rpar;&semi; &NewLine; filterManager&period;setFilter&lpar;new AuthenticationFilter&lpar;&rpar;&rpar;&semi; &NewLine; filterManager&period;setFilter&lpar;new DebugFilter&lpar;&rpar;&rpar;&semi; &NewLine; &NewLine; Client client &equals; new Client&lpar;&rpar;&semi; &NewLine; client&period;setFilterManager&lpar;filterManager&rpar;&semi; &NewLine; client&period;sendRequest&lpar;request&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;structural-design-patterns&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;context-object-design-pattern&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '3589'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago