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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC
  • Microservices With Apache Camel and Quarkus (Part 5)
  • Microservices With Apache Camel and Quarkus (Part 3)
  • Microservices With Apache Camel and Quarkus (Part 2)

Trending

  • Build Retrieval-Augmented Generation (RAG) With Milvus
  • Java 23: What Developers Need to Know
  • Boosting Efficiency: Implementing Natural Language Processing With AWS RDS Using CloudFormation
  • 12 Expert Tips for Secure Cloud Deployments
  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.0K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC
  • Microservices With Apache Camel and Quarkus (Part 5)
  • Microservices With Apache Camel and Quarkus (Part 3)
  • Microservices With Apache Camel and Quarkus (Part 2)

Partner Resources


Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: