Skip to content

Commit

Permalink
[GEOS-11287] MapML throws unclear exceptions when asked to produce ma…
Browse files Browse the repository at this point in the history
…ps in unsupported CRSs
  • Loading branch information
aaime committed Feb 1, 2024
1 parent 8442ff5 commit 1b24fd3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.geoserver.mapml.xml.RelType;
import org.geoserver.mapml.xml.Select;
import org.geoserver.mapml.xml.UnitType;
import org.geoserver.ows.Dispatcher;
import org.geoserver.ows.URLMangler;
import org.geoserver.ows.util.ResponseUtils;
import org.geoserver.platform.ServiceException;
Expand Down Expand Up @@ -107,9 +108,8 @@ public class MapMLDocumentBuilder {
private final String layersCommaDelimited;
private final String layerTitlesCommaDelimited;
private final String stylesCommaDelimited;

private final GetMapRequest getMapRequest;
private String defaultStyle;

private String layerTitle;
private String imageFormat;
private String baseUrl;
Expand Down Expand Up @@ -153,7 +153,7 @@ public MapMLDocumentBuilder(
this.geoServer = geoServer;
this.request = request;
// this.layers = mapContent.layers();
GetMapRequest getMapRequest = mapContent.getRequest();
this.getMapRequest = mapContent.getRequest();
String rawLayersCommaDL = getMapRequest.getRawKvp().get("layers");
this.layers = toRawLayers(rawLayersCommaDL);
this.stylesCommaDelimited =
Expand Down Expand Up @@ -242,12 +242,7 @@ private Optional<Object> getFormat(GetMapRequest getMapRequest) {
* @throws ServiceException In the event of a service error.
*/
public Mapml getMapMLDocument() throws ServiceException {
try {
initialize();
} catch (RuntimeException re) {
LOGGER.log(Level.INFO, re.getMessage());
return null;
}
initialize();
prepareDocument();
return this.mapml;
}
Expand Down Expand Up @@ -357,12 +352,7 @@ private MapMLLayerMetadata layersToOneMapMLLayerMetadata(List<RawLayer> layers)
mapMLLayerMetadata.setTimeEnabled(false);
mapMLLayerMetadata.setElevationEnabled(false);
mapMLLayerMetadata.setTransparent(transparent.orElse(false));
ProjType projType = null;
try {
projType = ProjType.fromValue(proj.toUpperCase());
} catch (IllegalArgumentException | FactoryException iae) {
throw new ServiceException("Invalid TCRS name");
}
ProjType projType = parseProjType();
mapMLLayerMetadata.setBbbox(layersToBBBox(layers, projType));
mapMLLayerMetadata.setQueryable(layersToQueryable(layers));
mapMLLayerMetadata.setLayerLabel(layersToLabel(layers));
Expand All @@ -371,6 +361,31 @@ private MapMLLayerMetadata layersToOneMapMLLayerMetadata(List<RawLayer> layers)
return mapMLLayerMetadata;
}

/**
* Parses the projection into a ProjType, or throws a proper service exception indicating the
* unsupported CRS
*/
private ProjType parseProjType() {
try {
return ProjType.fromValue(proj.toUpperCase());
} catch (IllegalArgumentException | FactoryException iae) {
// figure out the parameter name (version dependent) and the actual original
// string value for the srs/crs parameter
String parameterName =
Optional.ofNullable(getMapRequest.getVersion())
.filter(v -> v.equals("1.3.0"))
.map(v -> "crs")
.orElse("srs");
Map<String, Object> rawKvp = Dispatcher.REQUEST.get().getRawKvp();
String value = (String) rawKvp.get("srs");
if (value == null) value = (String) rawKvp.get("crs");
throw new ServiceException(
"This projection is not supported by MapML: " + value,
ServiceException.INVALID_PARAMETER_VALUE,
parameterName);
}
}

/**
* Generate a merged queryable flag for a collection of raw layers
*
Expand Down Expand Up @@ -479,7 +494,6 @@ private MapMLLayerMetadata layerToMapMLLayerMetadata(RawLayer layer, String styl
String layerTitle = null;
ResourceInfo resourceInfo = null;
boolean isTransparent = true;
ProjType projType = null;
String styleName = null;
boolean tileLayerExists = false;
if (isLayerGroup) {
Expand Down Expand Up @@ -514,11 +528,7 @@ private MapMLLayerMetadata layerToMapMLLayerMetadata(RawLayer layer, String styl
layerName = layerInfo.getName().isEmpty() ? layer.getTitle() : layerInfo.getName();
layerTitle = getTitle(layerInfo, layerName);
}
try {
projType = ProjType.fromValue(proj.toUpperCase());
} catch (IllegalArgumentException | FactoryException iae) {
throw new ServiceException("Invalid TCRS name");
}
ProjType projType = parseProjType();
styleName = style != null ? style : "";
tileLayerExists =
gwc.hasTileLayer(isLayerGroup ? layerGroupInfo : layerInfo)
Expand Down Expand Up @@ -1444,11 +1454,7 @@ private void generateWMSQueryClientLinks(MapMLLayerMetadata mapMLLayerMetadata)
* @return String
*/
public String getMapMLHTMLDocument() {
try {
initialize();
} catch (ServiceException se) {
throw se;
}
initialize();
String layerLabel = "";
String layer = "";
String styleName = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,46 @@ public void testHTMLWorkspaceQualified() throws Exception {
assertThat(layerSrc, containsString("LAYERS=Lakes"));
}

@Test
public void testInvalidProjectionHTML() throws Exception {
String path =
"cite/wms?LAYERS=Lakes"
+ "&STYLES=&FORMAT="
+ MapMLConstants.MAPML_HTML_MIME_TYPE
+ "&SERVICE=WMS&VERSION=1.3.0"
+ "&REQUEST=GetMap"
+ "&SRS=EPSG:32632"
+ "&BBOX=-13885038,2870337,-7455049,6338174"
+ "&WIDTH=150"
+ "&HEIGHT=150"
+ "&format_options="
+ MapMLConstants.MAPML_WMS_MIME_TYPE_OPTION
+ ":image/png";
org.w3c.dom.Document dom = getAsDOM(path);
String message = checkLegacyException(dom, "InvalidParameterValue", "crs").trim();
assertEquals("This projection is not supported by MapML: EPSG:32632", message);
}

@Test
public void testInvalidProjectionMapML() throws Exception {
String path =
"cite/wms?LAYERS=Lakes"
+ "&STYLES=&FORMAT="
+ MapMLConstants.MAPML_MIME_TYPE
+ "&SERVICE=WMS&VERSION=1.3.0"
+ "&REQUEST=GetMap"
+ "&SRS=EPSG:32632"
+ "&BBOX=-13885038,2870337,-7455049,6338174"
+ "&WIDTH=150"
+ "&HEIGHT=150"
+ "&format_options="
+ MapMLConstants.MAPML_WMS_MIME_TYPE_OPTION
+ ":image/png";
org.w3c.dom.Document dom = getAsDOM(path);
String message = checkLegacyException(dom, "InvalidParameterValue", "crs").trim();
assertEquals("This projection is not supported by MapML: EPSG:32632", message);
}

@Test
public void testLargeBounds() throws Exception {
// a layer whose bounds exceed the capabilities of the projected MapML TileCRS,
Expand Down

0 comments on commit 1b24fd3

Please sign in to comment.