Sample - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

In this sample, we define a route that exposes a HTTP service at http://localhost:8080/camel/services/hello. First, you need to publish the CamelHttpTransportServlet through the normal Web Container, or OSGi Service. Use the Web.xml file to publish the CamelHttpTransportServlet as follows:

<web-app>
   <servlet>
      <servlet-name>CamelServlet</servlet-name>
      <display-name>Camel Http Transport Servlet</display-name>
      <servlet-class>
         org.apache.camel.component.servlet.CamelHttpTransportServlet
      </servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>CamelServlet</servlet-name>
      <url-pattern>/services/*</url-pattern>
   </servlet-mapping>
</web-app>

Then you can define your route as follows:

from("servlet:///hello?matchOnUriPrefix=true").process(new Processor() {
   public void process(Exchange exchange)                     
      throws Exception {
      String contentType = 
         exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
      String path = 
         exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
      assertEquals("Got a wrong content type", CONTENT_TYPE, contentType);
      // assert Camel http header
      String charsetEncoding = exchange.getIn()
         .getHeader(Exchange.HTTP_CHARACTER_ENCODING, String.class);
      assertEquals("Got a wrong charset name from the message header", 
         "UTF-8", charsetEncoding);
      // assert exchange charset
      assertEquals("Got a wrong charset name from the exchange property", 
         "UTF-8", exchange.getProperty(Exchange.CHARSET_NAME));
      exchange.getOut().setHeader(Exchange.CONTENT_TYPE, contentType + 
         "; charset=UTF-8");                        
      exchange.getOut().setHeader("PATH", path);
      exchange.getOut().setBody("<b>Hello World</b>");
   }
});

Note

Since we are binding the Http transport with a published servlet, and we don't know the servlet's application context path, the camel-servlet endpoint uses the relative path to specify the endpoint's URL. A client can access the camel-servlet endpoint through the servlet publish address: ("http://localhost:8080/camel/services") + RELATIVE_PATH("/hello").

See the Camel Website for more examples of this component in use.