1
1
package fr .free .nrw .commons ;
2
2
3
3
import androidx .annotation .NonNull ;
4
-
5
- import org .wikipedia .dataclient .SharedPreferenceCookieManager ;
6
- import org .wikipedia .dataclient .okhttp .HttpStatusException ;
7
-
8
4
import java .io .File ;
9
5
import java .io .IOException ;
10
-
11
6
import okhttp3 .Cache ;
12
7
import okhttp3 .Interceptor ;
13
8
import okhttp3 .OkHttpClient ;
14
9
import okhttp3 .Request ;
15
10
import okhttp3 .Response ;
11
+ import okhttp3 .ResponseBody ;
16
12
import okhttp3 .logging .HttpLoggingInterceptor ;
13
+ import okhttp3 .logging .HttpLoggingInterceptor .Level ;
14
+ import org .wikipedia .dataclient .SharedPreferenceCookieManager ;
15
+ import org .wikipedia .dataclient .okhttp .HttpStatusException ;
16
+ import timber .log .Timber ;
17
17
18
18
public final class OkHttpConnectionFactory {
19
19
private static final String CACHE_DIR_NAME = "okhttp-cache" ;
20
20
private static final long NET_CACHE_SIZE = 64 * 1024 * 1024 ;
21
21
@ NonNull private static final Cache NET_CACHE = new Cache (new File (CommonsApplication .getInstance ().getCacheDir (),
22
22
CACHE_DIR_NAME ), NET_CACHE_SIZE );
23
23
24
- @ NonNull private static OkHttpClient CLIENT = createClient ();
24
+ @ NonNull private static final OkHttpClient CLIENT = createClient ();
25
25
26
26
@ NonNull public static OkHttpClient getClient () {
27
27
return CLIENT ;
@@ -39,8 +39,8 @@ private static OkHttpClient createClient() {
39
39
}
40
40
41
41
private static HttpLoggingInterceptor getLoggingInterceptor () {
42
- HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor ()
43
- .setLevel (HttpLoggingInterceptor . Level .BASIC );
42
+ final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor ()
43
+ .setLevel (Level .BASIC );
44
44
45
45
httpLoggingInterceptor .redactHeader ("Authorization" );
46
46
httpLoggingInterceptor .redactHeader ("Cookie" );
@@ -49,18 +49,30 @@ private static HttpLoggingInterceptor getLoggingInterceptor() {
49
49
}
50
50
51
51
private static class CommonHeaderRequestInterceptor implements Interceptor {
52
- @ Override @ NonNull public Response intercept (@ NonNull Chain chain ) throws IOException {
53
- Request request = chain .request ().newBuilder ()
52
+ @ Override @ NonNull public Response intercept (@ NonNull final Chain chain ) throws IOException {
53
+ final Request request = chain .request ().newBuilder ()
54
54
.header ("User-Agent" , CommonsApplication .getInstance ().getUserAgent ())
55
55
.build ();
56
56
return chain .proceed (request );
57
57
}
58
58
}
59
59
60
60
public static class UnsuccessfulResponseInterceptor implements Interceptor {
61
- @ Override @ NonNull public Response intercept (@ NonNull Chain chain ) throws IOException {
62
- Response rsp = chain .proceed (chain .request ());
61
+
62
+ private static final String ERRORS_PREFIX = "{\" error" ;
63
+
64
+ @ Override @ NonNull public Response intercept (@ NonNull final Chain chain ) throws IOException {
65
+ final Response rsp = chain .proceed (chain .request ());
63
66
if (rsp .isSuccessful ()) {
67
+ try (final ResponseBody responseBody = rsp .peekBody (ERRORS_PREFIX .length ())) {
68
+ if (ERRORS_PREFIX .equals (responseBody .string ())){
69
+ try (final ResponseBody body = rsp .body ()) {
70
+ throw new IOException (body .string ());
71
+ }
72
+ }
73
+ }catch (final IOException e ){
74
+ Timber .e (e );
75
+ }
64
76
return rsp ;
65
77
}
66
78
throw new HttpStatusException (rsp );
0 commit comments