From 92be713a7a41911b3f687e86cea7b1f8b12b12cd Mon Sep 17 00:00:00 2001 From: liuyongkui Date: Tue, 18 Jul 2017 16:35:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E4=BF=A1=E4=BB=BB=E6=89=80=E6=9C=89ht?= =?UTF-8?q?tps=20url=E7=9A=84=E6=8E=A5=E5=8F=A3=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E8=BF=9B=E4=B8=80=E6=AD=A5=E4=BC=98=E5=8C=96=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/tamic/novate/Novate.java | 72 +++++++++++++++--- .../com/tamic/novate/NovateHttpsFactroy.java | 76 ++++++++++++++----- 2 files changed, 116 insertions(+), 32 deletions(-) diff --git a/novate/src/main/java/com/tamic/novate/Novate.java b/novate/src/main/java/com/tamic/novate/Novate.java index 3fd2184..9e56cbb 100644 --- a/novate/src/main/java/com/tamic/novate/Novate.java +++ b/novate/src/main/java/com/tamic/novate/Novate.java @@ -123,6 +123,8 @@ public final class Novate { private Map> downMaps = new HashMap>() { }; private Observable.Transformer exceptTransformer = null; + + public static final String KEY_CACHE = "Novate_Http_cache"; private static final int DEFAULT_TIMEOUT = 15; private static final int DEFAULT_MAXIDLE_CONNECTIONS = 5; private static final long DEFAULT_KEEP_ALIVEDURATION = 8; @@ -152,7 +154,6 @@ public final class Novate { * create ApiService */ public T create(final Class service) { - return retrofit.create(service); } @@ -181,16 +182,26 @@ public T call(Observable observable, ResponseCallback callback) { .subscribe(new RxSubscriber(observable.getClass().getSimpleName(), callback)); } + /*public Observable compose() { return schedulersIo(okhttpBuilder); } */ + + /** + * Thread IO + * @param observable + */ public Observable schedulersIo(Observable observable) { return observable.subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(Schedulers.io()); } + /** + * Thread mainThread + * @param observable + */ public Observable schedulersMain(Observable observable) { return observable.subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) @@ -211,6 +222,13 @@ public T execute(NovateRequest request, BaseSubscriber subscriber) { return call(request, subscriber); } + /** + * call NovateRequest + * @param request + * @param subscriber + * @param + * @return + */ private T call(NovateRequest request, BaseSubscriber subscriber) { return (T) createRx(request) .compose(schedulersTransformer) @@ -1574,7 +1592,7 @@ public static final class Builder { private int writeTimeout = DEFAULT_TIMEOUT; private int readTimeout = DEFAULT_TIMEOUT; private int default_maxidle_connections = DEFAULT_MAXIDLE_CONNECTIONS; - private long default_keep_aliveduration = DEFAULT_MAXIDLE_CONNECTIONS; + private long default_keep_aliveduration = DEFAULT_KEEP_ALIVEDURATION; private long cacheMaxSize = DEFAULT_CACHEMAXSIZE; private int cacheTimeout = DEFAULT_MAX_STALE; private okhttp3.Call.Factory callFactory; @@ -1583,6 +1601,7 @@ public static final class Builder { private Object tag; private Boolean isCookie = false; private Boolean isCache = true; + private Boolean isSkip = false; private List certificateList; private HostnameVerifier hostnameVerifier; private CertificatePinner certificatePinner; @@ -1887,18 +1906,35 @@ public Builder cookieManager(NovateCookieManager cookie) { } /** - * + *skipSSLSocketFactory + */ + public Builder skipSSLSocketFactory(boolean isSkip) { + this.isSkip = isSkip; + return this; + } + + /** + * addSSLSocketFactory */ public Builder addSSLSocketFactory(SSLSocketFactory sslSocketFactory) { + if (sslSocketFactory == null) throw new NullPointerException("sslSocketFactory == null"); this.sslSocketFactory = sslSocketFactory; return this; } + /** + * HostnameVerifier + * @param hostnameVerifier + * @return Builder + */ public Builder addHostnameVerifier(HostnameVerifier hostnameVerifier) { this.hostnameVerifier = hostnameVerifier; return this; } + /** + * addCertificatePinner + */ public Builder addCertificatePinner(CertificatePinner certificatePinner) { this.certificatePinner = certificatePinner; return this; @@ -1922,6 +1958,7 @@ public Builder addSSL(String[] hosts, int[] certificates) { } public Builder addNetworkInterceptor(Interceptor interceptor) { + if (interceptor == null) throw new NullPointerException("interceptor == null"); okhttpBuilder.addNetworkInterceptor(interceptor); return this; } @@ -2023,7 +2060,14 @@ public Novate build() { new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)); } - if (sslSocketFactory != null) { + if (isSkip) { + okhttpBuilder.sslSocketFactory(NovateHttpsFactroy.getSSLSocketFactory(), + NovateHttpsFactroy.creatX509TrustManager()); + + okhttpBuilder.hostnameVerifier(NovateHttpsFactroy.creatSkipHostnameVerifier()); + } + + if (!isSkip && sslSocketFactory != null) { okhttpBuilder.sslSocketFactory(sslSocketFactory); } @@ -2031,9 +2075,8 @@ public Novate build() { okhttpBuilder.hostnameVerifier(hostnameVerifier); } - if (httpCacheDirectory == null) { - httpCacheDirectory = new File(mContext.getCacheDir(), "Novate_Http_cache"); + httpCacheDirectory = new File(mContext.getCacheDir(), KEY_CACHE); } if (isCache) { @@ -2077,7 +2120,7 @@ public Novate build() { *

If unset, a new connection pool will be used. */ if (connectionPool == null) { - connectionPool = new ConnectionPool(default_maxidle_connections, default_maxidle_connections, TimeUnit.SECONDS); + connectionPool = new ConnectionPool(default_maxidle_connections, default_keep_aliveduration, TimeUnit.SECONDS); } okhttpBuilder.connectionPool(connectionPool); @@ -2102,6 +2145,11 @@ public Novate build() { } if (isCookie) { + /** + * Returns a modifiable list of interceptors that observe a single network request and response. + * These interceptors must call {@link Interceptor.Chain#proceed} exactly once: it is an error + * for a network interceptor to short-circuit or repeat a network request. + */ okhttpBuilder.addInterceptor(new ReceivedCookiesInterceptor(context)); okhttpBuilder.addInterceptor(new AddCookiesInterceptor(context, "")); } @@ -2187,16 +2235,16 @@ public Observable call(Throwable throwable) { @Deprecated public interface ResponseCallBack { - public void onStart(); + void onStart(); - public void onCompleted(); + void onCompleted(); - public abstract void onError(Throwable e); + void onError(Throwable e); @Deprecated - public abstract void onSuccee(NovateResponse response); + void onSuccee(NovateResponse response); - public void onsuccess(int code, String msg, T response, String originalResponse); + void onsuccess(int code, String msg, T response, String originalResponse); } } diff --git a/novate/src/main/java/com/tamic/novate/NovateHttpsFactroy.java b/novate/src/main/java/com/tamic/novate/NovateHttpsFactroy.java index cc31b80..1f18089 100644 --- a/novate/src/main/java/com/tamic/novate/NovateHttpsFactroy.java +++ b/novate/src/main/java/com/tamic/novate/NovateHttpsFactroy.java @@ -32,6 +32,7 @@ import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; +import java.util.Arrays; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; @@ -98,7 +99,7 @@ protected static SSLSocketFactory getSSLSocketFactory(Context context, int[] cer * set HostnameVerifier * {@link HostnameVerifier} */ - protected static HostnameVerifier getHostnameVerifier(final String[] hostUrls) { + public static HostnameVerifier getHostnameVerifier(final String[] hostUrls) { HostnameVerifier TRUSTED_VERIFIER = new HostnameVerifier() { @@ -112,7 +113,6 @@ public boolean verify(String hostname, SSLSession session) { return ret; } }; - return TRUSTED_VERIFIER; } @@ -138,6 +138,16 @@ protected static String getPins(Context context, int certificate) { } + public static SSLSocketFactory getSSLSocketFactory() { + try { + SSLContext sslContext = SSLContext.getInstance("SSL"); + sslContext.init(null, creatTrustManager(), new SecureRandom()); + return sslContext.getSocketFactory(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + /** * get SSLSocketFactory * @@ -181,6 +191,7 @@ public static SSLSocketFactory creatSSLSocketFactory(Context context, String nam /** * get SkipHostnameVerifier + * * @return */ public static HostnameVerifier creatSkipHostnameVerifier() { @@ -195,24 +206,49 @@ public boolean verify(String s, SSLSession sslSession) { /** - * TrustManager + * X509TrustManager */ - private static TrustManager[] creatTrustManager() { - TrustManager[] trustAllCerts = new TrustManager[]{ - new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) { - } - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) { - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[]{}; - } - } - }; - return trustAllCerts; + public static X509TrustManager creatX509TrustManager() { + + return new X509TrustManager(){ + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; } + }; + + } + + /** + * TrustManager + */ + public static TrustManager[] creatTrustManager() { + TrustManager[] trustAllCerts = new TrustManager[]{ + new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[]{}; + } + } + }; + return trustAllCerts; + } }