1

I added a custom metadata in CQ5 with name ./dc:sample. Is there a way I can programmatically set this metadata for an asset?

I've written a workflow that intercepts the uploaded assets and replaces them with inputstream sent from the third party service. I accomplish this by doing the following in my workflow.

Rendition rendition = resource.adaptTo(Rendition.class);
Asset asset = rendition.getAsset();
InputStream newInputStream = myService.sendFile(is);
asset.addRendition(rendition.getName(),newInputStream,asset.getMimeType());

Question

At this time I would like to set the ./dc:sample metadata to a string like "test checking". Is this possible to do?

Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43
Anthony
  • 33,838
  • 42
  • 169
  • 278

1 Answers1

2

You can adapt the Asset to Resource, get its jcr:content/metadata grandchild and adapt it to ModifiableValueMap:

Resource metadataRes = asset.adaptTo(Resource.class).getChild("jcr:content/metadata");
ModifiableValueMap map = metadataRes.adaptTo(ModifiableValueMap.class);
map.put("dc:sample", "test checking");
resourceResolver.commit();
Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43
  • It seems that `ModifiableValueMap` is available in `2.5.0`. I had been using `2.2.4`. I changed the version in my POM and the code compiles and bundle is built. However, when I upload this bundle to CQ5 it gives error: `Unresolved constraint in bundle [345]: Unable to resolve 345.0: missing requirement [345.0] osgi.wiring.package; (&(osgi.wiring.package=org.apache.sling.api.resource)(version>=2.4.0)(!(version>=3.0.0)))) )` – Anthony Jul 09 '14 at 16:00
  • By default there is a bundle installed `Apache Sling API (org.apache.sling.api)` with version `2.4.3.R1488084`. Should I delete that and upload the `2.5.0`? Would that break other things in CQ5 since it was there by default. – Anthony Jul 09 '14 at 16:22
  • [This answer](http://stackoverflow.com/questions/24663090/how-to-set-a-resource-property/24663091#24663091) provides a few ways of setting property in different Sling and CQ versions. – Tomek Rękawek Jul 09 '14 at 20:19