12 December 2011

Make your date range queries in Jackrabbit go faster!

As you might know Hippo CMS uses Apache Jackrabbit as the core for it's content repository. One of Jackrabbits features is 'search' and the execution of most queries are delegated to Apache Lucene. If you want to keep the queries as fast as possible you sometimes have to analyze how your repository is behaving with the content in your project.

The problem

One of our customers noticed that they had some unexpected memory peaks and slow response times while their system was running during the day. They were using Jackrabbit 2.2.5 at the time. To get some more insight on what was going on we first started out by looking at the server logs. They were using the Jackrabbit search functionality quite heavily, so we replayed the server request logs on the acceptance environment. During the replay we set the log level of the Query to debug, so we were able to see how long each query took. We soon discovered that the longest queries were 'range queries' and also noticed that during the execution of such a range query the memory usage reached some peaks.

User view of a date range
In this specific case the problamatic queries were date range queries. A date range query is a query that for instance searches for a document between day x and day y.

As an example: Give me all documents that were created between 2007-01-01 and 2011-01-01. This is a very typical search filter, which you will see in all kind of applications.

In plain Jackrabbit a date range query (xpath notation) will look something like this:
//element(*,custom:document)[@custom:date >=xs:dateTime('2007-01-01T00:00:00.000Z') and @custom:date <=xs:dateTime('2010-01-01T00:00:00.000Z')] order by @custom:date descending

Now if you would be using the Hippo HST you would have probably used the Filter.addGreaterOrEqualThan and passed along a Date object as an argument, which automatically is being converted into the above syntax.

The analysis

To get some insight in what was causing this behaviour I created a unit test that performed a variety of range queries on a set of 100.000 simple documents/nodes. For this test I created a very simple nodetype definition that would hold a date/date-time property in different kind of formats. The used nodetype looks like this:

[custom:document]
- custom:date (date) 
- custom:dateasstringwithhoursandminutesandseconds (string)
- custom:dateasstringwithhoursandminutes (string)
- custom:dateasstring (string)

So looking at the above nodetype definition we have a node that contains 4 properties, where we have a normal JCR date (and time) property and next to that there are 3 properties that have a more fine-grained format of the date-time.

Trying to create a real world scenario the unit test generates documents starting from 01-01-2001. With every new document the test adds 1 hour and 3 seconds to the date field. After creating 100.000
nodes it ends up somewhere around 2012-07-03 03:19:57. The test will then sleeps for about 60 seconds (gives lucene time to finish up it's indexing) before it starts doing the range queries.

In finding a solution I created 4 different versions of the range query where I start narrowing down on the date format to get closer to the actual date (without the time). In the test case 4 different kind of queries are peformed and repeated 5 times before moving on to the next type of query. The range queries performed are:
  • Normal range query (with JCR date-time format like mentioned above)
  • Range query with date as string with format yyyyMMddHHmmss
  • Range query with date as string with format yyyyMMddHHmm
  • Range query with date as string with format yyyyMMdd

Queries 1 to 3 took an average of 3500 ms (3.5 seconds) with a large memory footprint usage of about 380MB per query. That's huge and slow for just a simple query! You can imagine this might end up leading to OutOfMemory errors.
Memory usage overtime while performing the queries (graph comes from VisualVM)

However the fourth query is actually quite fast and takes less memory! It's a really significant difference. The fourth query takes about 180 ms on average and uses about 40-50MB. It's still a lot of memory (in my opinion), but since they are a lot faster the total amount of used memory might not be that large, because the amount of memory is freed much earlier in the process.

Looking at the graph on the right you will see there is no difference between query 1 to 3, but option 4 (which is in fact actually what a date range should do) showed a really large improvement on overall performance and memory usage. So in the end it turns out that the 'time' in the default JCR date format was actually giving us the issues. Because the time was added to the date value, the number of unique values for the date property in the lucene index had become larger then needed causing the slowdown.

A solution

Now as a solution we solved this by adding a derived data function that extracts the simplified date property. For range queries we now do the range on the 'yyyyMMdd' formatted date and order the results by the original date property, so that the time is taken into account and the sort order is correct. Using the simple date format will also help when trying to find documents/nodes that belong to a certain date. This has just turned into a simple 'equals' instead of a range from 0:00 till 23:59:59.

If you are currently using Apache Jackrabbit and are using these kind of queries you might want to rethink you current content model. A small change might give you a huge performance boost!


04 August 2011

Replacing the MacBookPro mid 2009 HDD part 2

As you might have read in my previous upgrade post I initially wanted to upgrade my MacBookPro5,4 mid 2009 model with an SSD. Since my initial search for a compatible SSD I've read tons of articles on the web describing this same issue, but without a solid answer. It's been quite a challenge to figure out what to do and I almost gave up on the idea of being able to use an SSD until I found the this excellent blogpost by David Leach.

I've read similar stories to that of David, where 5 attempts with different SSD's kept on failing and all of them gave them different kind of results from not recognizing the SSD to getting those colorful beachballs.

Davids post and the people in his comments gave me the confidence to go for a Kingston V+100 128 GB SSD as it seemed to be the one and only drive that worked for him and other readers of his blog.

So last week I ordered one and it arrived the next day! Since I had a day off I had plenty of time to install it. Only when you need to go from 250GB to 128GB you will notice how much 'noise' is on your laptop's hard drive. The 128GB version was already quite expensive and I thought that the 256 GB was not really worth it. A 256GB version is around € 420,  which is in my opinion a bit too much money for an upgrade. It's a fact that traditional HDD's are a lot cheaper than SSDs at the moment, but the performance upgrade is really worth it.

Just after I cloned the original HDD to the SSD with SuperDuper! I tried a cold start of the MBP with the original HDD installed. The bootup time was clocked at 1 minute and 19 seconds. That's quite long for a fresh start. Of course I don't restart all the time, but you get the initial idea. So after I replaced the HDD with the SSD I set the timer again and it really blew me away! The cold start with the SSD took only 21 seconds! That's really amazing.

Now for some actual real world test I fired up IntelliJ with four projects open. IntelliJ was open within 30 seconds (with indexing) for four projects. AWESOME! How's that for a productivity boost.

Some benchmarks


Now in my previous post I posted some benchmarks of the original stock Apple HDD vs the WD Scorpio Blue where there was only a very small improvement in overal read and write speeds.

For those of you who did not read my other post here is the performance of the Scorpio Blue 5400 rpm drive.

That looks nice right? But here is the Kingston V+100 SSD benchmark.


As you can see the read and write speeds have greatly improved. The average read speed is now almost four times the previous speed, which you really notice when booting up or starting an application. I've been using the SSD now for over more than a week during my daily work. I can tell you: I will never go back! The laptop is really fast and responsive now as I would expect a laptop should be.

I hope others struggling with their MacBookPro mid 2009 model will be able to find this post and get the most out of their MacBookPro. It's really worth the upgrade!

31 July 2011

Getting started with MongoDB and Spring Data

Last month I finally found some time to play around with a NoSQL database. Getting hands on experience with a NoSQL database has been on my list for quite some time, but due to busy times at work I was unable to find the energy to get things going.

A little background information


Most of you have probably have heard the term NoSQL before. The term is used in situations where you do not have a traditional relation database for storing information. There are many different sorts of NoSQL databases. To make a small summary these are probably the most well-known:


The above types cover most of the differences, but for each type there are a lot of different implementations. For a better overview you might want to take a look at the NOSQL database website.

For my own experiment I chose to use MongoDB, since I had read a lot about it and it seemed quite easy to get started with.

MongoDB is as they describe it on their website:
A scalable, high-performance, open source, document-oriented database.
The document-oriented aspect was one of the reasons why I chose MongoDB to start with. It allows you to store rich content with data structures inside your datastore.

Getting started with MongoDB


To begin with, I looked at the Quick start page for Mac OS X and I recommend you to do that too (unless you use a different OS). It will get you going and within a couple of minutes you'll have MongoDB up and running on your local machine.

MongoDB stores it's data by default in a certain location. Of course you can configure that, so I started MongoDB with the --dbpath parameter. This parameter will allow you to specificy your own storage location. It will look something like this:

$ ./mongodb-xxxxxxx/bin/mongod --dbpath=/Users/jreijn/Development/temp/mongodb/

If you do that you eventually will get a message saying:


Mon Jul 18 22:19:58 [initandlisten] waiting for connections on port 27017
Mon Jul 18 22:19:58 [websvr] web admin interface listening on port 28017


At this point MongoDB is running and we can proceed to the next step: using Spring Data to interact with MongoDB.

Getting started with Spring Data

The primary goal of the Spring Data project is to make it easier for developers to work with (No)SQL databases. The Spring Data project already has support for a number of the above mentioned NoSQL type of databases.
Since we're now using MongoDB, there is a specific sub project that handles MongoDB interaction. To be able to use this in our project we first need to add a Maven dependency to our pom.xml.

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>${spring.data.mongo.version}</version>
</dependency>

Looks easy right? Just one single Maven dependency. Of course in the end the spring-data-mongodb artifact depends on other artifacts which it will bring into your project. In this post I used version 1.0.2.RELEASE.  Now on to some Java code!

For my first experiment I used a simple Person domain object that I'm going to query and persist inside the database. The Person class is quite simple and looks as follows.

package com.jeroenreijn.mongodb.example.domain;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * A simple POJO representing a Person
 *
 */
@Document
public class Person {

    @Id
    private String personId;

    private String name;
    private String homeTown;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(final String personId) {
        this.personId = personId;
    }

    public String getName() {
        return name;
    }
    public void setName(final String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(final int age) {
        this.age = age;
    }

    public String getHomeTown() {
        return homeTown;
    }

    public void setHomeTown(final String homeTown) {
        this.homeTown = homeTown;
    }

    @Override
    public String toString() {
        return "Person [id=" + personId + ", name=" + name + ", age=" + age + ", home town=" + homeTown + "]";
    }

}

Now if you look at the class more closely you will see some Spring Data specific annotations like @Id and @Document . The @Document annotation identifies a domain object that is going to be persisted to MongoDB. Now that we have a persistable domain object we can move on to the real interaction.

For easy connectivity with MongoDB we can make use of Spring Data's MongoTemplate class. Here is a simple PersonRepository object that handles all 'Person' related interaction with MongoDB by means of the MongoTemplate.

package com.jeroenreijn.mongodb.example;

import java.util.Iterator;
import java.util.List;

import com.jeroenreijn.mongodb.example.domain.Person;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

/**
 * Repository for {@link Person}s
 *
 */
@Repository
public class PersonRepository {

    static final Logger logger = LoggerFactory.getLogger(PersonRepository.class);

    @Autowired
    MongoTemplate mongoTemplate;

    public void logAllPersons() {
        List<Person> results = mongoTemplate.findAll(Person.class);
        logger.info("Total amount of persons: {}", results.size());
        logger.info("Results: {}", results);
    }

    public void insertPersonWithNameJohnAndRandomAge() {
        //get random age between 1 and 100
        double age = Math.ceil(Math.random() * 100);

        Person p = new Person("John", (int) age);

        mongoTemplate.insert(p);
    }

    /**
     * Create a {@link Person} collection if the collection does not already exists
     */
    public void createPersonCollection() {
        if (!mongoTemplate.collectionExists(Person.class)) {
            mongoTemplate.createCollection(Person.class);
        }
    }

    /**
     * Drops the {@link Person} collection if the collection does already exists
     */
    public void dropPersonCollection() {
        if (mongoTemplate.collectionExists(Person.class)) {
            mongoTemplate.dropCollection(Person.class);
        }
    }
}


If you look at the above code you will see the MongoTemplate in action. There is quite a long list of method calls which you can use for inserting, querying and so on. The MongoTemplate in this case is @Autowired from the Spring configuration, so let's have a look at the configuration.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <!-- Activate annotation configured components -->
  <context:annotation-config/>

  <!-- Scan components for annotations within the configured package -->
  <context:component-scan base-package="com.jeroenreijn.mongodb.example">
    <context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
  </context:component-scan>

  <!-- Define the MongoTemplate which handles connectivity with MongoDB -->
  <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo"/>
    <constructor-arg name="databaseName" value="demo"/>
  </bean>

  <!-- Factory bean that creates the Mongo instance -->
  <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    <property name="host" value="localhost"/>
  </bean>

  <!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

The MongoTemplate is configured with a reference to a MongoDBFactoryBean (which handles the actual database connectivity) and is setup with a database name used for this example.

Now that we have all components in place, let's get something in and out of MongoDB.

package com.jeroenreijn.mongodb.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Small MongoDB application that uses spring data to interact with MongoDB.
 * 
 */
public class MongoDBApp {

  static final Logger logger = LoggerFactory.getLogger(MongoDBApp.class);

  public static void main( String[] args ) {
    logger.info("Bootstrapping MongoDemo application");

    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");

    PersonRepository personRepository = context.getBean(PersonRepository.class);

    // cleanup person collection before insertion
    personRepository.dropPersonCollection();

    //create person collection
    personRepository.createPersonCollection();

    for(int i=0; i<20; i++) {
      personRepository.insertPersonWithNameJohnAndRandomAge();
    }

    personRepository.logAllPersons();
    logger.info("Finished MongoDemo application");
  }
}

All this application does for now is setup a connection with MongoDB, insert 20 persons (documents),  fetch them all and write the information to the log. As a first experiment this was quite fun to do.

Conclusion

As you can see with Spring Data it's quite easy to get some basic functionality within only a couple of minutes. All the sources mentioned above and a working project can be found on GitHub. It was a fun first experiment and I already started working on a bit more advanced project, which combines Spring Data, MongoDB, HTML5 and CSS3. It will be on GitHub shortly together with another blog post here so be sure to come back.

26 June 2011

MacBook Pro: Replacing the internal HDD

A while ago I thought it would be nice to give my MacBook Pro (MBP) a performance upgrade by putting in a Solid State Drive (SSD). I had pretty high expectations, since I was under the impression that Apple delivers computers with a really high quality factor. Unfortunately it turned out that the mid 2009 model range (which I have) appeared to be a range that has a lot of problems with third party drives.  To be more precise I'm currently running a MacBookPro5,4 Intel Core 2 Duo 2.53 GHz.

Some background


Initially I had bought a 120 GB OCZ Vertex 2 SSD for about € 170,- because any SSD drive should give my MBP an enormous boost. After figuring out how to start fresh on a new disk, I took the bottom of my MBP and replaced the stock HDD with this SSD. Eager to start using the SSD, I powered the laptop and was confronted with a gray screen and a non booting laptop. Hmmm.. now what..

Booting while using the 'Option' key, which should allow me to choose from which medium to start, did not help either. I had inserted the original DVD that came with the MBP, so I could start from there, but even though I was able to select the DVD drive, my SSD was not recognized at all.
I put the stock drive back in and tried attaching the SSD through an external USB case, but that did not help at all, so I got pretty frustrated.
Attaching the USB case to an old desktop machine running the latest version of Ubuntu made the drive actually show up, so it wasn't the drive that was broken. 
It had to be something with my MBP or the hardware inside the MBP, but how hard could it be to create compatible hardware the year 2009-2011.. It wasn't like that my MPB was 10 years old or something.

Finding the exact cause of the problem did not even prove to be that simple. It took my quite a while before I figured out that apparently the mid 2009 range was famous for having hardware issues with third party drives. Even the Apple discussion forums were not very helpful if you did not know what to look for. In the end I found out that the cause of the problem was the negotiation between the Nvidia controller and the SSD drive. The Nvidia chipset inside the mid 2009 range model was the cause of the problem. Now that I knew what to look for I found hundreds op people trying to upgrade their internal drive with another without success. All hope went away, because after that I read about a lot of problems with a variety of drives, so I brought back the drive and postponed my wish for an SSD.

Two months later


This weekend I noticed that the internal HDD of my PS3 was getting full. I downloaded two free games from the 'Welcome back' program, but was unable to install them on my PS3 due to insufficient storage capacity.
The PS3 version I bought a couple of years ago only had a 40GB HDD, but fortunately Sony explicitly mentions that you can upgrade the PS3 HDD without any problems if you take a 5400 rpm version with a maximum height of 9.5 mm. So this saturday I went to the local computer store and came home with a WD Scorpio Blue 320 GB with an 8 MB cache, which had cost me only € 42,-  . I had read some good reviews on this particular drive and wanted to give it a go. The WD Scorpio Blue is a low power consuming 2.5 inch laptop drive, but appears to be one of the fastest 5400 rpm drives available.

With this new 2.5 inch drive in my hand I got tempted to see if my MPB would be able to recognize this drive, where it failed to detect the SSD two months ago. Just for the fun of it I placed it in the external USB case and Voilà! it appeared inside Apples disk utility.
This triggered me to try it inside the MBP as well, just to see if it would recognize the drive. To be able to create a bootable drive I used the build in 'Restore' functionality of Disk Utility and cloned my old drive to this new drive.

Some figures


Before switching to the new drive I ran an Xbench test for the stock drive to see what the 'old' performance was like. So here are the result of the stock FUJITSU drive.


As you can see this is quite nice for a 5400 rpm drive, so after I replaced the stock drive with the WD Scorpio Blue I did another disk test and this is what the WD Scorpio Blue did.




Even though it's not as big as an improvement as an 7200 rpm or SSD drive I'm quite happy with the results of the new drive! Both read and write performance have increased, so for now I think I'll just stick with the 320 GB WD drive in my MBP and will put the 'old' stock drive into my PS3 where 250GB should be plenty of disk space. I'll test drive it for a week, just to be sure that I don't run into any issues during the week.

27 March 2011

Simple XML processing with Apache Cocoon 3

It's been a while since I've last used Apache Cocoon. I can still remember the day that I was using Cocoon for doing all my web development projects. My first introduction with Cocoon was when I started at Hippo about 8 years ago. In comparison to other frameworks, I sometimes miss the simplicity of the Cocoon pipeline concept when I have to work with XML. Especially processing larger XML files is pain in most IDE's.

The Cocoon team has been working on Cocoon 3 for a while now, so I wanted to give it a try. Having worked with both Cocoon 2.1 and 2.2, version 3 is created with two new goals in mind:
  • slim down the framework
  • make it easier to use/combine with other frameworks

From what I've seen so far, this results in that you can now do a lot more with just plain Java. Where you would need to use a sitemap XML file before, you can now do a lot with just a few lines of Java. Even though Cocoon 3 is still in alpha stage, it already looks quite promising.

Getting started

To be able to process XML with Cocoon 3, all we need are the following two maven dependencies.

<dependencies>
  <dependency>
    <groupId>org.apache.cocoon.pipeline</groupId>
    <artifactId>cocoon-pipeline</artifactId>
    <version>3.0.0-alpha-2</version>
  </dependency>

  <dependency>
    <groupId>org.apache.cocoon.sax</groupId>
    <artifactId>cocoon-sax</artifactId>
    <version>3.0.0-alpha-2</version>
  </dependency>
</dependencies>

These dependencies drag in just two more dependencies (commons-logging and cocoon-xml, so the end result will be quite small, which is really nice compared to for instance Cocoon 2.1, which came with quite some baggage.

The code

Now let's have a look at some Java code. To play around with Cocoon 3, I'm going to use the RSS feed of this blog. Let's see how Cocoon's new Java based coding works. To be able to process the XML result of the RSS feed, I've created the RSSFeedInfoGenerator. The RSSFeedInfoGenerator is a simple class that will parse a provided RSS feed url.

/**
 * RSS Feed info generator
 */
public class RSSFeedInfoGenerator {

  private static final String DEFAULT_RSS_URL = "http://blog.jeroenreijn.com/feeds/posts/default?alt=rss";

  public static void main(String[] args) {
    RSSParser parser = new RSSParser();
    if(args!=null && args.length > 0) {
      parser.setFeedURL(args[0]);
    } else {
      parser.setFeedURL(DEFAULT_RSS_URL);
    }
    parser.parse();
  }
}

So now there is a start from which we can actually build the RSS parser and use Cocoon for processing the XML. Now let's take a look at the actual RSSParser.

/**
 * Rss parser that uses Cocoon 3 pipelines for generating
 * and transforming the RSS feed to a simple XML response.
 */
public class RSSParser {

  private static final Log LOG = LogFactory.getLog(RSSParser.class);
  private String feedURL;

  /**
   * Parse the provided feed URL and generate the Feed INFO.
   */
  public void parse() {
    try {
       Pipeline<SAXPipelineComponent> pipeline = new NonCachingPipeline<SAXPipelineComponent>();
       XSLTTransformer xsltTransformer = new XSLTTransformer(this.getClass().getResource("simplify-rss.xsl"));

       pipeline.addComponent(new XMLGenerator(new URL(getFeedURL())));
       pipeline.addComponent(new CleaningTransformer());
       pipeline.addComponent(xsltTransformer);
       pipeline.addComponent(new XMLSerializer().setIndent(true));
       pipeline.setup(System.out);
       pipeline.execute();

    } catch (MalformedURLException e) {
       LOG.error("An exception occurred while parsing the RSS URL: " + e.getMessage());
    } catch (FileNotFoundException e) {
       LOG.error("An exception occurred while parsing the RSS URL: " + e.getMessage());
    } catch (Exception e) {
       LOG.error("An exception occurred trying to parse the RSS feed: " + e.getMessage());
    }
  }

  public String getFeedURL() {
    return feedURL;
  }

  public void setFeedURL(final String feedURL) {
    this.feedURL = feedURL;
  }
}


As you can see I first created a Pipeline, which in this case is SAX based. In a Cocoon pipeline you can add multiple components, so we add a Generator, two Transformers and a Serializer. The normal XML version of the RSS feed is quite large, so to make the XML result for this example quite small, we use an XSL template to remove all but the title and lastBuildDate from the RSS feed. Let's have a look at the XSL template.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>

  <xsl:template match="rss">
    <info>
      <xsl:copy-of select="channel/title"/>
      <xsl:copy-of select="channel/lastBuildDate"/>
    </info>
  </xsl:template>

  <xsl:template match="@*|node()|text()|comment()|processing-instruction()" priority="-1">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Looks quite simple right? Now when we run the above code, the RSSParser will output an XML snippet to the terminal/console which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<info>
  <title>Jeroen Reijn</title>
  <lastBuildDate>Mon, 21 Mar 2011 22:54:06 +0000</lastBuildDate>
</info>

Final thoughts

I was able to put this example together in about 20 minutes. That's quite fast if you compare this to the old styled processing of Cocoon.
I think the Cocoon team is really far with reaching their goals. Because you are now able to write the processing logic with just some Java, this makes it easy to integrate with any existing Java based framework.
I'm curious what else will get into the first official Cocoon 3 release, because it's already quite powerful. From now on I will be using Cocoon 3, when I need to process large (and small) XML files. With the new Java based model it's easy to create a small but powerful processor.

For those interested in the source code, you can find the code on GitHub.

16 February 2011

HIPPOs RESTful JAX-RS Component Support and Spring Android

The new Hippo CMS 7.5 release brings some quite interesting features. The most interesting new feature for me was support for RESTful components within the Hippo Site Toolkit (HST-2 v2.20.01). Being able to expose data in a RESTful manner opens up a whole new set of possibilities for external application developers.

As you might have read in my previous post, I'm building a sample application to get acquainted with the Android platform. My previous post was mainly focussed on layouts and ListViews, but this time I will be focussing on information retrieval from an external REST service. That's why I've used the default REST service that comes with the online Hippo GoGreen demo as my source of information.  The GoGreen REST service exposes a list of 'top products' with additional information about the products that can be used nicely for this demo project, but first let's start at the beginning.

Getting started with RESTful HST-2 components

From what I've seen in the documentation and in the GoGreen source code, there are two different methods of exposing data with the RESTful components.
  1. The data can be exposed based on the primary JCR NodeType of a resource inside the Hippo repository. The HST-2 sitemap will determine the URLs of the items based on the relative path of the items inside the repository. This approach can be done with the JaxrsRestContentPipeline.
  2. A sitemap item (or mount) can be configured as a JaxrsRestPlainPipeline. By doing so, the HST will try to match the request within a Jax-RS based resource provider component that handles all the (relative) URL matching from there on.   
    In this example I will use the JaxrsRestPlainPipeline approach, which is also used by the Hippo GoGreen demo to create the 'top products' resource. The response output of a REST pipeline can be in all kinds of different formats. For this example we will use JSON, but you can also use XML instead.

    Configuration


    The first step in the proces of setting up our own REST service is to create an HST mount. The configuration for our mount has to look something similar to :

    <sv:node sv:name="restapi">
      <sv:property sv:name="jcr:primaryType" sv:type="Name">
        <sv:value>hst:mount</sv:value>
      </sv:property>
      <sv:property sv:name="hst:alias" sv:type="String">
        <sv:value>restapi</sv:value>
      </sv:property>
      <sv:property sv:name="hst:authenticated" sv:type="Boolean">
        <sv:value>false</sv:value>
      </sv:property>
      <sv:property sv:name="hst:isSite" sv:type="Boolean">
        <sv:value>false</sv:value>
      </sv:property>
      <sv:property sv:name="hst:mountpoint" sv:type="String">
        <sv:value>/hst:hst/hst:sites/rest-live</sv:value>
      </sv:property>
      <sv:property sv:name="hst:mountsite" sv:type="String">
        <sv:value>site</sv:value>
      </sv:property>
      <sv:property sv:name="hst:namedpipeline" sv:type="String">
        <sv:value>JaxrsRestContentPipeline</sv:value>
      </sv:property>
      <sv:property sv:name="hst:roles" sv:type="String">
        <sv:value>everybody</sv:value>
      </sv:property>
      <sv:property sv:name="hst:showport" sv:type="Boolean">
        <sv:value>true</sv:value>
      </sv:property>
      <sv:property sv:name="hst:subjectbasedsession" sv:type="Boolean">
        <sv:value>true</sv:value>
      </sv:property>
      <sv:property sv:name="hst:types" sv:type="String">
        <sv:value>rest</sv:value>
      </sv:property>
    </sv:node>
    

    As you can see there is lot to configure for a mount, but I don not want to go into much detail. The next step is to setup an HST sitemap for this mount. In the configuration above, our mount uses a default namedpipeline of type  JaxrsRestContentPipeline , since we want to use a  JaxrsRestPlainPipeline, we can override the type of pipeline by specifying the hst:namedpipeline property on an HST sitemap item for this mount, for example for the sitemap item called 'topproducts'.

    <sv:node sv:name="topproducts">
      <sv:property sv:name="jcr:primaryType" sv:type="Name">
        <sv:value>hst:sitemapitem</sv:value>
      </sv:property>
      <sv:property sv:name="hst:namedpipeline" sv:type="String">
        <sv:value>JaxrsRestPlainPipeline</sv:value>
      </sv:property>
    </sv:node>
    

    Spring Configuration

    Now after we stored the HST-2 configuration in the repository, the next step is to register our new component as a plain resource provider in our website Spring configuration. We can do this by creating a file called custom-jaxrs-resources.xml in the src/main/resources/META-INF/hst-assembly/overrides/ folder of our Hippo site project with the following content.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
      
      <import resource="classpath:/org/hippoecm/hst/site/optional/jaxrs/SpringComponentManager-rest-jackson.xml" />
      <import resource="classpath:/org/hippoecm/hst/site/optional/jaxrs/SpringComponentManager-rest-plain-pipeline.xml" />
      <import resource="classpath:/org/hippoecm/hst/site/optional/jaxrs/SpringComponentManager-rest-content-pipeline.xml" />
      
      <!-- Custom JAX-RS REST Plain Resource Providers to be overriden. -->
      <bean id="customRestPlainResourceProviders" class="org.springframework.beans.factory.config.ListFactoryBean">
        <property name="sourceList">
          <list>
            <bean class="org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider">
              <constructor-arg>
                <bean class="com.onehippo.gogreen.jaxrs.services.TopProductsResource" />
              </constructor-arg>
            </bean>
          </list>
        </property>
      </bean>
          
    </beans>
    

    With this configuration in place the HST-2 has knowledge of our custom resource and the TopProductsResource can start creating the response.

    Now let's take a look at our TopProductsResource.

    @Path("/topproducts/") 
    public class TopProductsResource extends AbstractResource {
      @GET
      @Path("/topproducts/")
      public List<ProductLinkRepresentation> getProductResources(@Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse, @Context UriInfo uriInfo,
                @QueryParam("sortby") @DefaultValue("hippogogreen:rating") String sortBy, 
                @QueryParam("sortdir") @DefaultValue("descending") String sortDirection,
                @QueryParam("max") @DefaultValue("10") String maxParam) {
            
          List<ProductLinkRepresentation> productRepList = new ArrayList<ProductLinkRepresentation>();
          HstRequestContext requestContext = getRequestContext(servletRequest);
            
          try {
              Node mountContentNode = getNodeFromMount(requestContext);
              HstQueryResult result = getHstQueryResult(sortBy, sortDirection, maxParam, requestContext, mountContentNode);
              HippoBeanIterator iterator = result.getHippoBeans();
    
              while (iterator.hasNext()) {
                  Product productBean = (Product) iterator.nextHippoBean();
                    
                  if (productBean != null) {
                    ProductLinkRepresentation productRep = new ProductLinkRepresentation(requestContext).represent(productBean);
                    productRepList.add(productRep);
                  }
              }
          } catch (Exception e) {
            log.warn("Failed to retrieve top products. {}", e);        
            throw new WebApplicationException(e);
          }
            
          return productRepList;
      }
    }
    

    The TopProductsResource has a @Path("/topproducts/") annotation set on the class level. This is what's making the request to '/topproducts' being handled by this specific resource. As you can see the only other thing the resource does is perform the query from the getProductResources() method. Take a look at the full source code for more details on the TopProductsResource class.

    Response output


    Now that we've setup the configuration and put the component in place, let's take a look at our actual response. You can see what the response of the TopProductsResource is if you go to the following URL:

    http://www.demo.onehippo.com/restapi/topproducts?_type=json

    Note: the URL might not be available at the time you try it, because the GoGreen demo is restarted every 30 minutes with a fresh set of content. If the URL does not work try again in 5 minutes.

    Since we specified the response type as JSON, the actual response should look something like what is shown below. For readability I've removed some properties, but I guess you get the idea.

    [
      {
        productLink: "http://www.demo.onehippo.com/restapi/products/food/2010/07/organic-cotton-reusable-lunch-bag./"
        price: 34
        rating: 5
        smallThumbnail: "http://www.demo.onehippo.com/binaries/smallthumbnail/content/gallery/products/2010/06/organic-lunch-bag.jpg"
        localizedName: "Organic Cotton Reusable Lunch Bag"
        primaryNodeTypeName: "hippogogreen:product"
      },
      {
        productLink: "http://www.demo.onehippo.com/restapi/products/food/2010/07/birch-wood-compostable-cutlery./"
        price: 5
        rating: 4.25
        smallThumbnail: "http://www.demo.onehippo.com/binaries/smallthumbnail/content/gallery/products/2010/07/wooden-cutlery.png"
        localizedName: "Birch Wood Compostable Cutlery"
        primaryNodeTypeName: "hippogogreen:product"
      }
    ]
    

    As you can see the response is quite simple and contains an array of product items with their properties.
    If you want to know more about RESTful Component support there is a nice page on the HST-2 wiki. Now let's move on with the Android part of this post.

    Spring Android

    Android version 2.2 has native support for handling JSON. I tried that, but I recently discovered Spring Android. Spring Android is quite new and gives you an easy to use REST client. The reason I chose to use Spring Android is that it takes less code to handle requests then by doing it the native Android way with the default HttpClient. Now when we combining Spring Android with Jackson it makes working with JSON really easy. All you have to do is create a mapping class, so that Jackson knows how to map the response array.

    To be able to work with the JSON response we will need the following three libraries in our Android project.
    • spring-android-rest-template-1.0.0.M2.jar
    • jackson-core-asl-1.7.1.jar
    • jackson-mapper-asl-1.7.1.jar

    Using Spring Android


    For my Android application I've created a service class called ProductService.

    public class ProductService {
      private static final String RESTAPI_BASE_URI = "http://www.demo.onehippo.com/restapi";
      private static final String RESTAPI_RESPONSE_TYPE = "_type=json";
    
      public static ArrayList<Product> getAllProductsFromHippo() {
        ArrayList<Product> products = new ArrayList<Product>();
        RestTemplate restTemplate = new RestTemplate();
        
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        //add the Jackson mapper for easy mapping of JSON to POJO's
        messageConverters.add(new MappingJacksonHttpMessageConverter());
    
        String url = RESTAPI_BASE_URI + "/topproducts./?" + RESTAPI_RESPONSE_TYPE;
    
        Product[] productsFromHippo = restTemplate.getForObject(url, Product[].class);
        products.addAll(Arrays.asList(productsFromHippo));
        return products;
      }
    }
    

    As you can see the getAllProductsFromHippo method uses the Spring Android RestTemplate in combination with the MappingJacksonHttpMessageConverter to map the JSON response to an array of Product classes. Let's have a closer look at a Product class.

    package org.onehippo.gogreen.android.data;
    
    import org.codehaus.jackson.annotate.JsonIgnoreProperties;
    import org.codehaus.jackson.annotate.JsonProperty;
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Product {
    
      @JsonProperty
      private String localizedName;
    
      public String getLocalizedName() {
          return localizedName;
      }
    
      public void setLocalizedName(final String localizedName) {
          this.localizedName = localizedName;
      }
    }
    

    The Product class is quite simple. It only contains the localized name (for now). To make sure the mapping succeeds, I've also added the annotation JsonIgnoreProperties , so that it will ignore unknown properties during the mapping phase.
    Now if we provide the list of Product items to the Android ArrayAdapter, which is used by our ListView we will see all the items in the list returned by the HST-2 REST service.

    Resources used

    The following resources were used to create this post:

    09 February 2011

    Working with Android Layouts and ListViews

    I've been the owner of an Android phone for about 5 months now. The thought of creating an application for the Android platform has appealed to me ever since. That's why I recently started with Android development as a learning project for the next couple of weeks. In this post I will start sharing my experience with developing Android applications.

    Getting started

    The basic thing, while starting out with a new technology, is getting to know the fundamentals. There are some great introduction and advanced videos by Google on how to develop applications for the Android platform. The use of proper tooling can also help out a lot on this part. Both Eclipse and IntelliJ has great support for developing Android application since IntelliJ 10 (and it's free to use).

    For this project I'm trying to create a native Android client based on the Hippo GoGreen mobile website. If you take a look at the mobile site, there are two main entry points for browsing the site: Products and Events. I started out with Events, where I wanted to create a list of event items and show the event with a nice calendar item on the left next to the title of the event. I wanted the end result to look something like:


    In Android you can create a screen/page by creating an Activity. Adding a ListView to an Activity is a matter of configuration. With Android you can define the layout of your View either by defining a piece of XML, or by writing the code in Java. For this example I use the XML notation.

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <TextView
            android:id="@android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/empty_events"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </FrameLayout>
    

    As you can see the layout contains a ListView and a TextView. You can probably guess what the ListView is for, but I've added the TextView to show a message with 'No events' to the end user if no events are found.
    Adding some text to a list item in a ListView is quite simple and there are some good examples available in the Android tutorials. When I got to the point where I needed to add the dynamic calendar, I really had to start digging into layout options. Android has several kinds of default layouts available.

    Hovering the text over an image might not be that hard for an experienced Android developer (and there might be other ways than how I solved it), but when you're first introduced to the Android layout system it might be a bit confusing sometimes. My main goal was to show the day of the month and an abbreviation of the month dynamically on top of the calendar image and this is how I did it.

    Customizing the list item view


    To be able to show a customized list item View we first need to create a snippet of XML that represents the layout of our list item.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list_item_event"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        style="@style/ListItem">
    
        <org.onehippo.gogreen.android.ui.view.SimpleCalendarView
            android:id="@+id/calendar_today"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:gravity="center_horizontal|top"
            />
    
        <TextView
            android:gravity="left"
            android:id="@+id/event_title"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_alignWithParentIfMissing="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/calendar_today"
            style="@style/EventTitle" />
    </RelativeLayout>
    

    As you can see in the above snippet, I'm using a RelativeLayout for my list item. The nice thing about the RelativeLayout is that you can define the relative position of the TextView versus the SimpleCalendarView. In my case the text is to the right of the calendar item. In the above layout configuration the list item contains two elements: the calendar (which is our custom View component) and a TextView, which will contain the title.

    Creating the calendar view


    For the dynamic calendar I've created a custom View which will position my 'inner' views in such a way that both TextViews containing the day and month position nicely on top of the ImageView. Now let's take a look at some code.

    public class SimpleCalendarView extends FrameLayout {
    
        private ImageView calendarImageView = null;
        private TextView calendarMonthTextView = null;
        private TextView calendarDayTextView = null;
    
        public SimpleCalendarView(Context context) {
            super(context);
        }
    
        public SimpleCalendarView(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
    
            setUpImageView(context);
            setUpDayView(context);
            setUpMonthView(context);
    
            /* Add child views to this object. */
            addView(calendarImageView);
            addView(calendarMonthTextView);
            addView(calendarDayTextView);
        }
    
        private void setUpImageView(final Context context) {
            calendarImageView = new ImageView(context);
            calendarImageView.setImageResource(R.drawable.bg_calendar);
            calendarImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        }
    
        private void setUpMonthView(final Context context) {
            calendarMonthTextView = new TextView(context);
            calendarMonthTextView.setTextSize(7);
            calendarMonthTextView.setTypeface(Typeface.DEFAULT_BOLD);
            calendarMonthTextView.setPadding(0, 4, 0, 0);
            calendarMonthTextView.setTextColor(Color.WHITE);
            calendarMonthTextView.setGravity(Gravity.CENTER_HORIZONTAL);
        }
    
        private void setUpDayView(final Context context) {
            calendarDayTextView = new TextView(context);
            calendarDayTextView.setTextSize(10);
            calendarDayTextView.setTypeface(Typeface.DEFAULT_BOLD);
            calendarDayTextView.setPadding(0, 13, 0, 0);
            calendarDayTextView.setTextColor(Color.WHITE);
            calendarDayTextView.setGravity(Gravity.CENTER_HORIZONTAL);
        }
     
        public void setDayOfMonth(final int day) {
            this.calendarDayTextView.setText(Integer.toString(day));
        }
    
        public void setMonth(final String month) {
            this.calendarMonthTextView.setText(month);
        }
    }
    

    If you look at the above code you can see that adding the ImageView and TextViews is quite straight-forward. Because the SimpleCalendarView extends a FrameLayout, all inner views are positioned to the top left of the View by default. By setting the gravity of both TextViews to CENTER_HORIZONTAL the text is positioned in the middle of the image. Now by setting some top padding, the day and month are put into place.
    So after all it wasn't that hard to do. You just have to get to know the possibilities of the different Layouts. For my own convenience I added two extra methods to the SimpleCalendarView, so that I can easily set the month and day for each event without having to call the individual TextViews.

    What's next?

    This has been an interesting lesson in working with layouts, views and lists. My next post will probably be about how to retrieve data from a remote server and use that data to feed the list.

    Resources used

    The following resources were used while trying to create this view:

    10 January 2011

    Tip of the day: sharing information between HST components

    If you're working as a web developer with Hippo CMS, I guess you have written quite a few HST components. I presume that by now you will have a basic understanding of what HST components can and can't do.
    I've had the situation myself where I wanted to share some information between components on a single page. I first thought I could simply achieve this by adding an attribute to the request, but that didn't work. To show you what you can do, let's first start of with a bit of background information about what's actually going on inside the HST, when an incoming request is being processed.

    HST request processing


    Let's first have a look at a page definition. In a traditional HST page definition you have a tree of components. The figure below describes a normal page layout defintion.


    As you can see the page definition in the above figure has a root component (the page definition itself) with three child components: component 1, component 2 and component 3. Now the follow flow chart shows what the HST will do when it's handling a request.

    This flow chart of couse does not show all the steps taken by the HST,
    but it should give you a good impression of what's going on.

    At first the HST will lookup the correct page definition based on the HST sitemap. Once the correct page definition has been found the HST will start processing.

    The entire component tree for the current page definition is fetched and once the tree is there, the HST will use the AggregationValve to run down the component tree and invoke the doBeforeRender() methods of all of the available components.

    Once the entire component three has been processed in the 'before render phase', it will start processing the doRender() methods of all the components, so that the output of every single component will be generated and aggregated to end up in the end result.

    Now the important part to know here is that each invoked component will have it's own individual HStRequest object. Now if you want to share any kind of information you cannot simply use the HstRequest, because all the information is gone while processing the next component.
    However there is an object attached to these individual HstRequest objects and that is the HstRequestContext. The HstRequestContext hold some quite useful information, which you help support your components, but you can also add your own information by setting some attribute.


    Enough theory for now. If you would like to have a deeper knowledge of the HST request processing, the proces is described in much more detail on the HST2 wiki.


    Now for some code


    As an example let's take the usage of banners on a page. Let's say we have a boolean flag configured somewhere, which will define if banners should be shown on our page.

    Let's presume that a banner can be shown by multiple components on a page. If we take figure 1, we could say that a banner could appear above component 2 and underneath component 3.

    Now we could let both component 2 and component 3 figure out if the banners should be shown, but we could also share the information if component 2 or 3 is executed first, so that the other component does not have to read the configuration over again. The resulting code is quite simple. Let's have a look.

    @Override
    public void doBeforeRender(HstRequest request, HstResponse response) {
      boolean isBannerEnabled;
      HstRequestContext requestContext = request.getRequestContext();
      //let's see if the flag has been set on the request context
      if(requestContext.getAttribute(IS_BANNER_ENABLED_ATTRIBUTE)!=null){
        isBannerEnabled = (Boolean)requestContext.getAttribute(IS_BANNER_ENABLED_ATTRIBUTE);
      } else {
        //nothing on the request context, so lets figure it out
        isBannerEnabled = isBannerEnabled(request);
        //put the result on the request context so all other components can benefit
        requestContext.setAttribute(IS_BANNER_ENABLED_ATTRIBUTE, isBannerEnabled);
      }
      //put on the request for the current component
      request.setAttribute(IS_BANNER_ENABLED_ATTRIBUTE, isBannerEnabled);
    }
    
    /**
     * Simply return true for this example.
     */
    boolean isBannerEnabled(HstRequest request) {
      return true;
    }
    

    As you will see the actual code is really simple. All you will have to do is store information on the HstRequestContext. That's it. Well these were my 2 cents for today. Go and have fun and try to leverage the power of the HST2.