Restlet Component
The Restlet component provides Restlet based endpoints for consuming and producing RESTful resources.
Maven users will need to add the following dependency to their pom.xml
for this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-restlet</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
URI format
restlet:restletUrl[?options]
Format of restletUrl:
protocol://hostname[:port][/resourcePattern]
Restlet promotes decoupling of protocol and application concerns. The reference implementation of Restlet Engine 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.
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
Name |
Default Value |
Description |
---|---|---|
|
An instance of |
Use the |
|
An instance of |
The bean ID of a |
|
|
On a producer endpoint, specifies the request method to use. On a consumer endpoint, specifies that the endpoint consumes only |
|
None |
Consumer only Specify one or more methods separated by commas (e.g. |
|
|
The bean ID of the Realm Map in the Camel Registry. |
|
None |
Consumer only Specify one ore more URI templates to be serviced by a restlet consumer endpoint, using the |
|
true |
*Producer only * Throws exception on a producer failure. |
Message Headers
Camel 1.x
Name |
Type |
Description |
---|---|---|
|
|
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. |
|
|
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. |
|
|
Specifies the content type, which can be set on the OUT message by the application/processor. The value is the |
|
|
The query string of the request URI. It is set on the IN message by |
|
|
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. |
|
|
Attributes of a restlet message that get propagated to Camel IN headers. |
Camel 2.x
Name |
Type |
Description |
---|---|---|
|
|
Specifies the content type, which can be set on the OUT message by the application/processor. The value is the |
|
|
The HTTP request method. This is set in the IN message header. |
|
|
The query string of the request URI. It is set on the IN message by |
|
|
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. |
|
|
The HTTP request URI. This is set in the IN message header. |
|
|
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. |
|
|
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. |
|
|
Attributes of a Restlet message that get propagated to Camel IN headers. |
Message Body
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.
Samples
Restlet Endpoint with Authentication
The following route starts a restlet
consumer endpoint that listens for POST
requests on http://localhost:8080. The processor creates a response that echoes the request body and the value of the id
header.
The restletRealm
setting (in 2.x, use the #
notation, that is, restletRealm=#refName
)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 authenticated 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 restletRealmRef.
The following sample starts a direct
endpoint that sends requests to the server on http://localhost:8080 (that is, our restlet consumer endpoint).
That is all we need. We are ready to send a request and try out the restlet component:
The sample client sends a request to the direct:start-auth
endpoint with the following headers:
CamelRestletLogin
(used internally by Camel)CamelRestletPassword
(used internally by Camel)id
(application header)
Note
org.apache.camel.restlet.auth.login
and org.apache.camel.restlet.auth.password
will not be propagated as Restlet header.
The sample client gets a response like the following:
received [<order foo='1'/>] as an order id = 89531
Single restlet endpoint to service multiple methods and URI templates (2.0 or later)
It is possible to create a single route to service multiple HTTP methods using the restletMethods
option. This snippet also shows how to retrieve the request method from the header:
In addition to servicing multiple methods, the next snippet shows how to create an endpoint that supports multiple URI templates using the restletUriPatterns
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 restletUriPatterns
option will be honored.
The restletUriPatterns=#uriTemplates
option references the List<String>
bean defined in the Spring XML configuration.
<util:list id="uriTemplates"> <value>/users/{username}</value> <value>/atom/collection/{id}/component/{cid}</value> </util:list>