Intercepting filter pattern

Last updated

Intercepting Filter is a JavaEE pattern which creates pluggable filters to process common services in a standard manner without requiring changes to core request processing code. The filters intercept incoming requests and outgoing responses, allowing preprocessing and post-processing, and these filters can be added or removed unobtrusively without changing existing code. [1] This pattern applies reusable processing transparently before and after the actual request execution by the front and page controllers. [2]

Contents

Structure

Filter manager, filter chain, filters and target are components of the pattern.

Filter manager

This manages filter processing and creates the filter chain with the appropriate filters, in the correct order, and initiates processing. [1]

Filter chain

A Filter Chain is a specific series of filters, composed so as to form a logical chain. [1]

Filters

These are the individual filters that are mapped to a target and their processing is coordinated by filter chain. [1]

Target

This is the resource requested by the client. [1]

Consequences

Following benefits can be considered:

Reduced performance can be a concern, as unnecessarily long chains of interceptors and filters may hurt performance. [2]

Sample code

Sample code implementation for filters with custom filter strategy is given below.

Code for implementing a filter - debugging filter:

publicclassDebuggingFilterimplementsProcessor{privateProcessortarget;publicDebuggingFilter(ProcessormyTarget){target=myTarget;}publicvoidexecute(ServletRequestreq,ServletResponseres)throwsIOException,ServletException{//Do some filter processing here, such as // displaying request parameterstarget.execute(req,res);}}

[1]

Code for implementing a filter - core processor:

publicclassCoreProcessorimplementsProcessor{privateProcessortarget;publicCoreProcessor(){this(null);}publicCoreProcessor(ProcessormyTarget){target=myTarget;}publicvoidexecute(ServletRequestreq,ServletResponseres)throwsIOException,ServletException{//Do core processing here}}

[1]

Code for handling requests:

publicvoidprocessRequest(ServletRequestreq,ServletResponseres)throwsIOException,ServletException{Processorprocessors=newDebuggingFilter(newAuthenticationFilter(newCoreProcessor()));processors.execute(req,res);//Then dispatch to next resource, which is probably // the View to displaydispatcher.dispatch(req,res);}

[1]

Code for filter manager:

publicvoidprocessRequest(ServletRequestreq,ServletResponseres)throwsIOException,ServletException{Processorprocessors=newDebuggingFilter(newAuthenticationFilter(newCoreProcessor()));processors.execute(req,res);//Then dispatch to next resource, which is probably // the View to displaydispatcher.dispatch(req,res);}

[1]

Code for filter chain:

publicclassFilterChain{// filter chain // apply filtersfor(finalFilterfilter:filters){// pass request & response through various // filtersfilter.execute(request,response);}}}

[1]

See also

Related Research Articles

<span class="mw-page-title-main">Java (programming language)</span> Object-oriented programming language

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need to recompile. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and C++, but has fewer low-level facilities than either of them. The Java runtime provides dynamic capabilities that are typically not available in traditional compiled languages. As of 2019, Java was one of the most popular programming languages in use according to GitHub, particularly for client–server web applications, with a reported 9 million developers.

The Jakarta Transactions, one of the Jakarta EE APIs, enables distributed transactions to be done across multiple X/Open XA resources in a Java environment. JTA was a specification developed under the Java Community Process as JSR 907. JTA provides for:

Java Platform, Standard Edition is a computing platform for development and deployment of portable code for desktop and server environments. Java SE was formerly known as Java 2 Platform, Standard Edition (J2SE).

<span class="mw-page-title-main">Jakarta Servlet</span> Jakarta EE programming language class

A Jakarta Servlet is a Java software component that extends the capabilities of a server. Although servlets can respond to many types of requests, they most commonly implement web containers for hosting web applications on web servers and thus qualify as a server-side servlet web API. Such web servlets are the Java counterpart to other dynamic web content technologies such as PHP and ASP.NET.

In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code without modifying the code itself, instead separately specifying which code is modified via a "pointcut" specification, such as "log all function calls when the function's name begins with 'set'". This allows behaviors that are not central to the business logic to be added to a program without cluttering the code core to the functionality.

In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

The event dispatching thread (EDT) is a background thread used in Java to process events from the Abstract Window Toolkit (AWT) graphical user interface event queue. It is an example of the generic concept of event-driven programming, that is popular in many other contexts than Java, for example, web browsers, or web servers.

Jazelle DBX is an extension that allows some ARM processors to execute Java bytecode in hardware as a third execution state alongside the existing ARM and Thumb modes. Jazelle functionality was specified in the ARMv5TEJ architecture and the first processor with Jazelle technology was the ARM926EJ-S. Jazelle is denoted by a "J" appended to the CPU name, except for post-v5 cores where it is required for architecture conformance.

In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object or class, retaining similar implementation. Also defined as deriving new classes from existing ones such as super class or base class and then forming them into a hierarchy of classes. In most class-based object-oriented languages, an object created through inheritance, a "child object", acquires all the properties and behaviors of the "parent object", with the exception of: constructors, destructors, overloaded operators and friend functions of the base class. Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors, to reuse code and to independently extend original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a directed acyclic graph.

The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object to the pool rather than destroying it; this can be done manually or automatically.

The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. Although the framework does not impose any specific programming model, it has become popular in the Java community as an addition to the Enterprise JavaBeans (EJB) model. The Spring Framework is open source.

The active object design pattern decouples method execution from method invocation for objects that each reside in their own thread of control. The goal is to introduce concurrency, by using asynchronous method invocation and a scheduler for handling requests.

Jakarta Mail is a Jakarta EE API used to send and receive email via SMTP, POP3 and IMAP. Jakarta Mail is built into the Java EE platform, but also provides an optional package for use in Java SE.

The front controller software design pattern is listed in several pattern catalogs and is related to the design of web applications. It is "a controller that handles all requests for a website," which is a useful structure for web application developers to achieve flexibility and reuse without code redundancy.

In object-oriented design, the chain-of-responsibility pattern is a behavioral design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.

In the field of software development, an interceptor pattern is a software design pattern that is used when software systems or frameworks want to offer a way to change, or augment, their usual processing cycle. For example, a (simplified) typical processing sequence for a web-server is to receive a URI from the browser, map it to a file on disk, open the file and send its contents to the browser. Any of these steps could be replaced or changed, e.g. by replacing the way URIs are mapped to filenames, or by inserting a new step which processes the files contents.

Apache Attic is a project of Apache Software Foundation to provide processes to make it clear when an Apache project has reached its end-of-life. The Attic project was created in November 2008. Also the retired projects can be retained.

The Brutos Application Framework is MVC controller written in Java. Designed to reduce the complexity of web development, with configurable mapping, view resolution as well as support for uploading and downloading files. Can be configured using XML, annotations and CoC.

Composite entity is a Java EE Software design pattern and it is used to model, represent, and manage a set of interrelated persistent objects rather than representing them as individual fine-grained entity beans, and also a composite entity bean represents a graph of objects.

Business delegate is a Java EE design pattern. This pattern is directed towards reducing the coupling in between business services and the connected presentation tier, and to hide the implementation details of services. Business delegates acts as an adaptor to invoke business objects from the presentation tier.

References

  1. 1 2 3 4 5 6 7 8 9 10 11 "Core J2EE Patterns - Intercepting Filter". Oracle. Oracle. Retrieved 6 February 2016.
  2. 1 2 3 Kayal, D. (2008). Pro Java EE Spring Patterns. New York: Apress. pp. 98–106.