Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

get component policy values in servlet

sreedobe
Level 4
Level 4

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;
	}

}
1 Accepted Solution
Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

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

View solution in original post

7 Replies
snbaem
Community Advisor
Community Advisor

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

sreedobe
Level 4
Level 4
It is not directly mapped to component but one of component dialog fields. that would we component/cq:dialog/items/item/field
snbaem
Community Advisor
Community Advisor
Did you try the comment at https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/get-component-resource-in-... ? Seems like we are getting the node from the component i.e under /apps so try to see if the option of getting the page resource works or using a admin service resolver works
Anudeep_Garnepudi
Community Advisor
Community Advisor

Hi @sreedobe 

Check your request has read access to the content path.

AG

Manjunath_K
Community Advisor
Community Advisor

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!

 

sreedobe
Level 4
Level 4
Here resource is not /component rather "/component/cq_dialog/items/item/field"
Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

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

View solution in original post