How to get assets from DAM in a java class?

Avatar

Avatar

frankml_ve

Avatar

frankml_ve

frankml_ve

31-10-2019

Hello.

I'm trying to make a java class that use the script to get image from the DAM posted here Adobe Experience Manager Help | Creating an Adobe Experience Manager DAM Image component

but I'm having troubles with the Sling variable, because on th JSP file, the sling variable is declared in  the global.jsp file.

<%@include file="/libs/foundation/global.jsp"%>

SlingRepository slingRep = sling.getService(SlingRepository.class);

Session session = slingRep.loginAdministrative(null);

QueryBuilder qb ;

Map<String, String> map = new HashMap<String,String>();

map.put("type", "dam:Asset");

map.put("property", "jcr:content/metadata/dc:format");

map.put("property.value", "image/jpeg");

qb=resource.getResourceResolver().adaptTo(QueryBuilder.class);

Query query = qb.createQuery(PredicateGroup.create(map), session);

SearchResult sr= query.getResult();

String assetPath=null;

// iterating over the results

  for (Hit hit : sr.getHits()) {

      String path = hit.getPath();

      Resource rs = resourceResolver.getResource(path);

      Asset asset = rs.adaptTo(Asset.class);    

     assetPath = asset.getPath();

In the code above the sling variable is on the line 01.

When I paste this code on my java class, I get an error because Eclipse IDE can't reach the Sling API library.

How can I initialize this variable to make my code work?

View Entire Topic

Avatar

Avatar

anthonyt2853861

Avatar

anthonyt2853861

anthonyt2853861

13-11-2019

Hi Frank, that error may be because your publish instance is not exactly synchronized with your author instance.

I recommend that you verify if the bundle both in your author instance and publish are the same.

You can validate in the following url:

Author instance: http: localhost:4502/system/console/status-slingmodels

Instance publish: http://localhost:4503/system/console/status-slingmodels

If they are not the same, go to your core project and run the following command by console: mvn clean install -PautoInstallPackage -Daem.port=4503

In this way you will synchronize all your classes in the publish intance, just as you have them in the author instance.