camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Restlet
Date Sun, 17 Feb 2013 08:24: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/Restlet">Restlet</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~njiang">willem
jiang</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" >| {{CamelRestletResponse}} | {{Response}}
| *Camel 2.8:* The {{org.restlet.Response}} object. You can use this to create responses using
the API from Restlet. See examples below. | <br>| {{org.restlet.\*}} | | Attributes
of a Restlet message that get propagated to Camel IN headers. | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{cache-control}}| {{String}} or {{List&lt;CacheDirective&gt;}} | *Camel 2.11:* User
can set the cache-control with the String value or the List of CacheDirective of Restlet from
the camel message header. | <br></td></tr>
            <tr><td class="diff-unchanged" >{div} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Restlet-RestletComponent"></a>Restlet Component</h2>

<p>The <b>Restlet</b> component provides <a href="http://www.restlet.org"
class="external-link" rel="nofollow">Restlet</a> based <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">endpoints</a> for consuming and producing RESTful resources.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-restlet<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="Restlet-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
restlet:restletUrl[?options]
</pre>
</div></div>
<p>Format of restletUrl:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
protocol:<span class="code-comment">//hostname[:port][/resourcePattern]</span>
</pre>
</div></div>
<p>Restlet promotes decoupling of protocol and application concerns.  The reference
implementation of <a href="http://www.noelios.com/products/restlet-engine" class="external-link"
rel="nofollow">Restlet Engine</a> supports a number of protocols.  However, we have
tested the HTTP protocol only.  The default port is port 80.  We do not automatically switch
default port based on the protocol yet.</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="Restlet-Options"></a>Options</h3>
<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>headerFilterStrategy=#</tt><em>refName</em>
</td>
<td class='confluenceTd'> An instance of <tt>RestletHeaderFilterStrategy</tt>
</td>
<td class='confluenceTd'> Use the <tt>&#35;</tt> notation (<tt>headerFilterStrategy=#</tt><em>refName</em>)
to reference a header filter strategy in the Camel Registry.  The strategy will be plugged
into the restlet binding if it is <tt>HeaderFilterStrategyAware</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>restletBinding=#</tt><em>refName</em>
</td>
<td class='confluenceTd'> An instance of <tt>DefaultRestletBinding</tt>
</td>
<td class='confluenceTd'> The bean ID of a <tt>RestletBinding</tt> object
in the Camel Registry. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>restletMethod</tt> </td>
<td class='confluenceTd'> <tt>GET</tt> </td>
<td class='confluenceTd'> On a producer endpoint, specifies the request method to use.
 On a consumer endpoint, specifies that the endpoint consumes only <tt>restletMethod</tt>
requests.  The string value is converted to <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/data/Method.html"
class="external-link" rel="nofollow">org.restlet.data.Method</a> by the <tt>Method.valueOf(String)</tt>
method. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>restletMethods</tt> </td>
<td class='confluenceTd'> <em>None</em> </td>
<td class='confluenceTd'> <b>Consumer only</b> Specify one or more methods
separated by commas (e.g. <tt>restletMethods=post,put</tt>) to be serviced by
a restlet consumer endpoint.  If both <tt>restletMethod</tt> and <tt>restletMethods</tt>
options are specified, the <tt>restletMethod</tt> setting is ignored. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>restletRealm=#</tt><em>refName</em>
</td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The bean ID of the Realm Map in the Camel Registry. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>restletUriPatterns=#</tt><em>refName</em>
</td>
<td class='confluenceTd'> <em>None</em> </td>
<td class='confluenceTd'> <b>Consumer only</b> Specify one ore more URI
templates to be serviced by a restlet consumer endpoint, using the <tt>&#35;</tt>
notation to reference a <tt>List&lt;String&gt;</tt> in the Camel Registry.
 If a URI pattern has been defined in the endpoint URI, both the URI pattern defined in the
endpoint and the <tt>restletUriPatterns</tt> option will be honored. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>throwExceptionOnFailure</tt> (<b>2.6
or later</b>)</td>
<td class='confluenceTd'> true </td>
<td class='confluenceTd'> *Producer only * Throws exception on a producer failure. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Restlet-ComponentOptions"></a>Component Options</h3>
<p>The Restlet component can be configured with 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>controllerDaemon</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Indicates if the controller
thread should be a daemon (not blocking JVM exit). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>controllerSleepTimeMs</tt> </td>
<td class='confluenceTd'> <tt>100</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Time for the controller thread
to sleep between each control. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>inboundBufferSize</tt> </td>
<td class='confluenceTd'> <tt>8192</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> The size of the buffer when
reading messages. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>minThreads</tt> </td>
<td class='confluenceTd'> <tt>1</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b>  Minimum threads waiting to
service requests. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxThreads</tt> </td>
<td class='confluenceTd'> <tt>10</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Maximum threads that will service
requests. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxConnectionsPerHost</tt> </td>
<td class='confluenceTd'> <tt>-1</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Maximum number of concurrent
connections per host (IP address). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxTotalConnections</tt> </td>
<td class='confluenceTd'> <tt>-1</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Maximum number of concurrent
connections in total. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>outboundBufferSize</tt> </td>
<td class='confluenceTd'> <tt>8192</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> The size of the buffer when
writing messages. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>persistingConnections</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Indicates if connections should
be kept alive after a call. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>pipeliningConnections</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Indicates if pipelining connections
are supported. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>threadMaxIdleTimeMs</tt> </td>
<td class='confluenceTd'> <tt>60000</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b>  Time for an idle thread to
wait for an operation before being collected. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>useForwardedForHeader</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Lookup the "X-Forwarded-For"
header supported by popular proxies and caches and uses it to populate the Request.getClientAddresses()
method result. This information is only safe for intermediary components within your local
network. Other addresses could easily be changed by setting a fake header and should not be
trusted for serious security checks.</td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Restlet-MessageHeaders"></a>Message Headers</h3>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>Content-Type</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> Specifies the content type, which can be set on the OUT message
by the application/processor.  The value is the <tt>content-type</tt> of the response
message.  If this header is not set, the content type is based on the object type of the OUT
message body. In Camel 2.3 onward, if the Content-Type header is specified in the Camel IN
message, the value of the header determine the content type for the Restlet request message.&nbsp;&nbsp;
Otherwise, it is defaulted to "application/x-www-form-urlencoded'.  Prior to release 2.3,
it is not possible to change the request content type default. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelAcceptContentType</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <b>Since Camel 2.9.3, 2.10.0:</b> The HTTP Accept
request header.  </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelHttpMethod</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> The HTTP request method.  This is set in the IN message header.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelHttpQuery</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> The query string of the request URI.  It is set on the IN
message by <tt>DefaultRestletBinding</tt> when the restlet component receives
a request. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelHttpResponseCode</tt> </td>
<td class='confluenceTd'> <tt>String</tt> or <tt>Integer</tt>
</td>
<td class='confluenceTd'> The response code can be set on the OUT message by the application/processor.
 The value is the response code of the response message.  If this header is not set, the response
code is set by the restlet runtime engine. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelHttpUri</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> The HTTP request URI.  This is set in the IN message header.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelRestletLogin</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> Login name for basic authentication.  It is set on the IN
message by the application and gets filtered before the restlet request header by Camel. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelRestletPassword</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> Password name for basic authentication.  It is set on the
IN message by the application and gets filtered before the restlet request header by Camel.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelRestletRequest</tt> </td>
<td class='confluenceTd'> <tt>Request</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> The <tt>org.restlet.Request</tt>
object which holds all request details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelRestletResponse</tt> </td>
<td class='confluenceTd'> <tt>Response</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> The <tt>org.restlet.Response</tt>
object. You can use this to create responses using the API from Restlet. See examples below.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>org.restlet.&#42;</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Attributes of a Restlet message that get propagated to Camel
IN headers. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cache-control</tt></td>
<td class='confluenceTd'> <tt>String</tt> or <tt>List&lt;CacheDirective&gt;</tt>
</td>
<td class='confluenceTd'> <b>Camel 2.11:</b> User can set the cache-control
with the String value or the List of CacheDirective of Restlet from the camel message header.
</td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Restlet-MessageBody"></a>Message Body</h3>

<p>Camel will store the restlet response from the external server on the OUT body. All
headers from the IN message will be copied to the OUT message, so that headers are preserved
during routing.</p>

<h3><a name="Restlet-Samples"></a>Samples</h3>

<h4><a name="Restlet-RestletEndpointwithAuthentication"></a>Restlet Endpoint
with Authentication</h4>

<p>The following route starts a <tt>restlet</tt> consumer endpoint that
listens for <tt>POST</tt> requests on <a href="http://localhost:8080" class="external-link"
rel="nofollow">http://localhost:8080</a>.  The processor creates a response that
echoes the request body and the value of the <tt>id</tt> header.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"restlet:http:<span class="code-comment">//localhost:"</span>
+ port + <span class="code-quote">"/securedOrders?restletMethod=post&amp;restletRealm=#realm"</span>).process(<span
class="code-keyword">new</span> Processor() {
</span>    <span class="code-keyword">public</span> void process(Exchange
exchange) <span class="code-keyword">throws</span> Exception {
        exchange.getOut().setBody(
                <span class="code-quote">"received ["</span> + exchange.getIn().getBody()
                + <span class="code-quote">"] as an order id = "</span>
                + exchange.getIn().getHeader(<span class="code-quote">"id"</span>));
    }
});
</pre>
</div></div>
<p>The <tt>restletRealm</tt> setting in the URI query is used to look up
a Realm Map in the registry.  If this option is specified, the restlet consumer uses the information
to authenticate user logins.  Only <em>authenticated</em> requests can access
the resources.  In this sample, we create a Spring application context that serves as a registry.
 The bean ID of the Realm Map should match the <em>restletRealmRef</em>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;util:map id=<span class="code-quote">"realm"</span>&gt;</span>
	<span class="code-tag">&lt;entry key=<span class="code-quote">"admin"</span>
value=<span class="code-quote">"foo"</span> /&gt;</span>
	<span class="code-tag">&lt;entry key=<span class="code-quote">"bar"</span>
value=<span class="code-quote">"foo"</span> /&gt;</span>
<span class="code-tag">&lt;/util:map&gt;</span>
</pre>
</div></div>
<p>The following sample starts a <tt>direct</tt> endpoint that sends requests
to the server on <a href="http://localhost:8080" class="external-link" rel="nofollow">http://localhost:8080</a>
(that is, our restlet consumer endpoint).</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// Note: restletMethod and restletRealmRef
are stripped 
</span><span class="code-comment">// from the query before a request is sent as
they are 
</span><span class="code-comment">// only processed by Camel.
</span>from(<span class="code-quote">"direct:start-auth"</span>).to(<span
class="code-quote">"restlet:http:<span class="code-comment">//localhost:"</span>
+ port + <span class="code-quote">"/securedOrders?restletMethod=post"</span>);</span>
</pre>
</div></div>
<p>That is all we need.  We are ready to send a request and try out the restlet component:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">final</span> <span
class="code-object">String</span> id = <span class="code-quote">"89531"</span>;

Map&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt;
headers = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>,
<span class="code-object">Object</span>&gt;();
headers.put(RestletConstants.RESTLET_LOGIN, <span class="code-quote">"admin"</span>);
headers.put(RestletConstants.RESTLET_PASSWORD, <span class="code-quote">"foo"</span>);
headers.put(<span class="code-quote">"id"</span>, id);

<span class="code-object">String</span> response = (<span class="code-object">String</span>)template.requestBodyAndHeaders(
    <span class="code-quote">"direct:start-auth"</span>, <span class="code-quote">"&lt;order
foo='1'/&gt;"</span>, headers);
</pre>
</div></div>

<p>The sample client sends a request to the <tt>direct:start-auth</tt> endpoint
with the following headers:</p>
<ul>
	<li><tt>CamelRestletLogin</tt> (used internally by Camel)</li>
	<li><tt>CamelRestletPassword</tt> (used internally by Camel)</li>
	<li><tt>id</tt> (application header)</li>
</ul>


<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Note</b><br
/><tt>org.apache.camel.restlet.auth.login</tt> and <tt>org.apache.camel.restlet.auth.password</tt>
will not be propagated as Restlet header.</td></tr></table></div>

<p>The sample client gets a response like the following:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
received [&lt;order foo='1'/&gt;] as an order id = 89531
</pre>
</div></div>

<h4><a name="Restlet-SinglerestletendpointtoservicemultiplemethodsandURItemplates"></a>Single
restlet endpoint to service multiple methods and URI templates</h4>

<p>It is possible to create a single route to service multiple HTTP methods using the
<tt>restletMethods</tt> option.  This snippet also shows how to retrieve the request
method from the header:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"restlet:http:<span class="code-comment">//localhost:"</span>
+ portNum + <span class="code-quote">"/users/{username}?restletMethods=post,get,put"</span>)
</span>    .process(<span class="code-keyword">new</span> Processor() {
        <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
            <span class="code-comment">// echo the method
</span>            exchange.getOut().setBody(exchange.getIn().getHeader(Exchange.HTTP_METHOD,
<span class="code-object">String</span>.class));
        }
    });
</pre>
</div></div>

<p>In addition to servicing multiple methods, the next snippet shows how to create an
endpoint that supports multiple URI templates using the <tt>restletUriPatterns</tt>
option.  The request URI is available in the header of the IN message as well.  If a URI pattern
has been defined in the endpoint URI (which is not the case in this sample), both the URI
pattern defined in the endpoint and the <tt>restletUriPatterns</tt> option will
be honored.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"restlet:http:<span class="code-comment">//localhost:"</span>
+ portNum + <span class="code-quote">"?restletMethods=post,get&amp;restletUriPatterns=#uriTemplates"</span>)
</span>    .process(<span class="code-keyword">new</span> Processor() {
        <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
            <span class="code-comment">// echo the method
</span>            <span class="code-object">String</span> uri = exchange.getIn().getHeader(Exchange.HTTP_URI,
<span class="code-object">String</span>.class);
            <span class="code-object">String</span> out = exchange.getIn().getHeader(Exchange.HTTP_METHOD,
<span class="code-object">String</span>.class);
            <span class="code-keyword">if</span> ((<span class="code-quote">"http:<span
class="code-comment">//localhost:"</span> + portNum + <span class="code-quote">"/users/homer"</span>).equals(uri))
{
</span>                exchange.getOut().setBody(out + <span class="code-quote">"
"</span> + exchange.getIn().getHeader(<span class="code-quote">"username"</span>,
<span class="code-object">String</span>.class));
            } <span class="code-keyword">else</span> <span class="code-keyword">if</span>
((<span class="code-quote">"http:<span class="code-comment">//localhost:"</span>
+ portNum + <span class="code-quote">"/atom/collection/foo/component/bar"</span>).equals(uri))
{
</span>                exchange.getOut().setBody(out + <span class="code-quote">"
"</span> + exchange.getIn().getHeader(<span class="code-quote">"id"</span>,
<span class="code-object">String</span>.class)
                                          + <span class="code-quote">" "</span>
+ exchange.getIn().getHeader(<span class="code-quote">"cid"</span>, <span class="code-object">String</span>.class));
            }
        }
    });
</pre>
</div></div>

<p>The <tt>restletUriPatterns=#uriTemplates</tt> option references the <tt>List&lt;String&gt;</tt>
bean defined in the Spring XML configuration.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;util:list id=<span class="code-quote">"uriTemplates"</span>&gt;</span>
    <span class="code-tag">&lt;value&gt;</span>/users/{username}<span
class="code-tag">&lt;/value&gt;</span>
    <span class="code-tag">&lt;value&gt;</span>/atom/collection/{id}/component/{cid}<span
class="code-tag">&lt;/value&gt;</span>
<span class="code-tag">&lt;/util:list&gt;</span>
</pre>
</div></div>

<h4><a name="Restlet-UsingRestletAPItopopulateresponse"></a>Using Restlet
API to populate response</h4>
<p><b>Available as of Camel 2.8</b></p>

<p>You may want to use the <tt>org.restlet.Response</tt> API to populate
the response. This gives you full access to the Restlet API and fine grained control of the
response. See the route snippet below where we generate the response from an inlined Camel
<a href="/confluence/display/CAMEL/Processor" title="Processor">Processor</a>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Generating response using Restlet Response API</b></div><div
class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"restlet:http:<span class="code-comment">//localhost:"</span>
+ portNum + <span class="code-quote">"/users/{id}/like/{beer}"</span>)
</span>    .process(<span class="code-keyword">new</span> Processor() {
        <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
            <span class="code-comment">// the Restlet request should be available <span
class="code-keyword">if</span> neeeded
</span>            Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST,
Request.class);
            assertNotNull(<span class="code-quote">"Restlet Request"</span>, request);

            <span class="code-comment">// use Restlet API to create the response
</span>            Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE,
Response.class);
            assertNotNull(<span class="code-quote">"Restlet Response"</span>,
response);
            response.setStatus(Status.SUCCESS_OK);
            response.setEntity(<span class="code-quote">"&lt;response&gt;Beer
is Good&lt;/response&gt;"</span>, MediaType.TEXT_XML);
            exchange.getOut().setBody(response);
        }
    });
</pre>
</div></div>

<h4><a name="Restlet-UsingtheRestletservletwithinawebapp"></a>Using the
Restlet servlet within a webapp</h4>
<p><b>Available as of Camel 2.8</b><br/>
There are <a href="http://www.restlet.org/documentation/2.0/jee/ext/org/restlet/ext/servlet/ServerServlet.html"
class="external-link" rel="nofollow">three possible ways</a> to configure a Restlet
application within a servlet container and using the subclassed SpringServerServlet enables
configuration within Camel by injecting the Restlet Component.</p>

<p>Use of the Restlet servlet within a servlet container enables routes to be configured
with relative paths in URIs (removing the restrictions of hard-coded absolute URIs) and for
the hosting servlet container to handle incoming requests (rather than have to spawn a separate
server process on a new port).</p>

<p>To configure, add the following to your camel-context.xml;</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext&gt;</span>
  <span class="code-tag">&lt;route id=<span class="code-quote">"RS_RestletDemo"</span>&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"restlet:/demo/{id}"</span>
/&gt;</span>
    <span class="code-tag">&lt;transform&gt;</span>
      <span class="code-tag">&lt;simple&gt;</span>Request type : ${header.CamelHttpMethod}
and ID : ${header.id}<span class="code-tag">&lt;/simple&gt;</span>
    <span class="code-tag">&lt;/transform&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span> 
<span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"RestletComponent"</span>
class=<span class="code-quote">"org.restlet.Component"</span> /&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"RestletComponentService"</span>
class=<span class="code-quote">"org.apache.camel.component.restlet.RestletComponent"</span>&gt;</span>
  <span class="code-tag">&lt;constructor-arg index=<span class="code-quote">"0"</span>&gt;</span>
    <span class="code-tag">&lt;ref bean=<span class="code-quote">"RestletComponent"</span>
/&gt;</span>
  <span class="code-tag">&lt;/constructor-arg&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>And add this to your web.xml;</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag"><span class="code-comment">&lt;!-- Restlet Servlet
--&gt;</span></span>
<span class="code-tag">&lt;servlet&gt;</span>
  <span class="code-tag">&lt;servlet-name&gt;</span>RestletServlet<span
class="code-tag">&lt;/servlet-name&gt;</span>
  <span class="code-tag">&lt;servlet-class&gt;</span>org.restlet.ext.spring.SpringServerServlet<span
class="code-tag">&lt;/servlet-class&gt;</span>
  <span class="code-tag">&lt;init-param&gt;</span>
    <span class="code-tag">&lt;param-name&gt;</span>org.restlet.component<span
class="code-tag">&lt;/param-name&gt;</span>
    <span class="code-tag">&lt;param-value&gt;</span>RestletComponent<span
class="code-tag">&lt;/param-value&gt;</span>
  <span class="code-tag">&lt;/init-param&gt;</span>
<span class="code-tag">&lt;/servlet&gt;</span>

<span class="code-tag">&lt;servlet-mapping&gt;</span>
  <span class="code-tag">&lt;servlet-name&gt;</span>RestletServlet<span
class="code-tag">&lt;/servlet-name&gt;</span>
  <span class="code-tag">&lt;url-pattern&gt;</span>/rs/*<span class="code-tag">&lt;/url-pattern&gt;</span>
<span class="code-tag">&lt;/servlet-mapping&gt;</span>
</pre>
</div></div>
<p>You will then be able to access the deployed route at <a href="http://localhost:8080/mywebapp/rs/demo/1234"
class="external-link" rel="nofollow">http://localhost:8080/mywebapp/rs/demo/1234</a>
where;</p>

<p>localhost:8080 is the server and port of your servlet container<br/>
mywebapp is the name of your deployed webapp<br/>
Your browser will then show the following content;</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-quote">"Request type : GET and ID : 1234"</span>
</pre>
</div></div>

<p>You will need to add dependency on the Spring extension to restlet which you can
do in your Maven pom.xml file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
  <span class="code-tag">&lt;groupId&gt;</span>org.restlet.jee<span
class="code-tag">&lt;/groupId&gt;</span>
  <span class="code-tag">&lt;artifactId&gt;</span>org.restlet.ext.spring<span
class="code-tag">&lt;/artifactId&gt;</span>
  <span class="code-tag">&lt;version&gt;</span>${restlet-version}<span
class="code-tag">&lt;/version&gt;</span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<p>And you would need to add dependency on the restlet maven repository as well:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;repository&gt;</span>  
   <span class="code-tag">&lt;id&gt;</span>maven-restlet<span class="code-tag">&lt;/id&gt;</span>
 
   <span class="code-tag">&lt;name&gt;</span>Public online Restlet repository<span
class="code-tag">&lt;/name&gt;</span>  
   <span class="code-tag">&lt;url&gt;</span>http://maven.restlet.org<span
class="code-tag">&lt;/url&gt;</span>  
<span class="code-tag">&lt;/repository&gt;</span>
</pre>
</div></div>

<h3><a name="Restlet-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>

    </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/Restlet">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=106433&revisedVersion=30&originalVersion=29">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Restlet?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message