Writing a Camel Component – Camel-Static-Resource

Blue and Red CubesMany times the Camel examples I write need to serve up  a simple web UI (usually using the ever so awesome AngularJS).  I’ve just have been pasting the code below into a Camel route. This is fine but lately I’ve been also using Mule on a project and I noticed that Mule has a static resource component. Since Camel didn’t have one it provided the perfect opportunity to show just how easy it is to create a new Camel component. The bulk of implementing the new component will be taking the Processor from my code and implementing the process method in the new component producer.

I’m not looking to build a high capacity web server in Camel. This targets small use or, like the need in my case, demos.  The component will be agnostic to the HTTP provider that acts as the route consumer. The only requirement being that Exchange.HTTP_PATH be set before calling the endpoint. For example:

Create the new Camel Component Project

There’s a Maven archetype for creating a new Camel component.  To setup the project we specify the group and artifact id’s as well as the name of the component  and the scheme name to be used in routes.

This creates the needed core Java classes as well as the support files.

StaticContentComponent.java  - Manages parameters and creation of the end point.
StaticContentConsumer.java  - This component is producer only so we’ll delete this file.
StaticContentEndpoint.java  - The Endpoint for the component.
StaticContentProducer.java  - The producer will process requests and return the requested file.

Implement StaticContentEndpoint

The archetype setup a skeleton for a Camel component and we just need to make a few changes. First since this is a producer only component let’s change the createConsumer method to throw an exception instead of creating a consumer.

Next we’ll add two fields, rootDir and defaultFile as well as create accessors for these fields.

That’s it. The Endpoint is now all set.

Implement StaticContentComponent

The usage of our Camel component will look like this  from(...).to("static-content:///camel/examples/static-content/www where /camel/examples/static-content/www is the root directory for the content files.  Additionally let’s have a parameter ‘defaultFile’ that will specify what the default file if one isn’t specified in the request.

The archetype created StaticContentComponent with the following skeleton for createEndpoint.

The setProperties method inspects the bean passed in, in this case the endpoint, and sets any properties that match the parameters included in the route definition. So defaultFile is handled. Now we need to save the root directory.

Implement StaticContentProducer

First will implement a method to look at the message exchange pattern (MEP) which will decide if we write the results to the in or out Message.

Now we’ll take the Processor at the beginning of this post and add it to the Producer adding in support for the MEP pattern and other minor changes.

Odds and Ends

Just a couple of tweaks and we’ll be all set. Since the included MIME library lacked some types I created mime.types file which the library will also search and added the types there.

So how does Camel know this component is available? It looks for files in  META-INF/services/org/apache/camel/component and if the class is defined that component is loaded.

I added several tests to confirm the component is operating as expected and it’s all set. Running  mvn install  will compile the code and run the tests.

Using the New Camel Component

To use the new Camel component include it in your pom.xml

Then you can use with any component that sets Exchange.HTTP_PATH such as camel-jetty, camel-http, and camel-http4.

The full working project can be found, as always, on Github at https://github.com/tjsnell/camel-static-content.

 

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Comments on this entry are closed.

  • jstrachan

    Great article!

    BTW if you also use Endpoint Annotations
    http://camel.apache.org/endpoint-annotations.html

    then you’ll get a nicer auto-generated UI form in tools like hawtio – http://hawt.io/ – if you edit routes using this new component. You also get nice auto-generated static HTML too (similar to the auto-generated docs for maven plugins).

    You can even implement the new EndpointCompleter to get nice auto-completion of the URI path in hawtio (and other tools)
    http://camel.apache.org/endpointcompleter.html

    • tjsnell

      Thanks James.

      I saw the annotations the other day then promptly forgot about them when I wrote this. I’ll add them in later this week.

  • tanjax

    Hah, tjsnell has a blog. I dare you to visit ##java over at freenode, where he is one of the operators and a monumental asshole.

  • Scott Dawson

    Hi Tracy – I put in a plug for your static resource component on the Camel Users Forum. The Camel project lead wants to know if you’d be willing to donate your code to the Camel project. Please respond to the forum thread if you’re interested: http://camel.465427.n5.nabble.com/Corrupt-pdf-from-classpath-tp5756921.html

    Thanks!

    • tjsnell

      Hi Scott,

      I’ve been planning on donating the code. I have commit on Camel so I’ll actually go do so myself. Thanks!