Expand my Community achievements bar.

SOLVED

How to access dam XML file name in Java class

Avatar

Level 2

I need to access sample_content.xml file name present in DAM into the Java class and i need to store it in a method 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @vineel_k,

You can use mix of Sling and Asset java api to get any asset name.

Resource resource = resourceResolver.getResource("/content/dam/sample_content.xml");
Asset asset = resource.adaptTo(Asset.class);
String assetName = asset.getName();

Having asset object you can also get all other information about specific asset.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @vineel_k,

You can use mix of Sling and Asset java api to get any asset name.

Resource resource = resourceResolver.getResource("/content/dam/sample_content.xml");
Asset asset = resource.adaptTo(Asset.class);
String assetName = asset.getName();

Having asset object you can also get all other information about specific asset.

Avatar

Community Advisor

@vineel_k 

Below example worked in my local

 

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import com.day.cq.dam.commons.util.DamUtil;
import org.apache.sling.api.resource.ResourceResolver;

public class MyAssetReader {

public void readAssetName(String assetPath, ResourceResolver resourceResolver) {
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
Asset asset = DamUtil.resolveToAsset(resourceResolver.getResource(assetPath));
String assetName = asset.getName();
System.out.println("Asset name: " + assetName);
}

}