use of ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType in project ddf by codice.
the class TestWfsSource method testQueryTwoFeaturesOneInvalid.
@Test
public void testQueryTwoFeaturesOneInvalid() throws UnsupportedQueryException, WfsException, SecurityServiceException {
setUp(TWO_TEXT_PROPERTY_SCHEMA, null, null, TWO_FEATURES, null);
Filter orderPersonFilter = builder.attribute(EXT_PREFIX + sampleFeatures.get(0) + "." + ORDER_PERSON).is().like().text(LITERAL);
Filter mctFeature1Fitler = builder.attribute(Metacard.CONTENT_TYPE).is().like().text(sampleFeatures.get(0).getLocalPart());
Filter feature1Filter = builder.allOf(Arrays.asList(orderPersonFilter, mctFeature1Fitler));
Filter orderDogFilter = builder.attribute("FAKE").is().like().text(LITERAL);
Filter mctFeature2Fitler = builder.attribute(Metacard.CONTENT_TYPE).is().like().text(sampleFeatures.get(1).getLocalPart());
Filter feature2Filter = builder.allOf(Arrays.asList(orderDogFilter, mctFeature2Fitler));
Filter totalFilter = builder.anyOf(Arrays.asList(feature1Filter, feature2Filter));
QueryImpl inQuery = new QueryImpl(totalFilter);
inQuery.setPageSize(MAX_FEATURES);
ArgumentCaptor<GetFeatureType> captor = ArgumentCaptor.forClass(GetFeatureType.class);
source.query(new QueryRequestImpl(inQuery));
verify(mockWfs).getFeature(captor.capture());
GetFeatureType getFeatureType = captor.getValue();
assertMaxFeatures(getFeatureType, inQuery);
assertEquals(ONE_FEATURE.intValue(), getFeatureType.getQuery().size());
QueryType query = getFeatureType.getQuery().get(0);
assertTrue(query.getTypeName().equals(sampleFeatures.get(0)));
// The Text Properties should be ORed
assertTrue(query.getFilter().isSetComparisonOps());
assertTrue(query.getFilter().getComparisonOps().getValue() instanceof PropertyIsLikeType);
PropertyIsLikeType pilt = (PropertyIsLikeType) query.getFilter().getComparisonOps().getValue();
assertEquals(ORDER_PERSON, pilt.getPropertyName().getContent());
}
use of ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType in project ddf by codice.
the class TestWfsSource method setUp.
public void setUp(final String schema, final List<Object> supportedGeos, final String srsName, final Integer numFeatures, final Integer numResults) throws WfsException, SecurityServiceException {
mockFactory = mock(SecureCxfClientFactory.class);
when(mockFactory.getClient()).thenReturn(mockWfs);
// GetCapabilities Response
when(mockWfs.getCapabilities(any(GetCapabilitiesRequest.class))).thenReturn(mockCapabilites);
mockCapabilites.setFilterCapabilities(new FilterCapabilities());
mockCapabilites.getFilterCapabilities().setSpatialCapabilities(new SpatialCapabilitiesType());
mockCapabilites.getFilterCapabilities().getSpatialCapabilities().setSpatialOperators(new SpatialOperatorsType());
if (null != supportedGeos && !supportedGeos.isEmpty()) {
mockCapabilites.getFilterCapabilities().getSpatialCapabilities().getSpatialOperators().getBBOXOrEqualsOrDisjoint().addAll(supportedGeos);
}
// DescribeFeatureType Response
XmlSchema xmlSchema = null;
if (null != schema) {
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
wfsUriResolver.setGmlNamespace(Wfs10Constants.GML_NAMESPACE);
wfsUriResolver.setWfsNamespace(Wfs10Constants.WFS_NAMESPACE);
schemaCollection.setSchemaResolver(wfsUriResolver);
xmlSchema = schemaCollection.read(new StreamSource(new ByteArrayInputStream(schema.getBytes())));
}
when(mockWfs.describeFeatureType(any(DescribeFeatureTypeRequest.class))).thenReturn(xmlSchema);
sampleFeatures = new ArrayList<>();
mockCapabilites.setFeatureTypeList(new FeatureTypeListType());
if (numFeatures != null) {
for (int ii = 0; ii < numFeatures; ii++) {
FeatureTypeType feature = new FeatureTypeType();
QName qName;
if (ii == 0) {
qName = new QName("SampleFeature" + ii);
} else {
qName = new QName("http://example.com", "SampleFeature" + ii, "Prefix" + ii);
}
sampleFeatures.add(qName);
feature.setName(qName);
// feature.setName(SAMPLE_FEATURE[ii]);
if (null != srsName) {
feature.setSRS(srsName);
}
mockCapabilites.getFeatureTypeList().getFeatureType().add(feature);
}
}
// GetFeature Response
when(mockWfs.getFeature(any(GetFeatureType.class))).thenReturn(mockFeatureCollection);
when(mockFeatureCollection.getFeatureMembers()).thenAnswer(new Answer<List<Metacard>>() {
@Override
public List<Metacard> answer(InvocationOnMock invocation) {
// Create as many metacards as there are features
Integer resultsToReturn = numResults;
if (resultsToReturn == null && numFeatures != null) {
resultsToReturn = numFeatures;
}
List<Metacard> metacards = new ArrayList<Metacard>(resultsToReturn);
for (int i = 0; i < resultsToReturn; i++) {
MetacardImpl mc = new MetacardImpl();
mc.setId("ID_" + String.valueOf(i + 1));
metacards.add(mc);
}
return metacards;
}
});
when(mockAvailabilityTask.isAvailable()).thenReturn(true);
source = new WfsSource(new GeotoolsFilterAdapterImpl(), mockContext, mockAvailabilityTask, mockFactory, encryptionService);
}
use of ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType in project ddf by codice.
the class WfsSource method query.
@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
Wfs 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() + "]");
}
SourceResponseImpl simpleResponse = null;
// WFS v1.0 specification does not support response indicating total
// number
// of features satisfying query constraints.
// Hence, we save off the original
// page size from the query request and create a copy of the query,
// changing
// the page size by a multiplier and the current page number of results
// so that
// more features are returned as the user pages through the results,
// getting
// a better sense of how many total features exist that satisfy the
// query.
int origPageSize = query.getPageSize();
if (origPageSize <= 0 || origPageSize > WFS_MAX_FEATURES_RETURNED) {
origPageSize = WFS_MAX_FEATURES_RETURNED;
}
QueryImpl modifiedQuery = new QueryImpl(query);
// Determine current page number of results being requested.
// Example: startIndex = 21 and origPageSize=10, then requesting to go
// to page number 3.
// Note: Integer division will truncate remainders so 4 / 2 will return 0 and not .5. Also,
// pages are numbered 1 - N so we add 1 to the result
int pageNumber = query.getStartIndex() / origPageSize + 1;
// Modified page size is based on current page number and a constant
// multiplier,
// but limited to a max value to prevent time consuming queries just to
// get an
// approximation of total number of features.
// So as page number increases the pageSize increases.
// Example:
// pageNumber=2, modifiedPageSize=60
// pageNumber=3, modifiedPageSize=90
int modifiedPageSize = Math.min(pageNumber * origPageSize * WFS_QUERY_PAGE_SIZE_MULTIPLIER, WFS_MAX_FEATURES_RETURNED);
LOGGER.debug("WFS Source {}: modified page size = {}", getId(), modifiedPageSize);
modifiedQuery.setPageSize(modifiedPageSize);
GetFeatureType getFeature = buildGetFeatureRequest(modifiedQuery);
try {
LOGGER.debug("WFS Source {}: Sending query ...", getId());
WfsFeatureCollection featureCollection = wfs.getFeature(getFeature);
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());
// 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());
List<Result> results = new ArrayList<Result>(numberOfResultsToReturn);
int stopIndex = Math.min((origPageSize * pageNumber) + query.getStartIndex(), featureCollection.getFeatureMembers().size() + 1);
LOGGER.debug("WFS Source {}: startIndex = {}, stopIndex = {}, origPageSize = {}, pageNumber = {}", getId(), query.getStartIndex(), stopIndex, origPageSize, pageNumber);
for (int i = query.getStartIndex(); i < stopIndex; i++) {
Metacard mc = featureCollection.getFeatureMembers().get(i - 1);
mc = transform(mc, DEFAULT_WFS_TRANSFORMER_ID);
Result result = new ResultImpl(mc);
results.add(result);
debugResult(result);
}
Long totalHits = (long) featureCollection.getFeatureMembers().size();
simpleResponse = 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);
}
return simpleResponse;
}
use of ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType in project ddf by codice.
the class WfsSource method buildGetFeatureRequest.
private GetFeatureType buildGetFeatureRequest(Query query) throws UnsupportedQueryException {
List<ContentType> contentTypes = getContentTypesFromQuery(query);
List<QueryType> queries = new ArrayList<QueryType>();
for (Entry<QName, WfsFilterDelegate> filterDelegateEntry : featureTypeFilters.entrySet()) {
if (contentTypes.isEmpty() || isFeatureTypeInQuery(contentTypes, filterDelegateEntry.getKey().getLocalPart())) {
QueryType wfsQuery = new QueryType();
wfsQuery.setTypeName(filterDelegateEntry.getKey());
FilterType filter = filterAdapter.adapt(query, filterDelegateEntry.getValue());
if (filter != null) {
if (areAnyFiltersSet(filter)) {
wfsQuery.setFilter(filter);
}
queries.add(wfsQuery);
} else {
LOGGER.debug("WFS Source {}: {} has an invalid filter.", getId(), filterDelegateEntry.getKey());
}
}
}
if (queries != null && !queries.isEmpty()) {
GetFeatureType getFeatureType = new GetFeatureType();
getFeatureType.setMaxFeatures(BigInteger.valueOf(query.getPageSize()));
getFeatureType.getQuery().addAll(queries);
getFeatureType.setService(Wfs10Constants.WFS);
getFeatureType.setVersion(Wfs10Constants.VERSION_1_0_0);
logMessage(getFeatureType);
return getFeatureType;
} else {
throw new UnsupportedQueryException("Unable to build query. No filters could be created from query criteria.");
}
}
use of ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType in project ddf by codice.
the class TestWfsSource method testGmlImport.
@Test
public void testGmlImport() throws UnsupportedQueryException, WfsException, SecurityServiceException {
List<Object> bbox = new ArrayList<Object>();
bbox.add(new BBOX());
setUp(GML_IMPORT_SCHEMA, bbox, SRS_NAME, ONE_FEATURE, null);
Filter intersectFilter = builder.attribute(Metacard.ANY_GEO).is().intersecting().wkt(POLYGON_WKT);
QueryImpl intersectQuery = new QueryImpl(intersectFilter);
intersectQuery.setPageSize(MAX_FEATURES);
ArgumentCaptor<GetFeatureType> captor = ArgumentCaptor.forClass(GetFeatureType.class);
source.query(new QueryRequestImpl(intersectQuery));
verify(mockWfs).getFeature(captor.capture());
GetFeatureType getFeatureType = captor.getValue();
assertMaxFeatures(getFeatureType, intersectQuery);
assertTrue(getFeatureType.getQuery().size() == ONE_FEATURE);
QueryType query = getFeatureType.getQuery().get(0);
assertTrue(query.getTypeName().equals(sampleFeatures.get(0)));
assertTrue(query.getFilter().isSetSpatialOps());
assertTrue(query.getFilter().getSpatialOps().getValue() instanceof SpatialOpsType);
}
Aggregations