Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

implements okhttp response caching #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
}
6 changes: 5 additions & 1 deletion vtm/src/org/oscim/tiling/source/OkHttpEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,6 +53,7 @@ public HttpEngine create(UrlTileSource tileSource) {

public OkHttpEngine(OkHttpClient client, UrlTileSource tileSource) {
mClient = client;
mClient.setResponseCache(tileSource.getResponseCache());
mTileSource = tileSource;
}

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions vtm/src/org/oscim/tiling/source/UrlTileSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> mRequestHeaders = Collections.emptyMap();
private TileUrlFormatter mTileUrlFormatter = URL_FORMATTER;
Expand Down Expand Up @@ -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<String, String> options) {
mRequestHeaders = options;
}
Expand Down