Enterprise Integration Zone is brought to you in partnership with:

Claus Ibsen is a principal software engineer from FuseSource. Claus is a full time committer on the open source integration framework Apache Camel (http://camel.apache.org) and author of the Camel in Action book (http://www.manning.com/ibsen). Claus is the most active contributor to Apache Camel and is very active in the Camel community. Claus is a frequent speaker at various conferences. Claus is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

Camel 2.11 - HTTP Proxy routes with URL Rewriting Functionality

01.04.2013
| 4953 views |
  • submit to reddit

In the upcoming Apache Camel 2.11 release I have recently added support for plugging in custom url rewrite implementations to HTTP based routes (http, http4, jetty). This allows people to control the url mappings, when you use Camel to proxy/bridge HTTP routes.

For example, suppose you need to proxy a legacy HTTP service and plugin a strategy for mapping the urls. This is now much easier with Camel 2.11. There is a new option urlRewrite added to the various HTTP components, to plugin a custom url rewriter.

For example having a http proxy route as shown, where we use the new urlRewrite option on the http producer endpoint.

from("jetty:http://localhost:{{port}}/myapp?matchOnUriPrefix=true")
    .to("jetty:http://somewhere:{{port2}}/myapp2?bridgeEndpoint=true&throwExceptionOnFailure=false&urlRewrite=#myRewrite");

In a nutshell you can implement a custom strategy by implementing the UrlRewrite interface, as shown below. As this is from an unit test, we just replace yahoo to google in the url (yes its not a real-life applicable example).

public class GoogleUrlRewrite implements UrlRewrite {

  @Override
  public String rewrite(String url, String relativeUrl, Producer producer) {
      return url.replaceAll("yahoo", "google");
  }
}

In the rewrite method Camel provides you with the absolute url (eg including scheme:host:port/path?query) or a relative url which is the offset from the uri configured in the route (see further below).

However it all gives you the full power to control the url mappings, and even return a new absolute url. If you return null, then the default strategy is used, which is a 1:1 url mapping.

That is not all there is also a new component

Introducing the new camel-urlrewrite component

The new camel-urlrewrite component is a implementation of the new url rewrite plugin based on the UrlRewriteFilter project. This project has strong support for specifying your rewrite strategies as rules, and have its engine evaluate the rules.

For example we can have N+ rules in the url rewrite XML configuration file. In the example below we have a rule to rewrite urls to adapt to a legacy system which is using JSP.
<urlrewrite>

  <rule>
    <from>/products/([0-9]+)</from>
    <to>/products/index.jsp?product_id=$1</to>
  </rule>

</urlrewrite>

This project has even support for Apache mod_rewrite styles which allow you to define rules as you would do with the Apache HTTP server. Though if you are not familiar with the mod_rewrite style then its dense and takes some time to understand - but very powerful.

All this is documented at the camel-urlrewrite component page with examples. And if you want to look for more, then checking the unit tests source code is also a good way to learn more.

I encourage you to take a look at the new camel-urlrewrite page as it has full examples and more details, that what I have outlined in this short blog.


Happy new-year and see you in 2013.


Published at DZone with permission of Claus Ibsen, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)