UrlRewrite ComponentAvailable 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. OptionsThe camel-urlrewrite component offers the following options
UsageThe following component producers supports using together with the camel-urlrewrite component: HTTP, HTTP4 and Jetty.
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: HttpUrlRewrite myRewrite = new HttpUrlRewrite(); myRewrite.setConfigFile("example/urlrewrite2.xml"); And in XML DSL you can do: <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. <urlrewrite> <!-- this is a rule where we map from /products/nnn to an url that is a jps page with the product id as a parameter --> <rule> <from>/products/([0-9]+)</from> <to>/products/index.jsp?product_id=$1</to> </rule> </urlrewrite>
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. from("jetty:http://localhost:{{port}}/myapp?matchOnUriPrefix=true") .to("jetty:http://localhost:{{port2}}/myapp2?bridgeEndpoint=true&throwExceptionOnFailure=false&urlRewrite=#myRewrite"); Using load balancing eipYou 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. // we want to use the failover loadbalancer // to have it to react we must set throwExceptionOnFailure=true, which is also the default value // so we can omit configuring this option from("jetty:http://localhost:{{port}}/myapp?matchOnUriPrefix=true") .loadBalance().failover(Exception.class) .to("jetty:http://localhost:{{port2}}/myapp2?bridgeEndpoint=true&throwExceptionOnFailure=true&urlRewrite=#myRewrite") .to("jetty:http://localhost:{{port2}}/myapp3?bridgeEndpoint=true&throwExceptionOnFailure=true&urlRewrite=#myRewrite"); Using Apache mod_rewrite style configurationThe 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: <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: # our custom mod rewrite rule RewriteRule page/([^/\.]+)/?$ index.php?page=$1 [L] You can have multiple RewriteRule rules and other configurations. See more details at UrlRewriteFilter project. Writing custom url rewritersYou can implement your custom url rewriters by implementing either of the following interfaces from the component of choice: Component(s): camel-http / camel-jetty
Component(s): camel-http4
The former is a simple and generic interface, which is not dependent on the Servlet API. A simple url rewrite filterIn 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. /** * A very simple url rewrite that replaces yahoo with google in the url. * <p/> * This is only used for testing purposes. */ public class GoogleUrlRewrite implements UrlRewrite { @Override public String rewrite(String url, String relativeUrl, Producer producer) { return url.replaceAll("yahoo", "google"); } } See Also |