get component policy values in servlet

Avatar

Avatar

sreedobe

Avatar

sreedobe

sreedobe

17-11-2020

Component Dialog has "select" drop down that needs to read values set by author in "design dialog".

Following is the servlet that mapped to dialog select field using resourceType mapping. How to get get component policy values in servlet. 

 

I tried below but content resource coming as null 

 

Resource contentResource = resolver.getResource((String) request.getAttribute(Value.CONTENTPATH_ATTRIBUTE));

 

Thanks for any help. Below is the servlet code.

package com.company.aem.core.servlets;

import com.adobe.granite.ui.components.Value;
import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.EmptyDataSource;
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.adobe.granite.ui.components.ds.ValueMapResource;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
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.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.osgi.service.component.annotations.Component;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 
 * Data source for links dropdown
 */
@Component(service = { Servlet.class }, property = {
		"sling.servlet.resourceTypes=" + AllowedArticleInsightsLinksServlet.RESOURCE_TYPE, "sling.servlet.methods=GET",
		"sling.servlet.extensions=html" })
public class AllowedArticleInsightsLinksServlet extends SlingSafeMethodsServlet
{

	public final static String RESOURCE_TYPE = "pi-web/components/content/articleinsights/datasource/allowedlinks";

	@Override
	protected void doGet(
			SlingHttpServletRequest request, SlingHttpServletResponse response)
			throws ServletException, IOException
	{
		SimpleDataSource
				allowedHeadingElementsDataSource =
				new SimpleDataSource(getAllowedHeadingElements(request).iterator());
		request.setAttribute(DataSource.class.getName(), allowedHeadingElementsDataSource);
	}

	private List
<
Resource
>
 getAllowedHeadingElements(SlingHttpServletRequest request)
	{
		List
<
Resource
>
 allowedHeadingElements = new ArrayList<>();
		ResourceResolver resolver = request.getResourceResolver();
		Resource contentResource = resolver.getResource((String) request.getAttribute(Value.CONTENTPATH_ATTRIBUTE));
		ContentPolicyManager policyManager = resolver.adaptTo(ContentPolicyManager.class);
		if (policyManager != null && contentResource != null)
		{
			ContentPolicy policy = policyManager.getPolicy(contentResource);
			if (policy != null)
			{
				request.setAttribute(DataSource.class.getName(), EmptyDataSource.instance());

				Resource allowedLinksResource = policy.adaptTo(Resource.class).getChild("links");
				if (allowedLinksResource != null)
				{
					ValueMap vm = null;

					for (Resource rs : allowedLinksResource.getChildren())
					{
						vm = new ValueMapDecorator(new HashMap<>());
						// Specify the value and text values
						String Value = rs.getValueMap().get("linkhref", String.class);
						String Text = rs.getValueMap().get("linkname", String.class);
						//populate the map
						vm.put("value", Value);
						vm.put("text", Text);

						allowedHeadingElements.add(
								new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", vm));
					}
				}

			}
		}
		return allowedHeadingElements;
	}

}

Accepted Solutions (0)

Answers (4)

Answers (4)

Avatar

Avatar

Anudeep_Garnepudi

Avatar

Anudeep_Garnepudi

Anudeep_Garnepudi

17-11-2020

Hi @Manjunath_K 

Can get ComponentContext from request.

ContentPolicyManager policyManager = resolver.adaptTo(ContentPolicyManager.class);
ComponentContext componentContxt = (ComponentContext) request.getAttribute("com.day.cq.wcm.componentcontext");
ContentPolicy policy = policyManager.getPolicy(componentContxt);

Hope it works!

AG

Avatar

Avatar

Manjunath_K

Avatar

Manjunath_K

Manjunath_K

17-11-2020

Hi @sreedobe 

If its resource based servlet then you can access component policy configuration by passing current resource to getPolicy(). in below example component policy configuration has title field & same configured value is retrieved using ContentPolicyManager.

 

ContentPolicyManager policyManager = request.getResourceResolver().adaptTo(ContentPolicyManager.class);
if (policyManager != null) {
ContentPolicy contentPolicy = policyManager.getPolicy(request.getResource()); //ex: request.getResource() path "/content/test-page/jcr:content/root/component"
if (contentPolicy != null) {
String title= contentPolicy.getProperties().get("title",String.class);
}
}

refer this article:

https://techrevel.blog/2017/08/28/aem-template-editor-design-configuration-via-policies/

 

 

Hope this helps!

 

Avatar

Avatar

Anudeep_Garnepudi

Avatar

Anudeep_Garnepudi

Anudeep_Garnepudi

17-11-2020

Hi @sreedobe 

Check your request has read access to the content path.

AG

Avatar

Avatar

snbaem

Avatar

snbaem

snbaem

17-11-2020

What is the value of (String)request.getAttribute(Value.CONTENTPATH_ATTRIBUTE) when you debug? We can check if its a valid resource path. May be if it has selector or something, we can try resolver.resolve(pathobtained)

Also check if the resolver has correct access, may be we need to use a admin user service resolver