이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 130. UrlRewrite


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
Expand
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.
Expand
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:
HttpUrlRewrite myRewrite = new HttpUrlRewrite();
myRewrite.setConfigFile("example/urlrewrite2.xml");
Copy to Clipboard Toggle word wrap
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>
Copy to Clipboard Toggle word wrap
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>
Copy to Clipboard Toggle word wrap
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.
from("jetty:http://localhost:{{port}}/myapp?matchOnUriPrefix=true")
    .to("jetty:http://localhost:{{port2}}/myapp2?bridgeEndpoint=true&throwExceptionOnFailure=false&urlRewrite=#myRewrite");
Copy to Clipboard Toggle word wrap

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.
// 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");
Copy to Clipboard Toggle word wrap

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:
<bean id="myRewrite" class="org.apache.camel.component.urlrewrite.HttpUrlRewrite">
  <property name="modRewriteConfFile" value="example/modrewrite.cxf"/>
</bean>
Copy to Clipboard Toggle word wrap
Which then can contain mod rewrite rules and the likes:
# our custom mod rewrite rule
RewriteRule page/([^/\.]+)/?$ index.php?page=$1 [L]

Copy to Clipboard Toggle word wrap
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.
/**
 * 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");
    }
}
Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat