Restlet has been edited by William Tam (Jun 14, 2009).

(View changes)

Content:

Restlet Component

The Restlet component provides Restlet based endpoints for consuming and producing RESTful resources.

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.


Options

Name Default Value Description
headerFilterStrategy=#refName (2.x or later) An instance of RestletHeaderFilterStrategy object Use the # notation (headerFilterStrategy=#refName) to reference a header filter strategy in the Camel Registry. The strategy will be plugged into the Restlet Binding if it is HeaderFilterStrategyAware.
restletBindingRef (1.x). For 2.x or later, use restletBinding=#refName An instance of DefaultRestletBinding object The name to lookup the RestletBinding object in the Camel Registry.
restletMethod GET The method to be used in the request for producer. For consumer, the Restlet Endpoint only consumes requests made by restletMethod. The string value is converted to org.restlet.data.Method by the Method.valueOf(String) method.
restletMethods (2.x or later) none This option is only valid for consumer. Specify one or more methods separated by commas (e.g. restletMethods=post,put) to be serviced by a Restlet endpoint. If both "restletMethod" and "restletMethods" options are specified, the value of restletMethod is ignored.
restletRealmRef (1.x). For 2.x or later use restletRealm=#refName null The name to lookup the Realm Map in the Camel Registry.
restletUriPatterns=#refName (2.x or later) none This option is only valid for consumer. Specify one ore more uri templates to be serviced by a Restlet endpoint using the # notation to reference a List (of String) in Camel Registry. If an URI pattern has been defined in the endpoint URI, both the URI pattern defined in endpoint and the restletUriPatterns option will be honored.

Message Headers

Camel 1.x

Name Type Description
org.apache.camel.restlet.auth.login String Login name for basic authentication. It is set on the IN message by application and get filtered before Restlet Request header by Camel.
org.apache.camel.restlet.auth.password String Password name for basic authentication. It is set on the IN message by application and get filtered before Restlet Request header by Camel.
org.apache.camel.restlet.mediaType String It can be set on the OUT message by application/processor. The value is the content-type of the response message. If this header is not set, content-type will be set based on the object type of the out message body.
org.apache.camel.restlet.queryString String The query string of the request URI. It is set on the IN message by DefaultRestletBinding when the Restlet Component receives a request.
org.apache.camel.restlet.responseCode String or Integer It can be set on the OUT message by application/processor. The value is the response code of the response message. If this header is not set, response code will be set by Restlet runtime engine.
org.restlet.*   Attributes of a Restlet message that get propagated to Camel IN headers.

Camel 2.0

Name Type Description
CamelContentType String It can be set on the OUT message by application/processor. The value is the content-type of the response message. If this header is not set, content-type will be set based on the object type of the out message body.
CamelHttpMethod String The HTTP request method. This is et in the IN message header.
CamelHttpQuery String The query string of the request URI. It is set on the IN message by DefaultRestletBinding when the Restlet Component receives a request.
CamelHttpRsponseCode String or Integer It can be set on the OUT message by application/processor. The value is the response code of the response message. If this header is not set, response code will be set by Restlet runtime engine.
CamelHttpUri String The HTTP request URI. This is set in the IN message header.
CamelRestletLogin String Login name for basic authentication. It is set on the IN message by application and get filtered before Restlet Request header by Camel.
CamelRestletPassword String Password name for basic authentication. It is set on the IN message by application and get filtered before Restlet Request header by Camel.
org.restlet.*   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 headers is preserved during routing.

Samples

Restlet Endpoint with Authentication

This sample starts a Restlet consumer endpoint that listens POST requests on http://localhost:8080. The route looks like follow. The consumer invokes a processor that creates a response to echo the request body and the value of header "id".

from("restlet:http://localhost:9080/securedOrders?restletMethod=post&restletRealm=#realm").process(new Processor() {
    public void process(Exchange exchange) throws Exception {
        exchange.getOut().setBody(
                "received [" + exchange.getIn().getBody()
                + "] as an order id = "
                + exchange.getIn().getHeader("id"));
    }
});

The restletRealmRef (in 2.x, use the # notation. i.e. restletRealm=#refName)in URI query is an option to lookup a Realm Map in the registry. If this option is specified, the Restlet consumer will use 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.

<util:map id="realm">
	<entry key="admin" value="foo" />
	<entry key="bar" value="foo" />
</util:map>

The sample starts a direct endpoint that sends requests to the server on http://localhost:8080 (i.e. our Restlet consumer endpoint).

// Note: restletMethod and restletRealmRef are stripped 
// from the query before a request is sent as they are 
// only processed by Camel.
from("direct:start-auth").to("restlet:http://localhost:9080/securedOrders?restletMethod=post");

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 "direct:start-auth" endpoint with the following headers. Notice that org.apache.camel.restlet.auth.login and org.apache.camel.restlet.auth.password will not be propagated as Restlet header.

  • CamelRestletLogin (internally used by Camel)
  • CamelRestletPassword (internally used by Camel)
  • id ("application" header)
final String id = "89531";

Map<String, Object> headers = new HashMap<String, Object>();
headers.put(RestletConstants.RESTLET_LOGIN, "admin");
headers.put(RestletConstants.RESTLET_PASSWORD, "foo");
headers.put("id", id);

String response = (String) template.requestBodyAndHeaders("direct:start-auth", 
        "<order foo='1'/>", headers);

The sample client will get a response:

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.

from("restlet:http://localhost:9080/users/{username}?restletMethods=post,get")
    .process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            // echo the method
            exchange.getOut().setBody(exchange.getIn().getHeader(Exchange.HTTP_METHOD,
                                                                 String.class));

        }
    });

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

from("restlet:http://localhost:9080?restletMethods=post,get&uriPatterns=#uriTemplates")
    .process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            // echo the method
            String uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
            String out = exchange.getIn().getHeader(Exchange.HTTP_METHOD, String.class);
            if ("http://localhost:9080/users/homer".equals(uri)) {
                exchange.getOut().setBody(out + " " + exchange.getIn().getHeader("username", String.class));
            } else if ("http://localhost:9080/atom/collection/foo/component/bar".equals(uri)) {
                exchange.getOut().setBody(out + " " + exchange.getIn().getHeader("id", String.class)
                                          + " " + exchange.getIn().getHeader("cid", String.class));
                                          
            }

        }
    });

The restletUriPatterns=#uriTemplates option references to the List bean (of String) defined in the Spring configuration.

<util:list id="uriTemplates">
	<value>/users/{username}</value>
	<value>/atom/collection/{id}/component/{cid}</value>
</util:list>

Reply via email to