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.springframework.util.LinkedCaseInsensitiveMap;
18  import org.springframework.web.util.UriComponentsBuilder;
19  import org.tailormap.api.geotools.wfs.SimpleWFSHelper;
20  import org.tailormap.api.persistence.TMFeatureSource;
21  import org.tailormap.api.persistence.TMFeatureType;
22  import org.tailormap.api.persistence.helper.GeoToolsHelper;
23  import org.tailormap.api.persistence.json.ServiceAuthentication;
24  import org.tailormap.api.persistence.json.TMFeatureTypeInfo;
25  
26  public class WFSFeatureSourceHelper extends FeatureSourceHelper {
27    @Override
28    public DataStore createDataStore(TMFeatureSource tmfs, Integer timeout) throws IOException {
29      Map<String, Object> params = new HashMap<>();
30      if (timeout != null) {
31        params.put(WFSDataStoreFactory.TIMEOUT.key, timeout);
32      }
33  
34      LinkedCaseInsensitiveMap<String> wfsUrlParams = new LinkedCaseInsensitiveMap<>();
35      wfsUrlParams.putAll(
36          UriComponentsBuilder.fromUriString(tmfs.getUrl())
37              .build()
38              .getQueryParams()
39              .toSingleValueMap());
40      String version = wfsUrlParams.get("VERSION");
41      if (!"2.0.0".equals(version)) {
42        version = SimpleWFSHelper.DEFAULT_WFS_VERSION;
43      }
44      params.put(
45          WFSDataStoreFactory.URL.key,
46          SimpleWFSHelper.getWFSRequestURL(tmfs.getUrl(), "GetCapabilities", version, null).toURL());
47  
48      ServiceAuthentication authentication = tmfs.getAuthentication();
49      if (authentication != null) {
50        if (authentication.getMethod() != ServiceAuthentication.MethodEnum.PASSWORD) {
51          throw new IllegalArgumentException(authentication.getMethod().getValue());
52        }
53        params.put(WFSDataStoreFactory.USERNAME.key, authentication.getUsername());
54        params.put(WFSDataStoreFactory.PASSWORD.key, authentication.getPassword());
55      }
56      return openDatastore(params, WFSDataStoreFactory.PASSWORD.key);
57    }
58  
59    @Override
60    protected TMFeatureTypeInfo getFeatureTypeInfo(
61        TMFeatureType pft, ResourceInfo info, SimpleFeatureSource gtFs) {
62      TMFeatureTypeInfo tmInfo = super.getFeatureTypeInfo(pft, info, gtFs);
63      if (info instanceof FeatureTypeInfo ftInfo) {
64        tmInfo
65            .schema(info.getSchema())
66            .wgs84BoundingBox(GeoToolsHelper.fromEnvelope(ftInfo.getWGS84BoundingBox()))
67            .defaultSrs(ftInfo.getDefaultSRS())
68            .otherSrs(Set.copyOf(ftInfo.getOtherSRS()))
69            .outputFormats(ftInfo.getOutputFormats())
70            .abstractText(ftInfo.getAbstract());
71      }
72      return tmInfo;
73    }
74  }