From 9a7c1d45947815967ee0c6793bda10afabeba711 Mon Sep 17 00:00:00 2001 From: simonpoole Date: Fri, 25 Aug 2023 10:48:15 +0200 Subject: [PATCH 1/2] Improve diagnostics when GetCapabilties fails --- .../android/resources/WmsEndpointDatabaseView.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/blau/android/resources/WmsEndpointDatabaseView.java b/src/main/java/de/blau/android/resources/WmsEndpointDatabaseView.java index a6e40f37ca..96e3b62b07 100644 --- a/src/main/java/de/blau/android/resources/WmsEndpointDatabaseView.java +++ b/src/main/java/de/blau/android/resources/WmsEndpointDatabaseView.java @@ -135,6 +135,10 @@ public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { } private class EndpointAdapter extends CursorAdapter { + private static final String SERVICE_PARAM = "service"; + private static final String REQUEST_PARAM = "request"; + private static final String GET_CAPABILITIES_REQUEST = "GetCapabilities"; + final SQLiteDatabase db; final FragmentActivity activity; @@ -178,16 +182,17 @@ protected void onPreExecute() { @Override protected WmsCapabilities doInBackground(Void params) throws IOException, ParserConfigurationException, SAXException { - String url = Util.appendQuery(sanitize(endpoint.getTileUrl()), "request=GetCapabilities&service=wms"); + String url = Util.appendQuery(sanitize(endpoint.getTileUrl()), + REQUEST_PARAM + "=" + GET_CAPABILITIES_REQUEST + "&" + SERVICE_PARAM + "=wms"); try (InputStream is = Server.openConnection(activity, new URL(url))) { return new WmsCapabilities(is); } } - @Override protected void onBackgroundError(Exception e) { Progress.dismissDialog(activity, Progress.PROGRESS_DOWNLOAD); - Snack.toastTopError(activity, activity.getString(R.string.toast_querying_wms_server_failed, e.getLocalizedMessage())); + Log.e(DEBUG_TAG, e.getMessage()); + Snack.toastTopError(context, activity.getString(R.string.toast_querying_wms_server_failed, e.getMessage())); } @Override @@ -236,7 +241,7 @@ private String sanitize(@NonNull String url) { Uri.Builder uriBuilder = uri.buildUpon(); uriBuilder.clearQuery(); for (String n : uri.getQueryParameterNames()) { - if (!"".equals(n) && !"request".equalsIgnoreCase(n) && !"service".equalsIgnoreCase(n)) { + if (!"".equals(n) && !REQUEST_PARAM.equalsIgnoreCase(n) && !SERVICE_PARAM.equalsIgnoreCase(n)) { uriBuilder.appendQueryParameter(n, uri.getQueryParameter(n)); } } From 03ebc2a4fe5937142e8b9b1e56772d24900b25a9 Mon Sep 17 00:00:00 2001 From: simonpoole Date: Sun, 27 Aug 2023 13:02:16 +0200 Subject: [PATCH 2/2] Don't add layers with unsupported image format or projection --- .../android/resources/WmsCapabilities.java | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/blau/android/resources/WmsCapabilities.java b/src/main/java/de/blau/android/resources/WmsCapabilities.java index bf1121573d..ede778a572 100644 --- a/src/main/java/de/blau/android/resources/WmsCapabilities.java +++ b/src/main/java/de/blau/android/resources/WmsCapabilities.java @@ -22,6 +22,7 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import de.blau.android.exception.UnsupportedFormatException; import de.blau.android.osm.BoundingBox; import de.blau.android.osm.Node; import de.blau.android.util.GeoMath; @@ -66,13 +67,21 @@ public class WmsCapabilities { private static final String REQUEST = "Request"; private static final String GETMAP = "GetMap"; private static final String GET = "Get"; + private static final String MAXY_ATTR = "maxy"; + private static final String MAXX_ATTR = "maxx"; + private static final String MINY_ATTR = "miny"; + private static final String MINX_ATTR = "minx"; + private static final String ONLINE_RESOURCE = "OnlineResource"; + private static final String XLINK_HREF_ATTR = "xlink:href"; - private static final String IMAGE_BMP = "image/bmp"; - private static final String IMAGE_PNG = "image/png"; - private static final String IMAGE_PNG8 = "image/png8"; - public static final String IMAGE_JPEG = "image/jpeg"; - private static final String IMAGE_JPEG_PNG = "image/vnd.jpeg-png"; - private static final String IMAGE_JPEG_PNG8 = "image/vnd.jpeg-png8"; + private static final String IMAGE_BMP = "image/bmp"; + private static final String IMAGE_PNG = "image/png"; + private static final String IMAGE_PNG8 = "image/png8"; + public static final String IMAGE_JPEG = "image/jpeg"; + private static final String IMAGE_JPEG_PNG = "image/vnd.jpeg-png"; + private static final String IMAGE_JPEG_PNG8 = "image/vnd.jpeg-png8"; + + // order is most preferred to least public static final List FORMAT_PREFERENCE = Collections .unmodifiableList(Arrays.asList(IMAGE_JPEG_PNG8, IMAGE_JPEG_PNG, IMAGE_PNG8, IMAGE_PNG, IMAGE_JPEG, IMAGE_BMP)); @@ -222,10 +231,10 @@ public void startElement(String uri, String localName, String name, Attributes a if (TileLayerSource.EPSG_4326.equals(tempCrs) || (TileLayerSource.is3857compatible(tempCrs) && !TileLayerSource.EPSG_4326.equals(current.boxCrs))) { try { - current.minx = new BigDecimal(attr.getValue("minx")); - current.miny = new BigDecimal(attr.getValue("miny")); - current.maxx = new BigDecimal(attr.getValue("maxx")); - current.maxy = new BigDecimal(attr.getValue("maxy")); + current.minx = new BigDecimal(attr.getValue(MINX_ATTR)); + current.miny = new BigDecimal(attr.getValue(MINY_ATTR)); + current.maxx = new BigDecimal(attr.getValue(MAXX_ATTR)); + current.maxy = new BigDecimal(attr.getValue(MAXY_ATTR)); current.boxCrs = tempCrs; } catch (NumberFormatException e) { Log.e(DEBUG_TAG, "Error in bounding box " + e.getMessage()); @@ -307,8 +316,8 @@ public void startElement(String uri, String localName, String name, Attributes a } break; case GET: - if ("OnlineResource".equals(name)) { - getMapUrl = attr.getValue("xlink:href"); + if (ONLINE_RESOURCE.equals(name)) { + getMapUrl = attr.getValue(XLINK_HREF_ATTR); } break; default: @@ -360,8 +369,12 @@ public void endElement(String uri, String localName, String name) throws SAXExce break; case LAYER: if (!current.group && current.name != null) { - Layer layer = constructLayer(layerStack); - layers.add(layer); + try { + Layer layer = constructLayer(layerStack); + layers.add(layer); + } catch (UnsupportedFormatException fex) { + Log.e(DEBUG_TAG, fex.getMessage()); + } } layerStack.pop(); currentState = stateStack.pop(); @@ -481,10 +494,13 @@ public void endElement(String uri, String localName, String name) throws SAXExce * @return a Layer object */ @NonNull - Layer constructLayer(@NonNull Deque stack) { + private Layer constructLayer(@NonNull Deque stack) { Layer layer = new Layer(); layer.wmsVersion = wmsVersion; layer.format = tileFormat; + if (layer.format == null) { + throw new UnsupportedFormatException("No supported image format"); + } StringBuilder resultTitle = new StringBuilder(); boolean first = true; Iterator it = stack.descendingIterator(); @@ -504,6 +520,8 @@ Layer constructLayer(@NonNull Deque stack) { } if (t.crs != null) { layer.proj = t.crs; + } else { + throw new UnsupportedFormatException("No supported projection"); } if (t.boxCrs != null) { try {