Add vector tile api key parameter, closes #49

This commit is contained in:
Emux 2016-07-06 17:12:48 +03:00
parent dfe4a10bf0
commit 7a88524343
2 changed files with 43 additions and 6 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
package org.oscim.tiling.source; package org.oscim.tiling.source;
import static org.fest.assertions.api.Assertions.assertThat; import static org.fest.assertions.api.Assertions.assertThat;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.oscim.core.Tile;
import org.oscim.tiling.ITileDataSource; import org.oscim.tiling.ITileDataSource;
public class UrlTileSourceTest { public class UrlTileSourceTest {
@ -14,6 +31,12 @@ public class UrlTileSourceTest {
tileSource = new TestTileSource("http://example.org/tiles/vtm", "/{Z}/{X}/{Z}.vtm"); 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 @Test
public void shouldNotBeNull() throws Exception { public void shouldNotBeNull() throws Exception {
assertThat(tileSource).isNotNull(); assertThat(tileSource).isNotNull();

View File

@ -1,5 +1,6 @@
/* /*
* Copyright 2013 Hannes Janetzek * Copyright 2013 Hannes Janetzek
* Copyright 2016 devemux86
* *
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * 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 tilePath;
protected String url; protected String url;
private HttpEngine.Factory engineFactory; private HttpEngine.Factory engineFactory;
private String apiKey;
protected Builder() { protected Builder() {
@ -43,6 +45,11 @@ public abstract class UrlTileSource extends TileSource {
this.zoomMax = zoomMax; this.zoomMax = zoomMax;
} }
public T apiKey(String apiKey) {
this.apiKey = apiKey;
return self();
}
public T tilePath(String tilePath) { public T tilePath(String tilePath) {
this.tilePath = tilePath; this.tilePath = tilePath;
return self(); return self();
@ -67,13 +74,15 @@ public abstract class UrlTileSource extends TileSource {
private HttpEngine.Factory mHttpFactory; private HttpEngine.Factory mHttpFactory;
private Map<String, String> mRequestHeaders = Collections.emptyMap(); private Map<String, String> mRequestHeaders = Collections.emptyMap();
private TileUrlFormatter mTileUrlFormatter = URL_FORMATTER; private TileUrlFormatter mTileUrlFormatter = URL_FORMATTER;
private String mApiKey;
public interface TileUrlFormatter { public interface TileUrlFormatter {
public String formatTilePath(UrlTileSource tileSource, Tile tile); String formatTilePath(UrlTileSource tileSource, Tile tile);
} }
protected UrlTileSource(Builder<?> builder) { protected UrlTileSource(Builder<?> builder) {
super(builder); super(builder);
mApiKey = builder.apiKey;
mUrl = makeUrl(builder.url); mUrl = makeUrl(builder.url);
mTilePath = builder.tilePath.split("\\{|\\}"); mTilePath = builder.tilePath.split("\\{|\\}");
mHttpFactory = builder.engineFactory; mHttpFactory = builder.engineFactory;
@ -97,7 +106,7 @@ public abstract class UrlTileSource extends TileSource {
} }
private URL makeUrl(String urlString) { private URL makeUrl(String urlString) {
URL url = null; URL url;
try { try {
url = new URL(urlString); url = new URL(urlString);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
@ -116,12 +125,20 @@ public abstract class UrlTileSource extends TileSource {
} }
public void setApiKey(String apiKey) {
mApiKey = apiKey;
}
public URL getUrl() { public URL getUrl() {
return mUrl; return mUrl;
} }
public String getTileUrl(Tile tile) { 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) { public void setHttpEngine(HttpEngine.Factory httpFactory) {
@ -140,9 +157,6 @@ public abstract class UrlTileSource extends TileSource {
return mTilePath; return mTilePath;
} }
/**
*
*/
public void setUrlFormatter(TileUrlFormatter formatter) { public void setUrlFormatter(TileUrlFormatter formatter) {
mTileUrlFormatter = formatter; mTileUrlFormatter = formatter;
} }