bhalkian
bhalkian

Reputation: 499

Creating http Client for Apache Camel to call certificate enabled webservice

i have following https client but it has deprecated methods in it . When i am trying to remove them I am not getting the response back . can you help me get this client without deprecated methods.

Deprecated methods: SSLSocketFactory SchemeRegistry Scheme getConnectionManager()

My Client :

HttpComponent httpComponent = context.getComponent("https4", HttpComponent.class);

    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(new FileInputStream(new File("XXXXXX")), "XXXXXX".toCharArray());
    //keystore.load(new FileInputStream(new File("C:/certificate/ToPankaj/clientcert.jks")), "changeit".toCharArray());

    KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyFactory.init(keystore, "changeit".toCharArray());


    SSLContext sslcontext = SSLContext.getInstance("SSLv3");
    sslcontext.init(keyFactory.getKeyManagers(), null, null);


    @SuppressWarnings("deprecation")
    SSLSocketFactory factory = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    @SuppressWarnings("deprecation")
    SchemeRegistry registry = new SchemeRegistry();

    @SuppressWarnings("deprecation")
    final Scheme scheme = new Scheme("https4", 443, factory);

    //registry.register(scheme);

    httpComponent.setHttpClientConfigurer(new HttpClientConfigurer() 
    {

        @SuppressWarnings("deprcecation")
        @Override
        public void configureHttpClient(HttpClient client) {
            client.getConnectionManager().getSchemeRegistry().register(scheme);
        }
    });

    httpComponent.setClientConnectionManager(new ThreadSafeClientConnManager());
    //httpComponent.
    Endpoint endpoint =httpComponent.createEndpoint(Uri);

    return endpoint;

Upvotes: 1

Views: 2527

Answers (1)

Peter Keller
Peter Keller

Reputation: 7636

Use the example from the Camel doc:

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.http.SSLContextParametersSecureProtocolSocketFactory;
import org.apache.camel.main.Main;
import org.apache.camel.util.jsse.KeyManagersParameters;
import org.apache.camel.util.jsse.KeyStoreParameters;
import org.apache.camel.util.jsse.SSLContextParameters;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;

public class SSLHttpClientRouteBuilder extends RouteBuilder {

    @Override
    public void configure() {
        final KeyStoreParameters ksp = new KeyStoreParameters();
        ksp.setResource("/users/home/server/keystore.jks");
        ksp.setPassword("keystorePassword");

        final KeyManagersParameters kmp = new KeyManagersParameters();
        kmp.setKeyStore(ksp);
        kmp.setKeyPassword("keyPassword");

        final SSLContextParameters scp = new SSLContextParameters();
        scp.setKeyManagers(kmp);

        final ProtocolSocketFactory factory = new SSLContextParametersSecureProtocolSocketFactory(scp);

        Protocol.registerProtocol("https", new Protocol("https", factory, 443));

        from("direct:start").to("https://mail.google.com/mail/").to("mock:results");
    }
}

Alternativelly, you can also directly use the Apache HTTP client as mentioned in the Camel doc

Upvotes: 2

Related Questions