From af7d70cfd83cf2f7e954164fe87943b5dc7d18d6 Mon Sep 17 00:00:00 2001 From: Chuck Greb Date: Fri, 28 Mar 2014 03:29:24 -0400 Subject: [PATCH 01/16] Extracts configurable networking layer. * Extracts HttpEngine interface. * Uses LwHttp as default networking client. * Implements alternate networking client using OkHttp. --- vtm/build.gradle | 5 ++ .../org/oscim/tiling/source/HttpEngine.java | 19 +++++ vtm/src/org/oscim/tiling/source/LwHttp.java | 9 +-- .../org/oscim/tiling/source/OkHttpEngine.java | 70 +++++++++++++++++++ .../tiling/source/UrlTileDataSource.java | 4 +- .../oscim/tiling/source/UrlTileSource.java | 13 ++++ .../source/bitmap/BitmapTileSource.java | 2 +- .../source/oscimap4/OSciMap4TileSource.java | 3 +- .../tiling/source/UrlTileSourceTest.java | 58 +++++++++++++++ .../source/bitmap/BitmapTileSourceTest.java | 48 +++++++++++++ .../oscimap4/OSciMap4TileSourceTest.java | 42 +++++++++++ 11 files changed, 264 insertions(+), 9 deletions(-) create mode 100644 vtm/src/org/oscim/tiling/source/HttpEngine.java create mode 100644 vtm/src/org/oscim/tiling/source/OkHttpEngine.java create mode 100644 vtm/test/org/oscim/tiling/source/UrlTileSourceTest.java create mode 100644 vtm/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java create mode 100644 vtm/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java diff --git a/vtm/build.gradle b/vtm/build.gradle index 45747427..adb2f2b0 100644 --- a/vtm/build.gradle +++ b/vtm/build.gradle @@ -5,11 +5,16 @@ configurations { providedCompile } dependencies { compile 'org.slf4j:slf4j-api:1.7.6' + compile 'com.squareup.okhttp:okhttp:1.5.2' providedCompile 'com.google.code.findbugs:annotations:2.0.1' + testCompile 'junit:junit:4.11' + testCompile 'org.mockito:mockito-all:1.9.5' + testCompile 'org.easytesting:fest-assert-core:2.0M10' } sourceSets { main.java.srcDirs = ['src'] + test.java.srcDirs = ['test'] main.resources.srcDirs = ['resources'] main.compileClasspath += configurations.providedCompile } diff --git a/vtm/src/org/oscim/tiling/source/HttpEngine.java b/vtm/src/org/oscim/tiling/source/HttpEngine.java new file mode 100644 index 00000000..bf3f3240 --- /dev/null +++ b/vtm/src/org/oscim/tiling/source/HttpEngine.java @@ -0,0 +1,19 @@ +package org.oscim.tiling.source; + +import org.oscim.core.Tile; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public interface HttpEngine { + InputStream read() throws IOException; + + boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException; + + void close(); + + void setCache(OutputStream os); + + boolean requestCompleted(boolean success); +} diff --git a/vtm/src/org/oscim/tiling/source/LwHttp.java b/vtm/src/org/oscim/tiling/source/LwHttp.java index 385a4bdf..8f3978ff 100644 --- a/vtm/src/org/oscim/tiling/source/LwHttp.java +++ b/vtm/src/org/oscim/tiling/source/LwHttp.java @@ -36,11 +36,8 @@ import org.slf4j.LoggerFactory; /** * Lightweight HTTP connection for tile loading. Does not do redirects, * https, full header parsing or stuff. - * - * TODO extract API interface to be used by UrlTileSource so that one - * could also use HttpUrlConnection, etc. */ -public class LwHttp { +public class LwHttp implements HttpEngine { static final Logger log = LoggerFactory.getLogger(LwHttp.class); static final boolean dbg = false; @@ -332,6 +329,7 @@ public class LwHttp { return is; } + @Override public boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException { if (mSocket != null) { @@ -405,6 +403,7 @@ public class LwHttp { return true; } + @Override public void close() { if (mSocket == null) return; @@ -415,6 +414,7 @@ public class LwHttp { mResponseStream = null; } + @Override public void setCache(OutputStream os) { if (mResponseStream == null) return; @@ -422,6 +422,7 @@ public class LwHttp { mResponseStream.setCache(os); } + @Override public boolean requestCompleted(boolean success) { if (mResponseStream == null) return false; diff --git a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java new file mode 100644 index 00000000..e68d31a0 --- /dev/null +++ b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java @@ -0,0 +1,70 @@ +package org.oscim.tiling.source; + +import com.squareup.okhttp.OkHttpClient; + +import org.oscim.core.Tile; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +public class OkHttpEngine implements HttpEngine { + private final URL baseUrl; + private final OkHttpClient client; + private InputStream inputStream; + + public OkHttpEngine(URL baseUrl) { + this.baseUrl = baseUrl; + this.client = new OkHttpClient(); + } + + @Override + public InputStream read() throws IOException { + return inputStream; + } + + @Override + public boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException { + final URL requestUrl = new URL(baseUrl.toString() + + "/" + + Byte.toString(tile.zoomLevel) + + "/" + + tile.tileX + + "/" + + tile.tileY + + ".vtm"); + + final HttpURLConnection connection = client.open(requestUrl); + + try { + inputStream = connection.getInputStream(); + } catch (Exception e) { + e.printStackTrace(); + } + + return true; + } + + @Override + public void close() { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @Override + public void setCache(OutputStream os) { + // TODO: Evaluate OkHttp response cache and determine if additional caching is required. + } + + @Override + public boolean requestCompleted(boolean success) { + return true; + } +} diff --git a/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java b/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java index f26c90c0..a97272c3 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java @@ -38,12 +38,12 @@ import org.slf4j.LoggerFactory; public class UrlTileDataSource implements ITileDataSource { static final Logger log = LoggerFactory.getLogger(UrlTileDataSource.class); - protected final LwHttp mConn; + protected final HttpEngine mConn; protected final ITileDecoder mTileDecoder; protected final UrlTileSource mTileSource; protected final boolean mUseCache; - public UrlTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder, LwHttp conn) { + public UrlTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder, HttpEngine conn) { mTileDecoder = tileDecoder; mTileSource = tileSource; mUseCache = (tileSource.tileCache != null); diff --git a/vtm/src/org/oscim/tiling/source/UrlTileSource.java b/vtm/src/org/oscim/tiling/source/UrlTileSource.java index 89515d14..d6ff2b99 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileSource.java @@ -26,6 +26,7 @@ public abstract class UrlTileSource extends TileSource { private final URL mUrl; private byte[] mExt; + private HttpEngine httpEngine; public UrlTileSource(String urlString) { URL url = null; @@ -107,4 +108,16 @@ public abstract class UrlTileSource extends TileSource { public URL getUrl() { return mUrl; } + + public void setHttpEngine(HttpEngine httpEngine) { + this.httpEngine = httpEngine; + } + + public HttpEngine getHttpEngine() { + if (httpEngine == null) { + httpEngine = new LwHttp(getUrl()); + } + + return httpEngine; + } } diff --git a/vtm/src/org/oscim/tiling/source/bitmap/BitmapTileSource.java b/vtm/src/org/oscim/tiling/source/bitmap/BitmapTileSource.java index 92a718de..40197f63 100644 --- a/vtm/src/org/oscim/tiling/source/bitmap/BitmapTileSource.java +++ b/vtm/src/org/oscim/tiling/source/bitmap/BitmapTileSource.java @@ -32,7 +32,7 @@ public class BitmapTileSource extends UrlTileSource { @Override public ITileDataSource getDataSource() { - return new UrlTileDataSource(this, new BitmapTileDecoder(), new LwHttp(getUrl())); + return new UrlTileDataSource(this, new BitmapTileDecoder(), getHttpEngine()); } public class BitmapTileDecoder implements ITileDecoder { diff --git a/vtm/src/org/oscim/tiling/source/oscimap4/OSciMap4TileSource.java b/vtm/src/org/oscim/tiling/source/oscimap4/OSciMap4TileSource.java index b5dbc5a8..3d2788a7 100644 --- a/vtm/src/org/oscim/tiling/source/oscimap4/OSciMap4TileSource.java +++ b/vtm/src/org/oscim/tiling/source/oscimap4/OSciMap4TileSource.java @@ -17,7 +17,6 @@ package org.oscim.tiling.source.oscimap4; import org.oscim.tiling.ITileDataSource; -import org.oscim.tiling.source.LwHttp; import org.oscim.tiling.source.UrlTileDataSource; import org.oscim.tiling.source.UrlTileSource; @@ -34,6 +33,6 @@ public class OSciMap4TileSource extends UrlTileSource { @Override public ITileDataSource getDataSource() { - return new UrlTileDataSource(this, new TileDecoder(), new LwHttp(getUrl())); + return new UrlTileDataSource(this, new TileDecoder(), getHttpEngine()); } } diff --git a/vtm/test/org/oscim/tiling/source/UrlTileSourceTest.java b/vtm/test/org/oscim/tiling/source/UrlTileSourceTest.java new file mode 100644 index 00000000..b708cb40 --- /dev/null +++ b/vtm/test/org/oscim/tiling/source/UrlTileSourceTest.java @@ -0,0 +1,58 @@ +package org.oscim.tiling.source; + +import com.squareup.okhttp.OkHttpClient; + +import org.junit.Before; +import org.junit.Test; +import org.oscim.tiling.ITileDataSource; + +import static org.fest.assertions.api.Assertions.assertThat; + +public class UrlTileSourceTest { + private UrlTileSource tileSource; + + @Before + public void setUp() throws Exception { + tileSource = new TestTileSource("http://example.org/tiles/vtm"); + } + + @Test + public void shouldNotBeNull() throws Exception { + assertThat(tileSource).isNotNull(); + } + + @Test + public void shouldUseDefaultHttpEngine() throws Exception { + TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource(); + assertThat(dataSource.getConnection()).isInstanceOf(LwHttp.class); + } + + @Test + public void shouldUseCustomHttpEngine() throws Exception { + tileSource.setHttpEngine(new OkHttpEngine(tileSource.getUrl())); + TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource(); + assertThat(dataSource.getConnection()).isInstanceOf(OkHttpEngine.class); + } + + class TestTileSource extends UrlTileSource { + public TestTileSource(String urlString) { + super(urlString); + } + + @Override + public ITileDataSource getDataSource() { + return new TestTileDataSource(this, null, getHttpEngine()); + } + } + + class TestTileDataSource extends UrlTileDataSource { + public TestTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder, + HttpEngine conn) { + super(tileSource, tileDecoder, conn); + } + + public HttpEngine getConnection() { + return mConn; + } + } +} diff --git a/vtm/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java b/vtm/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java new file mode 100644 index 00000000..8fde2545 --- /dev/null +++ b/vtm/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java @@ -0,0 +1,48 @@ +package org.oscim.tiling.source.bitmap; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.oscim.tiling.ITileDataSource; +import org.oscim.tiling.source.LwHttp; +import org.oscim.tiling.source.OkHttpEngine; + +import static org.fest.assertions.api.Assertions.assertThat; + +public class BitmapTileSourceTest { + private BitmapTileSource tileSource; + + @Before + public void setUp() throws Exception { + tileSource = new TestBitmapTileSource("http://tile.openstreetmap.org", 0, 18); + } + + @Test + public void shouldNotBeNull() throws Exception { + assertThat(tileSource).isNotNull(); + } + + @Test + public void shouldUseLwHttp() throws Exception { + LwHttp lwHttp = Mockito.mock(LwHttp.class); + tileSource.setHttpEngine(lwHttp); + ITileDataSource dataSource = tileSource.getDataSource(); + dataSource.destroy(); + Mockito.verify(lwHttp).close(); + } + + @Test + public void shouldUseOkHttp() throws Exception { + OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class); + tileSource.setHttpEngine(okHttp); + ITileDataSource dataSource = tileSource.getDataSource(); + dataSource.destroy(); + Mockito.verify(okHttp).close(); + } + + class TestBitmapTileSource extends BitmapTileSource { + public TestBitmapTileSource(String url, int zoomMin, int zoomMax) { + super(url, zoomMin, zoomMax); + } + } +} diff --git a/vtm/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java b/vtm/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java new file mode 100644 index 00000000..711cd310 --- /dev/null +++ b/vtm/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java @@ -0,0 +1,42 @@ +package org.oscim.tiling.source.oscimap4; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.oscim.tiling.ITileDataSource; +import org.oscim.tiling.source.LwHttp; +import org.oscim.tiling.source.OkHttpEngine; + +import static org.fest.assertions.api.Assertions.assertThat; + +public class OSciMap4TileSourceTest { + private OSciMap4TileSource tileSource; + + @Before + public void setUp() throws Exception { + tileSource = new OSciMap4TileSource("http://www.example.org/tiles/vtm"); + } + + @Test + public void shouldNotBeNull() throws Exception { + assertThat(tileSource).isNotNull(); + } + + @Test + public void shouldUseLwHttp() throws Exception { + LwHttp lwHttp = Mockito.mock(LwHttp.class); + tileSource.setHttpEngine(lwHttp); + ITileDataSource dataSource = tileSource.getDataSource(); + dataSource.destroy(); + Mockito.verify(lwHttp).close(); + } + + @Test + public void shouldUseOkHttp() throws Exception { + OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class); + tileSource.setHttpEngine(okHttp); + ITileDataSource dataSource = tileSource.getDataSource(); + dataSource.destroy(); + Mockito.verify(okHttp).close(); + } +} From f3035d827b00778ffec69041b74423f07ae0d7be Mon Sep 17 00:00:00 2001 From: Chuck Greb Date: Fri, 28 Mar 2014 15:57:31 -0400 Subject: [PATCH 02/16] Tests OkHttp integration --- vtm/build.gradle | 1 + .../org/oscim/tiling/source/OkHttpEngine.java | 7 +- .../oscim/tiling/source/OkHttpEngineTest.java | 97 +++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 vtm/test/org/oscim/tiling/source/OkHttpEngineTest.java diff --git a/vtm/build.gradle b/vtm/build.gradle index adb2f2b0..f27d0b20 100644 --- a/vtm/build.gradle +++ b/vtm/build.gradle @@ -10,6 +10,7 @@ dependencies { testCompile 'junit:junit:4.11' testCompile 'org.mockito:mockito-all:1.9.5' testCompile 'org.easytesting:fest-assert-core:2.0M10' + testCompile 'com.squareup.okhttp:mockwebserver:1.5.2' } sourceSets { diff --git a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java index e68d31a0..6c5dc423 100644 --- a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java @@ -27,6 +27,10 @@ public class OkHttpEngine implements HttpEngine { @Override public boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException { + if (tile == null) { + throw new IllegalArgumentException("Tile cannot be null."); + } + final URL requestUrl = new URL(baseUrl.toString() + "/" + Byte.toString(tile.zoomLevel) @@ -65,6 +69,7 @@ public class OkHttpEngine implements HttpEngine { @Override public boolean requestCompleted(boolean success) { - return true; + close(); + return success; } } diff --git a/vtm/test/org/oscim/tiling/source/OkHttpEngineTest.java b/vtm/test/org/oscim/tiling/source/OkHttpEngineTest.java new file mode 100644 index 00000000..d97aef2c --- /dev/null +++ b/vtm/test/org/oscim/tiling/source/OkHttpEngineTest.java @@ -0,0 +1,97 @@ +package org.oscim.tiling.source; + +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; +import org.oscim.core.Tile; +import org.oscim.tiling.source.oscimap4.OSciMap4TileSource; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; + +import static org.fest.assertions.api.Assertions.assertThat; + +public class OkHttpEngineTest { + private OkHttpEngine engine; + private MockWebServer server; + private MockResponse mockResponse; + + @Before + public void setUp() throws Exception { + mockResponse = new MockResponse(); + mockResponse.setBody("TEST RESPONSE".getBytes()); + server = new MockWebServer(); + server.enqueue(mockResponse); + server.play(); + engine = new OkHttpEngine(server.getUrl("/tiles/vtm")); + } + + @After + public void tearDown() throws Exception { + server.shutdown(); + } + + @Test + public void shouldNotBeNull() throws Exception { + assertThat(engine).isNotNull(); + } + + @Test(expected = IllegalArgumentException.class) + public void sendRequest_shouldRejectNullTile() throws Exception { + engine.sendRequest(null, null); + } + + @Test + public void sendRequest_shouldAppendXYZToPath() throws Exception { + engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"), + new Tile(1, 2, new Integer(3).byteValue())); + + RecordedRequest request = server.takeRequest(); + assertThat(request.getPath()).isEqualTo("/tiles/vtm/3/1/2.vtm"); + } + + @Test + public void read_shouldReturnResponseStream() throws Exception { + engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"), + new Tile(1, 2, new Integer(3).byteValue())); + + InputStream responseStream = engine.read(); + String response = new BufferedReader(new InputStreamReader(responseStream)).readLine(); + assertThat(response).isEqualTo("TEST RESPONSE"); + } + + @Test(expected = IOException.class) + public void close_shouldCloseInputStream() throws Exception { + engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"), + new Tile(1, 2, new Integer(3).byteValue())); + engine.close(); + + // Calling read after the stream is closed should throw an exception. + InputStream responseStream = engine.read(); + responseStream.read(); + } + + @Test(expected = IOException.class) + public void requestCompleted_shouldCloseInputStream() throws Exception { + engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"), + new Tile(1, 2, new Integer(3).byteValue())); + engine.requestCompleted(true); + + // Calling read after the stream is closed should throw an exception. + InputStream responseStream = engine.read(); + responseStream.read(); + } + + @Test + public void requestCompleted_shouldReturnValueGiven() throws Exception { + assertThat(engine.requestCompleted(true)).isTrue(); + assertThat(engine.requestCompleted(false)).isFalse(); + } +} From f75702a575d423c82c88c9f2734afe39dde2bc65 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Sat, 29 Mar 2014 01:35:59 +0100 Subject: [PATCH 03/16] create HttpEngine instances for each loader thread --- .../org/oscim/tiling/source/HttpEngine.java | 19 +++++--- .../org/oscim/tiling/source/OkHttpEngine.java | 44 ++++++++++++------- .../oscim/tiling/source/UrlTileSource.java | 12 ++--- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/vtm/src/org/oscim/tiling/source/HttpEngine.java b/vtm/src/org/oscim/tiling/source/HttpEngine.java index bf3f3240..a843a1d6 100644 --- a/vtm/src/org/oscim/tiling/source/HttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/HttpEngine.java @@ -1,19 +1,24 @@ package org.oscim.tiling.source; -import org.oscim.core.Tile; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import org.oscim.core.Tile; + public interface HttpEngine { - InputStream read() throws IOException; - boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException; + InputStream read() throws IOException; - void close(); + boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException; - void setCache(OutputStream os); + void close(); - boolean requestCompleted(boolean success); + void setCache(OutputStream os); + + boolean requestCompleted(boolean success); + + public interface Factory { + public HttpEngine create(); + } } diff --git a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java index 6c5dc423..2f4c7349 100644 --- a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java @@ -1,23 +1,35 @@ package org.oscim.tiling.source; -import com.squareup.okhttp.OkHttpClient; - -import org.oscim.core.Tile; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import org.oscim.core.Tile; + +import com.squareup.okhttp.OkHttpClient; + public class OkHttpEngine implements HttpEngine { - private final URL baseUrl; private final OkHttpClient client; + + public static class OkHttpFactory implements HttpEngine.Factory { + private final OkHttpClient client; + + public OkHttpFactory() { + this.client = new OkHttpClient(); + } + + @Override + public HttpEngine create() { + return new OkHttpEngine(client); + } + } + private InputStream inputStream; - public OkHttpEngine(URL baseUrl) { - this.baseUrl = baseUrl; - this.client = new OkHttpClient(); + public OkHttpEngine(OkHttpClient client) { + this.client = client; } @Override @@ -31,14 +43,14 @@ public class OkHttpEngine implements HttpEngine { throw new IllegalArgumentException("Tile cannot be null."); } - final URL requestUrl = new URL(baseUrl.toString() - + "/" - + Byte.toString(tile.zoomLevel) - + "/" - + tile.tileX - + "/" - + tile.tileY - + ".vtm"); + final URL requestUrl = new URL(tileSource.getUrl() + + "/" + + Byte.toString(tile.zoomLevel) + + "/" + + tile.tileX + + "/" + + tile.tileY + + ".vtm"); final HttpURLConnection connection = client.open(requestUrl); diff --git a/vtm/src/org/oscim/tiling/source/UrlTileSource.java b/vtm/src/org/oscim/tiling/source/UrlTileSource.java index d6ff2b99..3d00bbd4 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileSource.java @@ -26,7 +26,7 @@ public abstract class UrlTileSource extends TileSource { private final URL mUrl; private byte[] mExt; - private HttpEngine httpEngine; + private HttpEngine.Factory mHttpFactory; public UrlTileSource(String urlString) { URL url = null; @@ -109,15 +109,15 @@ public abstract class UrlTileSource extends TileSource { return mUrl; } - public void setHttpEngine(HttpEngine httpEngine) { - this.httpEngine = httpEngine; + public void setHttpEngine(HttpEngine.Factory httpFactory) { + mHttpFactory = httpFactory; } public HttpEngine getHttpEngine() { - if (httpEngine == null) { - httpEngine = new LwHttp(getUrl()); + if (mHttpFactory == null) { + return new LwHttp(getUrl()); } - return httpEngine; + return mHttpFactory.create(); } } From 2bf5313c2be0f46efd4349f2a1f4b1df3a3970fa Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Sat, 29 Mar 2014 12:54:12 +0100 Subject: [PATCH 04/16] move tests to separate project --- settings.gradle | 1 + vtm-tests/build.gradle | 15 +++++++++++++++ .../oscim/tiling/source/OkHttpEngineTest.java | 0 .../oscim/tiling/source/UrlTileSourceTest.java | 0 .../source/bitmap/BitmapTileSourceTest.java | 16 ++++++++-------- .../source/oscimap4/OSciMap4TileSourceTest.java | 0 vtm/build.gradle | 5 ----- 7 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 vtm-tests/build.gradle rename {vtm => vtm-tests}/test/org/oscim/tiling/source/OkHttpEngineTest.java (100%) rename {vtm => vtm-tests}/test/org/oscim/tiling/source/UrlTileSourceTest.java (100%) rename {vtm => vtm-tests}/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java (74%) rename {vtm => vtm-tests}/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java (100%) diff --git a/settings.gradle b/settings.gradle index 3cbd0ee1..06ecb65d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ include ':vtm' +include ':vtm-tests' include ':vtm-extras' include ':vtm-android' include ':vtm-android-example' diff --git a/vtm-tests/build.gradle b/vtm-tests/build.gradle new file mode 100644 index 00000000..3acb6285 --- /dev/null +++ b/vtm-tests/build.gradle @@ -0,0 +1,15 @@ +apply plugin: 'java' +apply plugin: 'maven' + +dependencies { + compile project(':vtm') + testCompile 'junit:junit:4.11' + testCompile 'org.mockito:mockito-all:1.9.5' + testCompile 'org.easytesting:fest-assert-core:2.0M10' + testCompile 'com.squareup.okhttp:mockwebserver:1.5.2' +} + +sourceSets { + main.java.srcDirs = ['src'] + test.java.srcDirs = ['test'] +} diff --git a/vtm/test/org/oscim/tiling/source/OkHttpEngineTest.java b/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java similarity index 100% rename from vtm/test/org/oscim/tiling/source/OkHttpEngineTest.java rename to vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java diff --git a/vtm/test/org/oscim/tiling/source/UrlTileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java similarity index 100% rename from vtm/test/org/oscim/tiling/source/UrlTileSourceTest.java rename to vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java diff --git a/vtm/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java similarity index 74% rename from vtm/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java rename to vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java index 8fde2545..34f87ce5 100644 --- a/vtm/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java @@ -1,13 +1,13 @@ package org.oscim.tiling.source.bitmap; +import static org.fest.assertions.api.Assertions.assertThat; + import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.oscim.tiling.ITileDataSource; -import org.oscim.tiling.source.LwHttp; -import org.oscim.tiling.source.OkHttpEngine; - -import static org.fest.assertions.api.Assertions.assertThat; +import org.oscim.tiling.source.OkHttpEngine.OkHttpFactory; +import org.oscim.tiling.source.UrlTileDataSource; public class BitmapTileSourceTest { private BitmapTileSource tileSource; @@ -24,7 +24,7 @@ public class BitmapTileSourceTest { @Test public void shouldUseLwHttp() throws Exception { - LwHttp lwHttp = Mockito.mock(LwHttp.class); + LwHttpFactory lwHttp = Mockito.mock(LwHttpFactory.class); tileSource.setHttpEngine(lwHttp); ITileDataSource dataSource = tileSource.getDataSource(); dataSource.destroy(); @@ -33,11 +33,11 @@ public class BitmapTileSourceTest { @Test public void shouldUseOkHttp() throws Exception { - OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class); + OkHttpFactory okHttp = Mockito.mock(OkHttpFactory.class); tileSource.setHttpEngine(okHttp); - ITileDataSource dataSource = tileSource.getDataSource(); + UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource(); dataSource.destroy(); - Mockito.verify(okHttp).close(); + //Mockito.verify(dataSource.mConn).close(); } class TestBitmapTileSource extends BitmapTileSource { diff --git a/vtm/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java similarity index 100% rename from vtm/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java rename to vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java diff --git a/vtm/build.gradle b/vtm/build.gradle index f27d0b20..6f148d81 100644 --- a/vtm/build.gradle +++ b/vtm/build.gradle @@ -7,15 +7,10 @@ dependencies { compile 'org.slf4j:slf4j-api:1.7.6' compile 'com.squareup.okhttp:okhttp:1.5.2' providedCompile 'com.google.code.findbugs:annotations:2.0.1' - testCompile 'junit:junit:4.11' - testCompile 'org.mockito:mockito-all:1.9.5' - testCompile 'org.easytesting:fest-assert-core:2.0M10' - testCompile 'com.squareup.okhttp:mockwebserver:1.5.2' } sourceSets { main.java.srcDirs = ['src'] - test.java.srcDirs = ['test'] main.resources.srcDirs = ['resources'] main.compileClasspath += configurations.providedCompile } From d709d7f39a77cbcec55f0e57b40a0396719c7a6e Mon Sep 17 00:00:00 2001 From: Chuck Greb Date: Mon, 31 Mar 2014 13:09:06 -0400 Subject: [PATCH 05/16] Fixes HttpEngine tests --- .../oscim/tiling/source/OkHttpEngineTest.java | 11 +++---- .../tiling/source/UrlTileSourceTest.java | 4 +-- .../source/bitmap/BitmapTileSourceTest.java | 32 +++++++++++++------ .../oscimap4/OSciMap4TileSourceTest.java | 21 ++++++++++-- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java b/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java index d97aef2c..45ff6b89 100644 --- a/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java @@ -14,7 +14,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.net.URL; import static org.fest.assertions.api.Assertions.assertThat; @@ -30,7 +29,7 @@ public class OkHttpEngineTest { server = new MockWebServer(); server.enqueue(mockResponse); server.play(); - engine = new OkHttpEngine(server.getUrl("/tiles/vtm")); + engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory().create(); } @After @@ -50,7 +49,7 @@ public class OkHttpEngineTest { @Test public void sendRequest_shouldAppendXYZToPath() throws Exception { - engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"), + engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), new Tile(1, 2, new Integer(3).byteValue())); RecordedRequest request = server.takeRequest(); @@ -59,7 +58,7 @@ public class OkHttpEngineTest { @Test public void read_shouldReturnResponseStream() throws Exception { - engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"), + engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), new Tile(1, 2, new Integer(3).byteValue())); InputStream responseStream = engine.read(); @@ -69,7 +68,7 @@ public class OkHttpEngineTest { @Test(expected = IOException.class) public void close_shouldCloseInputStream() throws Exception { - engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"), + engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), new Tile(1, 2, new Integer(3).byteValue())); engine.close(); @@ -80,7 +79,7 @@ public class OkHttpEngineTest { @Test(expected = IOException.class) public void requestCompleted_shouldCloseInputStream() throws Exception { - engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"), + engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), new Tile(1, 2, new Integer(3).byteValue())); engine.requestCompleted(true); diff --git a/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java index b708cb40..395738c6 100644 --- a/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java @@ -1,7 +1,5 @@ package org.oscim.tiling.source; -import com.squareup.okhttp.OkHttpClient; - import org.junit.Before; import org.junit.Test; import org.oscim.tiling.ITileDataSource; @@ -29,7 +27,7 @@ public class UrlTileSourceTest { @Test public void shouldUseCustomHttpEngine() throws Exception { - tileSource.setHttpEngine(new OkHttpEngine(tileSource.getUrl())); + tileSource.setHttpEngine(new OkHttpEngine.OkHttpFactory()); TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource(); assertThat(dataSource.getConnection()).isInstanceOf(OkHttpEngine.class); } diff --git a/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java index 34f87ce5..f0f93560 100644 --- a/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java @@ -6,7 +6,9 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.oscim.tiling.ITileDataSource; -import org.oscim.tiling.source.OkHttpEngine.OkHttpFactory; +import org.oscim.tiling.source.HttpEngine; +import org.oscim.tiling.source.LwHttp; +import org.oscim.tiling.source.OkHttpEngine; import org.oscim.tiling.source.UrlTileDataSource; public class BitmapTileSourceTest { @@ -14,7 +16,7 @@ public class BitmapTileSourceTest { @Before public void setUp() throws Exception { - tileSource = new TestBitmapTileSource("http://tile.openstreetmap.org", 0, 18); + tileSource = new BitmapTileSource("http://tile.openstreetmap.org", 0, 18); } @Test @@ -24,8 +26,8 @@ public class BitmapTileSourceTest { @Test public void shouldUseLwHttp() throws Exception { - LwHttpFactory lwHttp = Mockito.mock(LwHttpFactory.class); - tileSource.setHttpEngine(lwHttp); + LwHttp lwHttp = Mockito.mock(LwHttp.class); + tileSource.setHttpEngine(new TestHttpFactory(lwHttp)); ITileDataSource dataSource = tileSource.getDataSource(); dataSource.destroy(); Mockito.verify(lwHttp).close(); @@ -33,16 +35,26 @@ public class BitmapTileSourceTest { @Test public void shouldUseOkHttp() throws Exception { - OkHttpFactory okHttp = Mockito.mock(OkHttpFactory.class); - tileSource.setHttpEngine(okHttp); + OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class); + tileSource.setHttpEngine(new TestHttpFactory(okHttp)); UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource(); dataSource.destroy(); - //Mockito.verify(dataSource.mConn).close(); + Mockito.verify(okHttp).close(); } - class TestBitmapTileSource extends BitmapTileSource { - public TestBitmapTileSource(String url, int zoomMin, int zoomMax) { - super(url, zoomMin, zoomMax); + /** + * Test factory that allows the specific {@link HttpEngine} instance to be set. + */ + class TestHttpFactory implements HttpEngine.Factory { + final HttpEngine engine; + + public TestHttpFactory(HttpEngine engine) { + this.engine = engine; + } + + @Override + public HttpEngine create() { + return engine; } } } diff --git a/vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java index 711cd310..060d0c77 100644 --- a/vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java @@ -4,6 +4,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.oscim.tiling.ITileDataSource; +import org.oscim.tiling.source.HttpEngine; import org.oscim.tiling.source.LwHttp; import org.oscim.tiling.source.OkHttpEngine; @@ -25,7 +26,7 @@ public class OSciMap4TileSourceTest { @Test public void shouldUseLwHttp() throws Exception { LwHttp lwHttp = Mockito.mock(LwHttp.class); - tileSource.setHttpEngine(lwHttp); + tileSource.setHttpEngine(new TestHttpFactory(lwHttp)); ITileDataSource dataSource = tileSource.getDataSource(); dataSource.destroy(); Mockito.verify(lwHttp).close(); @@ -34,9 +35,25 @@ public class OSciMap4TileSourceTest { @Test public void shouldUseOkHttp() throws Exception { OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class); - tileSource.setHttpEngine(okHttp); + tileSource.setHttpEngine(new TestHttpFactory(okHttp)); ITileDataSource dataSource = tileSource.getDataSource(); dataSource.destroy(); Mockito.verify(okHttp).close(); } + + /** + * Test factory that allows the specific {@link HttpEngine} instance to be set. + */ + class TestHttpFactory implements HttpEngine.Factory { + final HttpEngine engine; + + public TestHttpFactory(HttpEngine engine) { + this.engine = engine; + } + + @Override + public HttpEngine create() { + return engine; + } + } } From 9c4e04c4d672e02050294fa3e94f665f6cecea9d Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Tue, 1 Apr 2014 03:25:00 +0200 Subject: [PATCH 06/16] api: UrlTileSource - use replacement string for tilePath - move 'low-level' formatTilePath to LwHttp - implement LwHttpFactory --- .../source/geojson/GeoJsonTileSource.java | 18 +-- .../source/mapnik/MapnikVectorTileSource.java | 29 ++--- .../source/oscimap/OSciMap1TileSource.java | 7 +- .../source/oscimap2/OSciMap2TileSource.java | 7 +- .../org/oscim/tiling/source/HttpEngine.java | 5 +- vtm/src/org/oscim/tiling/source/LwHttp.java | 66 +++++++--- .../org/oscim/tiling/source/OkHttpEngine.java | 41 +++--- .../tiling/source/UrlTileDataSource.java | 2 +- .../oscim/tiling/source/UrlTileSource.java | 122 +++++++++--------- .../source/bitmap/BitmapTileSource.java | 11 +- .../tiling/source/bitmap/DefaultSources.java | 55 ++------ .../source/oscimap4/OSciMap4TileSource.java | 3 +- 12 files changed, 178 insertions(+), 188 deletions(-) diff --git a/vtm-extras/src/org/oscim/tiling/source/geojson/GeoJsonTileSource.java b/vtm-extras/src/org/oscim/tiling/source/geojson/GeoJsonTileSource.java index e381f6a1..eb83cdd2 100644 --- a/vtm-extras/src/org/oscim/tiling/source/geojson/GeoJsonTileSource.java +++ b/vtm-extras/src/org/oscim/tiling/source/geojson/GeoJsonTileSource.java @@ -22,27 +22,29 @@ import java.util.Map; import org.oscim.core.MapElement; import org.oscim.core.Tag; import org.oscim.tiling.ITileDataSource; -import org.oscim.tiling.source.LwHttp; import org.oscim.tiling.source.UrlTileDataSource; import org.oscim.tiling.source.UrlTileSource; public abstract class GeoJsonTileSource extends UrlTileSource { public GeoJsonTileSource(String url) { - super(url); - setExtension(".json"); + super(url, "/{Z}/{X}/{Y}.json"); + Map opt = new HashMap(); + opt.put("Accept-Encoding", "gzip"); + setHttpRequestHeaders(opt); } public GeoJsonTileSource(String url, int zoomMin, int zoomMax) { - super(url, zoomMin, zoomMax); - setExtension(".json"); + super(url, "/{Z}/{X}/{Y}.json", zoomMin, zoomMax); + Map opt = new HashMap(); + opt.put("Accept-Encoding", "gzip"); + setHttpRequestHeaders(opt); } @Override public ITileDataSource getDataSource() { - Map opt = new HashMap(); - opt.put("Accept-Encoding", "gzip"); - return new UrlTileDataSource(this, new GeoJsonTileDecoder(this), new LwHttp(getUrl(), opt)); + + return new UrlTileDataSource(this, new GeoJsonTileDecoder(this), getHttpEngine()); } public Tag getFeatureTag() { diff --git a/vtm-extras/src/org/oscim/tiling/source/mapnik/MapnikVectorTileSource.java b/vtm-extras/src/org/oscim/tiling/source/mapnik/MapnikVectorTileSource.java index abb551ad..60d1ee03 100644 --- a/vtm-extras/src/org/oscim/tiling/source/mapnik/MapnikVectorTileSource.java +++ b/vtm-extras/src/org/oscim/tiling/source/mapnik/MapnikVectorTileSource.java @@ -18,22 +18,21 @@ package org.oscim.tiling.source.mapnik; import org.oscim.core.Tile; import org.oscim.tiling.ITileDataSource; -import org.oscim.tiling.source.LwHttp; import org.oscim.tiling.source.UrlTileDataSource; import org.oscim.tiling.source.UrlTileSource; public class MapnikVectorTileSource extends UrlTileSource { public MapnikVectorTileSource() { - super("http://d1s11ojcu7opje.cloudfront.net/dev/764e0b8d"); + super("http://d1s11ojcu7opje.cloudfront.net/dev/764e0b8d", ""); } @Override public ITileDataSource getDataSource() { - return new UrlTileDataSource(this, new TileDecoder(), new LwHttp(getUrl())); + return new UrlTileDataSource(this, new TileDecoder(), getHttpEngine()); } - public int formatTilePath(Tile tile, byte[] path, int pos) { + public String formatTilePath(Tile tile) { // url formatter for mapbox streets byte[] hexTable = { '0', '1', '2', '3', @@ -41,17 +40,17 @@ public class MapnikVectorTileSource extends UrlTileSource { '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + StringBuilder sb = new StringBuilder(); + sb.append('/'); + sb.append(hexTable[(tile.tileX) % 16]); + sb.append(hexTable[(tile.tileY) % 16]); + sb.append('/'); + sb.append(tile.zoomLevel); + sb.append('/'); + sb.append(tile.tileX); + sb.append('/'); + sb.append(tile.tileY); - path[pos++] = '/'; - path[pos++] = hexTable[(tile.tileX) % 16]; - path[pos++] = hexTable[(tile.tileY) % 16]; - path[pos++] = '/'; - pos = LwHttp.writeInt(tile.zoomLevel, pos, path); - path[pos++] = '/'; - pos = LwHttp.writeInt(tile.tileX, pos, path); - path[pos++] = '/'; - pos = LwHttp.writeInt(tile.tileY, pos, path); - - return pos; + return sb.toString(); } } diff --git a/vtm-extras/src/org/oscim/tiling/source/oscimap/OSciMap1TileSource.java b/vtm-extras/src/org/oscim/tiling/source/oscimap/OSciMap1TileSource.java index 50ae2b26..32626519 100644 --- a/vtm-extras/src/org/oscim/tiling/source/oscimap/OSciMap1TileSource.java +++ b/vtm-extras/src/org/oscim/tiling/source/oscimap/OSciMap1TileSource.java @@ -17,7 +17,6 @@ package org.oscim.tiling.source.oscimap; import org.oscim.tiling.ITileDataSource; -import org.oscim.tiling.source.LwHttp; import org.oscim.tiling.source.UrlTileDataSource; import org.oscim.tiling.source.UrlTileSource; @@ -28,13 +27,11 @@ import org.oscim.tiling.source.UrlTileSource; public class OSciMap1TileSource extends UrlTileSource { public OSciMap1TileSource(String url) { - super(url); - setExtension(".osmtile"); - setMimeType("application/osmtile"); + super(url, "/{Z}/{X}/{Y}.osmtile"); } @Override public ITileDataSource getDataSource() { - return new UrlTileDataSource(this, new TileDecoder(), new LwHttp(getUrl())); + return new UrlTileDataSource(this, new TileDecoder(), getHttpEngine()); } } diff --git a/vtm-extras/src/org/oscim/tiling/source/oscimap2/OSciMap2TileSource.java b/vtm-extras/src/org/oscim/tiling/source/oscimap2/OSciMap2TileSource.java index 77f881e2..54aa0bf9 100644 --- a/vtm-extras/src/org/oscim/tiling/source/oscimap2/OSciMap2TileSource.java +++ b/vtm-extras/src/org/oscim/tiling/source/oscimap2/OSciMap2TileSource.java @@ -27,7 +27,6 @@ import org.oscim.core.TagSet; import org.oscim.core.Tile; import org.oscim.tiling.ITileDataSink; import org.oscim.tiling.ITileDataSource; -import org.oscim.tiling.source.LwHttp; import org.oscim.tiling.source.PbfDecoder; import org.oscim.tiling.source.UrlTileDataSource; import org.oscim.tiling.source.UrlTileSource; @@ -37,14 +36,12 @@ import org.slf4j.LoggerFactory; public class OSciMap2TileSource extends UrlTileSource { public OSciMap2TileSource(String url) { - super(url); - setExtension(".osmtile"); - setMimeType("application/osmtile"); + super(url, "/{Z}/{X}/{Y}.osmtile"); } @Override public ITileDataSource getDataSource() { - return new UrlTileDataSource(this, new TileDecoder(), new LwHttp(getUrl())); + return new UrlTileDataSource(this, new TileDecoder(), getHttpEngine()); } static class TileDecoder extends PbfDecoder { diff --git a/vtm/src/org/oscim/tiling/source/HttpEngine.java b/vtm/src/org/oscim/tiling/source/HttpEngine.java index a843a1d6..63b29948 100644 --- a/vtm/src/org/oscim/tiling/source/HttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/HttpEngine.java @@ -10,7 +10,7 @@ public interface HttpEngine { InputStream read() throws IOException; - boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException; + boolean sendRequest(Tile tile) throws IOException; void close(); @@ -19,6 +19,7 @@ public interface HttpEngine { boolean requestCompleted(boolean success); public interface Factory { - public HttpEngine create(); + HttpEngine create(UrlTileSource tileSource); } + } diff --git a/vtm/src/org/oscim/tiling/source/LwHttp.java b/vtm/src/org/oscim/tiling/source/LwHttp.java index 8f3978ff..ac8abd80 100644 --- a/vtm/src/org/oscim/tiling/source/LwHttp.java +++ b/vtm/src/org/oscim/tiling/source/LwHttp.java @@ -24,7 +24,6 @@ import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.URL; -import java.util.Map; import java.util.Map.Entry; import org.oscim.core.Tile; @@ -41,6 +40,8 @@ public class LwHttp implements HttpEngine { static final Logger log = LoggerFactory.getLogger(LwHttp.class); static final boolean dbg = false; + private final UrlTileSource mTileSource; + private final static byte[] HEADER_HTTP_OK = "200 OK".getBytes(); //private final static byte[] HEADER_CONTENT_TYPE = "Content-Type".getBytes(); private final static byte[] HEADER_CONTENT_LENGTH = "Content-Length".getBytes(); @@ -69,16 +70,9 @@ public class LwHttp implements HttpEngine { private final byte[] REQUEST_GET_END; private final byte[] mRequestBuffer; - /** - * @param url - * Base url for tiles - */ - public LwHttp(URL url) { - this(url, null); - } - - public LwHttp(URL url, Map header) { - + private LwHttp(UrlTileSource tileSource) { + mTileSource = tileSource; + URL url = tileSource.getUrl(); int port = url.getPort(); if (port < 0) port = 80; @@ -90,9 +84,9 @@ public class LwHttp implements HttpEngine { REQUEST_GET_START = ("GET " + path).getBytes(); String addRequest = ""; - if (header != null) { + if (tileSource.getRequestHeader() != null) { StringBuffer sb = new StringBuffer(); - for (Entry l : header.entrySet()) + for (Entry l : tileSource.getRequestHeader().entrySet()) sb.append('\n').append(l.getKey()).append(": ").append(l.getValue()); addRequest = sb.toString(); } @@ -330,7 +324,7 @@ public class LwHttp implements HttpEngine { } @Override - public boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException { + public boolean sendRequest(Tile tile) throws IOException { if (mSocket != null) { if (mMaxReq-- <= 0) @@ -359,7 +353,7 @@ public class LwHttp implements HttpEngine { byte[] request = mRequestBuffer; int pos = REQUEST_GET_START.length; - pos = tileSource.formatTilePath(tile, request, pos); + pos = formatTilePath(mTileSource, tile, request, pos); int len = REQUEST_GET_END.length; System.arraycopy(REQUEST_GET_END, 0, request, pos, len); @@ -497,4 +491,46 @@ public class LwHttp implements HttpEngine { return true; } + + /** + * Write tile url - the low level, no-allocations method, + * + * override getTileUrl() for custom url formatting using + * Strings + * + * @param tile the Tile + * @param buf to write url string + * @param pos current position + * @return new position + */ + public int formatTilePath(UrlTileSource tileSource, Tile tile, byte[] buf, int pos) { + String p = tileSource.formatTilePath(tile); + log.debug("path {}", p); + //if (p != null) { + byte[] b = p.getBytes(); + System.arraycopy(b, 0, buf, pos, b.length); + return pos + b.length; + //} + // + // buf[pos++] = '/'; + // pos = LwHttp.writeInt(tile.zoomLevel, pos, buf); + // buf[pos++] = '/'; + // pos = LwHttp.writeInt(tile.tileX, pos, buf); + // buf[pos++] = '/'; + // pos = LwHttp.writeInt(tile.tileY, pos, buf); + // byte[] ext = tileSource.mExtBytes; + // if (ext == null) + // return pos; + // + // System.arraycopy(ext, 0, buf, pos, ext.length); + // return pos + ext.length; + } + + public static class LwHttpFactory implements HttpEngine.Factory { + + @Override + public HttpEngine create(UrlTileSource tileSource) { + return new LwHttp(tileSource); + } + } } diff --git a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java index 2f4c7349..7e39ad73 100644 --- a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; import org.oscim.core.Tile; @@ -11,25 +12,27 @@ import org.oscim.core.Tile; import com.squareup.okhttp.OkHttpClient; public class OkHttpEngine implements HttpEngine { - private final OkHttpClient client; + private final OkHttpClient mClient; + private final UrlTileSource mTileSource; public static class OkHttpFactory implements HttpEngine.Factory { - private final OkHttpClient client; + private final OkHttpClient mClient; public OkHttpFactory() { - this.client = new OkHttpClient(); + mClient = new OkHttpClient(); } @Override - public HttpEngine create() { - return new OkHttpEngine(client); + public HttpEngine create(UrlTileSource tileSource) { + return new OkHttpEngine(mClient, tileSource); } } private InputStream inputStream; - public OkHttpEngine(OkHttpClient client) { - this.client = client; + public OkHttpEngine(OkHttpClient client, UrlTileSource tileSource) { + mClient = client; + mTileSource = tileSource; } @Override @@ -37,28 +40,20 @@ public class OkHttpEngine implements HttpEngine { return inputStream; } + HttpURLConnection openConnection(Tile tile) throws MalformedURLException { + return mClient.open(new URL(mTileSource.getUrl() + + mTileSource.formatTilePath(tile))); + } + @Override - public boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException { + public boolean sendRequest(Tile tile) throws IOException { if (tile == null) { throw new IllegalArgumentException("Tile cannot be null."); } - final URL requestUrl = new URL(tileSource.getUrl() - + "/" - + Byte.toString(tile.zoomLevel) - + "/" - + tile.tileX - + "/" - + tile.tileY - + ".vtm"); + final HttpURLConnection connection = openConnection(tile); - final HttpURLConnection connection = client.open(requestUrl); - - try { - inputStream = connection.getInputStream(); - } catch (Exception e) { - e.printStackTrace(); - } + inputStream = connection.getInputStream(); return true; } diff --git a/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java b/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java index a97272c3..4cf814cf 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java @@ -75,7 +75,7 @@ public class UrlTileDataSource implements ITileDataSource { TileWriter cacheWriter = null; try { InputStream is; - if (!mConn.sendRequest(mTileSource, tile)) { + if (!mConn.sendRequest(tile)) { log.debug("{} Request failed", tile); } else if ((is = mConn.read()) == null) { log.debug("{} Network Error", tile); diff --git a/vtm/src/org/oscim/tiling/source/UrlTileSource.java b/vtm/src/org/oscim/tiling/source/UrlTileSource.java index 3d00bbd4..b7546ba3 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileSource.java @@ -18,30 +18,44 @@ package org.oscim.tiling.source; import java.net.MalformedURLException; import java.net.URL; +import java.util.Map; import org.oscim.core.Tile; import org.oscim.tiling.TileSource; +import org.oscim.tiling.source.LwHttp.LwHttpFactory; public abstract class UrlTileSource extends TileSource { private final URL mUrl; - private byte[] mExt; - private HttpEngine.Factory mHttpFactory; + private final String[] mTilePath; + + private HttpEngine.Factory mHttpFactory; + private Map mRequestHeaders; + + public UrlTileSource(String url, String tilePath, int zoomMin, int zoomMax) { + this(url, tilePath); + mZoomMin = zoomMin; + mZoomMax = zoomMax; + } + + /** + * @param urlString 'http://example.com/' + * @param tilePath replacement string for tile coordinates, + * e.g. '{Z}/{X}/{Y}.png' + */ + public UrlTileSource(String urlString, String tilePath) { + + if (tilePath == null) + throw new IllegalArgumentException("tilePath cannot be null."); - public UrlTileSource(String urlString) { URL url = null; try { url = new URL(urlString); } catch (MalformedURLException e) { - e.printStackTrace(); + throw new IllegalArgumentException(e); } mUrl = url; - } - - public UrlTileSource(String url, int zoomMin, int zoomMax) { - this(url); - mZoomMin = zoomMin; - mZoomMax = zoomMax; + mTilePath = tilePath.split("\\{|\\}"); } @Override @@ -54,70 +68,52 @@ public abstract class UrlTileSource extends TileSource { } - protected void setExtension(String ext) { - if (ext == null) { - mExt = null; - return; - } - mExt = ext.getBytes(); - } - - protected void setMimeType(String string) { - - } - - /** - * Create url path for tile - */ - protected String getTileUrl(Tile tile) { - return null; - } - - /** - * Write tile url - the low level, no-allocations method, - * - * override getTileUrl() for custom url formatting using - * Strings - * - * @param tile the Tile - * @param buf to write url string - * @param pos current position - * @return new position - */ - public int formatTilePath(Tile tile, byte[] buf, int pos) { - String p = getTileUrl(tile); - if (p != null) { - byte[] b = p.getBytes(); - System.arraycopy(b, 0, buf, pos, b.length); - return pos + b.length; - } - - buf[pos++] = '/'; - pos = LwHttp.writeInt(tile.zoomLevel, pos, buf); - buf[pos++] = '/'; - pos = LwHttp.writeInt(tile.tileX, pos, buf); - buf[pos++] = '/'; - pos = LwHttp.writeInt(tile.tileY, pos, buf); - if (mExt == null) - return pos; - - System.arraycopy(mExt, 0, buf, pos, mExt.length); - return pos + mExt.length; - } - public URL getUrl() { return mUrl; } + public String formatTilePath(Tile tile) { + // TODO only use the actual replacement positions. + + StringBuilder sb = new StringBuilder(); + for (String b : mTilePath) { + if (b.length() == 1) { + switch (b.charAt(0)) { + case 'X': + sb.append(tile.tileX); + continue; + case 'Y': + sb.append(tile.tileY); + continue; + case 'Z': + sb.append(tile.zoomLevel); + continue; + default: + break; + } + } + sb.append(b); + } + return sb.toString(); + } + public void setHttpEngine(HttpEngine.Factory httpFactory) { mHttpFactory = httpFactory; } + public void setHttpRequestHeaders(Map options) { + mRequestHeaders = options; + } + + protected Map getRequestHeader() { + return mRequestHeaders; + } + public HttpEngine getHttpEngine() { if (mHttpFactory == null) { - return new LwHttp(getUrl()); + mHttpFactory = new LwHttpFactory(); } - return mHttpFactory.create(); + return mHttpFactory.create(this); } } diff --git a/vtm/src/org/oscim/tiling/source/bitmap/BitmapTileSource.java b/vtm/src/org/oscim/tiling/source/bitmap/BitmapTileSource.java index 40197f63..3e2fc055 100644 --- a/vtm/src/org/oscim/tiling/source/bitmap/BitmapTileSource.java +++ b/vtm/src/org/oscim/tiling/source/bitmap/BitmapTileSource.java @@ -26,8 +26,15 @@ public class BitmapTileSource extends UrlTileSource { * implement getUrlString() for custom formatting. */ public BitmapTileSource(String url, int zoomMin, int zoomMax) { - super(url, zoomMin, zoomMax); - setExtension(".png"); + super(url, "/{Z}/{X}/{Y}.png", zoomMin, zoomMax); + } + + public BitmapTileSource(String url, int zoomMin, int zoomMax, String extension) { + super(url, "/{Z}/{X}/{Y}" + extension, zoomMin, zoomMax); + } + + public BitmapTileSource(String url, String tilePath, int zoomMin, int zoomMax) { + super(url, tilePath, zoomMin, zoomMax); } @Override diff --git a/vtm/src/org/oscim/tiling/source/bitmap/DefaultSources.java b/vtm/src/org/oscim/tiling/source/bitmap/DefaultSources.java index 92fea620..f74d5119 100644 --- a/vtm/src/org/oscim/tiling/source/bitmap/DefaultSources.java +++ b/vtm/src/org/oscim/tiling/source/bitmap/DefaultSources.java @@ -1,6 +1,5 @@ package org.oscim.tiling.source.bitmap; -import org.oscim.core.Tile; import org.oscim.layers.tile.bitmap.BitmapTileLayer.FadeStep; /** @@ -35,15 +34,13 @@ public class DefaultSources { public static class ImagicoLandcover extends BitmapTileSource { public ImagicoLandcover() { - super("http://www.imagico.de/map/tiles/landcover", 0, 6); - setExtension(".jpg"); + super("http://www.imagico.de/map/tiles/landcover", 0, 6, ".jpg"); } } public static class MapQuestAerial extends BitmapTileSource { public MapQuestAerial() { - super("http://otile1.mqcdn.com/tiles/1.0.0/sat", 0, 8); - setExtension(".jpg"); + super("http://otile1.mqcdn.com/tiles/1.0.0/sat", 0, 8, ".jpg"); } @Override @@ -64,38 +61,17 @@ public class DefaultSources { } public static class ArcGISWorldShaded extends BitmapTileSource { - private final StringBuilder sb = new StringBuilder(32); - public ArcGISWorldShaded() { - super("http://server.arcgisonline.com/ArcGIS/rest/services", 0, 13); - } - - @Override - public synchronized String getTileUrl(Tile tile) { - sb.setLength(0); - //sb.append("/World_Imagery/MapServer/tile/"); - sb.append("/World_Shaded_Relief/MapServer/tile/"); - sb.append(tile.zoomLevel); - sb.append('/').append(tile.tileY); - sb.append('/').append(tile.tileX); - return sb.toString(); + super("http://server.arcgisonline.com/ArcGIS/rest/services" + + "/World_Shaded_Relief/MapServer/tile/", + "{Z}/{Y}/{X}", 0, 13); } } public static class HillShadeHD extends BitmapTileSource { - private final StringBuilder sb = new StringBuilder(32); - public HillShadeHD() { - super("http://129.206.74.245:8004/tms_hs.ashx", 2, 16); - } - - @Override - public synchronized String getTileUrl(Tile tile) { - sb.setLength(0); - sb.append("?x=").append(tile.tileX); - sb.append("&y=").append(tile.tileY); - sb.append("&z=").append(tile.zoomLevel); - return sb.toString(); + super("http://129.206.74.245:8004/tms_hs.ashx", + "?x={X}&y={Y}&z={Z}", 2, 16); } } @@ -104,23 +80,8 @@ public class DefaultSources { * https://developers.google.com/maps/faq */ public static class GoogleMaps extends BitmapTileSource { - private final StringBuilder sb = new StringBuilder(60); - public GoogleMaps(String hostName) { - super(hostName, 1, 20); //jpeg for sat - } - - @Override - public synchronized String getTileUrl(Tile tile) { - sb.setLength(0); - sb.append("/vt/x="); //lyrs=y& - sb.append(tile.tileX); - sb.append("&y="); - sb.append(tile.tileY); - sb.append("&z="); - sb.append(tile.zoomLevel); - sb.append("&s=Galileo&scale=2"); - return sb.toString(); + super(hostName, "/vt/x={X}&y={Y}&z={Z}&s=Galileo&scale=2", 1, 20); //jpeg for sat } } diff --git a/vtm/src/org/oscim/tiling/source/oscimap4/OSciMap4TileSource.java b/vtm/src/org/oscim/tiling/source/oscimap4/OSciMap4TileSource.java index 3d2788a7..d98597cc 100644 --- a/vtm/src/org/oscim/tiling/source/oscimap4/OSciMap4TileSource.java +++ b/vtm/src/org/oscim/tiling/source/oscimap4/OSciMap4TileSource.java @@ -27,8 +27,7 @@ public class OSciMap4TileSource extends UrlTileSource { } public OSciMap4TileSource(String url) { - super(url); - setExtension(".vtm"); + super(url, "/{Z}/{X}/{Y}.vtm"); } @Override From 3f9847f61771284c726c70c982a4e396dd83a3da Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Wed, 2 Apr 2014 06:54:54 +0200 Subject: [PATCH 07/16] gradle: make okhttp optional --- vtm/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vtm/build.gradle b/vtm/build.gradle index 6f148d81..6df7c204 100644 --- a/vtm/build.gradle +++ b/vtm/build.gradle @@ -5,7 +5,7 @@ configurations { providedCompile } dependencies { compile 'org.slf4j:slf4j-api:1.7.6' - compile 'com.squareup.okhttp:okhttp:1.5.2' + providedCompile 'com.squareup.okhttp:okhttp:1.5.2' providedCompile 'com.google.code.findbugs:annotations:2.0.1' } From 5c277f4d5485ca0f3b61cd4eb91fd5d402ef5d21 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Wed, 2 Apr 2014 07:07:30 +0200 Subject: [PATCH 08/16] fix: HttpEngine tests --- vtm-tests/build.gradle | 1 + .../oscim/tiling/source/OkHttpEngineTest.java | 35 +++++++++---------- .../tiling/source/UrlTileSourceTest.java | 12 +++---- .../source/bitmap/BitmapTileSourceTest.java | 6 ++-- .../oscimap4/OSciMap4TileSourceTest.java | 10 +++--- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/vtm-tests/build.gradle b/vtm-tests/build.gradle index 3acb6285..02235b8a 100644 --- a/vtm-tests/build.gradle +++ b/vtm-tests/build.gradle @@ -3,6 +3,7 @@ apply plugin: 'maven' dependencies { compile project(':vtm') + compile 'com.squareup.okhttp:okhttp:1.5.2' testCompile 'junit:junit:4.11' testCompile 'org.mockito:mockito-all:1.9.5' testCompile 'org.easytesting:fest-assert-core:2.0M10' diff --git a/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java b/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java index 45ff6b89..ddc1e083 100644 --- a/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java @@ -1,8 +1,11 @@ package org.oscim.tiling.source; -import com.squareup.okhttp.mockwebserver.MockResponse; -import com.squareup.okhttp.mockwebserver.MockWebServer; -import com.squareup.okhttp.mockwebserver.RecordedRequest; +import static org.fest.assertions.api.Assertions.assertThat; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import org.junit.After; import org.junit.Before; @@ -10,12 +13,9 @@ import org.junit.Test; import org.oscim.core.Tile; import org.oscim.tiling.source.oscimap4.OSciMap4TileSource; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; - -import static org.fest.assertions.api.Assertions.assertThat; +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; +import com.squareup.okhttp.mockwebserver.RecordedRequest; public class OkHttpEngineTest { private OkHttpEngine engine; @@ -29,7 +29,8 @@ public class OkHttpEngineTest { server = new MockWebServer(); server.enqueue(mockResponse); server.play(); - engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory().create(); + engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory() + .create(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString())); } @After @@ -44,13 +45,12 @@ public class OkHttpEngineTest { @Test(expected = IllegalArgumentException.class) public void sendRequest_shouldRejectNullTile() throws Exception { - engine.sendRequest(null, null); + engine.sendRequest(null); } @Test public void sendRequest_shouldAppendXYZToPath() throws Exception { - engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), - new Tile(1, 2, new Integer(3).byteValue())); + engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue())); RecordedRequest request = server.takeRequest(); assertThat(request.getPath()).isEqualTo("/tiles/vtm/3/1/2.vtm"); @@ -58,8 +58,7 @@ public class OkHttpEngineTest { @Test public void read_shouldReturnResponseStream() throws Exception { - engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), - new Tile(1, 2, new Integer(3).byteValue())); + engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue())); InputStream responseStream = engine.read(); String response = new BufferedReader(new InputStreamReader(responseStream)).readLine(); @@ -68,8 +67,7 @@ public class OkHttpEngineTest { @Test(expected = IOException.class) public void close_shouldCloseInputStream() throws Exception { - engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), - new Tile(1, 2, new Integer(3).byteValue())); + engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue())); engine.close(); // Calling read after the stream is closed should throw an exception. @@ -79,8 +77,7 @@ public class OkHttpEngineTest { @Test(expected = IOException.class) public void requestCompleted_shouldCloseInputStream() throws Exception { - engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), - new Tile(1, 2, new Integer(3).byteValue())); + engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue())); engine.requestCompleted(true); // Calling read after the stream is closed should throw an exception. diff --git a/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java index 395738c6..b52eb830 100644 --- a/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java @@ -1,17 +1,17 @@ package org.oscim.tiling.source; +import static org.fest.assertions.api.Assertions.assertThat; + import org.junit.Before; import org.junit.Test; import org.oscim.tiling.ITileDataSource; -import static org.fest.assertions.api.Assertions.assertThat; - public class UrlTileSourceTest { private UrlTileSource tileSource; @Before public void setUp() throws Exception { - tileSource = new TestTileSource("http://example.org/tiles/vtm"); + tileSource = new TestTileSource("http://example.org/tiles/vtm", "/{Z}/{X}/{Z}.vtm"); } @Test @@ -33,8 +33,8 @@ public class UrlTileSourceTest { } class TestTileSource extends UrlTileSource { - public TestTileSource(String urlString) { - super(urlString); + public TestTileSource(String urlString, String tilePath) { + super(urlString, tilePath); } @Override @@ -45,7 +45,7 @@ public class UrlTileSourceTest { class TestTileDataSource extends UrlTileDataSource { public TestTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder, - HttpEngine conn) { + HttpEngine conn) { super(tileSource, tileDecoder, conn); } diff --git a/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java index f0f93560..8381a1d2 100644 --- a/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/bitmap/BitmapTileSourceTest.java @@ -10,6 +10,7 @@ import org.oscim.tiling.source.HttpEngine; import org.oscim.tiling.source.LwHttp; import org.oscim.tiling.source.OkHttpEngine; import org.oscim.tiling.source.UrlTileDataSource; +import org.oscim.tiling.source.UrlTileSource; public class BitmapTileSourceTest { private BitmapTileSource tileSource; @@ -43,7 +44,8 @@ public class BitmapTileSourceTest { } /** - * Test factory that allows the specific {@link HttpEngine} instance to be set. + * Test factory that allows the specific {@link HttpEngine} instance to be + * set. */ class TestHttpFactory implements HttpEngine.Factory { final HttpEngine engine; @@ -53,7 +55,7 @@ public class BitmapTileSourceTest { } @Override - public HttpEngine create() { + public HttpEngine create(UrlTileSource tileSource) { return engine; } } diff --git a/vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java index 060d0c77..54276965 100644 --- a/vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/oscimap4/OSciMap4TileSourceTest.java @@ -1,5 +1,7 @@ package org.oscim.tiling.source.oscimap4; +import static org.fest.assertions.api.Assertions.assertThat; + import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -7,8 +9,7 @@ import org.oscim.tiling.ITileDataSource; import org.oscim.tiling.source.HttpEngine; import org.oscim.tiling.source.LwHttp; import org.oscim.tiling.source.OkHttpEngine; - -import static org.fest.assertions.api.Assertions.assertThat; +import org.oscim.tiling.source.UrlTileSource; public class OSciMap4TileSourceTest { private OSciMap4TileSource tileSource; @@ -42,7 +43,8 @@ public class OSciMap4TileSourceTest { } /** - * Test factory that allows the specific {@link HttpEngine} instance to be set. + * Test factory that allows the specific {@link HttpEngine} instance to be + * set. */ class TestHttpFactory implements HttpEngine.Factory { final HttpEngine engine; @@ -52,7 +54,7 @@ public class OSciMap4TileSourceTest { } @Override - public HttpEngine create() { + public HttpEngine create(UrlTileSource tileSource) { return engine; } } From 5ad68ff2c7ec1ec12f3834d18b4dd694a0bdb719 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Wed, 2 Apr 2014 07:37:57 +0200 Subject: [PATCH 09/16] gwt: update UrlTileSource --- .../emu/org/oscim/tiling/source/LwHttp.java | 93 ++++++++----------- .../tiling/source/UrlTileDataSource.java | 69 ++++++-------- .../source/bitmap/BitmapTileSource.java | 19 ++-- .../source/geojson/GeoJsonTileSource.java | 3 +- .../tiling/source/JsonTileDataSource.java | 6 +- 5 files changed, 80 insertions(+), 110 deletions(-) diff --git a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/LwHttp.java b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/LwHttp.java index b42cd831..1ca98f7c 100644 --- a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/LwHttp.java +++ b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/LwHttp.java @@ -16,9 +16,11 @@ package org.oscim.tiling.source; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.URL; import org.oscim.core.Tile; +import org.oscim.layers.tile.MapTile; import com.google.gwt.typedarrays.client.Uint8ArrayNative; import com.google.gwt.typedarrays.shared.Uint8Array; @@ -26,20 +28,18 @@ import com.google.gwt.xhr.client.ReadyStateChangeHandler; import com.google.gwt.xhr.client.XMLHttpRequest; import com.google.gwt.xhr.client.XMLHttpRequest.ResponseType; -public class LwHttp { +public class LwHttp implements HttpEngine { //static final Logger log = LoggerFactory.getLogger(LwHttp.class); private final String mUrlPath; - private final byte[] mRequestBuffer; - - private int mContentLength = -1; private XMLHttpRequest mHttpRequest; private ReadyStateChangeHandler mResponseHandler; - public LwHttp(URL url) { + public LwHttp(UrlTileSource tileSource) { + mTileSource = tileSource; + URL url = tileSource.getUrl(); mUrlPath = url.toString(); - mRequestBuffer = new byte[1024]; } static class Buffer extends InputStream { @@ -63,21 +63,18 @@ public class LwHttp { } public void close() { - if (mHttpRequest != null) - mHttpRequest.abort(); + if (mHttpRequest == null) + return; + + mHttpRequest.abort(); + mHttpRequest = null; } - private UrlTileDataSource mDataSource; + private UrlTileSource mTileSource; - public boolean sendRequest(Tile tile, UrlTileDataSource dataSource) throws IOException { - mDataSource = dataSource; + public boolean sendRequest(MapTile tile, final UrlTileDataSource dataSource) { - byte[] request = mRequestBuffer; - int pos = 0; - - pos = dataSource.getTileSource().formatTilePath(tile, request, pos); - - String url = mUrlPath + (new String(request, 0, pos)); + String url = mUrlPath + mTileSource.formatTilePath(tile); mHttpRequest = XMLHttpRequest.create(); mHttpRequest.open("GET", url); @@ -91,16 +88,13 @@ public class LwHttp { //log.debug(mCurrentUrl + "response " + status + "/" + state); if (state == XMLHttpRequest.DONE) { - - int status = xhr.getStatus(); - - if (status == 200) { + if (xhr.getStatus() == 200) { Uint8Array buf = Uint8ArrayNative.create(xhr.getResponseArrayBuffer()); - - mDataSource.process(new Buffer(buf)); + dataSource.process(new Buffer(buf)); } else { - mDataSource.process(null); + dataSource.process(null); } + mHttpRequest = null; } } }; @@ -111,43 +105,32 @@ public class LwHttp { return true; } - // write (positive) integer to byte array - protected static int writeInt(int val, int pos, byte[] buf) { - if (val == 0) { - buf[pos] = '0'; - return pos + 1; + public static class LwHttpFactory implements HttpEngine.Factory { + + @Override + public HttpEngine create(UrlTileSource tileSource) { + return new LwHttp(tileSource); } - - int i = 0; - for (int n = val; n > 0; n = n / 10, i++) - buf[pos + i] = (byte) ('0' + n % 10); - - // reverse bytes - for (int j = pos, end = pos + i - 1, mid = pos + i / 2; j < mid; j++, end--) { - byte tmp = buf[j]; - buf[j] = buf[end]; - buf[end] = tmp; - } - - return pos + i; } - // parse (positive) integer from byte array - protected static int parseInt(byte[] buf, int pos, int end) { - int val = 0; - for (; pos < end; pos++) - val = val * 10 + (buf[pos]) - '0'; - - return val; + @Override + public InputStream read() throws IOException { + return null; } - public void requestCompleted() { - - mHttpRequest.clearOnReadyStateChange(); - mHttpRequest = null; + @Override + public void setCache(OutputStream os) { } - public int getContentLength() { - return mContentLength; + @Override + public boolean requestCompleted(boolean success) { + // mHttpRequest.clearOnReadyStateChange(); + // mHttpRequest = null; + return true; + } + + @Override + public boolean sendRequest(Tile tile) throws IOException { + return false; } } diff --git a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/UrlTileDataSource.java b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/UrlTileDataSource.java index f9afeb80..0218db48 100644 --- a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/UrlTileDataSource.java +++ b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/UrlTileDataSource.java @@ -35,65 +35,52 @@ public class UrlTileDataSource implements ITileDataSource { protected final ITileDecoder mTileDecoder; protected final UrlTileSource mTileSource; - public UrlTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder, LwHttp conn) { - mTileSource = tileSource; - mTileDecoder = tileDecoder; - mConn = conn; - } - - UrlTileSource getTileSource() { - return mTileSource; - } - private ITileDataSink mSink; private MapTile mTile; + public UrlTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder, HttpEngine conn) { + mTileSource = tileSource; + mTileDecoder = tileDecoder; + mConn = (LwHttp) conn; + } + @Override public void query(MapTile tile, ITileDataSink sink) { mTile = tile; mSink = sink; - try { - mConn.sendRequest(tile, this); - } catch (Exception e) { - ///e.printStackTrace(); - log.error("{} {}", mTile, e.getMessage()); - sink.completed(FAILED); - } + mConn.sendRequest(tile, this); } public void process(final InputStream is) { - TileLoader.postLoadDelay(new LoadDelayTask(mTile, mSink, is) { + if (is == null) { + log.debug("{} no inputstream", mTile); + mSink.completed(FAILED); + mTile = null; + mSink = null; + return; + } + TileLoader.postLoadDelay(new LoadDelayTask(mTile, mSink, is) { @Override public void continueLoading() { - + boolean win = false; if (tile.state(MapTile.State.LOADING)) { - boolean win = false; - if (is != null) { - try { - win = mTileDecoder.decode(tile, sink, data); - } catch (IOException e) { - e.printStackTrace(); - } + try { + win = mTileDecoder.decode(tile, sink, data); + } catch (IOException e) { + e.printStackTrace(); } - if (!win) - log.debug("{} failed", tile); - - // FIXME - mConn.requestCompleted(); - - sink.completed(win ? SUCCESS : FAILED); - } else { - // FIXME - mConn.requestCompleted(); - sink.completed(FAILED); } - - mTile = null; - mSink = null; + if (win) { + sink.completed(SUCCESS); + } else { + sink.completed(FAILED); + log.debug("{} decode failed", tile); + } } }); - + mTile = null; + mSink = null; } @Override diff --git a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/bitmap/BitmapTileSource.java b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/bitmap/BitmapTileSource.java index 87801f57..f8af2caf 100644 --- a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/bitmap/BitmapTileSource.java +++ b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/bitmap/BitmapTileSource.java @@ -31,9 +31,17 @@ public class BitmapTileSource extends UrlTileSource { * Use e.g. setExtension(".jpg") to overide ending or * implement getUrlString() for custom formatting. */ + public BitmapTileSource(String url, int zoomMin, int zoomMax) { - super(url, zoomMin, zoomMax); - setExtension(".png"); + super(url, "/{Z}/{X}/{Y}.png", zoomMin, zoomMax); + } + + public BitmapTileSource(String url, int zoomMin, int zoomMax, String extension) { + super(url, "/{Z}/{X}/{Y}" + extension, zoomMin, zoomMax); + } + + public BitmapTileSource(String url, String tilePath, int zoomMin, int zoomMax) { + super(url, tilePath, zoomMin, zoomMax); } @Override @@ -41,10 +49,9 @@ public class BitmapTileSource extends UrlTileSource { return new BitmapTileDataSource(this); } - public static class BitmapTileDataSource implements ITileDataSource { + public class BitmapTileDataSource implements ITileDataSource { protected final UrlTileSource mTileSource; - private final byte[] mRequestBuffer = new byte[1024]; public BitmapTileDataSource(BitmapTileSource bitmapTileSource) { mTileSource = bitmapTileSource; @@ -53,10 +60,8 @@ public class BitmapTileSource extends UrlTileSource { @Override public void query(final MapTile tile, final ITileDataSink sink) { - int pos = mTileSource.formatTilePath(tile, mRequestBuffer, 0); - String url = mTileSource.getUrl() - + (new String(mRequestBuffer, 0, pos)); + + BitmapTileSource.this.formatTilePath(tile); SafeUri uri = UriUtils.fromTrustedString(url); diff --git a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/geojson/GeoJsonTileSource.java b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/geojson/GeoJsonTileSource.java index 2e5c2c8b..934f8dc3 100644 --- a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/geojson/GeoJsonTileSource.java +++ b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/geojson/GeoJsonTileSource.java @@ -30,8 +30,7 @@ public abstract class GeoJsonTileSource extends UrlTileSource { static final Logger log = LoggerFactory.getLogger(GeoJsonTileSource.class); public GeoJsonTileSource(String url) { - super(url); - setExtension(".json"); + super(url, "/{Z}/{X}/{Y}.json"); } @Override diff --git a/vtm-web/src/org/oscim/tiling/source/JsonTileDataSource.java b/vtm-web/src/org/oscim/tiling/source/JsonTileDataSource.java index f4b257e0..35511ecc 100644 --- a/vtm-web/src/org/oscim/tiling/source/JsonTileDataSource.java +++ b/vtm-web/src/org/oscim/tiling/source/JsonTileDataSource.java @@ -41,8 +41,6 @@ public class JsonTileDataSource implements ITileDataSource { protected final GeoJsonTileDecoder mTileDecoder; protected final UrlTileSource mTileSource; - private final byte[] mRequestBuffer = new byte[1024]; - public JsonTileDataSource(GeoJsonTileSource tileSource) { mTileSource = tileSource; mTileDecoder = new GeoJsonTileDecoder(tileSource); @@ -61,10 +59,8 @@ public class JsonTileDataSource implements ITileDataSource { mSink = sink; try { - int pos = mTileSource.formatTilePath(tile, mRequestBuffer, 0); - String url = mTileSource.getUrl() - + (new String(mRequestBuffer, 0, pos)); + + mTileSource.formatTilePath(tile); doGet(url); } catch (Exception e) { From 98e30a468f58c24bd0df73880185422cf96df1c2 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Wed, 2 Apr 2014 17:51:44 +0200 Subject: [PATCH 10/16] add getter for split tilePath --- vtm/src/org/oscim/tiling/source/UrlTileSource.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vtm/src/org/oscim/tiling/source/UrlTileSource.java b/vtm/src/org/oscim/tiling/source/UrlTileSource.java index b7546ba3..100cd9a9 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileSource.java @@ -105,10 +105,14 @@ public abstract class UrlTileSource extends TileSource { mRequestHeaders = options; } - protected Map getRequestHeader() { + public Map getRequestHeader() { return mRequestHeaders; } + public String[] getTilePath() { + return mTilePath; + } + public HttpEngine getHttpEngine() { if (mHttpFactory == null) { mHttpFactory = new LwHttpFactory(); From 15cf4810cf0c9889738b34cb57008935c2f1eb3f Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Wed, 2 Apr 2014 17:59:28 +0200 Subject: [PATCH 11/16] LwHttp: prepare tilePath as byte[] --- vtm/src/org/oscim/tiling/source/LwHttp.java | 70 +++++++++++---------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/vtm/src/org/oscim/tiling/source/LwHttp.java b/vtm/src/org/oscim/tiling/source/LwHttp.java index ac8abd80..4130ab7f 100644 --- a/vtm/src/org/oscim/tiling/source/LwHttp.java +++ b/vtm/src/org/oscim/tiling/source/LwHttp.java @@ -40,10 +40,7 @@ public class LwHttp implements HttpEngine { static final Logger log = LoggerFactory.getLogger(LwHttp.class); static final boolean dbg = false; - private final UrlTileSource mTileSource; - private final static byte[] HEADER_HTTP_OK = "200 OK".getBytes(); - //private final static byte[] HEADER_CONTENT_TYPE = "Content-Type".getBytes(); private final static byte[] HEADER_CONTENT_LENGTH = "Content-Length".getBytes(); private final static byte[] HEADER_CONNECTION_CLOSE = "Connection: close".getBytes(); @@ -69,9 +66,11 @@ public class LwHttp implements HttpEngine { private final byte[] REQUEST_GET_START; private final byte[] REQUEST_GET_END; private final byte[] mRequestBuffer; + private final byte[][] mTilePath; + + private LwHttp(UrlTileSource tileSource, byte[][] tilePath) { + mTilePath = tilePath; - private LwHttp(UrlTileSource tileSource) { - mTileSource = tileSource; URL url = tileSource.getUrl(); int port = url.getPort(); if (port < 0) @@ -353,7 +352,7 @@ public class LwHttp implements HttpEngine { byte[] request = mRequestBuffer; int pos = REQUEST_GET_START.length; - pos = formatTilePath(mTileSource, tile, request, pos); + pos = formatTilePath(tile, request, pos); int len = REQUEST_GET_END.length; System.arraycopy(REQUEST_GET_END, 0, request, pos, len); @@ -453,7 +452,7 @@ public class LwHttp implements HttpEngine { } /** write (positive) integer to byte array */ - public static int writeInt(int val, int pos, byte[] buf) { + private static int writeInt(int val, int pos, byte[] buf) { if (val == 0) { buf[pos] = '0'; return pos + 1; @@ -469,7 +468,7 @@ public class LwHttp implements HttpEngine { } /** parse (positive) integer from byte array */ - protected static int parseInt(byte[] buf, int pos, int end) { + private static int parseInt(byte[] buf, int pos, int end) { int val = 0; for (; pos < end; pos++) val = val * 10 + (buf[pos]) - '0'; @@ -495,42 +494,47 @@ public class LwHttp implements HttpEngine { /** * Write tile url - the low level, no-allocations method, * - * override getTileUrl() for custom url formatting using - * Strings - * * @param tile the Tile * @param buf to write url string * @param pos current position * @return new position */ - public int formatTilePath(UrlTileSource tileSource, Tile tile, byte[] buf, int pos) { - String p = tileSource.formatTilePath(tile); - log.debug("path {}", p); - //if (p != null) { - byte[] b = p.getBytes(); - System.arraycopy(b, 0, buf, pos, b.length); - return pos + b.length; - //} - // - // buf[pos++] = '/'; - // pos = LwHttp.writeInt(tile.zoomLevel, pos, buf); - // buf[pos++] = '/'; - // pos = LwHttp.writeInt(tile.tileX, pos, buf); - // buf[pos++] = '/'; - // pos = LwHttp.writeInt(tile.tileY, pos, buf); - // byte[] ext = tileSource.mExtBytes; - // if (ext == null) - // return pos; - // - // System.arraycopy(ext, 0, buf, pos, ext.length); - // return pos + ext.length; + private int formatTilePath(Tile tile, byte[] buf, int pos) { + for (byte[] b : mTilePath) { + if (b.length == 1) { + if (b[0] == '/') { + buf[pos++] = '/'; + } else if (b[0] == 'X') { + pos = writeInt(tile.tileX, pos, buf); + continue; + } else if (b[0] == 'Y') { + pos = writeInt(tile.tileY, pos, buf); + continue; + } else if (b[0] == 'Z') { + pos = writeInt(tile.zoomLevel, pos, buf); + continue; + } + } + System.arraycopy(b, 0, buf, pos, b.length); + pos += b.length; + } + return pos; + } public static class LwHttpFactory implements HttpEngine.Factory { + private byte[][] mTilePath; @Override public HttpEngine create(UrlTileSource tileSource) { - return new LwHttp(tileSource); + if (mTilePath == null) { + String[] path = tileSource.getTilePath(); + mTilePath = new byte[path.length][]; + for (int i = 0; i < path.length; i++) + mTilePath[i] = path[i].getBytes(); + } + + return new LwHttp(tileSource, mTilePath); } } } From 6b7ccd68c9c5bf2ca5f559d6fca2e9816f11ad3e Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Thu, 3 Apr 2014 04:09:40 +0200 Subject: [PATCH 12/16] add missing headers --- vtm/src/org/oscim/tiling/source/HttpEngine.java | 16 ++++++++++++++++ .../org/oscim/tiling/source/ITileDecoder.java | 16 ++++++++++++++++ .../org/oscim/tiling/source/OkHttpEngine.java | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/vtm/src/org/oscim/tiling/source/HttpEngine.java b/vtm/src/org/oscim/tiling/source/HttpEngine.java index 63b29948..c99429a4 100644 --- a/vtm/src/org/oscim/tiling/source/HttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/HttpEngine.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014 Hannes Janetzek + * + * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see . + */ package org.oscim.tiling.source; import java.io.IOException; diff --git a/vtm/src/org/oscim/tiling/source/ITileDecoder.java b/vtm/src/org/oscim/tiling/source/ITileDecoder.java index 5f48c8f0..0412bb15 100644 --- a/vtm/src/org/oscim/tiling/source/ITileDecoder.java +++ b/vtm/src/org/oscim/tiling/source/ITileDecoder.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013 Hannes Janetzek + * + * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see . + */ package org.oscim.tiling.source; import java.io.IOException; diff --git a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java index 7e39ad73..1e892e96 100644 --- a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java @@ -1,3 +1,20 @@ +/* + * Copyright 2014 Charles Greb + * Copyright 2014 Hannes Janetzek + * + * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see . + */ package org.oscim.tiling.source; import java.io.IOException; From 85a4bbe12518f3ac4af698fcfdabcd47acde074e Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Thu, 3 Apr 2014 16:08:39 +0200 Subject: [PATCH 13/16] api: UrlTileSource: - add UrlTileSource.getTileUrl(Tile) to create complete url string - add TileUrlFormatter interface to override default formatter - remove unused return from sendRequest() --- .../source/mapnik/MapnikVectorTileSource.java | 46 ++++++------ .../emu/org/oscim/tiling/source/LwHttp.java | 13 +--- .../source/bitmap/BitmapTileSource.java | 3 +- .../tiling/source/JsonTileDataSource.java | 5 +- .../org/oscim/tiling/source/HttpEngine.java | 2 +- vtm/src/org/oscim/tiling/source/LwHttp.java | 55 ++++++++------ .../org/oscim/tiling/source/OkHttpEngine.java | 25 ++----- .../tiling/source/UrlTileDataSource.java | 7 +- .../oscim/tiling/source/UrlTileSource.java | 71 ++++++++++++------- 9 files changed, 121 insertions(+), 106 deletions(-) diff --git a/vtm-extras/src/org/oscim/tiling/source/mapnik/MapnikVectorTileSource.java b/vtm-extras/src/org/oscim/tiling/source/mapnik/MapnikVectorTileSource.java index 60d1ee03..8bd761aa 100644 --- a/vtm-extras/src/org/oscim/tiling/source/mapnik/MapnikVectorTileSource.java +++ b/vtm-extras/src/org/oscim/tiling/source/mapnik/MapnikVectorTileSource.java @@ -25,32 +25,34 @@ public class MapnikVectorTileSource extends UrlTileSource { public MapnikVectorTileSource() { super("http://d1s11ojcu7opje.cloudfront.net/dev/764e0b8d", ""); + setUrlFormatter(new TileUrlFormatter() { + @Override + public String formatTilePath(UrlTileSource tileSource, Tile tile) { + // url formatter for mapbox streets + byte[] hexTable = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f' + }; + StringBuilder sb = new StringBuilder(); + sb.append('/'); + sb.append(hexTable[(tile.tileX) % 16]); + sb.append(hexTable[(tile.tileY) % 16]); + sb.append('/'); + sb.append(tile.zoomLevel); + sb.append('/'); + sb.append(tile.tileX); + sb.append('/'); + sb.append(tile.tileY); + + return sb.toString(); + } + }); } @Override public ITileDataSource getDataSource() { return new UrlTileDataSource(this, new TileDecoder(), getHttpEngine()); } - - public String formatTilePath(Tile tile) { - // url formatter for mapbox streets - byte[] hexTable = { - '0', '1', '2', '3', - '4', '5', '6', '7', - '8', '9', 'a', 'b', - 'c', 'd', 'e', 'f' - }; - StringBuilder sb = new StringBuilder(); - sb.append('/'); - sb.append(hexTable[(tile.tileX) % 16]); - sb.append(hexTable[(tile.tileY) % 16]); - sb.append('/'); - sb.append(tile.zoomLevel); - sb.append('/'); - sb.append(tile.tileX); - sb.append('/'); - sb.append(tile.tileY); - - return sb.toString(); - } } diff --git a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/LwHttp.java b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/LwHttp.java index 1ca98f7c..b25da4b9 100644 --- a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/LwHttp.java +++ b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/LwHttp.java @@ -17,7 +17,6 @@ package org.oscim.tiling.source; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.URL; import org.oscim.core.Tile; import org.oscim.layers.tile.MapTile; @@ -31,15 +30,12 @@ import com.google.gwt.xhr.client.XMLHttpRequest.ResponseType; public class LwHttp implements HttpEngine { //static final Logger log = LoggerFactory.getLogger(LwHttp.class); - private final String mUrlPath; private XMLHttpRequest mHttpRequest; private ReadyStateChangeHandler mResponseHandler; public LwHttp(UrlTileSource tileSource) { mTileSource = tileSource; - URL url = tileSource.getUrl(); - mUrlPath = url.toString(); } static class Buffer extends InputStream { @@ -72,9 +68,9 @@ public class LwHttp implements HttpEngine { private UrlTileSource mTileSource; - public boolean sendRequest(MapTile tile, final UrlTileDataSource dataSource) { + public void sendRequest(MapTile tile, final UrlTileDataSource dataSource) { - String url = mUrlPath + mTileSource.formatTilePath(tile); + String url = mTileSource.getTileUrl(tile); mHttpRequest = XMLHttpRequest.create(); mHttpRequest.open("GET", url); @@ -101,8 +97,6 @@ public class LwHttp implements HttpEngine { mHttpRequest.setOnReadyStateChange(mResponseHandler); mHttpRequest.send(); - - return true; } public static class LwHttpFactory implements HttpEngine.Factory { @@ -130,7 +124,6 @@ public class LwHttp implements HttpEngine { } @Override - public boolean sendRequest(Tile tile) throws IOException { - return false; + public void sendRequest(Tile tile) throws IOException { } } diff --git a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/bitmap/BitmapTileSource.java b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/bitmap/BitmapTileSource.java index f8af2caf..e543929a 100644 --- a/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/bitmap/BitmapTileSource.java +++ b/vtm-web/src/org/oscim/gdx/emu/org/oscim/tiling/source/bitmap/BitmapTileSource.java @@ -60,8 +60,7 @@ public class BitmapTileSource extends UrlTileSource { @Override public void query(final MapTile tile, final ITileDataSink sink) { - String url = mTileSource.getUrl() - + BitmapTileSource.this.formatTilePath(tile); + String url = mTileSource.getTileUrl(tile); SafeUri uri = UriUtils.fromTrustedString(url); diff --git a/vtm-web/src/org/oscim/tiling/source/JsonTileDataSource.java b/vtm-web/src/org/oscim/tiling/source/JsonTileDataSource.java index 35511ecc..5d495c34 100644 --- a/vtm-web/src/org/oscim/tiling/source/JsonTileDataSource.java +++ b/vtm-web/src/org/oscim/tiling/source/JsonTileDataSource.java @@ -59,10 +59,7 @@ public class JsonTileDataSource implements ITileDataSource { mSink = sink; try { - String url = mTileSource.getUrl() - + mTileSource.formatTilePath(tile); - - doGet(url); + doGet(mTileSource.getTileUrl(tile)); } catch (Exception e) { e.printStackTrace(); sink.completed(FAILED); diff --git a/vtm/src/org/oscim/tiling/source/HttpEngine.java b/vtm/src/org/oscim/tiling/source/HttpEngine.java index c99429a4..c5e9c715 100644 --- a/vtm/src/org/oscim/tiling/source/HttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/HttpEngine.java @@ -26,7 +26,7 @@ public interface HttpEngine { InputStream read() throws IOException; - boolean sendRequest(Tile tile) throws IOException; + void sendRequest(Tile tile) throws IOException; void close(); diff --git a/vtm/src/org/oscim/tiling/source/LwHttp.java b/vtm/src/org/oscim/tiling/source/LwHttp.java index 4130ab7f..032a0574 100644 --- a/vtm/src/org/oscim/tiling/source/LwHttp.java +++ b/vtm/src/org/oscim/tiling/source/LwHttp.java @@ -53,7 +53,7 @@ public class LwHttp implements HttpEngine { private final String mHost; private final int mPort; - private int mMaxReq = 0; + private int mMaxRequests = 0; private Socket mSocket; private OutputStream mCommandStream; private Buffer mResponseStream; @@ -66,10 +66,13 @@ public class LwHttp implements HttpEngine { private final byte[] REQUEST_GET_START; private final byte[] REQUEST_GET_END; private final byte[] mRequestBuffer; + private final byte[][] mTilePath; + private final UrlTileSource mTileSource; private LwHttp(UrlTileSource tileSource, byte[][] tilePath) { mTilePath = tilePath; + mTileSource = tileSource; URL url = tileSource.getUrl(); int port = url.getPort(); @@ -109,6 +112,7 @@ public class LwHttp implements HttpEngine { // to avoid a copy in PbfDecoder one could manage the buffer // array directly and provide access to it. static class Buffer extends BufferedInputStream { + static final class Buffer extends BufferedInputStream { OutputStream cache; int bytesRead = 0; int bytesWrote; @@ -188,6 +192,7 @@ public class LwHttp implements HttpEngine { if (marked >= 0) bytesRead = marked; + /* TODO could check if the mark is already invalid */ super.reset(); } @@ -323,17 +328,19 @@ public class LwHttp implements HttpEngine { } @Override - public boolean sendRequest(Tile tile) throws IOException { + public void sendRequest(Tile tile) throws IOException { if (mSocket != null) { - if (mMaxReq-- <= 0) + if (--mMaxRequests < 0) close(); else if (System.nanoTime() - mLastRequest > RESPONSE_TIMEOUT) close(); else { try { - if (mResponseStream.available() > 0) + if (mResponseStream.available() > 0) { + log.debug("still bytes available"); close(); + } } catch (IOException e) { log.debug(e.getMessage()); close(); @@ -346,36 +353,32 @@ public class LwHttp implements HttpEngine { lwHttpConnect(); /* TODO parse from header */ - mMaxReq = RESPONSE_EXPECTED_LIVES; + mMaxRequests = RESPONSE_EXPECTED_LIVES; } - byte[] request = mRequestBuffer; int pos = REQUEST_GET_START.length; - - pos = formatTilePath(tile, request, pos); - int len = REQUEST_GET_END.length; - System.arraycopy(REQUEST_GET_END, 0, request, pos, len); + + pos = formatTilePath(tile, mRequestBuffer, pos); + System.arraycopy(REQUEST_GET_END, 0, mRequestBuffer, pos, len); len += pos; if (dbg) - log.debug("request: {}", new String(request, 0, len)); + log.debug("request: {}", new String(mRequestBuffer, 0, len)); try { - mCommandStream.write(request, 0, len); - mCommandStream.flush(); - return true; + writeRequest(mRequestBuffer, len); } catch (IOException e) { log.debug("recreate connection"); close(); - /* might throw IOException */ lwHttpConnect(); - - mCommandStream.write(request, 0, len); - mCommandStream.flush(); + writeRequest(mRequestBuffer, len); } + } - return true; + private void writeRequest(byte[] request, int length) throws IOException { + mCommandStream.write(request, 0, length); + mCommandStream.flush(); } private boolean lwHttpConnect() throws IOException { @@ -500,10 +503,19 @@ public class LwHttp implements HttpEngine { * @return new position */ private int formatTilePath(Tile tile, byte[] buf, int pos) { + if (mTilePath == null) { + String url = mTileSource.getUrlFormatter() + .formatTilePath(mTileSource, tile); + byte[] b = url.getBytes(); + System.arraycopy(b, 0, buf, pos, b.length); + return pos + b.length; + } + for (byte[] b : mTilePath) { if (b.length == 1) { if (b[0] == '/') { buf[pos++] = '/'; + continue; } else if (b[0] == 'X') { pos = writeInt(tile.tileX, pos, buf); continue; @@ -527,13 +539,16 @@ public class LwHttp implements HttpEngine { @Override public HttpEngine create(UrlTileSource tileSource) { + if (tileSource.getUrlFormatter() != UrlTileSource.URL_FORMATTER) + return new LwHttp(tileSource, null); + + /* use optimized formatter replacing the default */ if (mTilePath == null) { String[] path = tileSource.getTilePath(); mTilePath = new byte[path.length][]; for (int i = 0; i < path.length; i++) mTilePath[i] = path[i].getBytes(); } - return new LwHttp(tileSource, mTilePath); } } diff --git a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java index 1e892e96..ac297b85 100644 --- a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java @@ -21,10 +21,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; import org.oscim.core.Tile; +import org.oscim.utils.IOUtils; import com.squareup.okhttp.OkHttpClient; @@ -57,33 +57,20 @@ public class OkHttpEngine implements HttpEngine { return inputStream; } - HttpURLConnection openConnection(Tile tile) throws MalformedURLException { - return mClient.open(new URL(mTileSource.getUrl() + - mTileSource.formatTilePath(tile))); - } - @Override - public boolean sendRequest(Tile tile) throws IOException { + public void sendRequest(Tile tile) throws IOException { if (tile == null) { throw new IllegalArgumentException("Tile cannot be null."); } + URL url = new URL(mTileSource.getTileUrl(tile)); + HttpURLConnection conn = mClient.open(url); - final HttpURLConnection connection = openConnection(tile); - - inputStream = connection.getInputStream(); - - return true; + inputStream = conn.getInputStream(); } @Override public void close() { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + IOUtils.closeQuietly(inputStream); } @Override diff --git a/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java b/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java index 4cf814cf..001d46ee 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileDataSource.java @@ -74,10 +74,9 @@ public class UrlTileDataSource implements ITileDataSource { boolean success = false; TileWriter cacheWriter = null; try { - InputStream is; - if (!mConn.sendRequest(tile)) { - log.debug("{} Request failed", tile); - } else if ((is = mConn.read()) == null) { + mConn.sendRequest(tile); + InputStream is = mConn.read(); + if (is == null) { log.debug("{} Network Error", tile); } else { if (mUseCache) { diff --git a/vtm/src/org/oscim/tiling/source/UrlTileSource.java b/vtm/src/org/oscim/tiling/source/UrlTileSource.java index 100cd9a9..6504c676 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileSource.java @@ -26,11 +26,18 @@ import org.oscim.tiling.source.LwHttp.LwHttpFactory; public abstract class UrlTileSource extends TileSource { + public final static TileUrlFormatter URL_FORMATTER = new DefaultTileUrlFormatter(); + private final URL mUrl; private final String[] mTilePath; private HttpEngine.Factory mHttpFactory; private Map mRequestHeaders; + private TileUrlFormatter mTileUrlFormatter = URL_FORMATTER; + + public interface TileUrlFormatter { + public String formatTilePath(UrlTileSource tileSource, Tile tile); + } public UrlTileSource(String url, String tilePath, int zoomMin, int zoomMax) { this(url, tilePath); @@ -72,29 +79,8 @@ public abstract class UrlTileSource extends TileSource { return mUrl; } - public String formatTilePath(Tile tile) { - // TODO only use the actual replacement positions. - - StringBuilder sb = new StringBuilder(); - for (String b : mTilePath) { - if (b.length() == 1) { - switch (b.charAt(0)) { - case 'X': - sb.append(tile.tileX); - continue; - case 'Y': - sb.append(tile.tileY); - continue; - case 'Z': - sb.append(tile.zoomLevel); - continue; - default: - break; - } - } - sb.append(b); - } - return sb.toString(); + public String getTileUrl(Tile tile) { + return mUrl + mTileUrlFormatter.formatTilePath(this, tile); } public void setHttpEngine(HttpEngine.Factory httpFactory) { @@ -113,11 +99,48 @@ public abstract class UrlTileSource extends TileSource { return mTilePath; } + /** + * + */ + public void setUrlFormatter(TileUrlFormatter formatter) { + mTileUrlFormatter = formatter; + } + + public TileUrlFormatter getUrlFormatter() { + return mTileUrlFormatter; + } + public HttpEngine getHttpEngine() { if (mHttpFactory == null) { mHttpFactory = new LwHttpFactory(); } - return mHttpFactory.create(this); } + + static class DefaultTileUrlFormatter implements TileUrlFormatter { + @Override + public String formatTilePath(UrlTileSource tileSource, Tile tile) { + + StringBuilder sb = new StringBuilder(); + for (String b : tileSource.getTilePath()) { + if (b.length() == 1) { + switch (b.charAt(0)) { + case 'X': + sb.append(tile.tileX); + continue; + case 'Y': + sb.append(tile.tileY); + continue; + case 'Z': + sb.append(tile.zoomLevel); + continue; + default: + break; + } + } + sb.append(b); + } + return sb.toString(); + } + } } From 0b82df4c21388fbff021b455e0e5744d2b536dc0 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Thu, 3 Apr 2014 16:31:20 +0200 Subject: [PATCH 14/16] add http request options --- vtm/src/org/oscim/tiling/source/LwHttp.java | 12 ++++-------- vtm/src/org/oscim/tiling/source/OkHttpEngine.java | 4 ++++ vtm/src/org/oscim/tiling/source/UrlTileSource.java | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/vtm/src/org/oscim/tiling/source/LwHttp.java b/vtm/src/org/oscim/tiling/source/LwHttp.java index 032a0574..ad877b23 100644 --- a/vtm/src/org/oscim/tiling/source/LwHttp.java +++ b/vtm/src/org/oscim/tiling/source/LwHttp.java @@ -85,19 +85,15 @@ public class LwHttp implements HttpEngine { REQUEST_GET_START = ("GET " + path).getBytes(); - String addRequest = ""; - if (tileSource.getRequestHeader() != null) { - StringBuffer sb = new StringBuffer(); - for (Entry l : tileSource.getRequestHeader().entrySet()) - sb.append('\n').append(l.getKey()).append(": ").append(l.getValue()); - addRequest = sb.toString(); - } + StringBuilder opt = new StringBuilder(); + for (Entry l : tileSource.getRequestHeader().entrySet()) + opt.append('\n').append(l.getKey()).append(": ").append(l.getValue()); REQUEST_GET_END = (" HTTP/1.1" + "\nUser-Agent: vtm/0.5.9" + "\nHost: " + host + "\nConnection: Keep-Alive" + - addRequest + + opt.toString() + "\n\n").getBytes(); mHost = host; diff --git a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java index ac297b85..fe39c8a3 100644 --- a/vtm/src/org/oscim/tiling/source/OkHttpEngine.java +++ b/vtm/src/org/oscim/tiling/source/OkHttpEngine.java @@ -22,6 +22,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Map.Entry; import org.oscim.core.Tile; import org.oscim.utils.IOUtils; @@ -65,6 +66,9 @@ public class OkHttpEngine implements HttpEngine { URL url = new URL(mTileSource.getTileUrl(tile)); HttpURLConnection conn = mClient.open(url); + for (Entry opt : mTileSource.getRequestHeader().entrySet()) + conn.addRequestProperty(opt.getKey(), opt.getValue()); + inputStream = conn.getInputStream(); } diff --git a/vtm/src/org/oscim/tiling/source/UrlTileSource.java b/vtm/src/org/oscim/tiling/source/UrlTileSource.java index 6504c676..9f31bed6 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileSource.java @@ -18,6 +18,7 @@ package org.oscim.tiling.source; import java.net.MalformedURLException; import java.net.URL; +import java.util.Collections; import java.util.Map; import org.oscim.core.Tile; @@ -27,12 +28,11 @@ import org.oscim.tiling.source.LwHttp.LwHttpFactory; public abstract class UrlTileSource extends TileSource { public final static TileUrlFormatter URL_FORMATTER = new DefaultTileUrlFormatter(); - private final URL mUrl; private final String[] mTilePath; private HttpEngine.Factory mHttpFactory; - private Map mRequestHeaders; + private Map mRequestHeaders = Collections.emptyMap(); private TileUrlFormatter mTileUrlFormatter = URL_FORMATTER; public interface TileUrlFormatter { From cb5de5135f6dbfced3f6864dfe71ea8e8a765baf Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Thu, 3 Apr 2014 16:08:51 +0200 Subject: [PATCH 15/16] cleanup: LwHttp --- vtm/src/org/oscim/tiling/source/LwHttp.java | 54 ++++++--------------- 1 file changed, 15 insertions(+), 39 deletions(-) diff --git a/vtm/src/org/oscim/tiling/source/LwHttp.java b/vtm/src/org/oscim/tiling/source/LwHttp.java index ad877b23..91a4e523 100644 --- a/vtm/src/org/oscim/tiling/source/LwHttp.java +++ b/vtm/src/org/oscim/tiling/source/LwHttp.java @@ -34,7 +34,7 @@ import org.slf4j.LoggerFactory; /** * Lightweight HTTP connection for tile loading. Does not do redirects, - * https, full header parsing or stuff. + * https, full header parsing or other stuff. */ public class LwHttp implements HttpEngine { static final Logger log = LoggerFactory.getLogger(LwHttp.class); @@ -81,7 +81,7 @@ public class LwHttp implements HttpEngine { String host = url.getHost(); String path = url.getPath(); - log.debug("open database: " + host + " " + port + " " + path); + //log.debug("open database: {} {} {}", host, port, path); REQUEST_GET_START = ("GET " + path).getBytes(); @@ -104,10 +104,6 @@ public class LwHttp implements HttpEngine { mRequestBuffer, 0, REQUEST_GET_START.length); } - // TODO: - // to avoid a copy in PbfDecoder one could manage the buffer - // array directly and provide access to it. - static class Buffer extends BufferedInputStream { static final class Buffer extends BufferedInputStream { OutputStream cache; int bytesRead = 0; @@ -203,9 +199,6 @@ public class LwHttp implements HttpEngine { if (data >= 0) bytesRead += 1; - //if (dbg) - // log.debug("read {} {}", bytesRead, contentLength); - if (cache != null && bytesRead > bytesWrote) { bytesWrote = bytesRead; cache.write(data); @@ -280,27 +273,21 @@ public class LwHttp implements HttpEngine { break; } - if (!ok) { - /* ignore until end of header */ - } else if (first) { - first = false; - /* check only for OK ("HTTP/1.? ".length == 9) */ - if (!check(HEADER_HTTP_OK, buf, pos + 9, end)) - ok = false; + if (ok) { + if (first) { + first = false; + /* check only for OK ("HTTP/1.? ".length == 9) */ + if (!check(HEADER_HTTP_OK, buf, pos + 9, end)) + ok = false; - } else if (check(HEADER_CONTENT_LENGTH, buf, pos, end)) { - /* parse Content-Length */ - contentLength = parseInt(buf, pos + - HEADER_CONTENT_LENGTH.length + 2, end - 1); - } else if (check(HEADER_CONNECTION_CLOSE, buf, pos, end)) { - mMustClose = true; + } else if (check(HEADER_CONTENT_LENGTH, buf, pos, end)) { + /* parse Content-Length */ + contentLength = parseInt(buf, pos + + HEADER_CONTENT_LENGTH.length + 2, end - 1); + } else if (check(HEADER_CONNECTION_CLOSE, buf, pos, end)) { + mMustClose = true; + } } - //} else if (check(HEADER_CONTENT_TYPE, buf, pos, end)) { - // check that response contains the expected - // Content-Type - //if (!check(mContentType, buf, pos + - // HEADER_CONTENT_TYPE.length + 2, end)) - // ok = false; if (!ok || dbg) { String line = new String(buf, pos, end - pos - 1); @@ -423,15 +410,6 @@ public class LwHttp implements HttpEngine { mResponseStream.setCache(null); if (!mResponseStream.finishedReading()) { - // StringBuffer sb = new StringBuffer(); - // try { - // int val; - // while ((val = mResponseStream.read()) >= 0) - // sb.append((char) val); - // } catch (IOException e) { - // - // } - //log.debug("invalid buffer position {}", sb.toString()); log.debug("invalid buffer position"); close(); return true; @@ -491,8 +469,6 @@ public class LwHttp implements HttpEngine { } /** - * Write tile url - the low level, no-allocations method, - * * @param tile the Tile * @param buf to write url string * @param pos current position From e84afa32f72455f30b916bfe19465565121d4f73 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Thu, 3 Apr 2014 05:53:19 +0200 Subject: [PATCH 16/16] use debug log in IOUtils.closeQuietly --- vtm/src/org/oscim/utils/IOUtils.java | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/vtm/src/org/oscim/utils/IOUtils.java b/vtm/src/org/oscim/utils/IOUtils.java index 00a7ea70..2dc59a4e 100644 --- a/vtm/src/org/oscim/utils/IOUtils.java +++ b/vtm/src/org/oscim/utils/IOUtils.java @@ -19,39 +19,44 @@ package org.oscim.utils; import java.io.Closeable; import java.io.IOException; import java.net.Socket; -import java.util.logging.Level; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * A utility class with IO-specific helper methods. */ public final class IOUtils { + final static Logger log = LoggerFactory.getLogger(IOUtils.class); /** * Invokes the {@link Closeable#close()} method on the given object. If an * {@link IOException} occurs during the - * method call, it will be caught and logged on level {@link Level#WARNING}. + * method call, it will be caught and logged. * * @param closeable * the data source which should be closed (may be null). */ public static void closeQuietly(Closeable closeable) { + if (closeable == null) + return; + try { - if (closeable != null) { - closeable.close(); - } + closeable.close(); } catch (IOException e) { - //log.debug(e.getMessage() + " " + e); + log.debug(e.getMessage()); } } /* for old java versions */ public static void closeQuietly(Socket closeable) { + if (closeable == null) + return; + try { - if (closeable != null) { - closeable.close(); - } + closeable.close(); } catch (IOException e) { - //log.debug(e.getMessage() + " " + e); + log.debug(e.getMessage()); } }