interrupt a request to a load a page and the redirect if necessary | Community
Skip to main content
jayv25585659
Level 8
August 28, 2025
Solved

interrupt a request to a load a page and the redirect if necessary

  • August 28, 2025
  • 2 replies
  • 799 views

some info:

1. I need to check some stuff on the request before it can continue with normal processing.

2. if the check fails, I need to redirect the user to the 404 page.

3. I need to have access to Http session and HTTP request objects.

4.  I only need to filter on a specific path (example: /content/myapp/en/folder1 or /folder1)

 

Any ideas on what sort of Java "tech" can do this?

Best answer by arunpatidar

Hi @jayv25585659 

Use Sling Filter and set status 404 from that

https://medium.com/@neerajchaudhary856/mastering-sling-filters-in-aem-f9fb709bcc27

 

Example

package com.example.core.filters; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.propertytypes.ServiceRanking; import org.apache.sling.engine.EngineConstants; import javax.servlet.*; import java.io.IOException; @Component( service = Filter.class, property = { EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST, EngineConstants.SLING_FILTER_PATTERN + "=/content/myapp/en/(folder1|folder2).*" } ) @ServiceRanking(500) // Ensure the filter order public class Custom404Filter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response; // Example condition: block requests containing "?block=true" String blockParam = slingRequest.getParameter("block"); if ("true".equalsIgnoreCase(blockParam)) { slingResponse.sendError(404, "Resource not found"); return; // stop filter chain } // Otherwise, continue normal processing chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { // no-op } @Override public void destroy() { // no-op } }

 

2 replies

Anudeep_Garnepudi
Community Advisor
Community Advisor
August 28, 2025

@jayv25585659 

Write a Filter and target the path "/content/myapp/en/folder1", do your conditional checks in doFilter().

You need to set cache header to privte for this path in dispatcher.

jayv25585659
Level 8
August 28, 2025

can you please tell which specific header is involved?

I thought of filters but unsure at the time I created the post.

 

Thanks again.

Anudeep_Garnepudi
Community Advisor
Community Advisor
August 28, 2025

set below header.

Header always set Cache-Control "private"
arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
August 29, 2025

Hi @jayv25585659 

Use Sling Filter and set status 404 from that

https://medium.com/@neerajchaudhary856/mastering-sling-filters-in-aem-f9fb709bcc27

 

Example

package com.example.core.filters; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.propertytypes.ServiceRanking; import org.apache.sling.engine.EngineConstants; import javax.servlet.*; import java.io.IOException; @Component( service = Filter.class, property = { EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST, EngineConstants.SLING_FILTER_PATTERN + "=/content/myapp/en/(folder1|folder2).*" } ) @ServiceRanking(500) // Ensure the filter order public class Custom404Filter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response; // Example condition: block requests containing "?block=true" String blockParam = slingRequest.getParameter("block"); if ("true".equalsIgnoreCase(blockParam)) { slingResponse.sendError(404, "Resource not found"); return; // stop filter chain } // Otherwise, continue normal processing chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { // no-op } @Override public void destroy() { // no-op } }

 

Arun Patidar