About 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.

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.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


six × 1 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books