Ok, in the project I've been on, we have managed to do some pretty awesome
stuff with camel, so I'd like to spend some time posting about some of the
cool things we have done - with some example code.

This is exposing a database as a google data source so that you can use
google charts.... Its also shows why camel is made of so _very_ much win...

Google's visualization kits make for some very pretty interactive eye candy,
however we need to expose some views in a way that they can talk to it.

The online instructions talk about setting up servlets, war files etc,
however, we have the awesome power of camel, so we just don't need that at
all... we do need jetty and visualization-datasource 
so add

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jetty</artifactId>
            <version>${camel.version}</version>
        </dependency>

and

        <dependency>
            <groupId>com.google.visualization</groupId>
            <artifactId>visualization-datasource</artifactId>
            <version>1.0.2</version>
        </dependency>

to your pom file....

Here is our RouteBuilder (minus security, checks for which table, filters to
keep a user to see only their data etc) - the point being that it is trivial
example.

Make sure that the database setup so that the username and password only
have read only access to the things you want to share, and you are ready to
go.

@Component
public class ReportServices extends RouteBuilder {
        
        // Values come from jdbc.properties via Spring property-placeholder
        @Value("${jdbc.url}") private String url;
        @Value("${jdbc.username}") private String username;
        @Value("${jdbc.password}") private String password;

    final Processor visualizationProcessor = new Processor() {
                        public void process(Exchange exchange) throws Exception 
{
                                HttpMessage in = (HttpMessage) exchange.getIn();
                                
DataSourceHelper.executeDataSourceServletFlow(in.getRequest(),
in.getResponse(), tableGenerator, false);
                        }
    };

    final DataTableGenerator tableGenerator = new DataTableGenerator() {
        public Capabilities getCapabilities() {
            return Capabilities.SQL;
        }

        public DataTable generateDataTable(Query query, HttpServletRequest
request) throws DataSourceException {
            SqlDatabaseDescription db = new SqlDatabaseDescription(
                url, username, password, request.getParameter("table"));
            return SqlDataSourceHelper.executeQuery(query, db);
        }
    };

        @Override
        public void configure() throws Exception {
        
from("jetty:http://0.0.0.0/Reports";).process(visualizationProcessor).stop();
        }

}

And we are done :)

Camel for the win again!


--
View this message in context: 
http://camel.465427.n5.nabble.com/Google-Visualization-API-wire-protocol-from-camel-how-to-do-it-tp4637916p4637916.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to