Felix script console(Not the groovy console) | Community
Skip to main content
Level 4
July 26, 2021
Solved

Felix script console(Not the groovy console)

  • July 26, 2021
  • 1 reply
  • 1641 views

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

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Vijayalakshmi_S

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"

1 reply

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
July 26, 2021

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"