I’m working on a Spring Boot project where I use Freemarker for templating. I have configured multiple TemplateLoaders in Freemarker’s configuration to load templates from two different CDN URLs: a baselineUrl and a dynamicUrl. The idea is that if a template is not found in the baselineUrl, it should fall back to the dynamicUrl.
Here is my configuration :
@Slf4j
@Configuration
@ConfigurationProperties("email")
@Data
public class FtlConfiguration {
// https://dynamic/bhnpay-static/notification-template/ftl
private String baselineUrl;
// https://baseline/bhnpay-static/notification-template/ftl
private String dynamicUrl;
private long retentionTimeMillis;
@Bean("emailConfiguration")
public freemarker.template.Configuration createFtlConfigBean() throws IOException {
freemarker.template.Configuration cfg = new freemarker.template.Configuration(
freemarker.template.Configuration.VERSION_2_3_31);
cfg.setDefaultEncoding(NotificationConstants.UTF_8);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);
cfg.setFallbackOnNullLoopVariable(false);
cfg.setLocalizedLookup(false);
List<TemplateLoader> loaders = new ArrayList<>();
// CloudTemplateLoader is just a wrapper on UrlTemplateLoader
loaders.add(new CloudTemplateLoader(new URL(baselineUrl)));
loaders.add(new CloudTemplateLoader(new URL(dynamicUrl)));
cfg.setTemplateLoader(
new MultiTemplateLoader(loaders.toArray(new TemplateLoader[0])));
cfg.setCacheStorage(new MruCacheStorage(5000, Integer.MAX_VALUE));
cfg.setTemplateUpdateDelayMilliseconds(retentionTimeMillis);
return cfg;
}
}
Below is how i am trying to load template :
@Component
@Slf4j
public class FtlTemplateHandler {
private final Configuration configuration;
@Autowired
public FtlTemplateHandler(@Qualifier("emailConfiguration") Configuration configuration) {
this.configuration = configuration;
}
public String getTemplateAsHtml(String emailTemplateUrl) {
try {
Template template = configuration.getTemplate(emailTemplateUrl);
// Process the template
} catch (IOException e) {
try {
log.info("removing template from cache {}", emailTemplateUrl);
configuration.removeTemplateFromCache(emailTemplateUrl);
Thread.sleep(10000);
} catch (IOException | InterruptedException ex) {
// process exception
}
}
}
}
Getting error
Server returned HTTP response code: 403 for URL: https://baseline/bhnpay-static/notification-template/ftl/en-US/e17fa48f-d566-4085-b222-fbb8523e6ddc.ftl.
but the template is present at the dynamic url, that means after looking at baseline cdn it is not checking the dynamic cdn.
I have verified that the URLs are correct and accessible.
I tried clearing the cache to ensure it’s not a caching issue, but the fallback still doesn’t work.
user25045494 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.