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
Here is final complete code that can be used
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package com.simple.util;
import java.io.PrintWriter;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.dam.api.Asset;
import com.day.cq.tagging.TagManager;
import com.day.cq.tagging.Tag;
import com.day.cq.dam.api.Asset;
/**
*
* @author praveen
*
*/
@SlingServlet(paths = "/bin/getDamTags")
public class DamTags extends org.apache.sling.api.servlets.SlingAllMethodsServlet {
private static final Logger LOGGER = LoggerFactory.getLogger(DamTags.class);
protected void doGet(SlingHttpServletRequest slingRequest,
SlingHttpServletResponse slingResponse) {
try {
LOGGER.info("Getting request to .....");
// Get print writer object to output
PrintWriter out = slingResponse.getWriter();
String path = slingRequest.getParameter("path");
LOGGER.info("Path received.. {}", path);
Resource resource = slingRequest.getResourceResolver().getResource(path);
Asset asset = resource.adaptTo(Asset.class);
Object[] titleArray = null;
Object titleObj = asset.getMetadata("cq:tags");
if (titleObj instanceof Object[]) {
titleArray = (Object[]) titleObj;
}
JSONObject damTagsJson = new JSONObject();
JSONArray tags = new JSONArray();
for (Object ob : titleArray) {
String a = ob.toString();
TagManager tagManager = null;
tagManager = slingRequest.getResourceResolver().adaptTo(
TagManager.class);
Tag custTag = tagManager.resolve(a);
tags.put(custTag.getTitle());
}
// Output tags in json format
out.print(damTagsJson.put("damTags", tags));
} catch (Exception e) {
LOGGER.info("Exception while fetching DAM assets tags "
+ e.getMessage());
}
}
}
|
Following is the solution for getting data from DAM and writing the Data.
ReplyDeletehttps://aemsimplifiedbynikhil.wordpress.com/2019/10/04/read-write-data-in-json-file-of-dam-in-aem-making-rest-api-call/
ReplyDeleteشركات نقل عفش فى خميس مشيط
شركة تسليك مجارى بخميس مشيط
شركة تنظيف بابها و بخميس مشيط
شركة تنظيف بالخميس
شركة تنظيف بخميس
شركة كشف تسريبات المياة وارتفاع فاتورة المياه
شركة تنظيف المنازل والفلل
شركة تسليك مجارى بابها
ReplyDeleteتنظيف منازل بابها
شركة تنظيف منازل بابها
شركة عزل خزانات بابها
شركة نظافه بابها
شركة نقل اثاث بابها
شركة تنظيف منازل بابها
ReplyDeleteشركة تنظيف بابها
شركة تنظيف منازل بأحد رفيدة
شركة تنظيف بوادي بن هشبل
شركة تنظيف منازل بوادي بن هشبل
شركة تنظيف منازل ببيشة
شركة تنظيف بالجبيل
ReplyDeleteشركة تنظيف فلل بالجبيل
شركة تنظيف منازل بالجبيل
شركة تنظيف مساجد بالجبيل
شركة تنظيف مجالس بالجبيل
شركة نقل الاثاث والعفش وخدمات التسليك تحت اشراف عمالة فنية مدربة بابها وخميس مشيط
ReplyDeleteشركة نقل اثاث بابها
شركة نقل اثاث بخميس مشيط
شركة كشف تسربات المياه بابها
شركة كشف تسربات المياه بخميس مشيط
شركة تسليك مجاري بابها
شركة تسليك مجاري بخميس مشيط
ReplyDeleteشركة مكافحة حشرات بالجبيل
شركة رش مبيدات بالجبيل
شركة مكافحة النمل الابيض بالجبيل
شركة مكافحة فئران بالجبيل
شركة كشف تسربات المياه بأبها
ReplyDeleteشركة كشف تسربات المياه بأحد رفيدة
شركة كشف تسربات المياه ببيشة
شركة مكافحة حشرات بأبها
شركة مكافحة حشرات بخميس مشيط
شركة مكافحة حشرات بوادي بن هشبل