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?
Solved! Go to Solution.
Views
Replies
Total Likes
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
}
}
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.
can you please tell which specific header is involved?
I thought of filters but unsure at the time I created the post.
Thanks again.
Views
Replies
Total Likes
set below header.
Header always set Cache-Control "private"
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
}
}
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies