Expand my Community achievements bar.

Nomination window for the Adobe Community Advisor Program, Class of 2025, is now open!
SOLVED

org.apache.sling.api.resource.LoginException: Cannot derive user name for bundle my-project.core [bundleID] and sub service metadata-update-service

Avatar

Level 4

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

354 active mappings, looking for mapping for my-project.core/myService
23.01.2025 09:36:23.213 [] *INFO* [203.99.194.157 [1737624971609] POST /apps/acs-commons/content/manage-controlled-processes/jcr:content.start.json HTTP/1.1]my-project.core.service.impl.AssetImporterServiceImpl Content value:
23.01.2025 09:36:23.214 [cm-p15699-e36869-aem-author-59bfd6cff5-hr447] *DEBUG* [203.99.194.157 [1737624971609] POST /apps/acs-commons/content/manage-controlled-processes/jcr:content.start.json HTTP/1.1]
 
 
 
 
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl areValidPrincipals: principalNames are null -> invalid
getServicePrincipalNames(bundle my-project.core [640], subServiceName myService) returns [null] (raw principalNames=null, valid=false). I am passing this resourceResolver from MCP and getting resourceResolverFactory using @reference.
 
 
If anyone know what could be the issue, please let me know. Thanks in advance!
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();
}
}
}
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 4

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

View solution in original post

3 Replies

Avatar

Level 9

Hi @BhavaniBharani 

 

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

Avatar

Correct answer by
Level 4

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

Avatar

Level 7

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()