April 28, 2020
Estimated Post Reading Time ~

How to get the Inherited Properties in AEM Servlet's or Filter's

This post will explain the approach to get the inherited properties in AEM servlet's or Filters.

Sample Filter - Retrieves the inherited page properties and component properties:

HierarchyNodeInheritanceValueMap - This will help to retrieve the inherited properties from parent pages.

While accessing the property prop1 from /content/site/en/test/jcr:content - /content/site/en/test/jcr:content/@prop1,the system searches in the following paths

/content/site/en/test/jcr:content/@prop1
/content/site/en/jcr:content/@prop1
/content/site/jcr:content/@prop1


If the prop1 is located in any of the above paths then the corresponding values are returned, if not it will return a null value.

ComponentInheritanceValueMap - This will help to retrieve the inherited properties from parent components.

While accessing the prop2 property from /content/site/en/test/jcr:content/T01_region4/titletext_e8d0 - /content/site/en/test/jcr:content/T01_region4/titletext_e8d0/@prop2,the system searches in the following paths

/content/site/en/test/jcr:content/T01_region4/titletext_e8d0/@prop2
/content/site/en/test/jcr:content/T01_region4/@prop2
/content/site/en/test/jcr:content/@prop2


If the prop2 is located in any of the above paths then the corresponding values are returned, if not it will return a null value.

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.scr.annotations.*;
import org.apache.felix.scr.annotations.sling.SlingFilter;
import org.apache.felix.scr.annotations.sling.SlingFilterScope;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;

import com.day.cq.commons.inherit.ComponentInheritanceValueMap;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.commons.inherit.InheritanceValueMap;

@SlingFilter(label = "301 URL redirector", description = "301 URL redirector", metatype = true, generateComponent = true,
generateService = true, order = 0, scope = SlingFilterScope.REQUEST)

public class Redirect301HandlerFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
String resourcePathURL = slingRequest.getResource().getPath();

ResourceResolver resourceResolver = slingRequest.getResourceResolver();
String resourceContentPath = resourcePathURL.split(".html")[0] + "/jcr:content";
Resource res = resourceResolver.getResource(resourceContentPath);

InheritanceValueMap inheritedProp = new HierarchyNodeInheritanceValueMap(res);
String prop1 = inheritedProp.getInherited("prop1", String.class);

res = resourceResolver.getResource(resourceContentPath+"/T01_region4/titletext_e8d0");

InheritanceValueMap inheritedPropCom = new ComponentInheritanceValueMap(res);
String prop2 = inheritedPropCom.getInherited("prop2", String.class);

chain.doFilter(slingRequest, slingResponse);

}

@Override
public void destroy() {
// TODO Auto-generated method stub

}

}


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.