Get component resource in a servlet

Avatar

Avatar
Validate 1
Level 2
sreedobe
Level 2

Likes

4 likes

Total Posts

67 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Ignite 3
Ignite 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 1
Level 2
sreedobe
Level 2

Likes

4 likes

Total Posts

67 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Ignite 3
Ignite 1
Give Back 5
Give Back 3
View profile
sreedobe
Level 2

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
Boost 100
Level 6
snbaem
Level 6

Likes

115 likes

Total Posts

246 posts

Correct reply

37 solutions
Top badges earned
Boost 100
Springboard
Establish
Validate 25
Validate 10
View profile

Avatar
Boost 100
Level 6
snbaem
Level 6

Likes

115 likes

Total Posts

246 posts

Correct reply

37 solutions
Top badges earned
Boost 100
Springboard
Establish
Validate 25
Validate 10
View profile
snbaem
Level 6

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
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,314 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,314 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

17-11-2020

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

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
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile
Manjunath_K
MVP

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!