Expand my Community achievements bar.

SOLVED

Adding a property to an Asset in AEM DAM using Java

Avatar

Level 2

Dear community members, @arunpatidar , @lukasz-m 

 

I am trying to upload an asset as byte[] stream to AEM DAM. I am able to upload the asset to AEM DAM successfully using below code snippet.

 

But I am unable to add/set the required properties to this asset. I am getting below exception. Could you please tell me how to resolve this issue?

 

I thank you in advance.

 

Code snippet:

public void uploadAssetToAEM(TransflexFile file, byte[] assetData) {
Map<String, Object> authenticationInfo = new HashMap<>();
authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, "portal");

try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(authenticationInfo)) {
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
//Create the asset path
String assetPath = file.getFolderPath() + "/" + file.getFileName();
//Convert byte array to InputStream
InputStream inputStream = new ByteArrayInputStream(assetData);
Asset asset = assetManager.createAsset(assetPath, inputStream, file.getMimeType(), true);
logger.info("In uploadAssetToAEM function. after  createAsset.");
//Set additional properties(dc:title..etc.)
Resource assetResource = asset.adaptTo(Resource.class);
ModifiableValueMap properties = assetResource.adaptTo(ModifiableValueMap.class);
if (properties != null) {
properties.put("dc:title", file.getTitle().toString());
logger.info("In uploadAssetToAEM function. After properties(jcr:title) update.");
} else {
logger.info("Failed to adapt asset to ModifiableValueMap. Unable to update properties.");
}
resourceResolver.commit();
resourceResolver.close();
//publishing the uploaded the Asset.
publishAsset(asset);
logger.info("Successfully published the Asset using DAM workflow.");
} catch (Exception e) {
}
}

 

 

Exception:

22.08.2023 17:37:05.756 *INFO* [sling-default-2-com.c.o.aem.c.core.scheduler.ladfile.AssetUploadAndPublishScheduler:a345cf07-1679-4de4-af74-5f80ad3342d7] com.c.o.aem.c.core.scheduler.ladfile.AssetUploadAndPublishScheduler Exception occured in uploadAssetToAEM function. Value for key jcr:title can't be put into node: lad file 03 april 2023
java.lang.IllegalArgumentException: Value for key jcr:title can't be put into node: lad file 03 april 2023
at org.apache.sling.jcr.resource.internal.JcrModifiableValueMap.put(JcrModifiableValueMap.java:458) [org.apache.sling.jcr.resource:3.0.18]
at org.apache.sling.jcr.resource.internal.JcrModifiableValueMap.put(JcrModifiableValueMap.java:47) [org.apache.sling.jcr.resource:3.0.18]
at com.c.o.aem.c.core.scheduler.ladfile.AssetUploadAndPublishScheduler.uploadAssetToAEM(AssetUploadAndPublishScheduler.java:302)
at com.c.o.aem.c.core.scheduler.ladfile.AssetUploadAndPublishScheduler.run(AssetUploadAndPublishScheduler.java:260)
at org.apache.sling.commons.scheduler.impl.QuartzJobExecutor.execute(QuartzJobExecutor.java:349) [org.apache.sling.commons.scheduler:2.7.12]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [org.apache.sling.commons.scheduler:2.7.12]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.jcr.nodetype.ConstraintViolationException: No matching property definition: jcr:title = lad file 03 april 2023
at org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate.setProperty(NodeDelegate.java:519) [org.apache.jackrabbit.oak-jcr:1.22.8]
at org.apache.jackrabbit.oak.jcr.session.NodeImpl$36.perform(NodeImpl.java:1403) [org.apache.jackrabbit.oak-jcr:1.22.8]
at org.apache.jackrabbit.oak.jcr.session.NodeImpl$36.perform(NodeImpl.java:1390) [org.apache.jackrabbit.oak-jcr:1.22.8]
at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.perform(SessionDelegate.java:207) [org.apache.jackrabbit.oak-jcr:1.22.8]
at org.apache.jackrabbit.oak.jcr.session.ItemImpl.perform(ItemImpl.java:112) [org.apache.jackrabbit.oak-jcr:1.22.8]
at org.apache.jackrabbit.oak.jcr.session.NodeImpl.internalSetProperty(NodeImpl.java:1390) [org.apache.jackrabbit.oak-jcr:1.22.8]
at org.apache.jackrabbit.oak.jcr.session.NodeImpl.setProperty(NodeImpl.java:368) [org.apache.jackrabbit.oak-jcr:1.22.8]
at org.apache.sling.jcr.resource.internal.JcrModifiableValueMap.put(JcrModifiableValueMap.java:455) [org.apache.sling.jcr.resource:3.0.18]
... 8 common frames omitted

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@mahabubali ,

 

The dc:title property should be added to the metadata node under jcr:content node of your file/asset.

somefile.pdf/jcr:content/metadata node.

I think in your code, you are getting the ModifiableValueMap of asset itself instead of the metadata node. Try the below code and see if it works.

if (asset != null) {
    // Set the dc:title property
    asset.getMetadata().put("dc:title", "New Title");
}

 

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

@mahabubali ,

 

The dc:title property should be added to the metadata node under jcr:content node of your file/asset.

somefile.pdf/jcr:content/metadata node.

I think in your code, you are getting the ModifiableValueMap of asset itself instead of the metadata node. Try the below code and see if it works.

if (asset != null) {
    // Set the dc:title property
    asset.getMetadata().put("dc:title", "New Title");
}

 

Avatar

Level 2

Hi @Sudheer_Sundalam 

 

I have retrieved the asset/jcr:content/metadata resource, adapted to ModucfiableValueMap and updated the property successfully. 

Thanks for your response. 

 

-Thanks 

Mahabub Ali Mohammad.