Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Get component resource in a servlet

Avatar

Level 4

I have cq dialog field mapped to a servlet using resourceType to populate data source.

 

How can I get component resource object in aem servlet. request.getResource() returning the dialog field resource.   I would like to get the component resource of dialog field, so I could use it to get component policy values.

 

Thanks,

Sri

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

How is the call to servlet made? If there is an Ajax call via js, may be pass the resource path as a query param or form data making it a post call. That way we can do

String path = request.getParameter("resourcePath").

String path = request.getParameter("resourcePath");
Resource resource = request.getResourceResolver().resolve(path);

Other than may be we can try 

String pagePath = request.getRequestURI();
Resource pageRes = request.getResourceResolver().resolve(pagePath);
//Once we get page resource find the desired resource node below that with resourcetype needed

For datasource there are examples of using Model classes too 
In the datasource node we point the sling:resourceType property to a sighly html file that does a <sly data-sly-use.model="com.myproject.MyModel">
The MyModel class will contain the logic needed
I think I have seen similar implementation by @arunpatidar  unable to find the example using Model class.
But this https://github.com/arunpatidar02/aem63app-repo/blob/master/java/DatasourceJson.java seems to be using a servlet.

View solution in original post

7 Replies

Avatar

Correct answer by
Community Advisor

How is the call to servlet made? If there is an Ajax call via js, may be pass the resource path as a query param or form data making it a post call. That way we can do

String path = request.getParameter("resourcePath").

String path = request.getParameter("resourcePath");
Resource resource = request.getResourceResolver().resolve(path);

Other than may be we can try 

String pagePath = request.getRequestURI();
Resource pageRes = request.getResourceResolver().resolve(pagePath);
//Once we get page resource find the desired resource node below that with resourcetype needed

For datasource there are examples of using Model classes too 
In the datasource node we point the sling:resourceType property to a sighly html file that does a <sly data-sly-use.model="com.myproject.MyModel">
The MyModel class will contain the logic needed
I think I have seen similar implementation by @arunpatidar  unable to find the example using Model class.
But this https://github.com/arunpatidar02/aem63app-repo/blob/master/java/DatasourceJson.java seems to be using a servlet.

Avatar

Level 8

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

Level 4
That is what I followed in actual implementation. Thanks for aem core code reference though.

Avatar

Community Advisor
Do you see any errors in logs or browser console error?

Avatar

Level 4
yes, basically null pointer exception for contentResource node. If I do contentResouce = request.getResouce(), then policy is null. I am wondering if this has to do anything with having component in structure vs initial content etc

Avatar

Community Advisor

Hi @sreedobe 

Can get ComponentContext from request. Try this

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