From d0872d70af0d6ec5de4525aba6d421d6afcfb14f Mon Sep 17 00:00:00 2001 From: Gabriel Roldan Date: Fri, 29 Apr 2022 11:07:21 -0300 Subject: [PATCH] ogcapi's geo+json output formats cause NPE when request comes from WFS ogc-features introduce two GeoJSON output formats: - RFCGeoJSONFeaturesResponse - DGGSGeoJSONResponse As such, they're listed in WFS GetCapabilities as GetFeature operation's output formats, besides the usual 'application/json' one. When feature collection is requested through WFS with output format application/geo+json, an NPE exeption is thrown once it streamed the features and calls writeLinks(), resulting in an XML exception report mixed up with the JSON output. This patch checks whether APIRequestInfo.get() returns null, which means the response being produced by these new encoders is not coming from an ogc-api endpoint, and avoids using APIRequestInfo to write json links. --- .../src/main/java/org/geoserver/api/APIRequestInfo.java | 5 ++++- .../geoserver/api/features/RFCGeoJSONFeaturesResponse.java | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/community/ogcapi/ogcapi-core/src/main/java/org/geoserver/api/APIRequestInfo.java b/src/community/ogcapi/ogcapi-core/src/main/java/org/geoserver/api/APIRequestInfo.java index f83ea0c36b4..58caf5dee00 100644 --- a/src/community/ogcapi/ogcapi-core/src/main/java/org/geoserver/api/APIRequestInfo.java +++ b/src/community/ogcapi/ogcapi-core/src/main/java/org/geoserver/api/APIRequestInfo.java @@ -76,7 +76,10 @@ String buildURI(String base, String path) { return ResponseUtils.buildURL(baseURL, path, null, URLMangler.URLType.SERVICE); } - /** Returns the APIRequestInfo from the current {@link RequestContextHolder} */ + /** + * Returns the APIRequestInfo from the current {@link RequestContextHolder}, or {@code null} + * indicating there's no OGC API request bound to the calling thread + */ public static APIRequestInfo get() { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes == null) return null; diff --git a/src/community/ogcapi/ogcapi-features/src/main/java/org/geoserver/api/features/RFCGeoJSONFeaturesResponse.java b/src/community/ogcapi/ogcapi-features/src/main/java/org/geoserver/api/features/RFCGeoJSONFeaturesResponse.java index d3d06562a1f..5a83f38ac16 100644 --- a/src/community/ogcapi/ogcapi-features/src/main/java/org/geoserver/api/features/RFCGeoJSONFeaturesResponse.java +++ b/src/community/ogcapi/ogcapi-features/src/main/java/org/geoserver/api/features/RFCGeoJSONFeaturesResponse.java @@ -111,6 +111,10 @@ private void writeLinks( GeoJSONBuilder jw, String featureId) { APIRequestInfo requestInfo = APIRequestInfo.get(); + if (null == requestInfo) { + // request comes from WFS, not from ogcapi + return; + } GetFeatureRequest request = GetFeatureRequest.adapt(operation.getParameters()[0]); FeatureTypeInfo featureType = getFeatureType(request); String baseUrl = request.getBaseUrl();