Hi,
I am facing the above issues in one of the case, where i am trying to update the asset metadata from the serviceimpl.
My Requirement:
Via Mcp, doing a assetmigration, then checking the status code of the asset migration. If it is 200, then want to update the asset with few metadata properties. So From MCP, calling the method migrateAssets in serviceImpl to migrate assets, then updating the metadata properties in the serviceImpl method and returning a response back to mcp for the record.
Below is my piece of code, when debugging, i could see that repo init and service user mapping is already present in the developer console and user is also created under /system.
Also in logs noticed
public void updateAssetMetadata(ResourceResolver resourceResolver, String assetPath, String id, String creatorName, String contentType) throws LoginException, RepositoryException {
log.info("Inside update asset metadata method");
Map<String, Object> serviceAuth = singletonMap(ResourceResolverFactory.SUBSERVICE, "metadataService");
try {
// Obtain a ResourceResolver using a system user
resourceResolver = resourceResolverFactory.getServiceResourceResolver(serviceAuth);
// Navigate to the asset's jcr:content/metadata node
Resource assetMetadataResource = resourceResolver.getResource(assetPath + "/jcr:content/metadata");
log.info("Obtained resource");
if (assetMetadataResource != null) {
Node metadataNode = assetMetadataResource.adaptTo(Node.class);
log.info("Resource is not null");
if (metadataNode != null) {
// Update metadata properties
metadataNode.setProperty("Id", id);
metadataNode.setProperty("contentType", contentType);
// Save changes
resourceResolver.adaptTo(Session.class).save();
log.info("Metadata updated successfully for asset: {}", assetPath);
} else {
log.warn("Metadata node not found for asset: {}", assetPath);
}
} else {
log.warn("Asset metadata resource not found: {}", assetPath + "/jcr:content/metadata");
}
} catch (LoginException | RepositoryException e) {
log.error("Error updating metadata for asset: {}", assetPath, e);
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
The issue is there in the configuration file name.Below is the explanation:
BEFORE:
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.myproject.cfg.json
AFTER:
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended~myproject.cfg.json
Thanks all for your response.
Regards,
Bhavani Bharanidharan
I would try to do few things:
1. It is considered bad practice to update method arguments inside method itself, therefore I would create a new variable for resourceResolver with different name, instead of overriding the one coming as argument
2. Even if the metadataService system user is there as you said, I would still check its permissions (http://localhost:4502/crx/explorer/index.jsp)
3. I would create another system user from scratch and verify if things work with this one
4. Search for the internal error message "Cannot derive user name for bundle" in here:
https://github.com/apache/sling-org-apache-sling-resourceresolver/blob/master/src/main/java/org/apac...
, trying to understand the behaviour of AEM under the hood
5. I would also go with Sling API, rather then JCR, for updating resource properties
The issue is there in the configuration file name.Below is the explanation:
BEFORE:
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.myproject.cfg.json
AFTER:
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended~myproject.cfg.json
Thanks all for your response.
Regards,
Bhavani Bharanidharan
Hi @BhavaniBharani
Ensure your service user has neccessary permissions and add logger to debug the exact issue
Map<String, Object> serviceAuth = singletonMap(ResourceResolverFactory.SUBSERVICE, "metadata-update-service");
try (ResourceResolver serviceResolver = resourceResolverFactory.getServiceResourceResolver(serviceAuth)) {
if (serviceResolver == null) {
log.error("Failed to obtain ResourceResolver for subservice: metadata-update-service");
return;
}
log.info("Successfully obtained service resolver: {}", serviceResolver.getUserID());
} catch (LoginException e) {
log.error("LoginException while getting service resolver: {}", e.getMessage());
}
and lastly try changing the
resourceResolver.adaptTo(Session.class).save(); with resourceResolver.commit()
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies