user972418
user972418

Reputation: 827

AEM 6: SlingBindings object is null

I have created sling:OsgiConfig nodes inside config folders like config.author, config.publish and so on. I am trying to fetch properties from these nodes by doing something like this:

public static List fetchTokenLinksFromOsgiConfig(final SlingHttpServletRequest slingRequest) throws IOException {
        List<String> tokenlinksList = new ArrayList<String>();
        SlingBindings bindings = (SlingBindings) slingRequest.getAttribute(SlingBindings.class.getName());
        log.info("=================inside fetchTokenLinksFromOsgiConfig======================"+bindings);
        SlingScriptHelper sling = bindings.getSling();
        Configuration conf = sling.getService(org.osgi.service.cm.ConfigurationAdmin.class).getConfiguration("com.xxxxx.TokenLinksConfig");
        log.info("=================inside fetchTokenLinksFromOsgiConfig:::taking configurations======================");
        String TokenId = (String) conf.getProperties().get("TokenId");
        String TokenSecret = (String) conf.getProperties().get("TokenSecret");
        String OAuthLink = (String) conf.getProperties().get("OAuthLink");
        log.info("=================TokenId:::TokenSecret:::OAuthLink======================"+TokenId +" "+TokenSecret+" "+OAuthLink);
        if(!StringUtil.isEmpty(TokenId)) {
            tokenlinksList.add(TokenId);
        }
        if(!StringUtil.isEmpty(TokenSecret)) {
            tokenlinksList.add(TokenSecret);
        }
        if(!StringUtil.isEmpty(OAuthLink)) {
            tokenlinksList.add(OAuthLink);
        }
        return tokenlinksList;
    }

I am calling this method from a sling servlet like this:

List tokenList = OsgiConfigUtil.fetchTokenLinksFromOsgiConfig(slingRequest);

but the bindings object of type SlingBindings is coming null. I have no idea how to work this out ?

Thanks in advance

Upvotes: 0

Views: 778

Answers (1)

Tomek Rękawek
Tomek Rękawek

Reputation: 9304

Sling servlet is an OSGi component, so you can inject the ConfigurationAdmin service directly, using SCR @Reference annotation:

public MyServlet extends SlingSafeMethodServlet {

    @Reference
    private ConfigurationAdmin confAdmin;

    public doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        confAdmin.getConfiguration("com.myuhc.TokenLinksConfig");
    }
}

No need to use the SlingBindings object, which is meant to provide OSGi services inside the JSP scripts.

Upvotes: 1

Related Questions