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?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
Jörg_Hoh
Employee

12-11-2019

Hi,

if you already have the asset (as path), it's much more performant if you use the Asset API directly instead using the QB.

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

if (asset != null) {

  AssetMetadata meta = asset.getMetadata();

....

}

Answers (6)

Answers (6)

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.

Avatar

Avatar

frankml_ve

Avatar

frankml_ve

frankml_ve

12-11-2019

Thanks a to all!

My team and I solve the problem using a QueryBuilder API to get the metadata info from asset.

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
Jörg_Hoh
Employee

12-11-2019

next time, please open a new thread when a new issues arises and which cannot be clearly linked to the topic of thread. The mapping "one issue" - "one thread" makes it easier to keep track and also helps anyone who's searching this forum.. And of course you can cross-reference these issues.

Anyway, assuming that this is AEM 6.5.

* Please clear the filesystem classloader cache ($Host//system/console/fsclassloader, button at the top right)

* OpenJDK is not supported, please switch to Oracle Java 11.

HTH,

Jörg

Avatar

Avatar

frankml_ve

Avatar

frankml_ve

frankml_ve

12-11-2019

hey  Jörg!

Actually now my team and I we are having troubles with this component, when we publish it to our local publish instance it crash!!!!

In the Author instance works perfectly even the JS file and Less. But when we put this on Publish trows this error.

org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Compilation errors in org/apache/sling/scripting/sightly/apps/wknd/components/content/galeria/galeria_html.java: Line 42, column 1899 : com.adobe.aem.guides.wknd.core.models.ClassificationAssets cannot be resolved to a type

Cannot serve request to /content/wknd/asdasda.html on this server


ApacheSling/2.6 (jetty/9.4.15.v20190215, OpenJDK 64-Bit Server VM 11.0.4, Linux 4.15.0-66-generic amd64)
Do you know what are we missing on publish instance config??

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

01-11-2019

Avatar

Avatar

Ankur_Khare

MVP

Avatar

Ankur_Khare

MVP

Ankur_Khare
MVP

01-11-2019

Try like this in ur java class-

@Reference

private ResourceResolverFactory resolverFactory;

ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);

session = resourceResolver.adaptTo(Session.class);

// Obtain the query manager for the session ...

javax.jcr.query.QueryManager queryManager = session.getWorkspace().getQueryManager();

// Setup the query based on user input

String sqlStatement = "";

// Setup the query to get PATH

sqlStatement = "SELECT * ur query;

javax.jcr.query.Query query = queryManager.createQuery(sqlStatement, "JCR-SQL2");

// Execute the query and get the results ...

javax.jcr.query.QueryResult result = query.execute();