UrlRewrite Component

Available as of Camel 2.11

The camel-urlrewrite component allows to plugin url rewrite functionality to HTTP, HTTP4, Jetty, or AHC components. This component integrates the UrlRewriteFilter project with Apache Camel. This allows you to use the capabilities from the url rewrite project with your Camel routes.

This component requires that your Camel routes starts from a servlet based endpoint such as Jetty or Servlet.

Options

The camel-urlrewrite component offers the following options

Name

Default Value

Description

conf

null

To use a custom org.tuckey.web.filters.urlrewrite.Conf as the configuration for the url rewrite filter.

configFile

null

Refers to a file name to load as the configuration file. The file should be XML file.

modRewriteConfFile

null

Refers to a file name to load as the Apache mod_rewrite style based configuration file. This file should be a plain text file.

modRewriteConfText

null

Is used to quickly configure a Apache mod_rewrite style based configuration without using a file.

useQueryString

false

Whether to use query strings in the url rewrite rules.

useContext

false

Whether to include context-path or not in the matching. Will be default remove the context-path.

defaultMatchType

"regex"

Which match type to use. Can be either "wildcard" or "regex".

decodeUsing

null

To use a specific encoding such as "utf-8" or "iso-8859-1".

Usage

The following component producers supports using together with the camel-urlrewrite component: Http, Http4 and Jetty.

Component

Class

camel-http

org.apache.camel.component.urlrewrite.http.HttpUrlRewrite

camel-jetty

org.apache.camel.component.urlrewrite.http.HttpUrlRewrite

camel-http4

org.apache.camel.component.urlrewrite.http4.HttpUrlRewrite

You setup the url rewrite as a bean of the type org.apache.camel.component.urlrewrite.http.HttpUrlRewrite (when using Http component) as shown below:

And in XML DSL you can do:

In the bean above we configure it with the configFile option which is a XML UrlRewriteFilter configuration file.

Icon

See more details at UrlRewriteFilter for how to configure the XML rules file.

In the Camel routes, you bridge the endpoints, and refer to the url rewrite on the producer endpoint as shown below. Notice how we refer to our url rewrite filter using the urlRewrite option in the endpoint uri.

Using load balancing eip

You can also use Camel's Load Balancer together with UrlRewrite, as shown below. In this code we use the failover load balancer, to failover to the 2nd endpoint (which goes to myapp3) in case of any exception thrown.

Using Apache mod_rewrite style configuration

The UrlRewriteFilter project also supports Apache mod_rewrite style configuration.

For example to configure the bean using the modRewriteConfFile property to refer to a mod configuration file:

Which then can contain mod rewrite rules and the likes:

You can have multiple RewriteRule rules and other configurations. See more details at UrlRewriteFilter project.

Writing custom url rewriters

You can implement your custom url rewriters by implementing either of the following interfaces from the component of choice:

Component(s): camel-http / camel-jetty

  • org.apache.camel.component.http.UrlRewrite
  • org.apache.camel.component.http.HttpServletUrlRewrite

Component(s): camel-http4

  • org.apache.camel.component.http4.UrlRewrite
  • org.apache.camel.component.http4.HttpServletUrlRewrite

The former is a simple and generic interface, which is not dependent on the Servlet API.
The later is servlet based which requires the Camel route to start from a servlet based component such as Jetty or Servlet. The servlet based is more feature rich and that's the API we use to integrate with the UrlRewriteFilter project in this camel-urlrewrite component.

A simple url rewrite filter

In this example we implement a custom org.apache.camel.component.http.UrlRewrite that just replaces yahoo with google. Mind this example is based on a unit test, and only for demonstration purposes. As its java code you have full power to implement more complex logic.

The url parameter contains the full url including scheme://hostname:port/path?query. The relativeUrl parameter is the url without the endpoint configured path. Notice this option may be null if the url doesn't start with the endpoint configured path.

See Also

© 2004-2014 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram