Frage

As far as I understand, Camel by default uses the default RESTLET engine. How can I tell Camel to use Jetty instead?

I know there are ways to bundle your application and deploy it in Tomcat or Jetty. If I do that, however, the biggest question becomes how to integrate with RESTLET.

I did some further digging. I took one of the tomcat examples that come with the camel. Then I tried to make it RESTLET capable. It almost works. The problem now is that the parameters are not being passed to the route. I would expect that when calling this server: http://x.x.x.x:8080/rs/user/?name=Paul, I would get: Hello Paul how are you?

However, I simply get: Hello how are you?

My camel configuration is:

    <camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="restlet:/user/?restletMethods=GET,POST" />
        <transform>
            <simple>Hello ${header.name} how are you?</simple>
        </transform>
    </route>
</camelContext>
<bean id="RestletComponent" class="org.restlet.Component" />
<bean id="RestletComponentService" class="org.apache.camel.component.restlet.RestletComponent">
    <constructor-arg index="0">
        <ref bean="RestletComponent" />
    </constructor-arg>
</bean>

And my web.xml is:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:camel-config.xml</param-value>
</context-param>

<servlet>
    <servlet-name>RestletServlet</servlet-name>
    <servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
    <init-param>
        <param-name>org.restlet.component</param-name>
        <param-value>RestletComponent</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>RestletServlet</servlet-name>
    <url-pattern>/rs/*</url-pattern>
</servlet-mapping>

I am getting a little bit closer with my investigation. It appears that ${in.body} will indeed pass the body of the request to the route. I am still trying to find out what is going on with the header. I even tried using the producer template to call the headers, still it does work. So, this somehow suggests that the way to access the header in RESTLET is different than in the pure servlet? Here is my producer template:

 Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("name","Paul");

    context.createProducerTemplate().requestBodyAndHeaders(
            "restlet:http://localhost:8080/camel-example-servlet-tomcat/rs/user/?restletMethod=post", "Halleluia",headers);
War es hilfreich?

Lösung

Have a look at the org.apache.camel.component.restlet.DefaultRestletBinding class. By default form data is only bound when content type is "application/x-www-form-urlencoded". Also, only URI template values will be bound to headers.

To get at arbitrary query parameters you can do something like:

Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
String value = request.getResourceRef().getQueryAsForm().getFirstValue("foo");

Also, the raw query string will be available in the CamelHttpQuery header.

You can supply your own class which implements RestletBinding (or extend DefaultRestletBinding) by specifying the bean ID of a RestletBinding object in the Camel Registry using the 'restletBinding' endpoint query option. If you are using Spring register a bean like this:

    <bean id="myRestletBinding" class="com.example.MyRestletBinding"/>

Andere Tipps

OK. It appears I had to access the URL parameter with in.header.name instead of header.name. I have no explanation as to why header.name would work in the normal servlet. Also, I managed to get this working only with POST. With GET, the parameters are still not passed to the RESTLET engine or I do not know how to retrieve them. On the contrary, GET works fine when using pure servlet. Solution is good for me and I thus mark this as answered.

I would suggest you to use camel CXFRS component. Have a service class where all REST annotations are declared. You can have the username as path parameter or query parameter.

Syntax would be something like this :

from("cxfrs://http://localhost:9992/service?resourceClasses=com.xyz.web.resources.RESTresource")

REST resource would be your resource class. This will run a REST web service over a jetty web server.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top