i am getting myConfig.class returning as Null. below is the code to fetch it in my sling model class.
Resource contentResource = resolver.getResource(currentPage.getPath());
ConfigurationBuilder caconfig = contentResource.adaptTo(ConfigurationBuilder.class); //caconfig is returning value and its non-empty.
MyConfig pensConfig = caconfig.as(MyConfig .class); //pensConfig is returning NULL here.
pensConfig.apiURL();
------------------------------------------
It seems caconfig.as(MyConfig.class) is not working as expected. Below is the config class.
import org.apache.sling.caconfig.annotation.Configuration;
import org.apache.sling.caconfig.annotation.Property;
@Configuration(
label="API Configuration ",
description="Template configuration for Pension Pot Info",
name="templateDefinitions/MyConfig"
)
public @interface MyConfig{
@property(
label="GET API url",
description="API URL to get info",
order=1
)
String apiURL() default "http://somerandom";
}
i can see at http://localhost:4502/system/console/slingcaconfig, the CA config is resolving. What i am still missing??
Solved! Go to Solution.
Views
Replies
Total Likes
As I am not aware about PDPensionsPotInfo so, I have removed interface: PDPensionsPotInfo from your sling model and here is the code -
package com.aem.demo.core.models;
import javax.annotation.PostConstruct;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.caconfig.ConfigurationBuilder;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aem.demo.core.configurations.MyConfig;
import com.day.cq.wcm.api.Page;
@Model(adaptables = { SlingHttpServletRequest.class, Resource.class}, resourceType = PDPensionsPotInfoImpl.RESOURCE_TYPE)
public class PDPensionsPotInfoImpl {
private static final Logger LOG = LoggerFactory.getLogger(PDPensionsPotInfoImpl.class);
protected static final String RESOURCE_TYPE = "weretail/components/content/app";
@SlingObject private Resource resource;
@SlingObject
private ResourceResolver resolver;
@ScriptVariable
Page currentPage;
private String url;
/**
* @Return the url
*/
public String getUrl() {
return url;
}
@PostConstruct
protected void initModel() {
LOG.info("*** Current Page Path *** {}", currentPage.getPath());
Resource contentResource = resolver.getResource(currentPage.getPath());
ConfigurationBuilder caconfig = contentResource.adaptTo(ConfigurationBuilder.class);
MyConfig pensConfig = caconfig.as(MyConfig.class);
url = pensConfig.apiURL();
}
}
Context aware configuration
package com.aem.demo.core.configurations;
import org.apache.sling.caconfig.annotation.Configuration;
import org.apache.sling.caconfig.annotation.Property;
@Configuration(label = "API Configuration ", description = "Template configuration for Pension Pot Info", name = "templateDefinitions/MyConfig")
public @interface MyConfig {
@property(label = "GET API url", description = "API URL to get info", order = 1)
String apiURL() default "http://somerandom";
}
Configuration references and Context /Content resources below -
See this value is getting displayed on page -
could you please check Configuration references and Context /Content resources in your AEM instance.
How are you getting ResourceResolver object in your sling model , I feel issue with ResouceResolver object.
You can refer the below code and please review the ResourceResolver object -
package com.aem.demo.core.models;
import java.util.Objects;
import javax.annotation.PostConstruct;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.caconfig.ConfigurationBuilder;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import com.aem.demo.core.configurations.DemoCountryConfiguration;
import com.day.cq.wcm.api.Page;
import com.drew.lang.annotations.Nullable;
/**
* @author debal
*
*/
@Model(adaptables = { SlingHttpServletRequest.class }, resourceType = {
ReadCountryInfoModel.RESOURCE_TYPE }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ReadCountryInfoModel {
protected static final String RESOURCE_TYPE = "weretail/components/content/app";
@SlingObject
ResourceResolver resourceResoulver;
@ScriptVariable
private Page currentPage;
private String countryName;
private String capitalName;
private String financialCapital;
private String prefix;
private DemoCountryConfiguration demoCountryConfiguration;
/**
* @Return the countryName
*/
public String getCountryName() {
return countryName;
}
/**
* @Return the capitalName
*/
public String getCapitalName() {
return capitalName;
}
/**
* @Return the financialCapital
*/
public String getFinancialCapital() {
return financialCapital;
}
/**
* @Return the prefix
*/
public String getPrefix() {
return prefix;
}
@PostConstruct
protected void postConstruct() {
demoCountryConfiguration = getContextAwareConfiguration(currentPage.getPath(), resourceResoulver);
countryName = demoCountryConfiguration.countryName();
capitalName = demoCountryConfiguration.capitalName();
financialCapital = demoCountryConfiguration.financialcapitalName();
prefix = demoCountryConfiguration.prefix();
}
private DemoCountryConfiguration getContextAwareConfiguration(String path, ResourceResolver resourceResoulver) {
@nullable
Resource resource = resourceResoulver.getResource(path);
if (Objects.nonNull(resource)) {
@nullable
ConfigurationBuilder configurationBuilder = resource.adaptTo(ConfigurationBuilder.class);
if (Objects.nonNull(configurationBuilder)) {
return configurationBuilder.as(DemoCountryConfiguration.class);
}
}
return null;
}
}
Hope this will help.
Hi @DEBAL_DAS ,
i am getting resource resolver object through @SlingObject, here is my entire model class code.
package myprj.core.models.impl;
import javax.annotation.PostConstruct;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.caconfig.ConfigurationBuilder;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.wcm.api.Page;
import myprj.core.config.PensionPotConfiguration;
import myprj.core.models.PDPensionsPotInfo;
@Model(adaptables = { SlingHttpServletRequest.class, Resource.class }, adapters = {
PDPensionsPotInfo.class }, resourceType = PDPensionsPotInfoImpl.RESOURCE_TYPE)
public class PDPensionsPotInfoImpl implements PDPensionsPotInfo {
private static final Logger LOG = LoggerFactory.getLogger(PDPensionsPotInfoImpl.class);
protected static final String RESOURCE_TYPE = "myprj/core/components/content/pdp/pd-pensions-pot-info";
@Self
private PDPensionsPotInfoApiSettings pensionAPISettings;
@Self
private PDPensionsPotInfoMayBe pensionMayBe;
@Self
private PDPensionsPotInfoMatchFound pensionMatchFound;
@SlingObject private Resource resource;
@SlingObject private ResourceResolver resolver;
@ScriptVariable Page currentPage;
@PostConstruct
protected void initModel() {
Resource contentResource = resolver.getResource(currentPage.getPath());
ConfigurationBuilder caconfig = contentResource.adaptTo(ConfigurationBuilder.class);
PensionPotConfiguration pensConfig = caconfig.as(PensionPotConfiguration.class);
pensConfig.apiURL();
}
@Override
public PDPensionsPotInfoApiSettings getPensionAPISettings() {
return pensionAPISettings;
}
@Override
public PDPensionsPotInfoMayBe getPensionMayBe() {
return pensionMayBe;
}
@Override
public PDPensionsPotInfoMatchFound getPensionMatchFound() {
return pensionMatchFound;
}
}
As I am not aware about PDPensionsPotInfo so, I have removed interface: PDPensionsPotInfo from your sling model and here is the code -
package com.aem.demo.core.models;
import javax.annotation.PostConstruct;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.caconfig.ConfigurationBuilder;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aem.demo.core.configurations.MyConfig;
import com.day.cq.wcm.api.Page;
@Model(adaptables = { SlingHttpServletRequest.class, Resource.class}, resourceType = PDPensionsPotInfoImpl.RESOURCE_TYPE)
public class PDPensionsPotInfoImpl {
private static final Logger LOG = LoggerFactory.getLogger(PDPensionsPotInfoImpl.class);
protected static final String RESOURCE_TYPE = "weretail/components/content/app";
@SlingObject private Resource resource;
@SlingObject
private ResourceResolver resolver;
@ScriptVariable
Page currentPage;
private String url;
/**
* @Return the url
*/
public String getUrl() {
return url;
}
@PostConstruct
protected void initModel() {
LOG.info("*** Current Page Path *** {}", currentPage.getPath());
Resource contentResource = resolver.getResource(currentPage.getPath());
ConfigurationBuilder caconfig = contentResource.adaptTo(ConfigurationBuilder.class);
MyConfig pensConfig = caconfig.as(MyConfig.class);
url = pensConfig.apiURL();
}
}
Context aware configuration
package com.aem.demo.core.configurations;
import org.apache.sling.caconfig.annotation.Configuration;
import org.apache.sling.caconfig.annotation.Property;
@Configuration(label = "API Configuration ", description = "Template configuration for Pension Pot Info", name = "templateDefinitions/MyConfig")
public @interface MyConfig {
@property(label = "GET API url", description = "API URL to get info", order = 1)
String apiURL() default "http://somerandom";
}
Configuration references and Context /Content resources below -
See this value is getting displayed on page -
could you please check Configuration references and Context /Content resources in your AEM instance.
Hi @DEBAL_DAS ,
no luck i tried your config also.
Views
Replies
Total Likes
Views
Like
Replies
Views
Likes
Replies
Views
Likes
Replies