Sling Request Invocation Chain
Implementing Sling Filters
Registration of Filters
- Standard Servlet Filter (javax.servlet.Filter)
- Need to be registered as Service („manually“ or SlingFilter SCR Annotation)
- Order and FilterScopes can be defined
- Order defined by Serviceranking
- Filterscopes
- REQUEST
- COMPONENT
- ERROR
- INCLUDE *
- FORWARD *
* FilterChain executes COMPONENT scope for these scopes as well.
SCR Annotation
@SlingFilter(scope = SlingFilterScope.COMPONENT, order = -10000, metatype = false)
public class MyComponentFilter implements javax.servlet.Filter {
@Override
public void doFilter(ServletRequest pRequest, ServletResponse pResponse,
FilterChain pChain) throws IOException, ServletException {
// Implement Filter
// continue with filter chaining
pChain.doFilter(pRequest, pResponse);
}
}
|
Filter Order
Existing Filters might use the deprecated “filter.order” instead “service.ranking”.
Existing Filters and their execution ORDER can be inspected via http://<instance>:<port>/system/console/status-slingfilter
Check Filterconditions at first place
public void doFilter(ServletRequest pRequest, ServletResponse pResponse,
FilterChain pChain) throws IOException, ServletException {
if (!(pRequest instanceof SlingHttpServletRequest)) {
throw new ServletException("Request is not a Sling HTTP request.");
}
if (isFilterEnabled(slingRequest)) {
// Implement Filter-Logic
} else {
// continue with filter chaining
pChain.doFilter(pRequest, pResponse);
}
}
|
Check available Information from slingRequest if Filter should do anything.
Be aware that these checks may be performed a lot especially for COMPONENT scope
Accessing and Using Data in Filter
if (isFilterEnabled(slingRequest)) {
if (isFilterEnabled(slingRequest)) {
Resource resource = slingRequest.getResource(); // already resolved
if (null != resource) {
PrintWriter writer = pResponse.getWriter();
writer.write("<component resourceType=\"" + resource.getResourceType() + "\">");
pChain.doFilter(pRequest, pResponse);
writer.write("</component>");
return;
}
}
}
|
Nhận xét
Đăng nhận xét