From 7a88524343c0bb02766d921e4f0adf03bdfdf1e8 Mon Sep 17 00:00:00 2001 From: Emux Date: Wed, 6 Jul 2016 17:12:48 +0300 Subject: [PATCH] Add vector tile api key parameter, closes #49 --- .../tiling/source/UrlTileSourceTest.java | 23 ++++++++++++++++ .../oscim/tiling/source/UrlTileSource.java | 26 ++++++++++++++----- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java b/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java index b52eb830..f9441420 100644 --- a/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java +++ b/vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java @@ -1,9 +1,26 @@ +/* + * Copyright 2016 devemux86 + * + * 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 static org.fest.assertions.api.Assertions.assertThat; import org.junit.Before; import org.junit.Test; +import org.oscim.core.Tile; import org.oscim.tiling.ITileDataSource; public class UrlTileSourceTest { @@ -14,6 +31,12 @@ public class UrlTileSourceTest { tileSource = new TestTileSource("http://example.org/tiles/vtm", "/{Z}/{X}/{Z}.vtm"); } + @Test + public void setApiKey_shouldAppendApiKey() throws Exception { + tileSource.setApiKey("testkey"); + assertThat(tileSource.getTileUrl(new Tile(0, 0, (byte) 0))).endsWith("?api_key=testkey"); + } + @Test public void shouldNotBeNull() throws Exception { assertThat(tileSource).isNotNull(); diff --git a/vtm/src/org/oscim/tiling/source/UrlTileSource.java b/vtm/src/org/oscim/tiling/source/UrlTileSource.java index ac1c0140..b35cb947 100644 --- a/vtm/src/org/oscim/tiling/source/UrlTileSource.java +++ b/vtm/src/org/oscim/tiling/source/UrlTileSource.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Hannes Janetzek + * Copyright 2016 devemux86 * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -31,6 +32,7 @@ public abstract class UrlTileSource extends TileSource { protected String tilePath; protected String url; private HttpEngine.Factory engineFactory; + private String apiKey; protected Builder() { @@ -43,6 +45,11 @@ public abstract class UrlTileSource extends TileSource { this.zoomMax = zoomMax; } + public T apiKey(String apiKey) { + this.apiKey = apiKey; + return self(); + } + public T tilePath(String tilePath) { this.tilePath = tilePath; return self(); @@ -67,13 +74,15 @@ public abstract class UrlTileSource extends TileSource { private HttpEngine.Factory mHttpFactory; private Map mRequestHeaders = Collections.emptyMap(); private TileUrlFormatter mTileUrlFormatter = URL_FORMATTER; + private String mApiKey; public interface TileUrlFormatter { - public String formatTilePath(UrlTileSource tileSource, Tile tile); + String formatTilePath(UrlTileSource tileSource, Tile tile); } protected UrlTileSource(Builder builder) { super(builder); + mApiKey = builder.apiKey; mUrl = makeUrl(builder.url); mTilePath = builder.tilePath.split("\\{|\\}"); mHttpFactory = builder.engineFactory; @@ -97,7 +106,7 @@ public abstract class UrlTileSource extends TileSource { } private URL makeUrl(String urlString) { - URL url = null; + URL url; try { url = new URL(urlString); } catch (MalformedURLException e) { @@ -116,12 +125,20 @@ public abstract class UrlTileSource extends TileSource { } + public void setApiKey(String apiKey) { + mApiKey = apiKey; + } + public URL getUrl() { return mUrl; } public String getTileUrl(Tile tile) { - return mUrl + mTileUrlFormatter.formatTilePath(this, tile); + String tileUrl = mUrl + mTileUrlFormatter.formatTilePath(this, tile); + if (mApiKey != null) { + tileUrl += String.format("?api_key=%s", mApiKey); + } + return tileUrl; } public void setHttpEngine(HttpEngine.Factory httpFactory) { @@ -140,9 +157,6 @@ public abstract class UrlTileSource extends TileSource { return mTilePath; } - /** - * - */ public void setUrlFormatter(TileUrlFormatter formatter) { mTileUrlFormatter = formatter; }