Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Passing original request object in chain filter in Sling filter

Avatar

Level 3

Hi,

We have one sling filter performing some business logic and this is chain filter. But in the logic, we are processing Iterator Object from request attributes. And the same iterator is getting used in next level of chain.

When we try to parse the Iterator in next level we are getting error"Parameter not available" as we have already pasrsed the Iterator Object.

If there any way to pass the original request object in the chain from first filter?

Thanks,

Swati

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

What iterator from the request are you processing?

(I know that there is an issue when dealing with multi-part POST requiests. If you have functionality which first casts the SlingHttpServletRequest to a HttpServletRequest and reads it using this API, all subsequent calls to read the body will fail, because the input stream has already been consumed. The Sling implementation consumes it already and provides dedicated methods to access it. We came across this a year or two ago, so I don't know the details anymore.)

View solution in original post

5 Replies

Avatar

Community Advisor

Hi,

Can you try like below:

chain.doFilter(request, response);

chain.doFilter(request, slingResponse);

Apache Sling :: Servlet Filter Support



Arun Patidar

Avatar

Level 3

Hi Arun,

You suggesting to call doFilter twice?

I noticed the actual problem is reading the attribute twice. When I read the request attribute in filter and again in next level when actual logic is using the same parameter then it is not available and the actual functionality is breaking.

Thanks,

Swati

Avatar

Community Advisor

Nops, I am saying you can all doFilter with original request and response.It should be available.

Or you can check same in single filter by referencing other filters logic in same filter something like below:

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain)
throws IOException, ServletException {
doMyJob(request, response, params);
filterChain.doFilter(request, response);
}

I am not sure if is this suitable for your use case.



Arun Patidar

Avatar

Correct answer by
Employee Advisor

What iterator from the request are you processing?

(I know that there is an issue when dealing with multi-part POST requiests. If you have functionality which first casts the SlingHttpServletRequest to a HttpServletRequest and reads it using this API, all subsequent calls to read the body will fail, because the input stream has already been consumed. The Sling implementation consumes it already and provides dedicated methods to access it. We came across this a year or two ago, so I don't know the details anymore.)

Avatar

Level 3

In filter I am parsing one of the request attribute ('request-parts-iterator') to fetch uploaded file from OOTB DAM upload buttton. I am reading the inputstream stream from it and enabling antivirus scan for the uploaded file. Once scan done, I am doing chain filter(chainfiltter.doFilter(request,response)) but as I already read the Inputstream Once , CreateAssetFilter unable to read the stream again to process upload action.

I even tried to wrapping of the request. That dint worked too.

I am totally blocked here.

Thanks,