Enterprise Java

Camel 2.11 – HTTP proxy routes with url rewriting functionality

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.
 

Reference: Camel 2.11 – HTTP proxy routes with url rewriting functionality from our JCG partner Claus Ibsen at the Claus Ibsen riding the Apache Camel blog.

Claus Ibsen

Claus Ibsen is a principal software engineer from Red Hat. Claus is working full time as Apache Camel committer. And is author of the "Camel in Action" book.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button