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).
            expand("PageContent").execute().firstOrNull();

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

    OEntity pageContentEntity = pageEntity.getLink("PageContent", OLink.class).
            getRelatedEntity();
    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 {
    INSTANCE;
    public static final String CONFIG_FILENAME = "odata.properties";

    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().
                    getResourceAsStream(CONFIG_FILENAME);
            if (input == null) {
                throw new IOException("Cannot find properties file '" + CONFIG_FILENAME +
                        "' in classpath");
            }

            Properties properties = new Properties();
            properties.load(input);

            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 odata.properties 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 :)

Comments

Popular posts from this blog

Toolkit - Dynamic Content Queries

This post if part of a series about the  File System Toolkit  - a custom content delivery API for SDL Tridion. This post presents the Dynamic Content Query capability. The requirements for the Toolkit API are that it should be able to provide CustomMeta queries, pagination, and sorting -- all on the file system, without the use third party tools (database, search engines, indexers, etc). Therefore I had to implement a simple database engine and indexer -- which is described in more detail in post Writing My Own Database Engine . The querying logic does not make use of cache. This means the query logic is executed every time. When models are requested, the models are however retrieved using the ModelFactory and those are cached. Query Class This is the main class for dynamic content queries. It is the entry point into the execution logic of a query. The class takes as parameter a Criterion (presented below) which triggers the execution of query in all sub-criteria of a Criterio

A DD4T.net 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

Scaling Policies

This post is part of a bigger topic Autoscaling Publishers in AWS . In a previous post we talked about the Auto Scaling Groups , but we didn't go into details on the Scaling Policies. This is the purpose of this blog post. As defined earlier, the Scaling Policies define the rules according to which the group size is increased or decreased. These rules are based on instance metrics (e.g. CPU), CloudWatch custom metrics, or even CloudWatch alarms and their states and values. We defined a Scaling Policy with Steps, called 'increase_group_size', which is triggered first by the CloudWatch Alarm 'Publish_Alarm' defined earlier. Also depending on the size of the monitored CloudWatch custom metric 'Waiting for Publish', the Scaling Policy with Steps can add a difference number of instances to the group. The scaling policy sets the number of instances in group to 1 if there are between 1000 and 2000 items Waiting for Publish in the queue. It also sets the