OKhttp url factory

This commit is contained in:
Michal Kepkowski
2016-04-06 19:10:00 +02:00
parent 6c2efb0235
commit dac5f1db08
2 changed files with 26 additions and 8 deletions

View File

@@ -17,13 +17,18 @@ public class OkHttpClientFactory {
public static OkHttpClient getSimpleClient(){ public static OkHttpClient getSimpleClient(){
if(client == null){ if(client == null){
client = new OkHttpClient.Builder().build(); client = new OkHttpClient.Builder()
.connectTimeout(30000, TimeUnit.MILLISECONDS)
.readTimeout(45000, TimeUnit.MILLISECONDS)
.build();
} }
return client; return client;
} }
public static OkHttpClient getPinnedSimpleClient(CertificatePinner pinner){ public static OkHttpClient getPinnedSimpleClient(CertificatePinner pinner){
return new OkHttpClient.Builder() return new OkHttpClient.Builder()
.connectTimeout(30000, TimeUnit.MILLISECONDS)
.readTimeout(45000, TimeUnit.MILLISECONDS)
.certificatePinner(pinner) .certificatePinner(pinner)
.build(); .build();
} }

View File

@@ -21,32 +21,45 @@ package org.sufficientlysecure.keychain.util;
import com.textuality.keybase.lib.KeybaseUrlConnectionClient; import com.textuality.keybase.lib.KeybaseUrlConnectionClient;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.OkUrlFactory;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import java.io.IOException; import java.io.IOException;
import java.net.Proxy; import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;
/** /**
* Wrapper for Keybase Lib * Wrapper for Keybase Lib
*/ */
public class OkHttpKeybaseClient implements KeybaseUrlConnectionClient { public class OkHttpKeybaseClient implements KeybaseUrlConnectionClient {
private OkUrlFactory generateUrlFactory() {
OkHttpClient client = new OkHttpClient();
return new OkUrlFactory(client);
}
@Override @Override
public OkHttpClient getClient(URL url, Proxy proxy, boolean pin) throws IOException { public URLConnection openConnection(URL url, Proxy proxy, boolean isKeybase) throws IOException {
OkHttpClient client = OkHttpClientFactory.getSimpleClient();
OkUrlFactory factory = generateUrlFactory();
try { try {
if(pin) { if (isKeybase && proxy != null) {
return OkHttpClientFactory.getPinnedClient(url, proxy); client = OkHttpClientFactory.getPinnedClient(url, proxy);
}else{ } else if (proxy != null) {
return OkHttpClientFactory.getClient( proxy); client = OkHttpClientFactory.getClient(proxy);
} else {
client = OkHttpClientFactory.getSimpleClient();
} }
} catch (IOException e) {
throw new IOException("no pinned certificate found for URL!");
} catch (TlsHelper.TlsHelperException e) { } catch (TlsHelper.TlsHelperException e) {
Log.e(Constants.TAG, "TlsHelper failed", e); Log.e(Constants.TAG, "TlsHelper failed", e);
throw new IOException("TlsHelper failed"); throw new IOException("TlsHelper failed");
} }
factory.setClient(client);
return factory.open(url);
} }
@Override @Override