Get component resource in a servlet

Avatar

Avatar

sreedobe

Avatar

sreedobe

sreedobe

17-11-2020

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

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

snbaem

Avatar

snbaem

snbaem

17-11-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 @Arun_Patidar  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.

Answers (3)

Answers (3)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

17-11-2020

Avatar

Avatar

Anudeep_Garnepudi

Avatar

Anudeep_Garnepudi

Anudeep_Garnepudi

17-11-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

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!