Rich text resolution - kontent-ai/java-packages GitHub Wiki
This page describes the possibility to resolve rich text elements.
Prerequisites
- Generate models using Model Generator
- Register models using
scanClasspathForMappings, orregisterType.
If you wish to resolve the components/inline linked items into the string.You could use registerInlineContentItemsResolver.
// Tweet is strongly typed model
client.registerInlineContentItemsResolver(new InlineContentItemsResolver<Tweet>() {
@Override
public String resolve(Tweet item) {
return "<div>" + item.getTweetTitle() + "</div>";
}
});Create Thymeleaf templates to the resources to specific places (currently "kentico/templates/", "META-INF/kentico/templates/", "kentico/kontent/templates/", "META-INF/kentico/kontent/templates/"), with name <ITEM_TYPE_CODENAME>.html suffix - as for hosting_video.
Location customizable by extending the default Template engine.
TemplateEngineConfig config = new TemplateEngineConfig();
config.getViewResolverConfiguration().addPrefixes("acme/templates/");
config.getViewResolverConfiguration().setSuffix(".template");
DeliveryClient client = new DeliveryClient(
DeliveryOptions
.builder()
.projectId("975bf280-fd91-488c-994c-2f04416e5ee3")
.build(),
config);To completely customize the template configuration, it is possible to create your own implementation of TemplateEngineConfig (and TemplateEngine)
In the following example, it is possible i.e. to use the default implementation of
TemplateEngineConfigandTemplateEngineand i.e. add "Default fallback resolver" for all other types that are not registered viaregisterInlineContentItemsResolver.
class CustomTemplateEngine implements TemplateEngine {
private ViewResolverConfiguration viewResolverConfiguration;
@Override
public void setViewResolverConfiguration(ViewResolverConfiguration viewResolverConfiguration) {
this.viewResolverConfiguration = viewResolverConfiguration;
}
@Override
public String process(TemplateEngineModel data) {
// You can use viewResolverConfiguration if you need it as well
if(data.getInlineContentItem() instanceof Tweet) {
Tweet item = (Tweet) data.getInlineContentItem();
return "<div>" + item.getTweetTitle() + "</div>";
}
else {
// Or raise exception
return "UNKNOWN COMPONENT!";
}
}
}class CustomTemplateConfiguration extends TemplateEngineConfig {
@Override
public void init() {
super.init();
this.addResolvers(new TemplateEngineInlineContentItemsResolver() {
@Override
public boolean supports(TemplateEngineModel data) {
// Define supported content items
return data.getInlineContentItem() instanceof Tweet;
}
@Override
public TemplateEngine getTemplateEngine() {
return new CustomTemplateEngine();
}
});
}
}DeliveryClient client = new DeliveryClient(
DeliveryOptions
.builder()
.projectId("975bf280-fd91-488c-994c-2f04416e5ee3")
.customHeaders(Arrays.asList(
new Header(TRACKING_HEADER_NAME, TRACKING_HEADER_VALUE)
))
.build(),
new CustomTemplateConfiguration()
);