Affiliates Product Catalogue

In this example we create an input plugin to use product feeds from affiliate programs (in this case TradeDoubler). This is then coupled with an output plugin that presents this data as an HTML page for inclusion in a web site.

About the Product Feed

Trade Doubler is an affiliate programme in which some members provide XML feeds of their products. To retrieve these feeds you will need to register on the tradedoubler site. However, in order to see this example in action we have provided a sample document in src/examples/affiliateProductCatalogue/src/xdocs/exampleFeed.xml.

Creating a TradeDoubler Reader

TradeDoubler XML feeds are not Valid XML documents, that is they do not have a DTD definition within them. This means that we have to create a Reader to read these documents for us and assign a type to them so that Forrest knows how to process them in subsequent stages.

Fortunately, Forrest core provides a ChainedReader class that allows a reader to be placed before another. The ChainedReader can be used to perform processing on a document after it is read by another reader. For example, we can create a chained reader that will add a given document type to a document after it has been read by some other reader.

To define our ChainedReader we need to add the following to forrestContext.xml:

]]>

This reader is invoked with the pseudo protocol of "tradeDoubler" and returns a document with the type org.apache.forrest.example.affiliateProductCatalogue.TradeDoublerProductFeed. We will need to use this type value to select the correct input plugin to use.

Now we need to define which client requests will invoke this reader. Lets add a locationmap entry to read our example feed:

]]>

Notice how we use the tradeDoubler psuedo protocol to tell Forrest which reader to use and that, in this case, it is chained to a classpath reader. The source could define any kind of reader that is available, for example:

]]>

Checking our Progress

At this point we can check everything works. We should be able to retrieve our source document by requesting tradeDoubler.forrestSource. We'll use the CLI to test this.

cd [FORREST_HOME]/src/examples/afiliateProductCatalogue java -jar [FORREST_HOME]/forrest.jar org.apache.forrest.cli.CLI tradeDoubler.forrestSource

Creating the Input Plugin

A more complete description of creating an Input plugin is provided in the Create a Plugin How To.

Since the TradeDoubler feeds are XML our input plugin is just a standard XSL transformation. Such plugins can be created without having to write any Java code, but we will, of course, need an XSLT file to convert from the XML product feed to Forrests internal format.

Our XSLT is found in src/examples/affiliateProductCatalogue/src/xslt/tradeDoublerFeed-to-internal.xsl to define our input plugin we just need to add the following to forresContext.xml

]]>

Note how we use an ID that is the same as the document type created by our reader.

Checking our Progress

Now we can check that the internal document generation works OK. We should be able to retrieve our internal document by requesting tradeDoubler.forrestInternal. Use the CLI to test this:

cd [FORREST_HOME]/src/examples/afiliateProductCatalogue java -jar [FORREST_HOME]/forrest.jar org.apache.forrest.cli.CLI tradeDoubler.forrestInternal

Generating HTML

Since an HTML output plugin is only an XSLT transformation we need only configure a version of the XSLTOutputPlugin. To do this we need to add the following to our forrestContext.xml:

]]>

The above location of the xsltPath will need to change to someting more permanent.

In this case the id attribute, unlike with readers and input plugins, can be any unique value. The pattern property defines which request URLs should be processed by this output plugin. In this case, any URL ending in either "html" or "htm" will be processed by this plugin. This property is a Regular Expression so you can define some very complex pattern matching rules here.

Checking our Progress

Now we can check that the html document generation works OK. We should be able to retrieve our html document by requesting tradeDoubler.html. Use the CLI to test this:

cd [FORREST_HOME]/src/examples/afiliateProductCatalogue java -jar [FORREST_HOME]/forrest.jar org.apache.forrest.cli.CLI tradeDoubler.html

Whilst we are at it, lets check the other potential URL for this document:

cd [FORREST_HOME]/src/examples/afiliateProductCatalogue java -jar [FORREST_HOME]/forrest.jar org.apache.forrest.cli.CLI tradeDoubler.htm