Tag Archives: Apache Camel - Page 3

Camel: Access incoming headers

In HTTP requests (and other requests or messages) you usually get a payload and a bunch of headers. In Camel you can find the payload in the “body” variable like this:

public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
           from("timer:mytimer?repeatCount=1")
                  .to("https://www.google.com")
                  .log("${body}");
    }
}

So, how do you access the headers? There is another variable for this called “in” and it can be used like this:

public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
           from("timer:mytimer?repeatCount=1")
                  .to("https://www.google.com")
                  .log("${in.headers}") // Log all incoming  headers
                  .log("${in.headers.Content-Type}"); // Log a specific header
    }
}

Tested on Ubuntu 20.04.4 LTS, Apache Camel 3.20, Minikube v1.29.0 (Camel-K) and Java 1.8.0_352

Camel-K: handle self-signed server certificates as a client

Companies sometimes use self-signed certificates internally in their systems. When building a Camel-K application we need to tell Camel to trust those certificates. I’m here going to show one solution in Java for this, using a truststore.

MyHTTPClient.java

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.support.jsse.*;

public class MyHTTPClient extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        registerSslContextParameter(); // 4.

        from("timer:mytimer?repeatCount=1")
            .to("https://my.server.com/person?
                       sslContextParameters=#mySSLContextParameters") // 5.
            .log("${body}");
    }

    private void registerSslContextParameter() throws Exception { // 1.
        KeyStoreParameters tsp = new KeyStoreParameters();
        tsp.setResource("/etc/ssl/truststore.jks"); // 2.
        tsp.setPassword("password");

        TrustManagersParameters tmp = new TrustManagersParameters();
        tmp.setKeyStore(tsp);

        SSLContextParameters sslContextParameters = new SSLContextParameters();
        sslContextParameters.setTrustManagers(tmp);

        this.getContext()
            .getRegistry()
                 .bind("mySSLContextParameters", sslContextParameters); // 3.
    }
}

The important parts:

  1. We need a place to create our SSL context – I like to put it in a separate function
  2. Path to the truststore that contain the self-signed certificate
  3. Register our new SSL context in the Camel register
  4. Call our function to set the new SSL context before our Camel flow
  5. Now we need to tell the Camel HTTP-component to use our new SSL context via the components url parameters

Run parameters

kamel run --resource file:truststore.jks@/etc/ssl/truststore.jks MyHTTPClient.java

Lastly we need to import the truststore into the Camle-K pod. Note that we place the truststore in /etc/ssl/ which is the same as above path (bullet point 2)

Tested on Apache Camel 3.19.0, Minikube v1.29.0 in Ubuntu 20.04 and Java 1.8.0_352

Camel + SpringBoot: How to use custom properties in routes

Spring and Camel makes this quite easy (same solution can be used in Camel-K too). Here is an example of the use of custom property in a route

appilcation.properties

# Greeting
greeting = Hello World

# Timer
timer.period = 2000

Example route with the above properties

from("timer:hello?period={{timer.period}}")
     .log("{{greeting}}");

The route will now print out “Hello World” every 2 seconds

Tested on Apache Camel 3.20.0, SpringBoot 2.7.6, Java 11.0.18 and Ubuntu 20.04.4 LTS