Skip to content
Snippets Groups Projects
Commit ea29eefb authored by Jawnnypoo's avatar Jawnnypoo
Browse files

Optimize init where it only creates parts of the Okhttpclient that it needs

parent 57169d9e
No related branches found
No related tags found
No related merge requests found
Pipeline #
Loading
Loading
@@ -38,7 +38,8 @@ import timber.log.Timber;
public class App extends Application {
 
/**
* Register our type converters on our singleton LoganSquare create
* Register our type converters on our singleton LoganSquare instance. Needs to be set here
* since we are fetching accounts immediately with LoganSquare
*/
static {
LoganSquare.registerTypeConverter(Uri.class, new UriTypeConverter());
Loading
Loading
@@ -134,29 +135,32 @@ public class App extends Application {
 
public void setAccount(Account account) {
mAccount = account;
initGitLab(account);
initGitLabRss(account);
initPicasso(account);
}
private void initGitLab(Account account) {
OkHttpClient.Builder gitlabClientBuilder = OkHttpClientFactory.create(account);
OkHttpClient.Builder clientBuilder = OkHttpClientFactory.create(account);
if (BuildConfig.DEBUG) {
gitlabClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
clientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
}
mGitLab = GitLabFactory.create(account, gitlabClientBuilder.build());
}
private void initGitLabRss(Account account) {
OkHttpClient.Builder gitlabRssClientBuilder = OkHttpClientFactory.create(account);
OkHttpClient client = clientBuilder.build();
initGitLab(account, client);
initGitLabRss(account, client);
//This is kinda weird, but basically, I don't want to see all the annoying logs from bitmap
//decoding since the Okhttpclient is going to log everything, but it does not matter in release
//builds, and will actually speed up the init time to share the same client between all these
if (BuildConfig.DEBUG) {
gitlabRssClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
initPicasso(OkHttpClientFactory.create(account).build());
} else {
initPicasso(client);
}
mGitLabRss = GitLabRssFactory.create(account, gitlabRssClientBuilder.build());
}
 
private void initPicasso(Account account) {
OkHttpClient.Builder clientBuilder = OkHttpClientFactory.create(account);
mPicasso = PicassoFactory.createPicasso(clientBuilder.build());
private void initGitLab(Account account, OkHttpClient client) {
mGitLab = GitLabFactory.create(account, client);
}
private void initGitLabRss(Account account, OkHttpClient client) {
mGitLabRss = GitLabRssFactory.create(account, client);
}
private void initPicasso(OkHttpClient client) {
mPicasso = PicassoFactory.createPicasso(client);
}
}
Loading
Loading
@@ -14,23 +14,34 @@ public final class OkHttpClientFactory {
 
/**
* Creates an {@link OkHttpClient} configured with the account configuration
*
* @param account the account
* @return a configured okhttpclient
* @return a configured {@link okhttp3.OkHttpClient.Builder}
*/
public static OkHttpClient.Builder create(Account account) {
CustomTrustManager customTrustManager = new CustomTrustManager();
customTrustManager.setTrustedCertificate(account.getTrustedCertificate());
customTrustManager.setTrustedHostname(account.getTrustedHostname());
customTrustManager.setPrivateKeyAlias(account.getPrivateKeyAlias());
CustomTrustManager customTrustManager = null;
//Do we even need a custom trust manager?
if (account.getTrustedCertificate() != null
|| account.getTrustedHostname() != null
|| account.getPrivateKeyAlias() != null) {
customTrustManager = new CustomTrustManager();
customTrustManager.setTrustedCertificate(account.getTrustedCertificate());
customTrustManager.setTrustedHostname(account.getTrustedHostname());
customTrustManager.setPrivateKeyAlias(account.getPrivateKeyAlias());
}
OpenSignInAuthenticator authenticator = new OpenSignInAuthenticator(account);
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.sslSocketFactory(customTrustManager.getSSLSocketFactory())
.hostnameVerifier(customTrustManager.getHostnameVerifier())
.authenticator(authenticator)
.proxyAuthenticator(authenticator);
clientBuilder.addInterceptor(new AuthenticationRequestInterceptor(account));
.proxyAuthenticator(authenticator)
.addInterceptor(new AuthenticationRequestInterceptor(account));
//Only apply these custom things when needed, since they slow down the init
if (customTrustManager != null && customTrustManager.getSSLSocketFactory() != null) {
clientBuilder.sslSocketFactory(customTrustManager.getSSLSocketFactory(), X509TrustManagerProvider.get());
}
if (customTrustManager != null && customTrustManager.getHostnameVerifier() != null) {
clientBuilder.hostnameVerifier(customTrustManager.getHostnameVerifier());
}
return clientBuilder;
}
}
package com.commit451.gitlab.api;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
/**
* Gets the X509TrustManager on the system and caches it
*/
public class X509TrustManagerProvider {
private static X509TrustManager sX509TrustManager;
/**
* Get the static {@link X509TrustManager} for the system
* @return the static instance
*/
public static X509TrustManager get() {
if (sX509TrustManager == null) {
try {
init();
} catch (Exception any) {
//If they don't have X509 trust manager, they have bigger problems
throw new RuntimeException(any);
}
}
return sX509TrustManager;
}
private static void init() throws NoSuchAlgorithmException, KeyStoreException {
TrustManagerFactory factory = TrustManagerFactory.getInstance("X509");
factory.init((KeyStore) null);
TrustManager[] trustManagers = factory.getTrustManagers();
if (trustManagers != null) {
for (TrustManager trustManager : trustManagers) {
if (trustManager instanceof X509TrustManager) {
sX509TrustManager = (X509TrustManager) trustManager;
break;
}
}
}
}
}
Loading
Loading
@@ -11,9 +11,11 @@ import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
 
/**
* Custom SSL factory so that we can enforce using TLS 1.2 on Android 4.1-4.4
* Custom SSL factory so that we can enforce using TLS 1.2 on Android 4.1-4.4 where it is not
* enabled by default and also have custom trusted https servers
*/
public class CustomSSLSocketFactory extends SSLSocketFactory {
/**
* You may be wondering why this is named "delegate" which seems to break the convention
* of the rest of the project. See here for deets:
Loading
Loading
@@ -23,7 +25,6 @@ public class CustomSSLSocketFactory extends SSLSocketFactory {
 
public CustomSSLSocketFactory(SSLSocketFactory internalFactory) {
super();
this.delegate = internalFactory;
}
 
Loading
Loading
package com.commit451.gitlab.ssl;
 
import timber.log.Timber;
import com.commit451.gitlab.api.X509TrustManagerProvider;
 
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
 
Loading
Loading
@@ -13,32 +10,13 @@ import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
 
/**
* Allows for custom configurations, such as custom trusted hostnames, custom trusted certificates,
* and private keys
*/
public class CustomTrustManager implements X509TrustManager {
private static X509TrustManager sDefaultTrustManager;
static {
try {
TrustManagerFactory factory = TrustManagerFactory.getInstance("X509");
factory.init((KeyStore) null);
TrustManager[] trustManagers = factory.getTrustManagers();
if (trustManagers != null) {
for (TrustManager trustManager : trustManagers) {
if (trustManager instanceof X509TrustManager) {
sDefaultTrustManager = (X509TrustManager) trustManager;
break;
}
}
}
} catch (NoSuchAlgorithmException e) {
Timber.e(e, "Unable to get X509 TrustManager");
} catch (KeyStoreException e) {
Timber.e(e, "Exception while initializing CustomTrustManager");
}
}
 
private String mTrustedCertificate;
private String mTrustedHostname;
Loading
Loading
@@ -77,22 +55,22 @@ public class CustomTrustManager implements X509TrustManager {
 
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
if (sDefaultTrustManager == null) {
if (X509TrustManagerProvider.get() == null) {
throw new IllegalStateException("No default TrustManager available");
}
 
sDefaultTrustManager.checkClientTrusted(chain, authType);
X509TrustManagerProvider.get().checkClientTrusted(chain, authType);
}
 
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
if (sDefaultTrustManager == null) {
if (X509TrustManagerProvider.get() == null) {
throw new IllegalStateException("No default TrustManager available");
}
 
CertificateException cause;
try {
sDefaultTrustManager.checkServerTrusted(chain, authType);
X509TrustManagerProvider.get().checkServerTrusted(chain, authType);
return;
} catch (CertificateException e) {
cause = e;
Loading
Loading
@@ -107,11 +85,11 @@ public class CustomTrustManager implements X509TrustManager {
 
@Override
public X509Certificate[] getAcceptedIssuers() {
if (sDefaultTrustManager == null) {
if (X509TrustManagerProvider.get() == null) {
throw new IllegalStateException("No default TrustManager available");
}
 
return sDefaultTrustManager.getAcceptedIssuers();
return X509TrustManagerProvider.get().getAcceptedIssuers();
}
 
public SSLSocketFactory getSSLSocketFactory() {
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment