Camel Component: Servlet - 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

The servlet: component provides HTTP based endpoints for consuming HTTP requests that arrive at a HTTP endpoint and this endpoint is bound to a published Servlet.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-servlet</artifactId>
   <!-- use the same version as your Camel core version -->
   <version>x.x.x</version>
</dependency>

Servlet is stream based, which means the input it receives is submitted to Camel as a stream. That means you will only be able to read the content of the stream once. If you find a situation where the message body appears to be empty or you need to access the data multiple times (eg: doing multicasting, or redelivery error handling) you should use Stream Caching or convert the message body to a String which is safe to be read multiple times.

URI format and options

servlet://relative_path[?options]

You can append query options to the URI in the following format, ?option=value&option=value&..., where option can be:

Table 19. 

Name

Default Value

Description

httpBindingRef

null

Reference to an Camel HttpBinding object in the Registry. A HttpBinding implementation can be used to customize how to write a response.

matchOnUriPrefix

false

Whether or not the CamelServlet should try to find a target consumer by matching the URI prefix, if no exact match is found.

servletName

CamelServlet

Specifies the servlet name that the servlet endpoint will bind to. This name should match the name you define in web.xml file.


Message Headers

Camel will apply the same Message Headers as the Camel Component: HTTP4 component.

Camel will also populate all request.parameter and request.headers. For example, if a client request has the URL, http://myserver/myserver?orderid=123 , the exchange will contain a header named orderid with the value 123.

Usage

You can only consume from endpoints generated by the Servlet component. Therefore, it should only be used as input into your Camel routes. To issue HTTP requests against other HTTP endpoints, use the HTTP4 Component.

Sample

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.