1 |
efrain |
1 |
package com.cesams.twogetskills.library;
|
|
|
2 |
|
|
|
3 |
import android.util.Log;
|
|
|
4 |
|
|
|
5 |
import com.cesams.twogetskills.Constants;
|
|
|
6 |
|
|
|
7 |
import java.io.File;
|
|
|
8 |
import java.io.IOException;
|
|
|
9 |
import java.security.SecureRandom;
|
|
|
10 |
import java.security.cert.CertificateException;
|
|
|
11 |
import java.security.cert.X509Certificate;
|
|
|
12 |
|
|
|
13 |
import javax.net.ssl.HostnameVerifier;
|
|
|
14 |
import javax.net.ssl.SSLContext;
|
|
|
15 |
import javax.net.ssl.SSLSession;
|
|
|
16 |
import javax.net.ssl.SSLSocketFactory;
|
|
|
17 |
import javax.net.ssl.TrustManager;
|
|
|
18 |
import javax.net.ssl.X509TrustManager;
|
|
|
19 |
|
|
|
20 |
import okhttp3.Cache;
|
|
|
21 |
import okhttp3.Interceptor;
|
|
|
22 |
import okhttp3.OkHttpClient;
|
|
|
23 |
import okhttp3.Request;
|
|
|
24 |
import okhttp3.Response;
|
|
|
25 |
import okhttp3.logging.HttpLoggingInterceptor;
|
|
|
26 |
|
|
|
27 |
public class Http {
|
|
|
28 |
public final static int CACHE_SIZE = 100 * 1024 * 1024; // 100MB
|
|
|
29 |
private final static String SSL = "SSL";
|
|
|
30 |
private File mCacheDir;
|
|
|
31 |
private String mDeviceUuid;
|
|
|
32 |
private String mSecret;
|
|
|
33 |
private int mCreated;
|
|
|
34 |
private int mRand;
|
|
|
35 |
private boolean mUsingHeaderInterceptor;
|
|
|
36 |
|
|
|
37 |
public Http(File cacheDir)
|
|
|
38 |
{
|
|
|
39 |
mDeviceUuid = "";
|
|
|
40 |
mSecret = "";
|
|
|
41 |
mCreated = 0;
|
|
|
42 |
mRand = 0;
|
|
|
43 |
mCacheDir = cacheDir;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public Http(File cacheDir, String deviceUuid, String secret, int created, int rand)
|
|
|
47 |
{
|
|
|
48 |
mDeviceUuid = deviceUuid;
|
|
|
49 |
mSecret = secret;
|
|
|
50 |
mCreated = created;
|
|
|
51 |
mRand = rand;
|
|
|
52 |
mUsingHeaderInterceptor = true;
|
|
|
53 |
mCacheDir = cacheDir;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
public OkHttpClient getHttpClient(boolean usingCache) throws Exception
|
|
|
58 |
{
|
|
|
59 |
Interceptor headerInterceptor = null;
|
|
|
60 |
|
|
|
61 |
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
|
|
|
62 |
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
|
|
|
63 |
|
|
|
64 |
if(mUsingHeaderInterceptor) {
|
|
|
65 |
|
|
|
66 |
headerInterceptor = new Interceptor() {
|
|
|
67 |
@Override
|
|
|
68 |
public Response intercept(Chain chain) throws IOException {
|
|
|
69 |
Request newRequest = chain.request().newBuilder()
|
|
|
70 |
.addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
|
|
|
71 |
.addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, mDeviceUuid)
|
|
|
72 |
.addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, mSecret)
|
|
|
73 |
.addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(mCreated))
|
|
|
74 |
.addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(mRand))
|
|
|
75 |
.build();
|
|
|
76 |
return chain.proceed(newRequest);
|
|
|
77 |
}
|
|
|
78 |
};
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
TrustManager [] trustAllCerts = new TrustManager [] { trustManager () };
|
|
|
82 |
|
|
|
83 |
SSLContext sslContext = SSLContext.getInstance (SSL);
|
|
|
84 |
sslContext.init (null, trustAllCerts, new SecureRandom ());
|
|
|
85 |
|
|
|
86 |
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory ();
|
|
|
87 |
|
|
|
88 |
OkHttpClient.Builder builder = new OkHttpClient.Builder ();
|
|
|
89 |
|
|
|
90 |
if(usingCache) {
|
|
|
91 |
Log.d("GetHttpClient", "usingCache = true");
|
|
|
92 |
builder.cache(new Cache(mCacheDir, CACHE_SIZE));
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
builder.addInterceptor(logging);
|
|
|
96 |
if(headerInterceptor != null) {
|
|
|
97 |
builder.addInterceptor(headerInterceptor);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
builder.sslSocketFactory (sslSocketFactory, (X509TrustManager)trustAllCerts [0]);
|
|
|
101 |
builder.hostnameVerifier (hostnameVerifier ());
|
|
|
102 |
|
|
|
103 |
return builder.build ();
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
private static TrustManager trustManager () {
|
|
|
107 |
return new X509TrustManager () {
|
|
|
108 |
|
|
|
109 |
@Override
|
|
|
110 |
public void checkClientTrusted (X509Certificate [] chain, String authType) throws CertificateException { }
|
|
|
111 |
|
|
|
112 |
@Override
|
|
|
113 |
public void checkServerTrusted (X509Certificate[] chain, String authType) throws CertificateException { }
|
|
|
114 |
|
|
|
115 |
@Override
|
|
|
116 |
public X509Certificate [] getAcceptedIssuers () {
|
|
|
117 |
return new X509Certificate [] { };
|
|
|
118 |
}
|
|
|
119 |
};
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
private static HostnameVerifier hostnameVerifier () {
|
|
|
123 |
return new HostnameVerifier () {
|
|
|
124 |
|
|
|
125 |
@Override
|
|
|
126 |
public boolean verify (String hostname, SSLSession session) {
|
|
|
127 |
return true;
|
|
|
128 |
}
|
|
|
129 |
};
|
|
|
130 |
}
|
|
|
131 |
}
|