Acciente Company Logo

Using Request Interceptors in Induction

  1. Using Request Interceptors in Induction
    1. What is a Request Interceptor and what problem does its solve?
    2. What does an Request Interceptor look like?

This tutorial discusses the use of request interceptors in Induction.

What is a Request Interceptor and what problem does its solve?

A request interceptor is a piece of code that gets activated for every single HTTP request received by your application. Interceptors are very useful when you need to perform some common processing for every HTTP request.

If not for request interceptor support we would have to put a call to the common processing code in every one of our controllers/views to achieve the same effect. Placing a call to our common code in every controller/view is not a convenient solution, when a new controller/view is added we need to remember to add the call to the common code. Now if we our call is to check something like security an ommission may have serious consequences. Interceptors offer a guaranteed way to call common code before (or after or at some other point as we shall shortly see) a controller/view executes. Next let's look at two examples of problems that may be solved elegantly using request interceptors.

Consider the problem of performing a check before the execution of certain types of controllers and/or views. For example, we may want certain controllers/views to only be callable if a user is logged in.

A request interceptor may also be used to manipulate cookie data. For example, certain application configurations require that the path of the session cookie created by the servlet container be changed, since the application is being proxied by a front-end web server (such as Apache's HTTP server) off a different path. In these configurations the web application runs in a servlet container (such as Tomcat) under a path such as /appname and is proxied by a front-end web server off the root of domain name such www.appname.com/. Now Tomcat creates the HTTP session cookie with the cookie path set to /appname, but since public browsers see the application hosted from / the cookie does not get sent back. We can solve this problem by using a simple request interceptor to write a cookie to the response with the same session data but with the path set to /.

What does an Request Interceptor look like?

An request interceptor is simply an ordinary Java class that is required to implement the interface com.acciente.induction.interceptor.RequestInterceptor. Induction looks for the method names preResolution, postResolution, preResponse and postResponse in our interceptor class. If it finds one or more of these methods they get called at different points in the request processing cycle.

For parameters declared in any of the methods Induction will attempt to inject a value based on the type. The parameter types for which Induction will inject values are documented here Interceptor METHODS : commonly used parameter types.

A request interceptor method is not required to return a value, however if a value is returned Induction processes returned value. Induction's rules for processing the return value are described below:

Next lets look at when Induction calls each of the request interceptor methods.