Monday, December 18, 2017

How to get the Inherited Properties in Adobe Experience Manager Servlet's or Filter's

How to get the Inherited Properties in Adobe Experience Manager Servlet's or Filter's

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

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 is returned, if not it will return 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 is returned, if not it will return 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

}

}



No comments:

Post a Comment