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