Skip to content

Commit

Permalink
Harvesting / WFS feature / Add WFS2 support for MapServer
Browse files Browse the repository at this point in the history
Mapserver may return by default GetCapabilities in version 2.0.0.
Depending on the URL set in metadata records, WFS harvesting may fails
with:

```
2023-10-27T10:54:42,350 ERROR [geonetwork.harvest.wfs.features] -
Failed to connect to server 'http://geoservices.brgm.fr/geologie?language=fre&'.
Error is class net.opengis.wfs20.impl.WFSCapabilitiesTypeImpl
cannot be cast to class net.opengis.wfs.WFSCapabilitiesType
(net.opengis.wfs20.impl.WFSCapabilitiesTypeImpl and net.opengis.wfs.WFSCapabilitiesType
are in unnamed module of loader org.eclipse.jetty.webapp.WebAppClassLoader @cda6100)
```
because of the `MapServerWFSStrategy` extending WFS1 strategy.

Use `StrictWFS_2_0_Strategy` if harvesting is done using 2.0.0 on
MapServer.

Can be tested with
```
curl 'http://localhost:8080/geonetwork/srv/api/workers/data/wfs/actions/start' \
  -X 'PUT' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: eng' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Cookie: JSESSIONID=node0hjdye0js612o1qex2n36lekt81.node0; serverTime=1698396822574; sessionExpiry=1698398922574; XSRF-TOKEN=c353b2ca-a77c-4552-839c-37405771c4e2;' \
  -H 'X-XSRF-TOKEN: c353b2ca-a77c-4552-839c-37405771c4e2' \
  --data-raw '{"url":"http://geoservices.brgm.fr/geologie?language=fre&","strategy":"investigator","typeName":"GITES_EMP","version":"2.0.0"}' \
  --compressed
```
  • Loading branch information
fxprunayre committed Jan 5, 2024
1 parent 016399b commit 81efcfc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*/
@XmlRootElement(name = "wfs")
public class WFSHarvesterParameter implements Serializable {
public static final String DEFAULT_VERSION = "1.1.0";
private String metadataUuid;

private String url;
Expand All @@ -41,7 +42,7 @@ public class WFSHarvesterParameter implements Serializable {

private String typeName;

private String version = "1.1.0";
private String version = DEFAULT_VERSION;

private int timeOut = 300000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.geotools.data.wfs.internal.WFSGetCapabilities;
import org.geotools.data.wfs.internal.WFSStrategy;
import org.geotools.data.wfs.internal.v1_x.MapServerWFSStrategy;
import org.geotools.data.wfs.internal.v2_0.StrictWFS_2_0_Strategy;
import org.geotools.http.HTTPClient;
import org.geotools.http.HTTPResponse;
import org.geotools.ows.ServiceException;
Expand All @@ -38,6 +39,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import static org.fao.geonet.harvester.wfsfeatures.worker.WFSHarvesterExchangeState.MAPSERVER_STRATEGY;
import static org.fao.geonet.harvester.wfsfeatures.worker.WFSHarvesterExchangeState.QGIS_STRATEGY;
Expand Down Expand Up @@ -84,12 +86,16 @@ private WFSStrategy determineCorrectStrategy(HTTPClient httpClient) {
while ((readCount = inputStream.read(buff)) != -1) {
out.write(buff, 0, readCount);
}
String responsePayload = out.toString("UTF-8");
String responsePayload = out.toString(StandardCharsets.UTF_8);
if (responsePayload.contains("targetNamespace=\"http://www.qgis.org/gml\"")) {
strategy = new QgisStrategy();
strategyId = QGIS_STRATEGY;
} else if (responsePayload.contains("targetNamespace=\"http://mapserver.gis.umn.edu/mapserver\"")) {
strategy = new MapServerWFSStrategy(capabilities.getRawDocument());
if (capabilities.getVersion().equals("2.0.0")) {
strategy = new StrictWFS_2_0_Strategy();
} else {
strategy = new MapServerWFSStrategy(capabilities.getRawDocument());
}
strategyId = MAPSERVER_STRATEGY;
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@
*/
public class WFSDataStoreWithStrategyInvestigator extends WFSDataStoreFactory {

private static Logger LOGGER = LoggerFactory.getLogger(WFSHarvesterRouteBuilder.LOGGER_NAME);
private String describeFeatureTypeUrl;

public void init (String url, String typeName) throws Exception {
this.describeFeatureTypeUrl = new OwsUtils().getDescribeFeatureTypeUrl(url, typeName, "1.1.0");
public void init (String url, String typeName, String version) throws Exception {
this.describeFeatureTypeUrl = new OwsUtils().getDescribeFeatureTypeUrl(url, typeName, version);
}

@Override
Expand All @@ -66,7 +65,7 @@ public WFSDataStore createDataStore(Map params) throws IOException {
}
}

final URL capabilitiesURL = (URL) URL.lookUp(params);
final URL capabilitiesURL = URL.lookUp(params);

final HTTPClient http = getHttpClient(params);
http.setTryGzip(config.isTryGZIP());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void initDataStore() throws Exception {
if (INVESTIGATOR_STRATEGY.equals(parameters.getStrategy())) {
factory = new WFSDataStoreWithStrategyInvestigator();
((WFSDataStoreWithStrategyInvestigator) factory).init(
parameters.getUrl(), parameters.getTypeName());
parameters.getUrl(), parameters.getTypeName(), parameters.getVersion());
} else {
factory = new WFSDataStoreFactory();
}
Expand Down Expand Up @@ -145,15 +145,9 @@ public void initDataStore() throws Exception {
}

wfsDatastore = factory.createDataStore(m);
// Default to GeoTools auto mode for MapServer.
if(factory instanceof WFSDataStoreWithStrategyInvestigator) {
WFSClientWithStrategyInvestigator wfsClientWithStrategyInvestigator = (WFSClientWithStrategyInvestigator) wfsDatastore.getWfsClient();
this.strategyId = wfsClientWithStrategyInvestigator.getStrategyId();
if (MAPSERVER_STRATEGY.equals(wfsClientWithStrategyInvestigator.getStrategyId())) {
Map<String, Object> connectionParameters = new HashMap<>();
connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", parameters.getUrl());
wfsDatastore = (WFSDataStore) DataStoreFinder.getDataStore(connectionParameters);
}
}
} catch (IOException e) {
String errorMsg = String.format(
Expand Down

0 comments on commit 81efcfc

Please sign in to comment.