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/myapp/myservice:

from("jetty:http://localhost:{{port}}/myapp/myservice").process(
   new MyBookService());

Note

When you specify localhost in a URL, Camel exposes the endpoint only on the local TCP/IP network interface, so it cannot be accessed from outside the machine it operates on.

If you need to expose a Jetty endpoint on a specific network interface, the numerical IP address of this interface should be used as the host. If you need to expose a Jetty endpoint on all network interfaces, the 0.0.0.0 address should be used.

Our business logic is implemented in the MyBookService class, which accesses the HTTP request contents and then returns a response. Note: The assert call appears in this example, because the code is part of an unit test.

public class MyBookService implements Processor {
   public void process(Exchange exchange) throws Exception {
      // just get the body as a string
      String body = exchange.getIn().getBody(String.class);
      // we have access to the HttpServletRequest here and we
      // can grab it if we need it
      HttpServletRequest req = 
         exchange.getIn().getBody(HttpServletRequest.class);
      assertNotNull(req);

      // for unit testing
      assertEquals("bookid=123", body);

      // send a html response
      exchange.getOut().setBody(
         "<html><body>Book 123 is Factory Patterns</body></html>");
   }
}

The following sample shows a content-based route that routes all requests containing the URI parameter, one, to the endpoint, mock:one, and all others to mock:other.

from("jetty:" + serverUri)
   .choice()
   .when().simple("in.header.one").to("mock:one")
   .otherwise()
   .to("mock:other");

So if a client sends the HTTP request, http://serverUri?one=hello , the Jetty component will copy the HTTP request parameter, one to the exchange's in.header. We can then use the Simple language to route exchanges that contain this header to a specific endpoint and all others to another. If we used a language more powerful than Simple (such as EL or OGNL) we could also test for the parameter value and do routing based on the header value as well.