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(UriComponentsBuilder.fromUriString(tmfs.getUrl())
36          .build()
37          .getQueryParams()
38          .toSingleValueMap());
39      String version = wfsUrlParams.get("VERSION");
40      if (!"2.0.0".equals(version)) {
41        version = SimpleWFSHelper.DEFAULT_WFS_VERSION;
42      }
43      params.put(
44          WFSDataStoreFactory.URL.key,
45          SimpleWFSHelper.getWFSRequestURL(tmfs.getUrl(), "GetCapabilities", version, null)
46              .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(TMFeatureType pft, ResourceInfo info, SimpleFeatureSource gtFs) {
61      TMFeatureTypeInfo tmInfo = super.getFeatureTypeInfo(pft, info, gtFs);
62      if (info instanceof FeatureTypeInfo ftInfo) {
63        tmInfo.schema(info.getSchema())
64            .wgs84BoundingBox(GeoToolsHelper.fromEnvelope(ftInfo.getWGS84BoundingBox()))
65            .defaultSrs(ftInfo.getDefaultSRS())
66            .otherSrs(Set.copyOf(ftInfo.getOtherSRS()))
67            .outputFormats(ftInfo.getOutputFormats())
68            .abstractText(ftInfo.getAbstract());
69      }
70      return tmInfo;
71    }
72  }