Questions tagged [interceptor]

The interceptor design pattern allows logic to be "inserted" before, during or after the execution of a piece of code, such as a method.

Interceptors are used to implement cross-cutting concerns, such as logging, auditing, and security, from the business logic.

As the name suggests, when messages are being passed , say- from one object to another, interceptors are used to catch that message and do some operations on it before it reaches the destination object.

In Java EE 5, Interceptors were allowed only on EJBs. In Java EE 6, Interceptors became a new specification of its own, abstracted at a higher level so that it can be more generically applied to a broader set of specifications in the platform.

They intercept invocations and life-cycle events on an associated target class. Basically, an interceptor is a class whose methods are invoked when business methods on a target class are invoked, life-cycle events such as methods that create/destroy the bean occur, or an EJB timeout method occurs. The CDI specification defines a type-safe mechanism for associating interceptors to beans using interceptor bindings.

Look for a working code sample at:

https://github.com/arun-gupta/javaee7-samples/tree/master/cdi/interceptors

Java EE 7 also introduced a new @Transactional annotation in Java Transaction API. This allows you to have container-managed transactions outside an EJB. This annotation is defined as an interceptor binding and implemented by the Java EE runtime. A working sample of @Transactional is at:

https://github.com/arun-gupta/javaee7-samples/tree/master/jta/transaction-scope

2288 questions
115
votes
4 answers

How to intercept all AJAX requests made by different JS libraries

I am building a web app with different JS libraries (AngularJS, OpenLayers,...) and need a way to intercept all AJAX responses to be able, in case the logged user session expired (response gets back with 401 Unauthorized status), to redirect him to…
mettjus
  • 2,768
  • 4
  • 19
  • 30
90
votes
1 answer

Add multiple HTTP Interceptors to Angular Application

How to add multiple, independent HTTP interceptors to an Angular 4 application? I tried to add them by extending the providers array with more than one interceptors. But only the last one is actually executed, Interceptor1 is ignored. @NgModule({ …
str
  • 33,096
  • 11
  • 88
  • 115
67
votes
4 answers

Spring HandlerInterceptor vs Servlet Filters

HandlerInterceptors in Spring can now be configured to be invoked only on certain URLs using . Servlet Filters can achieve same functionality (logging, security etc). So which one should be used? I think with Interceptors, one can…
aces.
  • 3,612
  • 9
  • 36
  • 48
67
votes
4 answers

Angular 4.3 HttpClient : Intercept response

In the documentation about the new HttpClientModule included in the new version of Angular 4.3, the mechanism to intercept requests is explained very well. There is also mention of the response interceptor mechanism however I cannot find anything…
olivier houssin
  • 686
  • 1
  • 5
  • 11
51
votes
3 answers

What are Interceptors in Java EE?

I'm trying to clear my concept about Interceptors in Java EE. I have read Java EE specification but I'm little confused about it. Please provide me some useful link or tutorial which could clear my concept. How, When, Why do we use interceptors?
Umair
  • 751
  • 2
  • 12
  • 27
49
votes
2 answers

AngularJS Intercept all $http JSON responses

I have an application built using AngularJS and a server-side backend that delivers all requests in JSON form. Each and every request is wrapped in a JSON container that contains a data variable which contains the data specific to the request. The…
matsko
  • 20,727
  • 21
  • 94
  • 138
45
votes
3 answers

Spring MVC 3, Interceptor on all excluding some defined paths

Is it possible to apply an interceptor to all controllers and actions, except some that are defined? Just to be clear, I am not interested in applying an interceptor on a list of defined ones. I want to define those to exclude. Thanks!
mmm
  • 18,431
  • 26
  • 99
  • 165
40
votes
7 answers

GNU gcc/ld - wrapping a call to symbol with caller and callee defined in the same object file

to clarify, my question refers to wrapping/intercepting calls from one function/symbol to another function/symbol when the caller and the callee are defined in the same compilation unit with the GCC compiler and linker. I have a situation resembling…
luis.espinal
  • 9,728
  • 5
  • 35
  • 54
39
votes
1 answer

Intercept and retry call by means of OkHttp Interceptors

I need to retry request inside of OkHttp Interceptor. For example there is incoming request which needs Authorization token. If Authorization token is expired, server returns response with 403 code. In this case I am retrieving a new token and…
Araz Abishov
  • 1,361
  • 3
  • 13
  • 24
38
votes
1 answer

Angular.js $http intercept "net::ERR_CONNECTION_REFUSED"

I'm trying to write a generic error handler for my website using $http's interceptors but they don't seem to be able to do what I want to do. I placed interceptors on 'response' and 'responseError' but they never get called when the server is…
SBSTP
  • 3,031
  • 6
  • 26
  • 38
37
votes
1 answer

Android Retrofit 2, differences between addInterceptor & addNetworkInterceptor for editing responses

I've been trying to implement an interceptor ( OkHttp 3.2 & Retrofit 2 ) for editing the JSON response before is returned as response. The server we request data returns different data dependes on success or error and that makes difficult to map the…
Jose M Lechon
  • 5,366
  • 4
  • 40
  • 54
33
votes
3 answers

Java config for spring interceptor where interceptor is using autowired spring beans

I want to add spring mvc interceptor as part of Java config. I already have a xml based config for this but I am trying to move to a Java config. For interceptors, I know that it can be done like this from the spring…
user3565529
  • 1,137
  • 2
  • 11
  • 20
32
votes
5 answers

Intercept fetch() API requests and responses in JavaScript

I want to intercept fetch API requests and responses in JavaScript. For example, before sending the request I want to intercept the request URL. I'd like to intercept the response once it arrives as well. The below code is for intercepting responses…
32
votes
4 answers

How can I send request again in response interceptor?

I've made an interceptor in my application that detects session loss (server sends an HTTP 419). In this case, I need to request a new session from the server, and then I would like to send the original request again automatically. Maybe I could…
D0m3
  • 1,222
  • 2
  • 11
  • 16
32
votes
3 answers

Custom annotation as Interceptor for a method logging

Java Gurus, I am pretty new for annotations and haven't searched for this a lot, so please bear with me... I would like to implement a Custom Annotation which will intercept a method call; to start with something very basic it can just print the…
Bharat Sinha
  • 12,727
  • 6
  • 35
  • 63
1
2 3
99 100