Get component resource in a servlet | Community
Skip to main content
sreedobe
Level 4
November 17, 2020
Solved

Get component resource in a servlet

  • November 17, 2020
  • 4 replies
  • 7574 views

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

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Shubham_borole

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.

4 replies

Shubham_borole
Community Advisor
Shubham_boroleCommunity AdvisorAccepted solution
Community Advisor
November 17, 2020

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.

Manjunath_K
Level 7
November 17, 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!

 

sreedobe
sreedobeAuthor
Level 4
November 17, 2020
That is what I followed in actual implementation. Thanks for aem core code reference though.
Anudeep_Garnepudi
Community Advisor
Community Advisor
November 18, 2020

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