Niedzielski has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/334454 )

Change subject: Update: allow OkHttp Cache to serve stale responses
......................................................................

Update: allow OkHttp Cache to serve stale responses

Bug: T148675
Change-Id: Ie4a99141462497c13ff63dee666ed4948e7c5421
---
M app/src/main/java/org/wikipedia/dataclient/OkHttpConnectionFactory.java
1 file changed, 69 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/54/334454/1

diff --git 
a/app/src/main/java/org/wikipedia/dataclient/OkHttpConnectionFactory.java 
b/app/src/main/java/org/wikipedia/dataclient/OkHttpConnectionFactory.java
index 51febf2..eb21de5 100644
--- a/app/src/main/java/org/wikipedia/dataclient/OkHttpConnectionFactory.java
+++ b/app/src/main/java/org/wikipedia/dataclient/OkHttpConnectionFactory.java
@@ -11,8 +11,10 @@
 import java.net.HttpURLConnection;
 import java.net.Proxy;
 import java.net.URL;
+import java.util.concurrent.TimeUnit;
 
 import okhttp3.Cache;
+import okhttp3.CacheControl;
 import okhttp3.CookieJar;
 import okhttp3.Interceptor;
 import okhttp3.JavaNetCookieJar;
@@ -24,8 +26,13 @@
 
 public class OkHttpConnectionFactory implements HttpRequest.ConnectionFactory {
     private static final long HTTP_CACHE_SIZE = 64 * 1024 * 1024;
-    private static final Cache HTTP_CACHE = new 
Cache(WikipediaApp.getInstance().getCacheDir(), HTTP_CACHE_SIZE);
-    private static OkHttpClient CLIENT;
+    @NonNull private static final Cache HTTP_CACHE = new 
Cache(WikipediaApp.getInstance().getCacheDir(),
+            HTTP_CACHE_SIZE);
+    @NonNull private static final OkHttpClient CLIENT = createClient();
+
+    @NonNull public static OkHttpClient getClient() {
+        return CLIENT;
+    }
 
     @Override
     public HttpURLConnection create(URL url) throws IOException {
@@ -36,14 +43,6 @@
     public HttpURLConnection create(URL url, Proxy proxy) throws IOException {
         throw new UnsupportedOperationException(
                 "Per-connection proxy is not supported. Use OkHttpClient's 
setProxy instead.");
-    }
-
-    @NonNull
-    public static OkHttpClient getClient() {
-        if (CLIENT == null) {
-            CLIENT = createClient();
-        }
-        return CLIENT;
     }
 
     @NonNull
@@ -60,9 +59,69 @@
                 .cache(HTTP_CACHE)
                 .addInterceptor(loggingInterceptor)
                 .addInterceptor(new CommonHeaderInterceptor())
+                .addInterceptor(new DefaultMaxStaleInterceptor())
+                .addNetworkInterceptor(new CacheResponseInterceptor())
                 .build();
     }
 
+    /** Sets a default max-stale cache-control argument on all requests that 
do not specify one */
+    private static class DefaultMaxStaleInterceptor implements Interceptor {
+        @Override public Response intercept(Interceptor.Chain chain) throws 
IOException {
+            Request req = chain.request();
+
+            int maxStaleSeconds = req.cacheControl().maxStaleSeconds() < 0
+                    ? Integer.MAX_VALUE
+                    : req.cacheControl().maxStaleSeconds();
+            CacheControl cacheControl = 
newCacheControlBuilder(req.cacheControl())
+                    .maxStale(maxStaleSeconds, TimeUnit.SECONDS)
+                    .build();
+            req = req.newBuilder().cacheControl(cacheControl).build();
+
+            return chain.proceed(req);
+        }
+
+        private CacheControl.Builder newCacheControlBuilder(CacheControl 
cacheControl) {
+            CacheControl.Builder builder = new CacheControl.Builder();
+            if (cacheControl.noCache()) {
+                builder.noCache();
+            }
+            if (cacheControl.noStore()) {
+                builder.noStore();
+            }
+            if (cacheControl.maxAgeSeconds() >= 0) {
+                builder.maxAge(cacheControl.maxAgeSeconds(), TimeUnit.SECONDS);
+            }
+            if (cacheControl.maxStaleSeconds() >= 0) {
+                builder.maxStale(cacheControl.maxStaleSeconds(), 
TimeUnit.SECONDS);
+            }
+            if (cacheControl.minFreshSeconds() >= 0) {
+                builder.minFresh(cacheControl.minFreshSeconds(), 
TimeUnit.SECONDS);
+            }
+            if (cacheControl.onlyIfCached()) {
+                builder.onlyIfCached();
+            }
+            if (cacheControl.noTransform()) {
+                builder.noTransform();
+            }
+            return builder;
+        }
+    }
+
+    /** Allow response caching expressly strictly forbidden */
+    private static class CacheResponseInterceptor implements Interceptor {
+        @Override public Response intercept(Interceptor.Chain chain) throws 
IOException {
+            Request req = chain.request();
+            Response rsp = chain.proceed(req);
+            // todo: add !rsp.cacheControl().mustRevalidate() when 
mobile-sections-* doesn't respond
+            //       with must-revalidate
+            if (!rsp.cacheControl().noStore()) {
+                rsp = rsp.newBuilder().removeHeader("cache-control").build();
+            }
+
+            return rsp;
+        }
+    }
+
     // If adding a new header here, make sure to duplicate it in the MWAPI 
header builder
     // (WikipediaApp.buildCustomHeadersMap()).
     // TODO: remove above comment once buildCustomHeadersMap() is removed.

-- 
To view, visit https://gerrit.wikimedia.org/r/334454
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie4a99141462497c13ff63dee666ed4948e7c5421
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org>
Gerrit-Reviewer: Sniedzielski <sniedziel...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to