Expand my Community achievements bar.

SOLVED

Felix script console(Not the groovy console)

Avatar

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

Avatar

Correct answer by
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

Avatar

Correct answer by
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"