-
Notifications
You must be signed in to change notification settings - Fork 7
/
OkHttpClient.java
113 lines (100 loc) · 4.6 KB
/
OkHttpClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package io.embrace.android.embracesdk.okhttp3.swazzle.callback.okhttp3;
import java.util.List;
import io.embrace.android.embracesdk.Embrace;
import io.embrace.android.embracesdk.annotation.InternalApi;
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3ApplicationInterceptor;
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3NetworkInterceptor;
import okhttp3.Interceptor;
/**
* Callback hooks for the okhttp3.OkHttpClient class.
*/
@InternalApi
public final class OkHttpClient {
private OkHttpClient() {
}
@InternalApi
public static final class Builder {
private Builder() {
}
/**
* As there was a way to clear the injected interceptors during the OkHttpClient
* initialization using the builder, we are hooking the build method as well, instead of
* just the Builder constructor.
* <p>
* Once the build method is called, OkHTTP mushes everything and returns the OkHttpClient
* instance, where the developer has no way to alter any of the interceptors during or
* after this point, without having to rebuild the client.
*/
@SuppressWarnings("MethodNameCheck")
public static void _preBuild(okhttp3.OkHttpClient.Builder thiz) {
logInfo("Embrace OkHTTP Wrapper; onPrebuild");
addEmbraceInterceptors(thiz);
}
@SuppressWarnings("MethodNameCheck")
public static void _constructorOnPostBody(okhttp3.OkHttpClient.Builder thiz) {
logInfo("Embrace OkHTTP Wrapper; onPostBody");
addEmbraceInterceptors(thiz);
}
/**
* Adds embrace interceptors if they don't exist already to the OkHTTPClient provided.
*
* @param thiz the OkHttpClient builder in matter.
*/
private static void addEmbraceInterceptors(okhttp3.OkHttpClient.Builder thiz) {
try {
logInfo("Embrace OkHTTP Wrapper; Adding interceptors");
addInterceptor(thiz.interceptors(), new EmbraceOkHttp3ApplicationInterceptor());
addInterceptor(thiz.networkInterceptors(), new EmbraceOkHttp3NetworkInterceptor());
} catch (NoSuchMethodError exception) {
// The customer may be overwriting OkHttpClient with their own implementation, and some of the
// methods we use are missing.
logError("Altered OkHttpClient implementation, could not add OkHttp interceptor. ", exception);
} catch (Exception exception) {
logError("Could not add OkHttp interceptor. ", exception);
}
}
/**
* Adds the interceptor to the interceptors list if it doesn't exist already.
*
* @param interceptors list of existing interceptors.
* @param interceptor interceptor to be added.
*/
private static void addInterceptor(List<Interceptor> interceptors,
Interceptor interceptor) {
if (interceptors != null && !containsInstance(interceptors, interceptor.getClass())) {
interceptors.add(0, interceptor);
} else {
logInfo(
"Not adding interceptor [" + interceptor.getClass().getSimpleName() + "]"
);
}
}
/**
* Checks for the existence in the elements list of an instance of the same class as the
* one provided in the arguments.
*
* @param elementsList list of elements.
* @param clazz class of the instance that's being checked if exists.
* @return if an instance of the provided class exists in the list of elements.
*/
private static <T> boolean containsInstance(List<T> elementsList,
Class<? extends T> clazz) {
for (T classInstance : elementsList) {
if (clazz.isInstance(classInstance)) {
logInfo(
"[" + clazz.getSimpleName() + "] already present in list"
);
return true;
}
}
return false;
}
private static void logInfo(String message) {
Embrace.getInstance().getInternalInterface().logInfo(message, null);
}
private static void logError(String message, Throwable throwable) {
Embrace.getInstance().getInternalInterface().logError(message, null, null, false);
Embrace.getInstance().getInternalInterface().logInternalError(throwable);
}
}
}