org.apache.sling.api.resource.LoginException: Cannot derive user name for bundle my-project.core [bundleID] and sub service metadata-update-service
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();
}
}
}