Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Felix script console(Not the groovy console)

AEMnewbie
Level 4
Level 4

Hello , 

 

I am working on a script to get some nodes and save it as json in /content/dam. I. was first. testing out creating a random file in content/dam. but  i keep getting this javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: resourceResolver for class.  which makes sense , but All the examples  like the few mentioned in here https://hashimkhan.in/aem-adobecq5-code-templates/groovy-script/ have just the import and a direct usage of it. Wondering if there is any additional setup needed for it?

 

import com.day.cq.dam.api.AssetManager
import org.apache.sling.api.resource.ResourceResolver

def assetManager = resourceResolver.adaptTo(AssetManager)
assetManager.createAsset("/content/dam/test", "TEST", "application/json", true)-- This is more of random text

 

I am trying to figure out how resourceResolver/AssetManager  can be accessed? -- Or if there is any alternative methods to  run this same use case

 

1 Accepted Solution
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

Hi @AEMnewbie,

You can get ResourceResolver from ResourceResolverFactory OSGi service.

Sample snippet of doing the same in Apache Felix Script Console ( with Groovy) is

 

import org.apache.sling.api.resource.ResourceResolverFactory
import org.apache.sling.api.resource.ResourceResolver
import com.day.cq.dam.api.AssetManager


ResourceResolverFactory resolverFactory = osgi.getService(ResourceResolverFactory.class)
def authMap = ["sling.service.subservice" : "demoproject"]
ResourceResolver rescResolver = resolverFactory.getServiceResourceResolver(authMap)
AssetManager assetMgr = rescResolver.adaptTo(AssetManager.class);
// Rest of the desired logic

 

  • Create a Service User mapper OSGi Factory config instance with Service mappings as - org.apache.felix.webconsole.plugins.scriptconsole:demoproject=groovy-script-serviceuser
    • where demoproject is subServiceName
    • groovy-script-serviceuser is a System user (Provide desired permissions)
  • If you are using Administrative login in the script, whitelist this bundle -org.apache.felix.webconsole.plugins.scriptconsole  in OSGi Factory config  named "Apache Sling Login Admin Whitelist Configuration Fragment"

View solution in original post

1 Reply
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

Hi @AEMnewbie,

You can get ResourceResolver from ResourceResolverFactory OSGi service.

Sample snippet of doing the same in Apache Felix Script Console ( with Groovy) is

 

import org.apache.sling.api.resource.ResourceResolverFactory
import org.apache.sling.api.resource.ResourceResolver
import com.day.cq.dam.api.AssetManager


ResourceResolverFactory resolverFactory = osgi.getService(ResourceResolverFactory.class)
def authMap = ["sling.service.subservice" : "demoproject"]
ResourceResolver rescResolver = resolverFactory.getServiceResourceResolver(authMap)
AssetManager assetMgr = rescResolver.adaptTo(AssetManager.class);
// Rest of the desired logic

 

  • Create a Service User mapper OSGi Factory config instance with Service mappings as - org.apache.felix.webconsole.plugins.scriptconsole:demoproject=groovy-script-serviceuser
    • where demoproject is subServiceName
    • groovy-script-serviceuser is a System user (Provide desired permissions)
  • If you are using Administrative login in the script, whitelist this bundle -org.apache.felix.webconsole.plugins.scriptconsole  in OSGi Factory config  named "Apache Sling Login Admin Whitelist Configuration Fragment"

View solution in original post