Java tutorial
/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mifos.tools.provider; import com.squareup.okhttp.OkHttpClient; import org.mifos.tools.util.PPIUploaderConstant; import org.springframework.stereotype.Component; import retrofit.RestAdapter; import retrofit.client.OkClient; import javax.net.ssl.*; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Properties; @Component public class RestAdapterProvider { public RestAdapter get(final Properties properties) { return new RestAdapter.Builder().setEndpoint(properties.getProperty(PPIUploaderConstant.PROPERTY_URI)) .setClient(new OkClient(this.createClient())).build(); } private OkHttpClient createClient() { final OkHttpClient client = new OkHttpClient(); final TrustManager[] certs = new TrustManager[] { new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { } @Override public void checkClientTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { } } }; SSLContext ctx = null; try { ctx = SSLContext.getInstance("TLS"); ctx.init(null, certs, new SecureRandom()); } catch (final java.security.GeneralSecurityException ex) { // do nothing, ignore } try { final HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(final String hostname, final SSLSession session) { return true; } }; client.setHostnameVerifier(hostnameVerifier); client.setSslSocketFactory(ctx.getSocketFactory()); } catch (final Exception e) { // do nothing, ignore } return client; } }