<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: [AEM6.5] Create a service which receives the path of... in Adobe Experience Manager Questions</title>
    <link>https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-create-a-service-which-receives-the-path-of-a-folder-in/m-p/413471#M1522</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;LI-USER uid="13016566"&gt;&lt;/LI-USER&gt;,&lt;/P&gt;&lt;P&gt;Please find below sample snippet making use of &lt;EM&gt;Query Builder&lt;/EM&gt; API to bring in the AEM assets under specific path.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Result sets are adapted to Asset object (&lt;EM&gt;com.day.cq.dam.api.Asset&lt;/EM&gt;)&lt;/LI&gt;&lt;LI&gt;Assuming &lt;EM&gt;altText&lt;/EM&gt; is a custom Metadata property, we can gain access to the same via &lt;EM&gt;Asset&lt;/EM&gt; object.&lt;/LI&gt;&lt;LI&gt;&lt;EM&gt;org.json&lt;/EM&gt; API&lt;EM&gt;&lt;FONT size="2"&gt;(org.json.JSONObject &amp;amp; org.json.JSONArray)&lt;/FONT&gt;&lt;/EM&gt; is used for JSON output.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="java"&gt;@Component(service = Servlet.class, property = {"sling.servlet.methods=" + HttpConstants.METHOD_GET,
        "sling.servlet.paths=/bin/assetdetails", "sling.servlet.extensions=json"})
public class AssetDetailsJSON extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = 1L;
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Reference
    private QueryBuilder queryBuilder;

    @Override
    protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) {
        //String assetFolderPath = req.getParameter("assetFolder");
        String assetFolderPath = "/content/dam/we-retail/en/features";
        Map&amp;lt;String, String&amp;gt; predicatesMap = new HashMap&amp;lt;String, String&amp;gt;();

        ResourceResolver rescResolver = req.getResourceResolver();
        Session session = rescResolver.adaptTo(Session.class);

        if (null != assetFolderPath) {
            predicatesMap.put("path", assetFolderPath);
            predicatesMap.put("type", "dam:Asset");
            predicatesMap.put("p.limit", "-1");
            Query query = queryBuilder.createQuery(PredicateGroup.create(predicatesMap), session);
            SearchResult queryResults = query.getResult();            
            JSONArray jsonArr = new JSONArray();
            queryResults.getHits().forEach((hit) -&amp;gt; {
                try {
                    String assetPath = hit.getPath();
                    Resource assetResc = rescResolver.resolve(assetPath);
                    Asset assetObj = assetResc.adaptTo(Asset.class);
                    String assetName = assetObj.getName();
                    String assetDAMPath = assetObj.getPath();
                    String assetFormat = assetObj.getMetadataValue("dc:format"); // can use getMetadataValueFromJcr as well

                    JSONObject jsonObj = new JSONObject();
                    jsonObj.put("name", assetName);
                    jsonObj.put("path", assetDAMPath); // can have altText here
                    jsonObj.put("assetFormat", assetFormat);
                    jsonArr.put(jsonObj);
                } catch (RepositoryException e) {
                    LOG.error("RepositoryException={}", e.getMessage());
                } catch (JSONException e) {
                    LOG.error("JSONException={}", e.getMessage());
                }
            });
            try {
                LOG.debug("JSON Output={}", jsonArr.toString());
                resp.setContentType("application/json");
                resp.setCharacterEncoding("UTF-8");
                resp.getWriter().write(jsonArr.toString());
            } catch (IOException e) {
                LOG.error("IOException={}", e.getMessage());
            }

        }
        else{
            LOG.debug("No asset folder input !!");
        }


    }
}&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Vijayalakshmi_S_0-1624393253952.png" style="width: 999px;"&gt;&lt;img src="https://experienceleaguecommunities.adobe.com/t5/image/serverpage/image-id/32627i7385A63887C8D8D3/image-size/large/is-moderation-mode/true?v=v2&amp;amp;px=999" role="button" title="Vijayalakshmi_S_0-1624393253952.png" alt="Vijayalakshmi_S_0-1624393253952.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Jun 2021 20:21:50 GMT</pubDate>
    <dc:creator>Vijayalakshmi_S</dc:creator>
    <dc:date>2021-06-22T20:21:50Z</dc:date>
    <item>
      <title>[AEM6.5] Create a service which receives the path of a folder in DAM as input, and Output as JSON</title>
      <link>https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-create-a-service-which-receives-the-path-of-a-folder-in/m-p/413459#M1520</link>
      <description>&lt;P&gt;Hi Team,&lt;BR /&gt;I have a requirement, where&lt;BR /&gt;Create a service which receives the path of a folder in DAM as input and iterates through all image assets in that folder and returns a json response capturing the name and altText of each image in that folder. The output format should be -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;"name":"&amp;lt;imageName&amp;gt;",&lt;/P&gt;&lt;P&gt;"altText": "&amp;lt;imageAltText&amp;gt;"&lt;/P&gt;&lt;P&gt;},&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;"name":"&amp;lt;imageName&amp;gt;",&lt;/P&gt;&lt;P&gt;"altText": "&amp;lt;imageAltText&amp;gt;"&lt;/P&gt;&lt;P&gt;},&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;]&lt;BR /&gt;&lt;BR /&gt;Can anyone help me with this.&lt;BR /&gt;&lt;BR /&gt;&lt;LI-USER uid="11077056"&gt;&lt;/LI-USER&gt;&amp;nbsp; &amp;nbsp;&lt;LI-USER uid="6057500"&gt;&lt;/LI-USER&gt;&amp;nbsp;&amp;nbsp;&lt;LI-USER uid="6786635"&gt;&lt;/LI-USER&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Oct 2021 17:43:41 GMT</pubDate>
      <guid>https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-create-a-service-which-receives-the-path-of-a-folder-in/m-p/413459#M1520</guid>
      <dc:creator>tushaar_srivastava</dc:creator>
      <dc:date>2021-10-08T17:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: [AEM6.5] Create a service which receives the path of...</title>
      <link>https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-create-a-service-which-receives-the-path-of-a-folder-in/m-p/413465#M1521</link>
      <description>&lt;P&gt;&lt;LI-USER uid="13016566"&gt;&lt;/LI-USER&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Assuming that your use will be used within the author environment, I would create a Sling Servlet that will accept "path". The servlet will utilise the Query Manager, JCR-SQL2. Once when the results are obtained, then the backend logic will transform the result object, which will then output the formatted JSON.&lt;/P&gt;&lt;P&gt;// just a brief example below...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;private static final String JCR_SQL_QUERY = "SELECT * FROM [dam:AssetContent] AS nodes WHERE ISDESCENDANTNODE ([{my_query}]";
...

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    
    ...
    
    String formattedQuery = JCR_SQL_QUERY.replace("{my_query}", request.getParameter("path"));

    // JCR-SQL2 API (PICK ONE);
    Workspace workspace = session.getWorkspace();
    QueryManager queryManager = workspace.getQueryManager();
    Query query = queryManager.createQuery(formattedQuery, Query.JCR_SQL2);
    QueryResult result = query.execute();

    // return transform and return JSON
    Object formattedJSON = formattedJSON(result);

    // output the response in JSON 
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    response.getWriter().write(formattedJSON);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jun 2021 19:05:59 GMT</pubDate>
      <guid>https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-create-a-service-which-receives-the-path-of-a-folder-in/m-p/413465#M1521</guid>
      <dc:creator>BrianKasingli</dc:creator>
      <dc:date>2021-06-22T19:05:59Z</dc:date>
    </item>
    <item>
      <title>Re: [AEM6.5] Create a service which receives the path of...</title>
      <link>https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-create-a-service-which-receives-the-path-of-a-folder-in/m-p/413471#M1522</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;LI-USER uid="13016566"&gt;&lt;/LI-USER&gt;,&lt;/P&gt;&lt;P&gt;Please find below sample snippet making use of &lt;EM&gt;Query Builder&lt;/EM&gt; API to bring in the AEM assets under specific path.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Result sets are adapted to Asset object (&lt;EM&gt;com.day.cq.dam.api.Asset&lt;/EM&gt;)&lt;/LI&gt;&lt;LI&gt;Assuming &lt;EM&gt;altText&lt;/EM&gt; is a custom Metadata property, we can gain access to the same via &lt;EM&gt;Asset&lt;/EM&gt; object.&lt;/LI&gt;&lt;LI&gt;&lt;EM&gt;org.json&lt;/EM&gt; API&lt;EM&gt;&lt;FONT size="2"&gt;(org.json.JSONObject &amp;amp; org.json.JSONArray)&lt;/FONT&gt;&lt;/EM&gt; is used for JSON output.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="java"&gt;@Component(service = Servlet.class, property = {"sling.servlet.methods=" + HttpConstants.METHOD_GET,
        "sling.servlet.paths=/bin/assetdetails", "sling.servlet.extensions=json"})
public class AssetDetailsJSON extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = 1L;
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Reference
    private QueryBuilder queryBuilder;

    @Override
    protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) {
        //String assetFolderPath = req.getParameter("assetFolder");
        String assetFolderPath = "/content/dam/we-retail/en/features";
        Map&amp;lt;String, String&amp;gt; predicatesMap = new HashMap&amp;lt;String, String&amp;gt;();

        ResourceResolver rescResolver = req.getResourceResolver();
        Session session = rescResolver.adaptTo(Session.class);

        if (null != assetFolderPath) {
            predicatesMap.put("path", assetFolderPath);
            predicatesMap.put("type", "dam:Asset");
            predicatesMap.put("p.limit", "-1");
            Query query = queryBuilder.createQuery(PredicateGroup.create(predicatesMap), session);
            SearchResult queryResults = query.getResult();            
            JSONArray jsonArr = new JSONArray();
            queryResults.getHits().forEach((hit) -&amp;gt; {
                try {
                    String assetPath = hit.getPath();
                    Resource assetResc = rescResolver.resolve(assetPath);
                    Asset assetObj = assetResc.adaptTo(Asset.class);
                    String assetName = assetObj.getName();
                    String assetDAMPath = assetObj.getPath();
                    String assetFormat = assetObj.getMetadataValue("dc:format"); // can use getMetadataValueFromJcr as well

                    JSONObject jsonObj = new JSONObject();
                    jsonObj.put("name", assetName);
                    jsonObj.put("path", assetDAMPath); // can have altText here
                    jsonObj.put("assetFormat", assetFormat);
                    jsonArr.put(jsonObj);
                } catch (RepositoryException e) {
                    LOG.error("RepositoryException={}", e.getMessage());
                } catch (JSONException e) {
                    LOG.error("JSONException={}", e.getMessage());
                }
            });
            try {
                LOG.debug("JSON Output={}", jsonArr.toString());
                resp.setContentType("application/json");
                resp.setCharacterEncoding("UTF-8");
                resp.getWriter().write(jsonArr.toString());
            } catch (IOException e) {
                LOG.error("IOException={}", e.getMessage());
            }

        }
        else{
            LOG.debug("No asset folder input !!");
        }


    }
}&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Vijayalakshmi_S_0-1624393253952.png" style="width: 999px;"&gt;&lt;img src="https://experienceleaguecommunities.adobe.com/t5/image/serverpage/image-id/32627i7385A63887C8D8D3/image-size/large/is-moderation-mode/true?v=v2&amp;amp;px=999" role="button" title="Vijayalakshmi_S_0-1624393253952.png" alt="Vijayalakshmi_S_0-1624393253952.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jun 2021 20:21:50 GMT</pubDate>
      <guid>https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-create-a-service-which-receives-the-path-of-a-folder-in/m-p/413471#M1522</guid>
      <dc:creator>Vijayalakshmi_S</dc:creator>
      <dc:date>2021-06-22T20:21:50Z</dc:date>
    </item>
    <item>
      <title>Re: [AEM6.5] Create a service which receives the path of...</title>
      <link>https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-create-a-service-which-receives-the-path-of-a-folder-in/m-p/413493#M1523</link>
      <description>Thank you BrianKasingli for this approach, I have created a textbox and on click on submit button invoked the servlet and based on that able to get the json.</description>
      <pubDate>Wed, 23 Jun 2021 04:49:37 GMT</pubDate>
      <guid>https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-create-a-service-which-receives-the-path-of-a-folder-in/m-p/413493#M1523</guid>
      <dc:creator>tushaar_srivastava</dc:creator>
      <dc:date>2021-06-23T04:49:37Z</dc:date>
    </item>
  </channel>
</rss>

