DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Building a Sentiment Analysis Pipeline With Apache Camel and Deep Java Library (DJL)
  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Powering LLMs With Apache Camel and LangChain4j

Trending

  • A Hands-On ABAP RESTful Programming Model Guide
  • A System Cannot Protect What It Does Not Understand
  • Multi-Scale Feature Learning in CNN and U-Net Architectures
  • The Agentic Agile Office: Streamlining Enterprise Agile With Autonomous AI Agents
  1. DZone
  2. Coding
  3. Frameworks
  4. Apache Camel SSL on http4

Apache Camel SSL on http4

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Jun. 29, 15 · Interview
Likes (2)
Comment
Save
Tweet
Share
22.8K Views

Join the DZone community and get the full member experience.

Join For Free

When creating a camel route using http, the destination might require a ssl connection with a self signed certificate.

Therefore on our http client we should register a TrustManager that suports the certificate.

In our case we will use the https4 component of Apache Camel
Therefore we should configure the routes and add them to the camel context

RouteBuilder routeBuilder = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("http://localhost")
                        .to("https4://securepage");
            }
        };
routeBuilder.addRoutesToCamelContext(camelContext);

But before we proceed on starting the camel context we should register the trust store on the component we are going to use.
Therefore we should implement a function for creating an ssl context with the trustore.
Supposed the jks file that has the certificate imported is located on the root of our classpath.

private void registerTrustStore(CamelContext camelContext) {

     try {
         KeyStore truststore = KeyStore.getInstance("JKS");
         truststore.load(getClass().getClassLoader().getResourceAsStream("example.jks"), "changeit".toCharArray());

         TrustManagerFactory trustFactory = TrustManagerFactory.getInstance("SunX509");
         trustFactory.init(truststore);

         SSLContext sslcontext = SSLContext.getInstance("TLS");
         sslcontext.init(null, trustFactory.getTrustManagers(), null);

         SSLSocketFactory factory = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

         SchemeRegistry registry = new SchemeRegistry();
         final Scheme scheme = new Scheme("https4", 443, factory);
         registry.register(scheme);


         HttpComponent http4 = camelContext.getComponent("https4", HttpComponent.class);
         http4.setHttpClientConfigurer(new HttpClientConfigurer() {

             @Override
             public void configureHttpClient(HttpClientBuilder builder) {

                 builder.setSSLSocketFactory(factory);

                 Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                         .register("https", factory)
                         .build();

                 HttpClientConnectionManager connectionManager = new  BasicHttpClientConnectionManager(registry);

                 builder.setConnectionManager(ccm);
             }
         });
     } catch (IOException e) {
         e.printStackTrace();
     } catch (NoSuchAlgorithmException e) {
         e.printStackTrace();
     } catch (CertificateException e) {
         e.printStackTrace();
     } catch (KeyStoreException e) {
         e.printStackTrace();
     } catch (KeyManagementException e) {
         e.printStackTrace();
     }
 }

After that our route would be able to access the destination securely.

Apache Camel

Published at DZone with permission of Emmanouil Gkatziouras. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Building a Sentiment Analysis Pipeline With Apache Camel and Deep Java Library (DJL)
  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Powering LLMs With Apache Camel and LangChain4j

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook