AEM: Access JS File in DAM using JavaScript Use-API - Stack Overflow

I have a certain JS File in DAM, that contains a JSON.I would like to access that file in the helper J

I have a certain JS File in DAM, that contains a JSON. I would like to access that file in the helper JS using any methods of JavaScript USE API in Sightly. I know it can be done using Java quite easily, but I would like to do it in a manner that I do not want to touch any Java Code.

I tried things like below. But after that, an input stream is unavailable to convert it to a stream data.

request.resourceResolver.getResource("/path/to/dam/file.js");

AND

request.resourceResolver.getResource("/path/to/dam/file.js").adaptTo(.adobe.granite.asset.api.Asset);

I have a certain JS File in DAM, that contains a JSON. I would like to access that file in the helper JS using any methods of JavaScript USE API in Sightly. I know it can be done using Java quite easily, but I would like to do it in a manner that I do not want to touch any Java Code.

I tried things like below. But after that, an input stream is unavailable to convert it to a stream data.

request.resourceResolver.getResource("/path/to/dam/file.js");

AND

request.resourceResolver.getResource("/path/to/dam/file.js").adaptTo(.adobe.granite.asset.api.Asset);
Share Improve this question edited Oct 9, 2019 at 4:44 Akshay Rathnavas asked Oct 8, 2019 at 18:08 Akshay RathnavasAkshay Rathnavas 3685 silver badges16 bronze badges 2
  • 1 Random tip: if your file contains JSON then it should be a .json file not a .js ;) – theopendle Commented Oct 9, 2019 at 8:26
  • thanks for the tip. Even I was thinking about that, but it's getting rendered from a third-party. I'll pass it on to them to use .json next time :D – Akshay Rathnavas Commented Oct 10, 2019 at 12:34
Add a ment  | 

3 Answers 3

Reset to default 3

I saw the answer posted just now. But I had used another similar method before I saw this.

Here is it. It is very similar to the answer but with a few extra steps.

asset = request.resourceResolver.getResource(jsonPath).adaptTo(.day.cq.dam.api.Asset);
rend = asset.getOriginal().adaptTo(.day.cq.dam.api.Rendition);

OR DIRECTLY

rend= request.resourceResolver.getResource(jsonPath+"/jcr:content/renditions/original").adaptTo(.day.cq.dam.api.Rendition);

AND THEN

inputStream = rend.adaptTo(java.io.InputStream);
var is;
var c = '';
var flag = false;
try {
// reads till the end of the stream
while ((is = inputStream.read()) != -1) {
      c = c + String.fromCharCode(is);
}

 } catch (e) {
   // if any I/O error occurs
   log.debug("Input Stream Error " + e)
 }

I am not sure if there are pure JS Use API methods which allow you to do it. However, since the JS Use API allows you to use Java classes and methods in it, you should be able to use them to fetch the information.

Since your file is stored as an asset in DAM, you need to access the data from the original rendition. One way of doing it is to use the .day.cq.dam.api.Asset API to get the original rendition. The .adobe.granite.asset.api.Asset doesn't have a direct way of accessing the original rendition, hence using the other one.

Working sample custom.js

use(function (data) { 
    var asset = request.resourceResolver.getResource("/content/dam/we-retail/en/data.js").adaptTo(.day.cq.dam.api.Asset);
    var is = asset.getOriginal().adaptTo(java.io.InputStream);
    var jsonData = JSON.parse(org.apache.mons.io.IOUtils.toString(is, "UTF-8"));
    console.log(jsonData);
    return jsonData;
});

The contents of the file in DAM

{
    "fname": "abc",
    "lname": "xyz"
}

The HTL file

<sly data-sly-use.custom="custom.js">
    ${custom.fname} --> ${custom.lname}
</sly>

While trying the existing solutions posted here, I had issues reading an arbitrary text file from the DAM; the resource didn't want to be adapted to Asset or Rendition.

After messing around a bit, I found that the resource could be adapted directly to an InputStream. Here's what I came up with:

function readFileToString(location)
{
    var inputStream = request.resourceResolver.getResource(location).adaptTo(java.io.InputStream);
    var data = org.apache.mons.io.IOUtils.toString(inputStream, "UTF-8");

    return data + "";
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745582771a4634352.html

相关推荐

  • AEM: Access JS File in DAM using JavaScript Use-API - Stack Overflow

    I have a certain JS File in DAM, that contains a JSON.I would like to access that file in the helper J

    12小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信