Skip to content
Advertisement

Create http and https endpoint using camel in the same server with jetty

I am trying to create HTTP and HTTPS endpoint in one of my web service. I want secure few endpoints with HTTPS and other endpoints with plain HTTP.

I am using the below code to do the same.

    public void configure() {
        configureJetty();
        configureHttp4();
        //This works with configuring Jetty
        from("jetty:https://0.0.0.0:8085/sample1/?matchOnUriPrefix=true")
                .to("file://./?fileName=out.csv");
        //This url does not working with the configuring the jetty with configure jetty this works.
        from("jetty:http://0.0.0.0:8084/sample2/?matchOnUriPrefix=true")
                .to("file://./?fileName=out2.csv");
    }

private void configureJetty() {
        KeyStoreParameters ksp = new KeyStoreParameters();
        ksp.setResource("./trustStore.jks");
        ksp.setPassword("someSecretPassword");
        KeyManagersParameters kmp = new KeyManagersParameters();
        kmp.setKeyStore(ksp); kmp.setKeyPassword("someSecretPassword");
        SSLContextParameters scp = new SSLContextParameters();
        scp.setKeyManagers(kmp);
        JettyHttpComponent jettyComponent = getContext().getComponent("jetty", JettyHttpComponent.class);
        jettyComponent.setSslContextParameters(scp);
    }

The https works fine with this setup but the http endpoint does not work. If I remove the method call to configure Jetty the HTTP endpoint works. How I can configure both in the same server? I can not use spring boot but only plain camel components.

I have created a github repository with the sample code. You can find it here. sample code

Advertisement

Answer

You can

  • create two distinct instances of jetty component, one for plain http, the other for https.
  • register each of them with a specific alias (“jetty” and “jettys”)
  • use appropriate alias in your endpoint uris “from(“jettys:…”)

CDI Example:

@Produces
@ApplicationScoped 
@Named("jetty")
public final JettyHttpComponent createJettyComponent1() {       
    return this.configureJetty(false);
}

@Produces
@ApplicationScoped 
@Named("jettys")
public final JettyHttpComponent createJettyComponent2() {       
    return this.configureJetty(true);
}  

private void configureJetty(boolean ssl) {
   ...
    JettyHttpComponent jettyComponent = new JettyHttpComponent();
    if (ssl) {
        jettyComponent.setSslContextParameters(scp);
    }   
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement