diff options
author | Leith Bade <leith@mapbox.com> | 2014-11-26 20:18:28 +1100 |
---|---|---|
committer | Leith Bade <leith@mapbox.com> | 2014-11-28 00:17:14 +1100 |
commit | 6702bdab8302a1576562140fbbd3a57e8e13a6cb (patch) | |
tree | 915c412111abeeaed310d15f8d6cdc23f8901d75 | |
parent | 1a59112f367f4e33562f7f8884e5bd0e52456d75 (diff) | |
download | qtlocation-mapboxgl-6702bdab8302a1576562140fbbd3a57e8e13a6cb.tar.gz |
More validation for #645
3 files changed, 23 insertions, 12 deletions
diff --git a/android/java/app/src/main/java/com/mapbox/mapboxgl/app/MapFragment.java b/android/java/app/src/main/java/com/mapbox/mapboxgl/app/MapFragment.java index ac88952657..25723ea975 100644 --- a/android/java/app/src/main/java/com/mapbox/mapboxgl/app/MapFragment.java +++ b/android/java/app/src/main/java/com/mapbox/mapboxgl/app/MapFragment.java @@ -38,8 +38,8 @@ public class MapFragment extends Fragment { // Create the map mMap = (MapView) inflater.inflate(R.layout.fragment_main, container, true); - // Load the API key - mMap.setAccessToken("access key goes here"); + // Load the access token + mMap.setAccessToken("access token goes here"); // Need to pass on any saved state to the map mMap.onCreate(savedInstanceState); diff --git a/android/java/lib/build.gradle b/android/java/lib/build.gradle index 2818550c01..fc5b78c3d4 100644 --- a/android/java/lib/build.gradle +++ b/android/java/lib/build.gradle @@ -30,7 +30,7 @@ android { } dependencies { - compile 'commons-io:commons-io:2.4' + compile 'commons-validator:commons-validator:1.4.0' compile 'com.android.support:support-annotations:21.0.0' compile 'com.android.support:support-v4:21.0.0' } diff --git a/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java b/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java index 37d96a138e..453507b37b 100644 --- a/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java +++ b/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java @@ -1,8 +1,5 @@ package com.mapbox.mapboxgl.lib; -import org.json.JSONException; -import org.json.JSONObject; - import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -32,8 +29,11 @@ import android.widget.ZoomButtonsController; import com.almeros.android.multitouch.gesturedetectors.RotateGestureDetector; import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector; +import org.apache.commons.validator.routines.UrlValidator; + import java.util.ArrayList; import java.util.List; +import java.util.UnknownFormatConversionException; // Custom view that shows a Map // Based on SurfaceView as we use OpenGL ES to render @@ -309,6 +309,14 @@ public class MapView extends SurfaceView { mNativeMapView.toggleDebug(); } + private void validateStyleUrl(String url) { + String[] schemes = {"http", "https", "file", "asset"}; + UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS | UrlValidator.ALLOW_LOCAL_URLS); + if (!urlValidator.isValid(url)) { + throw new RuntimeException("Style URL is not a valid http, https, file or asset URL."); + } + } + public void setStyleUrl(String url) { mStyleUrl = url; mNativeMapView.setStyleUrl(url); @@ -318,7 +326,15 @@ public class MapView extends SurfaceView { return mStyleUrl; } + private void validateAccessToken(String accessToken) { + + if (accessToken.isEmpty() | (!getAccessToken().startsWith("pk.") && !getAccessToken().startsWith("sk."))) { + throw new RuntimeException("Using MapView requires setting a valid access token. See the README.md"); + } + } + public void setAccessToken(String accessToken) { + validateAccessToken(accessToken); mNativeMapView.setAccessToken(accessToken); } @@ -369,15 +385,10 @@ public class MapView extends SurfaceView { mNativeMapView.setDefaultTransitionDuration(savedInstanceState.getLong(STATE_DEFAULT_TRANSITION_DURATION)); } - if (mNativeMapView.getAccessToken().isEmpty()) { - throw new RuntimeException("Using MapView requires setting an access key."); - } + validateAccessToken(getAccessToken()); mNativeMapView.initializeDisplay(); mNativeMapView.initializeContext(); - if (!getAccessToken().startsWith("pk.") && !getAccessToken().startsWith("sk.")) { - throw new RuntimeException("You must set a valid access token! See the README.md"); - } mNativeMapView.start(); } |