Skip to content

Commit

Permalink
fix: download linked objects in a WFS using "resolvedepth"
Browse files Browse the repository at this point in the history
Using "resolvedepth" when trying to download data via WFS is now downloading the linked objects.
As the requested hits to a WFS is better to be setup to UNKNOWN_SIZE, so the size comes as well to be always UNKNOWN_SIZE, the code related to hits and size has been removed and the related methods have been changed accordingly.

ING-4128
  • Loading branch information
emanuelaepure10 authored and stempler committed May 22, 2024
1 parent 3149a02 commit 5519274
Showing 1 changed file with 19 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import eu.esdihumboldt.hale.common.instance.model.InstanceCollection;
import eu.esdihumboldt.hale.common.instance.model.InstanceReference;
import eu.esdihumboldt.hale.common.instance.model.InstanceResolver;
import eu.esdihumboldt.hale.common.instance.model.ResourceIterator;
import eu.esdihumboldt.hale.common.instance.model.ext.InstanceIterator;
import eu.esdihumboldt.hale.common.instance.model.impl.FilteredInstanceCollection;
import eu.esdihumboldt.hale.common.instance.model.impl.IndexInstanceReference;
Expand Down Expand Up @@ -101,7 +102,8 @@ public class WfsBackedGmlInstanceCollection implements InstanceCollection {
// Number of features to retrieve at most with one WFS GetFeature request
private final int featuresPerRequest;

private final int size;
private boolean emptyInitialized = false;
private boolean empty = false;

// Parameters needed for instantiation of GmlInstanceCollection
private final TypeIndex sourceSchema;
Expand Down Expand Up @@ -225,41 +227,6 @@ public WfsBackedGmlInstanceCollection(LocatableInputSupplier<? extends InputStre
}
}

// Use primordial URI and issue "hits" request to check if the WFS will
// return anything at all
int hits;
if (ignoreNumberMatched) {
hits = UNKNOWN_SIZE;
}
else {
try {
hits = requestHits(primordialUri);
} catch (WFSException e) {
log.debug(MessageFormat.format(
"Failed to perform hits query (REQUESTTYPE=hits): {0}", e.getMessage()), e);
hits = UNKNOWN_SIZE;
}
}

switch (wfsVersion) {
case "1.1.0":
// The "numberOfFeatures" reported by a 1.1.0 WFS may be smaller
// than the actual number of features matches by the query if the
// number of features returned per query is limited on the server
// side. Therefore do not rely on it as a size information here.
this.size = UNKNOWN_SIZE;
break;
case "2.0.0":
case "2.0.2":
// The "numberMatched" reported by a 2.0.0/2.0.2 WFS should be
// number of features matched by the query. If hits equals
// UNKNOWN_SIZE then size is also set to that value
this.size = isLimited() ? Math.min(maxNumberOfFeatures, hits) : hits;
break;
default:
this.size = UNKNOWN_SIZE;
}

if (featuresPerRequest != UNLIMITED && featuresPerRequest <= 0) {
throw new IllegalArgumentException(MessageFormat.format(
"featuresPerRequest must be a positive integer or {0} to disable pagination",
Expand Down Expand Up @@ -288,15 +255,15 @@ public void setMaxNumberOfFeatures(int maxNumberOfFeatures) {
*/
@Override
public boolean hasSize() {
return size != UNKNOWN_SIZE;
return false;
}

/**
* @see InstanceCollection#size()
*/
@Override
public int size() {
return size;
return UNKNOWN_SIZE;
}

/**
Expand All @@ -312,7 +279,18 @@ public WfsBackedGmlInstanceIterator iterator() {
*/
@Override
public boolean isEmpty() {
return size == 0;
if (!emptyInitialized) {
ResourceIterator<Instance> it = iterator();
try {
empty = !it.hasNext();
} finally {
it.close();
}

emptyInitialized = true;
}

return empty;
}

/**
Expand Down Expand Up @@ -372,21 +350,6 @@ public Instance getInstance(InstanceReference reference) {
}
}

private int requestHits(URI requestUri) throws WFSException {
URIBuilder builder = new URIBuilder(requestUri);
builder.addParameter("RESULTTYPE", "hits");

InputStream in;
try {
in = builder.build().toURL().openStream();
} catch (IOException | URISyntaxException e) {
throw new WFSException(
MessageFormat.format("Unable to execute WFS request: {0}", e.getMessage()), e);
}

return FeatureCollectionHelper.getNumberOfFeatures(in);
}

private String getMaxFeaturesParameterName(String version) {
// XXX Use WFSVersion
switch (version) {
Expand Down Expand Up @@ -556,9 +519,8 @@ public boolean hasNext() {
* number of results reported by the WFS.
*/
protected boolean isFeatureLimitReached() {
return (maxNumberOfFeatures != UNLIMITED
&& totalFeaturesProcessed >= maxNumberOfFeatures)
|| (size != UNKNOWN_SIZE && totalFeaturesProcessed >= size);
return maxNumberOfFeatures != UNLIMITED
&& totalFeaturesProcessed >= maxNumberOfFeatures;
}

/**
Expand Down

0 comments on commit 5519274

Please sign in to comment.