Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

Options

The camel-urlrewrite component offers the following options

...

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

Div
classconfluenceTableSmall

Component

Class

camel-http

org Camel 2.15 or older: org.apache.camel.component.urlrewrite.http.HttpUrlRewrite

camel-jetty

Camel 2.15 or older:org.apache.camel.component.urlrewrite.http.HttpUrlRewrite

camel-http4

Camel 2.15 or older:org.apache.camel.component.urlrewrite.http4.HttpUrlRewrite

 

From Camel 2.16 onwards this is no longer necessary and there is only one class, which is org.apache.camel.component.urlrewrite.HttpUrlRewrite that works with any of the components.

 

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

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-urlrewrite/src/test/java/org/apache/camel/component/urlrewrite/jetty/JettyUrlRewriteTest.java}
And in XML DSL you can do:

Code Block
xml
xml

<bean id="myRewrite" class="org.apache.camel.component.urlrewrite.HttpUrlRewrite">
  <property name="configFile" value="example/urlrewrite2.xml"/>
</bean>

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

Wiki Markup
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-urlrewrite/src/test/resources/example/urlrewrite2.xml}

Tip

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.

Wiki Markup
{snippet:id=e2|lang=java|url=camel/trunk/components/camel-urlrewrite/src/test/java/org/apache/camel/component/urlrewrite/jetty/JettyUrlRewriteTest.java}

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.

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-urlrewrite/src/test/java/org/apache/camel/component/urlrewrite/jetty/JettyUrlRewriteLoadBalanceFailoverTest.java}

Using Apache mod_rewrite style configuration

...

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

Code Block
xml
xml

<bean id="myRewrite" class="org.apache.camel.component.urlrewrite.HttpUrlRewrite">
  <property name="modRewriteConfFile" value="example/modrewrite.cxf"/>
</bean>

Which then can contain mod rewrite rules and the likes:

Wiki Markup
{snippet:e1|lang=none|url=camel/trunk/components/camel-urlrewrite/src/test/resources/example/modrewrite.cfg}
You can have multiple RewriteRule rules and other configurations. See more details at UrlRewriteFilter project.

...

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

...

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.

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/GoogleUrlRewrite.java}
Include Page
Endpoint See Also
Endpoint See Also