April 1, 2009

Handle Google Analytics scheduled e-mail reports with Apache Camel and Groovy

Yes, the title of this post is almost similar to my previous post Handle Google Analytics scheduled e-mail reports with Apache James. So we are going to do the same thing but this time we use Apache Camel instead of Apache James. Apache James is really a server-side solution and probably most people cannot install Apache James just like that. With Apache Camel we can have a solution without the need to install anything else.

This time we are going to send the scheduled Google Analytics report to our Google mail account instead of a James account. Read the previous post to see how we can schedule e-mail with report attachments. In Google we create a new filter which will assign the label GoogleAnalytics to the received e-mails. We are going to poll for mail messages with label GoogleAnalytics and look if the subject starts with the text Analytics. A similar proces is already described in another post. We extract the attachment from the e-mail and save it to a directory.

The following code shows how we poll messages with label GoogleAnalytics in Google mail every hour. We then check the subject of the message to see if it starts with Analytics. If so we process the message with the ExtractAttachment class. The message will contain an attachment with an XML file. So we get the contents of the attachment and save it to a file on disk.

#!/usr/bin/env groovy

import org.apache.camel.Exchange
import org.apache.camel.Processor
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.language.groovy.GroovyRouteBuilder

@Grab(group='org.apache.camel', module='camel-groovy', version='1.6.0')
@Grab(group='org.apache.camel', module='camel-mail', version='1.6.0')
@Grab(group='org.apache.camel', module='camel-core', version='1.6.0')
class AnalyticsRoute extends GroovyRouteBuilder {
    void configure(){
                                 + "&password=secret"
                                 + "&folderName=GoogleAnalytics"
                                 + "&consumer.delay=" + 1000 * 60 * 60)
            .filter { it.in.headers.subject.startsWith('Analytics') }
            .process(new ExtractAttachment())            

class ExtractAttachment implements Processor {
    void process(Exchange exchange) throws Exception {   
        def attachments = exchange.in.attachments
        attachments.each { attachment ->
            def datahandler = attachment.value
            def xml = exchange.context.typeConverter.convertTo(String.class, datahandler.inputStream)
            new File('/home/analytics/xml', datahandler.name) << xml

def camelCtx = new DefaultCamelContext()
camelCtx.addRoutes(new AnalyticsRoute());