Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
SOLVED

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

Avatar

Level 9

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

AEM LinksLinkedIn

View solution in original post

4 Replies

Avatar

Community Advisor

@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.

Avatar

Level 9

can you please tell which specific header is involved?

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

 

Thanks again.

Avatar

Community Advisor

set below header.

Header always set Cache-Control "private"

Avatar

Correct answer by
Community Advisor

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

AEM LinksLinkedIn