Sunday, 6 December 2015

Servlet to get all tags assigned to a DAM Asset in JSON format

Recently there was a requirement to fetch all tags assigned to a DAM Assets. It can be implemented via servlets where a Ajax call can be made. If you have never worked on such kind of use case then this is a must to learn and implement article for you. Its uses Tag Manager API and return data in JSON format. In our scenario it was easy to get data in JSON format, however you are free to convert it to any bases on your requirement
1. Define the annotaton @slingServlet with path you want to use for accessing it.
2.  Get the ‘path‘ parameter from request to get the image location. Once received, adapt this resource to Asset class. Start reading the ‘cq:tags‘ property where all tags are stored.
3. Now you have tags, but we need to get the title of tags and convert into JSON object that has to be returned.
4. At last, return the output in JSON format
5. The same request can be made via JS  to get the response
DAM Tags
Here is final complete code that can be used

Exception while calling a Servlet in AEM

Question:  I am trying to access a servlet with a doPost method from the JSP and getting the following exception -Caused by org.apache.sling.api.resource.PersistenceException: Resource at ‘/location/Stores’ is not modifiable.
  at rg.apache.sling.servlets.post.impl.helper.SlingPropertyValueHandler.setProperty(SlingPropertyValueHandler.java:153)
    at org.apache.sling.servlets.post.impl.operations.ModifyOperation.writeContent(ModifyOperation.java:411)
    at org.apache.sling.servlets.post.impl.operations.ModifyOperation.doRun(ModifyOperation.java:101)
My bundle is built successfully and the resource resolver shows the path as expected and resolves to the given path.Activate method of the servlet is successfully called.However while trying to give an Ajax call from the jsp it never reaches the doPost method.I tried it both with doPost and doGet.Please help.
Answer: Give your servlet path as /bin/location/Stores or /services/location/Stores which is configured in ‘Apache Sling Servlet Resolver’
If you want, you can add your own Servlet/Script resolver from /system/console/configMgr and your custom resourcePath will be resolved by the servlet resolver

Max number of users in CQ Publisher

There is no restriction on number of users in publisher. But if you have your users already outside, you can also have your own external login module for authentication and authorization. However, you need to ensure you are using AEM 6.0 or higher and Oak. With Jackrabbit/CRX 2, such scale was possible, but you do need to do some extra configuration if you have groups with thousands of members. Your post didn’t call this out specifically, but if you have tens of thousands of users, odds are you have some big groups 

How to find out AEM version you are using?

There are two ways to find the AEM version you are using:-
Bottom of Welcome Page [ http://host:port/libs/cq/core/content/welcome.html ] 
 AEM version welcome page
System Console [ http://host:port/system/console/status-productinfo ]
AEM version system console

JavaScript Use-API with Sightly

The Sightly JavaScript Use-API enables a Sightly file to access helper code written in JavaScript (server side execution). This allows all complex business logic to be encapsulated in the JavaScript code, while the Sightly code deals only with direct markup production.
A simple use of JavaScript Use-API can be be seen here. These are predefined method defined in sightly, those can be used in JS file to read the values and return it. Example has been shown with a simple component which excepts a title and description. These values are then read by server side JS using available Use API. Once read they will be returned to component sightly file for rendering in html.

 Read More