The SlingMainServlet implements a recursion level counter to prevent recursions going too deep in the checkRecursionLevel method. What this method does is incrementing a counter stored as a request attribute everytime RequestDispatcher.include is called (by whatever means).
The drawback of this mechanism is, that the counter is only incremented and never decremented after an inclusion returns. This in fact is not a recursion level counter but a counter limiting the number of times RequestDispatcher.include may be called. Another drawback of this is, that the implementation is overcomplicated, because the RequestData object created for each request keeps a stack of a inclusions and hence could easily implement a real recursion level checker by just checking the size of the stack.
I think, we should drop the SlingMainServlet.checkRecursionLevel method altogether and replace it with a new RequestData.checkAbort method which throws in case any of two cases occurrs:
- Recursion Level is too deep (50 seems like useful)
- Number of includes exceeds a given number (something like 1000 or more ?)
In addition the Exception should be made public in the Sling engine module such that users of any include mechanism could handle it if need be.