View Javadoc
1   /*
2    * Copyright (C) 2022 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.geotools.featuresources;
7   
8   import java.io.IOException;
9   import java.util.HashMap;
10  import java.util.Map;
11  import java.util.Set;
12  import org.geotools.api.data.DataStore;
13  import org.geotools.api.data.ResourceInfo;
14  import org.geotools.api.data.SimpleFeatureSource;
15  import org.geotools.data.wfs.WFSDataStoreFactory;
16  import org.geotools.data.wfs.internal.FeatureTypeInfo;
17  import org.geotools.util.PreventLocalEntityResolver;
18  import org.springframework.util.LinkedCaseInsensitiveMap;
19  import org.springframework.web.util.UriComponentsBuilder;
20  import org.tailormap.api.geotools.wfs.SimpleWFSHelper;
21  import org.tailormap.api.geotools.wfs.SortingWFSDataStoreFactory;
22  import org.tailormap.api.persistence.TMFeatureSource;
23  import org.tailormap.api.persistence.TMFeatureType;
24  import org.tailormap.api.persistence.helper.GeoToolsHelper;
25  import org.tailormap.api.persistence.json.ServiceAuthentication;
26  import org.tailormap.api.persistence.json.TMFeatureTypeInfo;
27  
28  public class WFSFeatureSourceHelper extends FeatureSourceHelper {
29    /** WFS versions that can be forced with a VERSION parameter in the feature source URL. */
30    private static final Set<String> SUPPORTED_WFS_VERSIONS = Set.of("1.0.0", "1.1.0", "2.0.0");
31  
32    /**
33     * Get the WFS version to use for a feature source URL. This is {@link SimpleWFSHelper#DEFAULT_WFS_VERSION}, unless
34     * the URL has a VERSION parameter with a version GeoTools supports, for example to force WFS 1.1.0 for a server
35     * with a broken WFS 2.0.0 implementation.
36     *
37     * @param url the feature source URL
38     * @return the WFS version
39     */
40    public static String getWFSVersion(String url) {
41      LinkedCaseInsensitiveMap<String> wfsUrlParams = new LinkedCaseInsensitiveMap<>();
42      wfsUrlParams.putAll(
43          UriComponentsBuilder.fromUriString(url).build().getQueryParams().toSingleValueMap());
44      String version = wfsUrlParams.get("VERSION");
45      // Set.of() throws a NullPointerException for contains(null)
46      return version != null && SUPPORTED_WFS_VERSIONS.contains(version)
47          ? version
48          : SimpleWFSHelper.DEFAULT_WFS_VERSION;
49    }
50  
51    @Override
52    public DataStore createDataStore(TMFeatureSource tmfs, Integer timeout) throws IOException {
53      Map<String, Object> params = new HashMap<>();
54      params.put(WFSDataStoreFactory.ENTITY_RESOLVER.key, PreventLocalEntityResolver.INSTANCE);
55  
56      if (timeout != null) {
57        params.put(WFSDataStoreFactory.TIMEOUT.key, timeout);
58      }
59  
60      params.put(
61          WFSDataStoreFactory.URL.key,
62          SimpleWFSHelper.getWFSRequestURL(tmfs.getUrl(), "GetCapabilities", getWFSVersion(tmfs.getUrl()), null)
63              .toURL());
64  
65      ServiceAuthentication authentication = tmfs.getAuthentication();
66      if (authentication != null) {
67        if (authentication.getMethod() != ServiceAuthentication.MethodEnum.PASSWORD) {
68          throw new IllegalArgumentException(authentication.getMethod().getValue());
69        }
70        params.put(WFSDataStoreFactory.USERNAME.key, authentication.getUsername());
71        params.put(WFSDataStoreFactory.PASSWORD.key, authentication.getPassword());
72      }
73      // Use a factory with a WFS client that lets a WFS 2.0.0 server sort, see SortingWFSClient
74      return openDatastore(params, WFSDataStoreFactory.PASSWORD.key, new SortingWFSDataStoreFactory());
75    }
76  
77    @Override
78    protected TMFeatureTypeInfo getFeatureTypeInfo(TMFeatureType pft, ResourceInfo info, SimpleFeatureSource gtFs) {
79      TMFeatureTypeInfo tmInfo = super.getFeatureTypeInfo(pft, info, gtFs);
80      if (info instanceof FeatureTypeInfo ftInfo) {
81        tmInfo.schema(info.getSchema())
82            .wgs84BoundingBox(GeoToolsHelper.fromEnvelope(ftInfo.getWGS84BoundingBox()))
83            .defaultSrs(ftInfo.getDefaultSRS())
84            .otherSrs(Set.copyOf(ftInfo.getOtherSRS()))
85            .outputFormats(ftInfo.getOutputFormats())
86            .abstractText(ftInfo.getAbstract());
87      }
88      return tmInfo;
89    }
90  }