Skip to content

Commit

Permalink
Improve diagnostics when GetCapabilties fails
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Aug 25, 2023
1 parent bd4b433 commit 13784dd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public void manageEndpoints(@NonNull final FragmentActivity activity, @Nullable
}

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;
final OnUpdateListener onUpdateListener;
Expand Down Expand Up @@ -129,51 +133,54 @@ protected void onPreExecute() {
}

@Override
protected WmsCapabilities doInBackground(Void params) {
String url = Util.appendQuery(sanitize(endpoint.getTileUrl()), "request=GetCapabilities&service=wms");
protected WmsCapabilities doInBackground(Void params) throws IOException, ParserConfigurationException, SAXException {
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);
} catch (IOException | ParserConfigurationException | SAXException e) {
Log.e(DEBUG_TAG, e.getMessage());
}
return null;
}

protected void onBackgroundError(Exception e) {
Progress.dismissDialog(activity, Progress.PROGRESS_DOWNLOAD);
Log.e(DEBUG_TAG, e.getMessage());
Snack.toastTopError(context, activity.getString(R.string.toast_error_wms_getcapabilities, e.getMessage()));
}

@Override
protected void onPostExecute(WmsCapabilities result) {
Progress.dismissDialog(activity, Progress.PROGRESS_DOWNLOAD);
if (result != null) {
Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.select_layer_title);
builder.setNeutralButton(R.string.Done, null);
View layerListView = LayoutInflater.from(activity).inflate(R.layout.wms_layer_list, null);
ListView layerList = (ListView) layerListView.findViewById(R.id.listViewLayer);
builder.setView(layerListView);
List<String> layers = new ArrayList<>();
for (Layer layer : result.layers) {
layers.add(layer.title);
}
WidestItemArrayAdapter<String> adapter = new WidestItemArrayAdapter<>(activity, R.layout.layer_list_item, R.id.name, layers);
layerList.setAdapter(adapter);
layerList.setOnItemClickListener((parent, view, position, id) -> {
LayerEntry entry = new LayerEntry();
Layer layer = result.layers.get(position);
entry.title = layer.title;
entry.tileUrl = layer.getTileUrl(result.getGetMapUrl() != null ? result.getGetMapUrl() : sanitize(endpoint.getTileUrl()));
entry.box = layer.extent;
entry.gsd = layer.gsd;
TileLayerDialog.showLayerDialog(activity, -1, entry, () -> {
TileLayerDatabaseView.resetLayer(activity, db);
if (onUpdateListener != null) {
onUpdateListener.update();
}
});
if (result.layers.isEmpty()) {
Snack.toastTopWarning(activity, R.string.toast_nothing_found);
}
Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.select_layer_title);
builder.setNeutralButton(R.string.Done, null);
View layerListView = LayoutInflater.from(activity).inflate(R.layout.wms_layer_list, null);
ListView layerList = (ListView) layerListView.findViewById(R.id.listViewLayer);
builder.setView(layerListView);
List<String> layers = new ArrayList<>();
for (Layer layer : result.layers) {
layers.add(layer.title);
}
WidestItemArrayAdapter<String> adapter = new WidestItemArrayAdapter<>(activity, R.layout.layer_list_item, R.id.name, layers);
layerList.setAdapter(adapter);
layerList.setOnItemClickListener((parent, view, position, id) -> {
LayerEntry entry = new LayerEntry();
Layer layer = result.layers.get(position);
entry.title = layer.title;
entry.tileUrl = layer.getTileUrl(result.getGetMapUrl() != null ? result.getGetMapUrl() : sanitize(endpoint.getTileUrl()));
entry.box = layer.extent;
entry.gsd = layer.gsd;
TileLayerDialog.showLayerDialog(activity, -1, entry, () -> {
TileLayerDatabaseView.resetLayer(activity, db);
if (onUpdateListener != null) {
onUpdateListener.update();
}
});
});

builder.create().show();
} else {
Snack.toastTopError(activity, R.string.toast_nothing_found);
}
builder.create().show();
}
}.execute();
});
Expand All @@ -191,7 +198,7 @@ private String sanitize(@NonNull String url) {
Uri.Builder uriBuilder = uri.buildUpon();
uriBuilder.clearQuery();
for (String n : uri.getQueryParameterNames()) {
if (!"request".equalsIgnoreCase(n) && !"service".equalsIgnoreCase(n)) {
if (!"".equals(n) && !REQUEST_PARAM.equalsIgnoreCase(n) && !SERVICE_PARAM.equalsIgnoreCase(n)) {
uriBuilder.appendQueryParameter(n, uri.getQueryParameter(n));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@
<string name="toast_updating_parents">Updating parents…</string>
<string name="toast_updating_members">Updating members…</string>
<string name="toast_error_loading_egm">Error loading EGM %1$s</string>
<string name="toast_error_wms_getcapabilities">Error querying WMS server: %1$s</string>
<!-- Error messages -->
<string name="error_mapsplit_missing_zoom">MapSplit sources must have min and max zoom set</string>
<string name="error_pbf_no_version">Version information missing in PBF file</string>
Expand Down

0 comments on commit 13784dd

Please sign in to comment.