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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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.
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!
Please check datasource example at
Datasource servlet
Dailog
Design Dialog
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
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
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies