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());
}
}
}
|