From cc115f1e769ab2d54d8da02202eb6aba6a3679eb Mon Sep 17 00:00:00 2001 From: Ankush Bose Date: Sun, 18 Dec 2022 15:33:29 +0530 Subject: [PATCH 1/2] 5120: Fix image item loading in ExploreFragment - removed redundant try block containing checker with `rsp.body` instead of using peekBody, resulting in closing the connection before even reading it. --- .../nrw/commons/OkHttpConnectionFactory.java | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java b/app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java index ab00d1721..94bac1ae7 100644 --- a/app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java +++ b/app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java @@ -3,7 +3,6 @@ package fr.free.nrw.commons; import androidx.annotation.NonNull; import java.io.File; import java.io.IOException; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; @@ -20,30 +19,34 @@ import org.wikipedia.dataclient.okhttp.HttpStatusException; import timber.log.Timber; public final class OkHttpConnectionFactory { + private static final String CACHE_DIR_NAME = "okhttp-cache"; private static final long NET_CACHE_SIZE = 64 * 1024 * 1024; - @NonNull private static final Cache NET_CACHE = new Cache(new File(CommonsApplication.getInstance().getCacheDir(), + @NonNull + private static final Cache NET_CACHE = new Cache( + new File(CommonsApplication.getInstance().getCacheDir(), CACHE_DIR_NAME), NET_CACHE_SIZE); @NonNull private static final OkHttpClient CLIENT = createClient(); - @NonNull public static OkHttpClient getClient() { + @NonNull + public static OkHttpClient getClient() { return CLIENT; } @NonNull private static OkHttpClient createClient() { return new OkHttpClient.Builder() - .cookieJar(SharedPreferenceCookieManager.getInstance()) - .cache(NET_CACHE) - .connectTimeout(120, TimeUnit.SECONDS) - .writeTimeout(120, TimeUnit.SECONDS) - .readTimeout(120, TimeUnit.SECONDS) - .addInterceptor(getLoggingInterceptor()) - .addInterceptor(new UnsuccessfulResponseInterceptor()) - .addInterceptor(new CommonHeaderRequestInterceptor()) - .build(); + .cookieJar(SharedPreferenceCookieManager.getInstance()) + .cache(NET_CACHE) + .connectTimeout(120, TimeUnit.SECONDS) + .writeTimeout(120, TimeUnit.SECONDS) + .readTimeout(120, TimeUnit.SECONDS) + .addInterceptor(getLoggingInterceptor()) + .addInterceptor(new UnsuccessfulResponseInterceptor()) + .addInterceptor(new CommonHeaderRequestInterceptor()) + .build(); } private static HttpLoggingInterceptor getLoggingInterceptor() { @@ -62,13 +65,14 @@ public final class OkHttpConnectionFactory { @NonNull public Response intercept(@NonNull final Chain chain) throws IOException { final Request request = chain.request().newBuilder() - .header("User-Agent", CommonsApplication.getInstance().getUserAgent()) - .build(); + .header("User-Agent", CommonsApplication.getInstance().getUserAgent()) + .build(); return chain.proceed(request); } } public static class UnsuccessfulResponseInterceptor implements Interceptor { + private static final List DO_NOT_INTERCEPT = Collections.singletonList( "api.php?format=json&formatversion=2&errorformat=plaintext&action=upload&ignorewarnings=1"); @@ -80,15 +84,13 @@ public final class OkHttpConnectionFactory { final Response rsp = chain.proceed(chain.request()); // Do not intercept certain requests and let the caller handle the errors - if(isExcludedUrl(chain.request())) { + if (isExcludedUrl(chain.request())) { return rsp; } if (rsp.isSuccessful()) { try (final ResponseBody responseBody = rsp.peekBody(ERRORS_PREFIX.length())) { if (ERRORS_PREFIX.equals(responseBody.string())) { - try (final ResponseBody body = rsp.body()) { - throw new IOException(body.string()); - } + throw new IOException(responseBody.string()); } } catch (final IOException e) { Timber.e(e); @@ -100,8 +102,8 @@ public final class OkHttpConnectionFactory { private boolean isExcludedUrl(final Request request) { final String requestUrl = request.url().toString(); - for(final String url: DO_NOT_INTERCEPT) { - if(requestUrl.contains(url)) { + for (final String url : DO_NOT_INTERCEPT) { + if (requestUrl.contains(url)) { return true; } } From f96a5edbb9f12310c7bd642dd7537a0c922ede50 Mon Sep 17 00:00:00 2001 From: Ankush Bose Date: Sun, 18 Dec 2022 15:37:11 +0530 Subject: [PATCH 2/2] 5120: Put back unwanted changes --- .../nrw/commons/OkHttpConnectionFactory.java | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java b/app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java index 94bac1ae7..7d03911a8 100644 --- a/app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java +++ b/app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java @@ -19,34 +19,30 @@ import org.wikipedia.dataclient.okhttp.HttpStatusException; import timber.log.Timber; public final class OkHttpConnectionFactory { - private static final String CACHE_DIR_NAME = "okhttp-cache"; private static final long NET_CACHE_SIZE = 64 * 1024 * 1024; - @NonNull - private static final Cache NET_CACHE = new Cache( - new File(CommonsApplication.getInstance().getCacheDir(), + @NonNull private static final Cache NET_CACHE = new Cache(new File(CommonsApplication.getInstance().getCacheDir(), CACHE_DIR_NAME), NET_CACHE_SIZE); @NonNull private static final OkHttpClient CLIENT = createClient(); - @NonNull - public static OkHttpClient getClient() { + @NonNull public static OkHttpClient getClient() { return CLIENT; } @NonNull private static OkHttpClient createClient() { return new OkHttpClient.Builder() - .cookieJar(SharedPreferenceCookieManager.getInstance()) - .cache(NET_CACHE) - .connectTimeout(120, TimeUnit.SECONDS) - .writeTimeout(120, TimeUnit.SECONDS) - .readTimeout(120, TimeUnit.SECONDS) - .addInterceptor(getLoggingInterceptor()) - .addInterceptor(new UnsuccessfulResponseInterceptor()) - .addInterceptor(new CommonHeaderRequestInterceptor()) - .build(); + .cookieJar(SharedPreferenceCookieManager.getInstance()) + .cache(NET_CACHE) + .connectTimeout(120, TimeUnit.SECONDS) + .writeTimeout(120, TimeUnit.SECONDS) + .readTimeout(120, TimeUnit.SECONDS) + .addInterceptor(getLoggingInterceptor()) + .addInterceptor(new UnsuccessfulResponseInterceptor()) + .addInterceptor(new CommonHeaderRequestInterceptor()) + .build(); } private static HttpLoggingInterceptor getLoggingInterceptor() { @@ -65,14 +61,13 @@ public final class OkHttpConnectionFactory { @NonNull public Response intercept(@NonNull final Chain chain) throws IOException { final Request request = chain.request().newBuilder() - .header("User-Agent", CommonsApplication.getInstance().getUserAgent()) - .build(); + .header("User-Agent", CommonsApplication.getInstance().getUserAgent()) + .build(); return chain.proceed(request); } } public static class UnsuccessfulResponseInterceptor implements Interceptor { - private static final List DO_NOT_INTERCEPT = Collections.singletonList( "api.php?format=json&formatversion=2&errorformat=plaintext&action=upload&ignorewarnings=1"); @@ -84,7 +79,7 @@ public final class OkHttpConnectionFactory { final Response rsp = chain.proceed(chain.request()); // Do not intercept certain requests and let the caller handle the errors - if (isExcludedUrl(chain.request())) { + if(isExcludedUrl(chain.request())) { return rsp; } if (rsp.isSuccessful()) { @@ -102,8 +97,8 @@ public final class OkHttpConnectionFactory { private boolean isExcludedUrl(final Request request) { final String requestUrl = request.url().toString(); - for (final String url : DO_NOT_INTERCEPT) { - if (requestUrl.contains(url)) { + for(final String url: DO_NOT_INTERCEPT) { + if(requestUrl.contains(url)) { return true; } }