4

I have Camel route that I would like to expose as a REST Web Service. Application is deployed on Web container (Jetty/Tomcat) and Spring is used as well for DI and other "infrastructural" things.

I took a look at both camel-restlet and camel-cxfrs components and while they both provide support for exposing routes as REST services I was not able to find out how to avoid starting of separate server. What I'm really looking for is ability to define Camel route in a similar way to how routes are defined for Spring-WS inbound endpoints, e.g.

from("restlet://application/user/{id}").to(...)

Configuration of Web application should take care of accepting requests and transferring them to appropriate endpoints.

Have to admit I was pretty much surprised that I was not able to find sufficient info on the topic and I don't think that my requirements are very exotic.

oiavorskyi
  • 2,893
  • 1
  • 20
  • 23

2 Answers2

5

See this example http://camel.apache.org/cxf-tomcat-example.html

For Apache CXF you can use the servlet transport which allows you to tap into Tomcat/Jetty being the host container.

And if you use OSGi then take a look at this: http://camel.apache.org/cxf-example-osgi.html it shows how to use CXF with OSGi HTTP service, that should work as well for CXFRS.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Claus, thank you for pointing this out. I was able to figure out how to achieve my goal. Unfortunately I feel that I will have to avoid using camel-cxfrs altogether. While I'm a big fan of Camel I found REST integration to be very unintuitive and cumbersome. Looks like I will just use CXF-RS capabilities directly and call Camel routes explicitly from JAX-RS annotated class. – oiavorskyi Apr 15 '11 at 15:06
  • P.S. Would be nice to see chapter dedicated to REST WS in next edition of Camel in Action – oiavorskyi Apr 15 '11 at 15:07
  • 1
    The camel-restlet is IMHO easier and more lightweight, its based on the resetlet.org REST framework. And yeah I think camel-cxf/cxfrs needs better documentation. And also Apache CXF is a tad to big/complicated framework. I would like if we added a camel-jersey which seems like a nice REST framework. – Claus Ibsen Apr 15 '11 at 17:18
  • Yeah, camel-restlet looks like good fit for my needs but inability to find information on how to expose services without starting separate server drives me crazy. – oiavorskyi Apr 15 '11 at 17:35
3

This is a late answer but may help others.

Apache Camel now appears to have support for exposing a Restlet web service using the host container (eg. Tomcat/Jetty)

==============8< snip snip ========================

Using the Restlet servlet within a webapp

Available as of Camel 2.8 There are three possible ways to configure a Restlet application within a servlet container and using the subclassed SpringServerServlet enables configuration within Camel by injecting the Restlet Component. 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). To configure, add the following to your camel-context.xml;

<camelContext>
  <route id="RS_RestletDemo">
    <from uri="restlet:/demo/{id}" />
    <transform>
      <simple>Request type : ${header.CamelHttpMethod} and ID : ${header.id}</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 add this to your web.xml;
<!-- Restlet Servlet -->
<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>

You will then be able to access the deployed route at

http://localhost:8080/mywebapp/rs/demo/1234

where localhost:8080 is the server and port of your servlet container

============== snip snip >8 ========================

This information was found at the bottom of http://camel.apache.org/restlet.html on Jan 16, 2014

whitestryder
  • 451
  • 5
  • 10