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 Wed, 06 Jul 2011 03:33: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>
        <div id="versionComment">
        <b>Comment:</b>
        CAMEL-4175<br />
    </div>
        <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" >You may want to use the {{org.restlet.Response}}
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 [Processor]: <br>{snippet:id=e1|title=Generating response using Restlet
Response API|lang=java|url=camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRequestAndResponseAPITest.java}
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
<br>h4. Using the Restlet servlet within a webapp <br>*Available as of Camel 2.8*
<br>There are [three possible ways|http://www.restlet.org/documentation/2.0/jee/ext/org/restlet/ext/servlet/ServerServlet.html]
to configure a Restlet application within a servlet container and using the subclassed SpringServerServlet
enables configuration within Camel by injecting the Restlet Component. <br> <br>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). <br> <br>To configure, add the following to your camel-context.xml;
<br>{code} <br>&lt;camelContext&gt; <br>&lt;route id=&quot;RS_RestletDemo&quot;&gt;
<br>&lt;from uri=&quot;restlet:/demo/{id}&quot; /&gt; <br>&lt;transform&gt;
<br>&lt;simple&gt;Request type : ${header.CamelHttpMethod} and ID : ${header.id}&lt;/simple&gt;
<br>&lt;/transform&gt; <br>&lt;/route&gt;  <br>&lt;/camelContext&gt;
<br> <br>&lt;bean id=&quot;RestletComponent&quot; class=&quot;org.restlet.Component&quot;
/&gt; <br>&lt;bean id=&quot;RestletComponentService&quot; class=&quot;org.apache.camel.component.restlet.RestletComponent&quot;&gt;
<br>&lt;constructor-arg index=&quot;0&quot;&gt; <br>&lt;ref
bean=&quot;RestletComponent&quot; /&gt; <br>&lt;/constructor-arg&gt;
<br>&lt;/bean&gt; <br>{code} <br> <br>And add this to your
web.xml; <br>{code} <br>&lt;!-- Restlet Servlet --&gt; <br>&lt;servlet&gt;
<br>&lt;servlet-name&gt;RestletServlet&lt;/servlet-name&gt; <br>&lt;servlet-class&gt;org.restlet.ext.spring.SpringServerServlet&lt;/servlet-class&gt;
<br>&lt;init-param&gt; <br>&lt;param-name&gt;org.restlet.component&lt;/param-name&gt;
<br>&lt;param-value&gt;RestletComponent&lt;/param-value&gt; <br>&lt;/init-param&gt;
<br>&lt;/servlet&gt; <br>&lt;servlet-mapping&gt; <br>&lt;servlet-name&gt;RestletServlet&lt;/servlet-name&gt;
<br>&lt;url-pattern&gt;/rs/*&lt;/url-pattern&gt; <br>&lt;/servlet-mapping&gt;
<br>{code} <br>You will then be able to access the deployed route at http://localhost:8080/mywebapp/rs/demo/1234
where; <br> <br>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; <br>{code} <br>&quot;Request type : GET and ID : 1234&quot;
<br>{code} <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>
(<b>2.x or later</b>) </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>restletBindingRef</tt> (<b>1.x</b>),
<br class="atl-forced-newline" />  <tt>restletBinding=#</tt><em>refName</em>
(<b>2.x or later</b>) </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> (<b>2.x or later</b>)
</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>restletRealmRef</tt> (<b>1.x</b>),
<br class="atl-forced-newline" />  <tt>restletRealm=#</tt><em>refName</em>
<b>(2.x or later)</b> </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>
(<b>2.x or later</b>) </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-MessageHeaders"></a>Message Headers</h3>

<h4><a name="Restlet-Camel1.x"></a>Camel 1.x</h4>
<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>org.apache.camel.restlet.auth.login</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>org.apache.camel.restlet.auth.password</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>org.apache.camel.restlet.mediaType</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 <tt>content-type</tt> is set based on
the object type of the OUT message body. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>org.apache.camel.restlet.queryString</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>org.apache.camel.restlet.responseCode</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>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>
</tbody></table>
</div>
</div>

<h4><a name="Restlet-Camel2.x"></a>Camel 2.x</h4>
<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>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>
</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 <b>2.x</b>, use the
<tt>&#35;</tt> notation,  that is, <tt>restletRealm=#<em>refName</em></tt>)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%282.0orlater%29"></a>Single
restlet endpoint to service multiple methods and URI templates (2.0 or later)</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"</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-java">
&lt;camelContext&gt;
&lt;route id=<span class="code-quote">"RS_RestletDemo"</span>&gt;
&lt;from uri=<span class="code-quote">"restlet:/demo/{id}"</span> /&gt;
&lt;transform&gt;
&lt;simple&gt;Request type : ${header.CamelHttpMethod} and ID : ${header.id}&lt;/simple&gt;
&lt;/transform&gt;
&lt;/route&gt; 
&lt;/camelContext&gt;

&lt;bean id=<span class="code-quote">"RestletComponent"</span> class=<span
class="code-quote">"org.restlet.Component"</span> /&gt;
&lt;bean id=<span class="code-quote">"RestletComponentService"</span> class=<span
class="code-quote">"org.apache.camel.component.restlet.RestletComponent"</span>&gt;
&lt;constructor-arg index=<span class="code-quote">"0"</span>&gt;
&lt;ref bean=<span class="code-quote">"RestletComponent"</span> /&gt;
&lt;/constructor-arg&gt;
&lt;/bean&gt;
</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-java">
&lt;!-- Restlet Servlet --&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;RestletServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.restlet.ext.spring.SpringServerServlet&lt;/servlet-class&gt;
&lt;init-param&gt;
&lt;param-name&gt;org.restlet.component&lt;/param-name&gt;
&lt;param-value&gt;RestletComponent&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;RestletServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/rs/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</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>

    </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=25&originalVersion=24">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