From 2853e7f3bbd46691cb1f1b42953ccae6b63fa353 Mon Sep 17 00:00:00 2001 From: Baldur Gudbjornsson Date: Mon, 25 Aug 2014 13:54:48 -0400 Subject: [PATCH] implements okhttp response caching --- .../org/oscim/tiling/source/OkHttpEngineTest.java | 15 +++++++++++++++ vtm/src/org/oscim/tiling/source/OkHttpEngine.java | 6 +++++- .../org/oscim/tiling/source/UrlTileSource.java | 11 +++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java b/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java index 6ba3f3b2d..641cf7cbb 100644 --- a/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java @@ -3,6 +3,7 @@ import static org.fest.assertions.api.Assertions.assertThat; import java.io.BufferedReader; +import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; @@ -12,6 +13,7 @@ import org.oscim.core.Tile; import org.oscim.tiling.source.oscimap4.OSciMap4TileSource; +import com.squareup.okhttp.HttpResponseCache; import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockWebServer; import com.squareup.okhttp.mockwebserver.RecordedRequest; @@ -20,6 +22,7 @@ public class OkHttpEngineTest { private OkHttpEngine engine; private MockWebServer server; private MockResponse mockResponse; + private HttpResponseCache cache; @Before public void setUp() throws Exception { @@ -87,4 +90,16 @@ public void requestCompleted_shouldReturnValueGiven() throws Exception { assertThat(engine.requestCompleted(true)).isTrue(); assertThat(engine.requestCompleted(false)).isFalse(); } + + @Test + public void create_shouldUseTileSourceCache() throws Exception { + cache = new HttpResponseCache(new File("tmp"), 1024); + OSciMap4TileSource tileSource = + new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()); + tileSource.setResponseCache(cache); + engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory().create(tileSource); + engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue())); + engine.requestCompleted(true); + assertThat(cache.getRequestCount()).isEqualTo(1); + } } diff --git a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java index 1393e65a0..aa14331f5 100644 --- a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java @@ -26,12 +26,15 @@ import org.oscim.core.Tile; import org.oscim.utils.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.squareup.okhttp.OkHttpClient; public class OkHttpEngine implements HttpEngine { private final OkHttpClient mClient; private final UrlTileSource mTileSource; + static final Logger log = LoggerFactory.getLogger(OkHttpEngine.class); public static class OkHttpFactory implements HttpEngine.Factory { private final OkHttpClient mClient; @@ -50,6 +53,7 @@ public HttpEngine create(UrlTileSource tileSource) { public OkHttpEngine(OkHttpClient client, UrlTileSource tileSource) { mClient = client; + mClient.setResponseCache(tileSource.getResponseCache()); mTileSource = tileSource; } @@ -89,7 +93,7 @@ public void run() { @Override public void setCache(OutputStream os) { - // TODO: Evaluate OkHttp response cache and determine if additional caching is required. + // OkHttp cache implented through tileSource setResponseCache } @Override diff --git a/vtm/src/org/oscim/tiling/source/UrlTileSource.java b/vtm/src/org/oscim/tiling/source/UrlTileSource.java index 9f31bed63..903b41728 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileSource.java @@ -16,6 +16,8 @@ */ package org.oscim.tiling.source; +import com.squareup.okhttp.HttpResponseCache; + import java.net.MalformedURLException; import java.net.URL; import java.util.Collections; @@ -31,6 +33,7 @@ public abstract class UrlTileSource extends TileSource { private final URL mUrl; private final String[] mTilePath; + private HttpResponseCache mResponseCache; private HttpEngine.Factory mHttpFactory; private Map mRequestHeaders = Collections.emptyMap(); private TileUrlFormatter mTileUrlFormatter = URL_FORMATTER; @@ -87,6 +90,14 @@ public void setHttpEngine(HttpEngine.Factory httpFactory) { mHttpFactory = httpFactory; } + public void setResponseCache(HttpResponseCache responseCache) { + mResponseCache = responseCache; + } + + public HttpResponseCache getResponseCache() { + return mResponseCache; + } + public void setHttpRequestHeaders(Map options) { mRequestHeaders = options; }