Skip to main content

OData Page Provider for DD4T Java

This little project has been on my mind for a while now: write a set of DD4T providers for OData (aka the Content Delivery Web Service). The approach is to remove all mentions of the CD API from the DD4T application itself and restrict Tridion interaction to the OData service. The DD4T application would use an OData client to talk with the service.

Since communication with OData goes over HTTP, the number of queries to the service must be kept to a minimum, so the DD4T PageFactory will make heavy use of caching. I implemented an EHCache provider for DD4T, but that's not the scope of this blog post.

So finally, this weekend I set on to writing these providers. The first challenge quickly showed up; the CD API is used in many DD4T classes and in the cases where the actual functionality is not used directly, DD4T still makes extensive use of CD API classes and interfaces.

It took me a couple of hours to remove the mentions to com.tridion.* packages. During this process, the provider interfaces (in package org.dd4t.providers of project dd4t-api) changed a bit, because the original interfaces returned Tridion objects. The Page provider I am going to focus on in this post changed to the following:

public interface PageProvider {
    public String getPageContentByURL(String url, int publication)
            throws ItemNotFoundException, SerializationException;

    public String getPageContentById(int id, int publication)
            throws ItemNotFoundException, SerializationException;

In the latest version of DD4T (currently not released yet), the DD4T templates can produce XML or JSON in plain or compressed format. Compression is for example GZip, then Base64 to bring it back to a ASCII encoding. This can all be configured in the Template Building Blocks. For this implementation, I am going to use JSON without compression.

The implementing Page provider goes something like this:

public String getPageContentByURL(String url, int publication)
            throws ItemNotFoundException, SerializationException {
    String filter = String.format("PublicationId eq %d and Url eq '%s'", publication, url);
    OEntity pageEntity = ODataClient.INSTANCE.getPageEntities().filter(filter).

    if (pageEntity == null) {
        throw new ItemNotFoundException();

    OEntity pageContentEntity = pageEntity.getLink("PageContent", OLink.class).
    if (pageContentEntity == null) {
        throw new ItemNotFoundException();

    return pageContentEntity.getProperty("Content", String.class).getValue();

The implementation makes use of the OData4J client. I had a previous encounter with this one, and I had to patch it in order to make it work with the non-standard Date format the Tridion CD WS is using. Make sure to use my patched version.

The OData4J client code is rather straight forward: get the singleton client (more about it below), filter the Pages collection to retrieve only from the given Publication and only the item with the given URL, then expand the PageContent related entity, because that's the one that contains the actual serialized DD4T Page model.

The OData client is actually of type org.odata4j.consumer.ODataConsumer and this is the one that handles the communication with the CD Web Services. The singleton ODataClient I have defined in package org.dd4t.providers.impl looks like this:

public enum ODataClient {
    public static final String CONFIG_FILENAME = "";

    private final Logger LOG = LoggerFactory.getLogger(ODataClient.class);
    private final ODataConsumer client;
    private final OQueryRequest<OEntity> pageEntities;

    private ODataClient() {
        String odataURL = "MISSING_" + CONFIG_FILENAME + "_";
        try {
            InputStream input = ODataClient.class.getClassLoader().
            if (input == null) {
                throw new IOException("Cannot find properties file '" + CONFIG_FILENAME +
                        "' in classpath");

            Properties properties = new Properties();

            odataURL = properties.getProperty("BaseURL");
        } catch (IOException ioe) {
            LOG.error("Cannot load OData properties file", ioe);

        client = ODataConsumers.create(odataURL);
        pageEntities = client.getEntities("Pages");

    public OQueryRequest<OEntity> getPageEntities() {
        return pageEntities;

The 'singleton' client piggy backs on the Java enum quirk, which guarantees there is at all times one and only one instance of the 'singleton'. The initialization code expects a file called to be available in the application classpath. It reads the CD WebService baseURL from this properties file (e.g. http://localhost:8080/odata.svc) and it instantiates the ODataConsumer client then. Finally, the utility getter returns a OQueryRequest object ready to be used against the OData service.

Finally, the DD4T PageFactory implementation class in package org.dd4t.factories.impl is pretty standard and makes use of a Cache provider and a Page provider. The findPageByUrl method is shown below as indication (I have not really modified this method except for the signature of the pageProvider.getPageContentByURL method):

public GenericPage findPageByUrl(String url, int publicationId, RequestContext context) throws ItemNotFoundException, FilterException, ParseException, SerializationException {
    String cacheKey = publicationId + "-" + url;
    GenericPage page = (GenericPage) getCacheProvider().loadFromLocalCache(cacheKey);
    if (page == null) {
        String pageSource = pageProvider.getPageContentByURL(url, publicationId);
        if (pageSource == null || pageSource.length() == 0) {
            throw new ItemNotFoundException();
        page = getPageFromSource(pageSource);
        doFilters(page, context, BaseFilter.RunPhase.BeforeCaching);
        getCacheProvider().storeInItemCache(cacheKey, page, publicationId);

    doFilters(page, context, BaseFilter.RunPhase.AfterCaching);
    return page;

Where the getPageFromSource method is simply calling the deserialization of the String to actual Page DD4T object model.

That's all folks... sweet and short :)


Popular posts from this blog

A Implementation - Custom Binary Publisher

The default way to publish binaries in DD4T is implemented in class DD4T.Templates.Base.Utils.BinaryPublisher and uses method RenderedItem.AddBinary(Component) . This produces binaries that have their TCM URI as suffix in their filename. In my recent project, we had a requirement that binary file names should be clean (without the TCM URI suffix). Therefore, it was time to modify the way DD4T was publishing binaries. The method in charge with publishing binaries is called PublishItem and is defined in class BinaryPublisher . I therefore extended the BinaryPublisher and overrode method PublishItem. public class CustomBinaryPublisher : BinaryPublisher { private Template currentTemplate; private TcmUri structureGroupUri; In its simplest form, method PublishItem just takes the item and passes it to the AddBinary. In order to accomplish the requirement, we must specify a filename while publishing. This is the file name part of the binary path of Component.BinaryConten

Event System to Create Mapped Structure Groups for Binary Publish

As a continuation of last week's Publish Binaries to Mapped Structure Group , this week's TBB is in fact the Event System part of that solution. Make sure you do check out the previous post first, which explains why and what this Event System does. To reiterate, the Event System intercepts a Multimedia Component save, take its Folder path and create a 1-to-1 mapping of Structure Groups. The original code was written, again, by my colleague Eric Huiza : [ TcmExtension ( "MyEvents" )] public class EventsManager  : TcmExtension {     private Configuration configuration;     private readonly Regex SAFE_DIRNAME_REGEX = new Regex ( @"[\W_]+" );     public EventsManager() {         ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap ();         fileMap.ExeConfigFilename = Path .GetDirectoryName( Assembly .GetExecutingAssembly().Location) + "\\EventSystem.config" ;         configuration = ConfigurationManager

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: I stumbled upon the following post from 2008 (!!!), , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as