camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > UrlRewrite
Date Sat, 26 Jan 2013 16:52:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/UrlRewrite">UrlRewrite</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=e2|lang=java|url=camel/trunk/components/camel-urlrewrite/src/test/java/org/apache/camel/component/urlrewrite/jetty/JettyUrlRewriteTest.java}
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
Using load balancing eip <br> <br>You can also use Camel&#39;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. <br>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-urlrewrite/src/test/java/org/apache/camel/component/urlrewrite/jetty/JettyUrlRewriteLoadBalanceFailoverTest.java}
<br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h4. Using Apache mod_rewrite style
configuration <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="UrlRewrite-UrlRewriteComponent"></a>UrlRewrite Component</h2>
<p><b>Available as of Camel 2.11</b></p>

<p>The <tt>camel-urlrewrite</tt> component allows to plugin url rewrite
functionality to <a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a>,
<a href="/confluence/display/CAMEL/HTTP4" title="HTTP4">HTTP4</a>, <a href="/confluence/display/CAMEL/Jetty"
title="Jetty">Jetty</a>, or <a href="/confluence/display/CAMEL/AHC" title="AHC">AHC</a>
components. This component integrates the <a href="http://code.google.com/p/urlrewritefilter/"
class="external-link" rel="nofollow">UrlRewriteFilter</a> project with Apache Camel.
This allows you to use the capabilities from the url rewrite project with your Camel routes.</p>

<p>This component <b>requires</b> that your Camel routes starts from a servlet
based endpoint such as <a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty</a>
or <a href="/confluence/display/CAMEL/SERVLET" title="SERVLET">SERVLET</a>.</p>

<h3><a name="UrlRewrite-Options"></a>Options</h3>

<p>The <tt>camel-urlrewrite</tt> component offers the following options</p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>conf</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>org.tuckey.web.filters.urlrewrite.Conf</tt>
as the configuration for the url rewrite filter. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>configFile</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Refers to a file name to load as the configuration file. The
file should be XML file. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>modRewriteConfFile</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Refers to a file name to load as the Apache mod_rewrite style
based configuration file. This file should be a plain text file. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>modRewriteConfText</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Is used to quickly configure a Apache mod_rewrite style based
configuration without using a file. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>useQueryString</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Whether to use query strings in the url rewrite rules. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>useContext</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Whether to include context-path or not in the matching. Will
be default remove the context-path. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>defaultMatchType</tt> </td>
<td class='confluenceTd'> <tt>"regex"</tt> </td>
<td class='confluenceTd'> Which match type to use. Can be either "wildcard" or "regex".
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>decodeUsing</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a specific encoding such as "utf-8" or "iso-8859-1".
</td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="UrlRewrite-Usage"></a>Usage</h3>

<p>The following component producers supports using together with the <tt>camel-urlrewrite</tt>
component: <a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a>, <a
href="/confluence/display/CAMEL/HTTP4" title="HTTP4">HTTP4</a> and <a href="/confluence/display/CAMEL/Jetty"
title="Jetty">Jetty</a>.</p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Component </th>
<th class='confluenceTh'> Class </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel-http</tt> </td>
<td class='confluenceTd'> <tt>org.apache.camel.component.urlrewrite.http.HttpUrlRewrite</tt>
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel-jetty</tt> </td>
<td class='confluenceTd'> <tt>org.apache.camel.component.urlrewrite.http.HttpUrlRewrite</tt>
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel-http4</tt> </td>
<td class='confluenceTd'> <tt>org.apache.camel.component.urlrewrite.http4.HttpUrlRewrite</tt>
</td>
</tr>
</tbody></table>
</div>
</div>

<p>You setup the url rewrite as a bean of the type <tt>org.apache.camel.component.urlrewrite.http.HttpUrlRewrite</tt>
(when using <a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a> component)
as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">HttpUrlRewrite myRewrite = <span class="code-keyword">new</span>
HttpUrlRewrite();
myRewrite.setConfigFile(<span class="code-quote">"example/urlrewrite2.xml"</span>);
</pre>
</div></div>

<p>And in XML DSL you can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"myRewrite"</span>
class=<span class="code-quote">"org.apache.camel.component.urlrewrite.HttpUrlRewrite"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"configFile"</span>
value=<span class="code-quote">"example/urlrewrite2.xml"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>In the bean above we configure it with the <tt>configFile</tt> option
which is a XML <a href="http://code.google.com/p/urlrewritefilter/" class="external-link"
rel="nofollow">UrlRewriteFilter</a> configuration file.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;urlrewrite&gt;</span>

  &lt;!-- 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 --&gt;
  <span class="code-tag">&lt;rule&gt;</span>
    <span class="code-tag">&lt;from&gt;</span>/products/([0-9]+)<span
class="code-tag">&lt;/from&gt;</span>
    <span class="code-tag">&lt;to&gt;</span>/products/index.jsp?product_id=$1<span
class="code-tag">&lt;/to&gt;</span>
  <span class="code-tag">&lt;/rule&gt;</span>

<span class="code-tag">&lt;/urlrewrite&gt;</span>
</pre>
</div></div>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>See more details at <a href="http://code.google.com/p/urlrewritefilter/"
class="external-link" rel="nofollow">UrlRewriteFilter</a> for how to configure the
XML rules file.</td></tr></table></div>

<p>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
<tt>urlRewrite</tt> option in the endpoint uri.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"jetty:http:<span class="code-comment">//localhost:{{port}}/myapp?matchOnUriPrefix=<span
class="code-keyword">true</span>"</span>)
</span>    .to(<span class="code-quote">"jetty:http:<span class="code-comment">//localhost:{{port2}}/myapp2?bridgeEndpoint=<span
class="code-keyword">true</span>&amp;throwExceptionOnFailure=<span class="code-keyword">false</span>&amp;urlRewrite=#myRewrite"</span>);</span>
</pre>
</div></div>

<h4><a name="UrlRewrite-Usingloadbalancingeip"></a>Using load balancing
eip</h4>

<p>You can also use Camel's <a href="/confluence/display/CAMEL/Load+Balancer" title="Load
Balancer">Load Balancer</a> together with <a href="/confluence/display/CAMEL/UrlRewrite"
title="UrlRewrite">UrlRewrite</a>, 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.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// we want to use the failover
loadbalancer
</span><span class="code-comment">// to have it to react we must set throwExceptionOnFailure=<span
class="code-keyword">true</span>, which is also the <span class="code-keyword">default</span>
value
</span><span class="code-comment">// so we can omit configuring <span class="code-keyword">this</span>
option
</span>from(<span class="code-quote">"jetty:http:<span class="code-comment">//localhost:{{port}}/myapp?matchOnUriPrefix=<span
class="code-keyword">true</span>"</span>)
</span>    .loadBalance().failover(Exception.class)
        .to(<span class="code-quote">"jetty:http:<span class="code-comment">//localhost:{{port2}}/myapp2?bridgeEndpoint=<span
class="code-keyword">true</span>&amp;throwExceptionOnFailure=<span class="code-keyword">true</span>&amp;urlRewrite=#myRewrite"</span>)
</span>        .to(<span class="code-quote">"jetty:http:<span class="code-comment">//localhost:{{port2}}/myapp3?bridgeEndpoint=<span
class="code-keyword">true</span>&amp;throwExceptionOnFailure=<span class="code-keyword">true</span>&amp;urlRewrite=#myRewrite"</span>);</span>
</pre>
</div></div>


<h4><a name="UrlRewrite-UsingApachemodrewritestyleconfiguration"></a>Using
Apache mod_rewrite style configuration</h4>

<p>The <a href="http://code.google.com/p/urlrewritefilter/" class="external-link"
rel="nofollow">UrlRewriteFilter</a> project also supports Apache mod_rewrite style
configuration. </p>

<p>For example to configure the bean using the modRewriteConfFile property to refer
to a mod configuration file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"myRewrite"</span>
class=<span class="code-quote">"org.apache.camel.component.urlrewrite.HttpUrlRewrite"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"modRewriteConfFile"</span>
value=<span class="code-quote">"example/modrewrite.cxf"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>Which then can contain mod rewrite rules and the likes:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-none"># our custom mod rewrite rule
RewriteRule page/([^/\.]+)/?$ index.php?page=$1 [L]

</pre>
</div></div>

<p>You can have multiple RewriteRule rules and other configurations. See more details
at <a href="http://code.google.com/p/urlrewritefilter/" class="external-link" rel="nofollow">UrlRewriteFilter</a>
project.</p>


<h3><a name="UrlRewrite-Writingcustomurlrewriters"></a>Writing custom url
rewriters</h3>

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

<p>Component(s): camel-http / camel-jetty</p>
<ul class="alternate" type="square">
	<li><tt>org.apache.camel.component.http.UrlRewrite</tt></li>
	<li><tt>org.apache.camel.component.http.HttpServletUrlRewrite</tt></li>
</ul>


<p>Component(s): camel-http4</p>
<ul class="alternate" type="square">
	<li><tt>org.apache.camel.component.http4.UrlRewrite</tt></li>
	<li><tt>org.apache.camel.component.http4.HttpServletUrlRewrite</tt></li>
</ul>


<p>The former is a simple and generic interface, which is not dependent on the Servlet
API.<br/>
The later is servlet based which requires the Camel route to start from a servlet based component
such as <a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty</a> or <a
href="/confluence/display/CAMEL/SERVLET" title="SERVLET">SERVLET</a>. The servlet
based is more feature rich and that's the API we use to integrate with the <a href="http://code.google.com/p/urlrewritefilter/"
class="external-link" rel="nofollow">UrlRewriteFilter</a> project in this <tt>camel-urlrewrite</tt>
component.</p>

<h4><a name="UrlRewrite-Asimpleurlrewritefilter"></a>A simple url rewrite
filter</h4>

<p>In this example we implement a custom <tt>org.apache.camel.component.http.UrlRewrite</tt>
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.</p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">/**
 * A very simple url rewrite that replaces yahoo with google in the url.
 * &lt;p/&gt;
 * This is only used <span class="code-keyword">for</span> testing purposes.
 */
<span class="code-keyword">public</span> class GoogleUrlRewrite <span class="code-keyword">implements</span>
UrlRewrite {

    @Override
    <span class="code-keyword">public</span> <span class="code-object">String</span>
rewrite(<span class="code-object">String</span> url, <span class="code-object">String</span>
relativeUrl, Producer producer) {
        <span class="code-keyword">return</span> url.replaceAll(<span class="code-quote">"yahoo"</span>,
<span class="code-quote">"google"</span>);
    }
}
</pre>
</div></div>


<h3><a name="UrlRewrite-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty</a></li>
	<li><a href="/confluence/display/CAMEL/HTTP4" title="HTTP4">HTTP4</a></li>
	<li><a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a></li>
	<li><a href="/confluence/display/CAMEL/AHC" title="AHC">AHC</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/UrlRewrite">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=30746665&revisedVersion=6&originalVersion=5">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/UrlRewrite?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message