use of org.codice.ddf.spatial.ogc.wfs.catalog.WfsFeatureCollection in project ddf by codice.
the class WfsSource method query.
@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
ExtendedWfs wfs = factory.getClient();
Query query = request.getQuery();
LOGGER.debug("WFS Source {}: Received query: \n{}", getId(), query);
if (query.getStartIndex() < 1) {
throw new UnsupportedQueryException("Start Index is one-based and must be an integer greater than 0; should not be [" + query.getStartIndex() + "]");
}
Query modifiedQuery;
int origPageSize = query.getPageSize();
if (origPageSize <= 0 || origPageSize > WFS_MAX_FEATURES_RETURNED) {
origPageSize = WFS_MAX_FEATURES_RETURNED;
}
int pageNumber = query.getStartIndex() / origPageSize + 1;
if (!supportsStartIndex) {
modifiedQuery = new QueryImpl(query, 1, Constants.DEFAULT_PAGE_SIZE, query.getSortBy(), false, 300000L);
int modifiedPageSize = Math.min(pageNumber * origPageSize, WFS_MAX_FEATURES_RETURNED);
LOGGER.debug("WFS Source {}: modified page size = {}", getId(), modifiedPageSize);
((QueryImpl) modifiedQuery).setPageSize(modifiedPageSize);
} else {
modifiedQuery = query;
}
final ExtendedGetFeatureType getHits = buildGetFeatureRequestHits(modifiedQuery);
final ExtendedGetFeatureType getResults = buildGetFeatureRequestResults(modifiedQuery);
try {
LOGGER.debug("WFS Source {}: Getting hits.", getId());
final WfsFeatureCollection hitsResponse = wfs.getFeature(getHits);
final long totalHits = hitsResponse.getNumberOfFeatures();
LOGGER.debug("The query has {} hits.", totalHits);
LOGGER.debug("WFS Source {}: Sending query ...", getId());
final WfsFeatureCollection featureCollection = wfs.getFeature(getResults);
if (featureCollection == null) {
throw new UnsupportedQueryException("Invalid results returned from server");
}
availabilityTask.updateLastAvailableTimestamp(System.currentTimeMillis());
LOGGER.debug("WFS Source {}: Received featureCollection with {} metacards.", getId(), featureCollection.getFeatureMembers().size());
LOGGER.debug("WFS Source {}: startIndex = {}, origPageSize = {}, pageNumber = {}", getId(), query.getStartIndex(), origPageSize, pageNumber);
List<Result> results;
if (supportsStartIndex) {
results = new ArrayList<>(featureCollection.getFeatureMembers().size());
for (Metacard mc : featureCollection.getFeatureMembers()) {
addTransformedResult(mc, results);
}
} else {
// Only return the number of results originally asked for in the
// query, or the entire list of results if it is smaller than the
// original page size.
int numberOfResultsToReturn = Math.min(origPageSize, featureCollection.getFeatureMembers().size());
results = new ArrayList<>(numberOfResultsToReturn);
int stopIndex = Math.min(origPageSize + query.getStartIndex(), featureCollection.getFeatureMembers().size() + 1);
LOGGER.debug("WFS Source {}: stopIndex = {}", getId(), stopIndex);
for (int i = query.getStartIndex(); i < stopIndex; i++) {
Metacard mc = featureCollection.getFeatureMembers().get(i - 1);
addTransformedResult(mc, results);
}
}
return new SourceResponseImpl(request, results, totalHits);
} catch (WfsException wfse) {
LOGGER.debug(WFS_ERROR_MESSAGE, wfse);
throw new UnsupportedQueryException("Error received from WFS Server", wfse);
} catch (Exception ce) {
String msg = handleClientException(ce);
throw new UnsupportedQueryException(msg, ce);
}
}
use of org.codice.ddf.spatial.ogc.wfs.catalog.WfsFeatureCollection in project ddf by codice.
the class FeatureTransformationServiceTest method testApplyBadXML.
@Test
public void testApplyBadXML() {
InputStream inputStream = new BufferedInputStream(FeatureTransformationServiceTest.class.getResourceAsStream("/Broken.xml"));
WfsMetadata wfsMetadata = mock(WfsMetadata.class);
when(wfsMetadata.getFeatureMemberNodeNames()).thenReturn(Collections.singletonList("featureMember"));
WfsFeatureCollection wfsFeatureCollection = featureTransformationService.apply(inputStream, wfsMetadata);
ArgumentCaptor<InputStream> inputStreamArgumentCaptor = ArgumentCaptor.forClass(InputStream.class);
ArgumentCaptor<WfsMetadata> wfsMetadataArgumentCaptor = ArgumentCaptor.forClass(WfsMetadata.class);
verify(transformerList.get(0), times(0)).apply(inputStreamArgumentCaptor.capture(), wfsMetadataArgumentCaptor.capture());
assertThat(wfsFeatureCollection.getNumberOfFeatures(), is(10L));
assertThat(wfsFeatureCollection.getFeatureMembers(), hasSize(0));
}
use of org.codice.ddf.spatial.ogc.wfs.catalog.WfsFeatureCollection in project ddf by codice.
the class FeatureTransformationServiceTest method testFeatureMembersWithNoNumberOfFeaturesAttribute.
@Test
public void testFeatureMembersWithNoNumberOfFeaturesAttribute() throws Exception {
try (final InputStream inputStream = getClass().getResourceAsStream("/NeverlandNoNumberOfFeaturesAttribute.xml")) {
final WfsMetadata wfsMetadata = mock(WfsMetadata.class);
when(wfsMetadata.getFeatureMemberNodeNames()).thenReturn(Collections.singletonList("featureMember"));
final WfsFeatureCollection wfsFeatureCollection = featureTransformationService.apply(inputStream, wfsMetadata);
assertThat(wfsFeatureCollection.getNumberOfFeatures(), is(10L));
assertThat(wfsFeatureCollection.getFeatureMembers(), hasSize(10));
}
}
use of org.codice.ddf.spatial.ogc.wfs.catalog.WfsFeatureCollection in project ddf by codice.
the class FeatureTransformationServiceTest method testMultipleFeatureMemberNodeNames.
@Test
public void testMultipleFeatureMemberNodeNames() throws Exception {
try (final InputStream inputStream = getClass().getResourceAsStream("/Tasmania.xml")) {
final WfsMetadata wfsMetadata = mock(WfsMetadata.class);
when(wfsMetadata.getFeatureMemberNodeNames()).thenReturn(Arrays.asList("tasmania_water_bodies", "streams", "roads", "poi", "giant_polygon", "archsites", "bugsites", "tasmania_state_boundaries", "tiger_roads", "states", "tasmania_cities", "restricted", "poly_landmarks", "tasmania_roads"));
final WfsFeatureCollection wfsFeatureCollection = featureTransformationService.apply(inputStream, wfsMetadata);
assertThat(wfsFeatureCollection.getNumberOfFeatures(), is(2L));
assertThat(wfsFeatureCollection.getFeatureMembers(), hasSize(2));
}
}
use of org.codice.ddf.spatial.ogc.wfs.catalog.WfsFeatureCollection in project ddf by codice.
the class FeatureTransformationServiceTest method validateTenMetacards.
private void validateTenMetacards(String inputFileName, String featureNodeName) {
InputStream inputStream = new BufferedInputStream(FeatureTransformationServiceTest.class.getResourceAsStream(inputFileName));
WfsMetadata wfsMetadata = mock(WfsMetadata.class);
when(wfsMetadata.getFeatureMemberNodeNames()).thenReturn(Collections.singletonList(featureNodeName));
WfsFeatureCollection wfsFeatureCollection = featureTransformationService.apply(inputStream, wfsMetadata);
ArgumentCaptor<InputStream> inputStreamArgumentCaptor = ArgumentCaptor.forClass(InputStream.class);
ArgumentCaptor<WfsMetadata> wfsMetadataArgumentCaptor = ArgumentCaptor.forClass(WfsMetadata.class);
verify(transformerList.get(0), times(FEATURE_MEMBER_COUNT)).apply(inputStreamArgumentCaptor.capture(), wfsMetadataArgumentCaptor.capture());
for (int i = 0; i < FEATURE_MEMBER_COUNT; i++) {
assertThat(inputStreamArgumentCaptor.getAllValues().get(i), notNullValue());
assertThat(wfsMetadataArgumentCaptor.getAllValues().get(i), notNullValue());
}
assertThat(wfsFeatureCollection.getNumberOfFeatures(), is(10L));
assertThat(wfsFeatureCollection.getFeatureMembers(), hasSize(10));
}
Aggregations