Skip to content

Commit

Permalink
OkHttp 3 engine, closes #138
Browse files Browse the repository at this point in the history
  • Loading branch information
devemux86 committed Feb 11, 2017
1 parent d22dc79 commit 817bd13
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 58 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- House numbers (nodes) fix visibility [#168](https://github.com/mapsforge/vtm/issues/168)
- Android fix quick scale vs long press [#250](https://github.com/mapsforge/vtm/issues/250)
- Use baseline 160dpi in scaling [#236](https://github.com/mapsforge/vtm/issues/236)
- OkHttp3 update [#138](https://github.com/mapsforge/vtm/issues/138)
- libGDX double tap zoom [#263](https://github.com/mapsforge/vtm/issues/263)
- MapFileTileSource zoom level API enhancements [#219](https://github.com/mapsforge/vtm/issues/219)
- Animator enhancements with easing functions [#246](https://github.com/mapsforge/vtm/issues/246)
Expand Down
2 changes: 1 addition & 1 deletion vtm-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dependencies {
compile project(':vtm-themes')
compile project(':vtm-extras')
compile 'com.noveogroup.android:android-logger:1.3.6'
compile 'com.squareup.okhttp:okhttp:2.6.0'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
}

android {
Expand Down
31 changes: 16 additions & 15 deletions vtm-app/src/org/osmdroid/utils/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

import android.util.Log;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.Request;
import okhttp3.Response;

/**
* A "very very simple to use" class for performing http get and post requests.
* So many ways to do that, and potential subtle issues.
* If complexity should be added to handle even more issues, complexity should be put here and only here.
* <p/>
* <p>
* Typical usage:
* <pre>HttpConnection connection = new HttpConnection();
* connection.doGet("http://www.google.com");
* InputStream stream = connection.getStream();
* if (stream != null) {
* //use this stream, for buffer reading, or XML SAX parsing, or whatever...
* //use this stream, for buffer reading, or XML SAX parsing, or whatever...
* }
* connection.close();</pre>
*/
Expand All @@ -35,9 +36,14 @@ public class HttpConnection {

private static OkHttpClient getOkHttpClient() {
if (client == null) {
client = new OkHttpClient();
Builder b = new Builder();
b.connectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS);
b.readTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS);
client = b.build();
/*
client.setConnectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS);
client.setReadTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS);
*/
}
return client;
}
Expand Down Expand Up @@ -73,15 +79,10 @@ public void doGet(final String url) {
* @return the opened InputStream, or null if creation failed for any reason.
*/
public InputStream getStream() {
try {
if (response == null)
return null;
stream = response.body().byteStream();
return stream;
} catch (IOException e) {
e.printStackTrace();
if (response == null)
return null;
}
stream = response.body().byteStream();
return stream;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion vtm-http/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'maven'

dependencies {
compile project(':vtm')
compile 'com.squareup.okhttp:okhttp:1.5.2'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
}

sourceSets {
Expand Down
41 changes: 19 additions & 22 deletions vtm-http/src/org/oscim/tiling/source/OkHttpEngine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright 2014 Charles Greb
* Copyright 2014 Hannes Janetzek
* Copyright 2017 devemux86
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
Expand All @@ -17,24 +18,21 @@
*/
package org.oscim.tiling.source;

import com.squareup.okhttp.HttpResponseCache;
import com.squareup.okhttp.OkHttpClient;

import org.oscim.core.Tile;
import org.oscim.utils.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map.Entry;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpEngine implements HttpEngine {
static final Logger log = LoggerFactory.getLogger(OkHttpEngine.class);

private final OkHttpClient mClient;
private final UrlTileSource mTileSource;
Expand All @@ -46,9 +44,10 @@ public OkHttpFactory() {
mClient = new OkHttpClient();
}

public OkHttpFactory(HttpResponseCache responseCache) {
mClient = new OkHttpClient();
mClient.setResponseCache(responseCache);
public OkHttpFactory(Cache cache) {
mClient = new OkHttpClient.Builder()
.cache(cache)
.build();
}

@Override
Expand All @@ -75,17 +74,13 @@ public void sendRequest(Tile tile) throws IOException {
throw new IllegalArgumentException("Tile cannot be null.");
}
URL url = new URL(mTileSource.getTileUrl(tile));
HttpURLConnection conn = mClient.open(url);

Request.Builder builder = new Request.Builder()
.url(url);
for (Entry<String, String> opt : mTileSource.getRequestHeader().entrySet())
conn.addRequestProperty(opt.getKey(), opt.getValue());

try {
inputStream = conn.getInputStream();
} catch (FileNotFoundException e) {
throw new IOException("ERROR " + conn.getResponseCode()
+ ": " + conn.getResponseMessage());
}
builder.addHeader(opt.getKey(), opt.getValue());
Request request = builder.build();
Response response = mClient.newCall(request).execute();
inputStream = response.body().byteStream();
}

@Override
Expand All @@ -103,9 +98,11 @@ public void run() {
}).start();
}

/**
* OkHttp cache implemented through {@link OkHttpClient.Builder#cache(Cache)}.
*/
@Override
public void setCache(OutputStream os) {
// OkHttp cache implented through tileSource setResponseCache
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions vtm-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apply plugin: 'java'

dependencies {
compile project(':vtm-http')
compile 'com.squareup.okhttp:okhttp:1.5.2'
testCompile 'com.squareup.okhttp:mockwebserver:1.5.2'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
testCompile 'com.squareup.okhttp3:mockwebserver:3.6.0'
testCompile 'junit:junit:4.12'
testCompile 'org.easytesting:fest-assert-core:2.0M10'
testCompile 'org.mockito:mockito-all:1.10.19'
Expand Down
32 changes: 15 additions & 17 deletions vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.oscim.tiling.source;

import com.squareup.okhttp.HttpResponseCache;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -16,23 +11,26 @@
import java.io.InputStream;
import java.io.InputStreamReader;

import okhttp3.Cache;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;

import static org.fest.assertions.api.Assertions.assertThat;

public class OkHttpEngineTest {
private OkHttpEngine engine;
private MockWebServer server;
private MockResponse mockResponse;
private HttpResponseCache cache;

@Before
public void setUp() throws Exception {
mockResponse = new MockResponse();
mockResponse.setBody("TEST RESPONSE".getBytes());
MockResponse mockResponse = new MockResponse();
mockResponse.setBody("TEST RESPONSE");
server = new MockWebServer();
server.enqueue(mockResponse);
server.play();
server.start();
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory()
.create(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()));
.create(new OSciMap4TileSource(server.url("/tiles/vtm").toString()));
}

@After
Expand All @@ -52,15 +50,15 @@ public void sendRequest_shouldRejectNullTile() throws Exception {

@Test
public void sendRequest_shouldAppendXYZToPath() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
engine.sendRequest(new Tile(1, 2, (byte) 3));

RecordedRequest request = server.takeRequest();
assertThat(request.getPath()).isEqualTo("/tiles/vtm/3/1/2.vtm");
}

@Test
public void read_shouldReturnResponseStream() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
engine.sendRequest(new Tile(1, 2, (byte) 3));

InputStream responseStream = engine.read();
String response = new BufferedReader(new InputStreamReader(responseStream)).readLine();
Expand Down Expand Up @@ -93,12 +91,12 @@ public void requestCompleted_shouldReturnValueGiven() throws Exception {

@Test
public void create_shouldUseTileSourceCache() throws Exception {
cache = new HttpResponseCache(new File("tmp"), 1024);
Cache cache = new Cache(new File("tmp"), 1024);
OSciMap4TileSource tileSource =
new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString());
new OSciMap4TileSource(server.url("/tiles/vtm").toString());
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(cache).create(tileSource);
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
engine.sendRequest(new Tile(1, 2, (byte) 3));
engine.requestCompleted(true);
assertThat(cache.getRequestCount()).isEqualTo(1);
assertThat(cache.requestCount()).isEqualTo(1);
}
}

0 comments on commit 817bd13

Please sign in to comment.