Search in sources :

Example 41 with GetFeatureType

use of net.opengis.wfs.v_1_1_0.GetFeatureType in project ddf by codice.

the class TestWfsSource method testBboxQuery.

@Test
public void testBboxQuery() throws UnsupportedQueryException, WfsException, SecurityServiceException {
    List<Object> bbox = new ArrayList<Object>();
    bbox.add(new BBOX());
    setUp(ONE_GML_PROPERTY_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);
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) BBOX(ogc.schema.opengis.filter_capabilities.v_1_0_0.BBOX) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) SpatialOpsType(ogc.schema.opengis.filter.v_1_0_0.SpatialOpsType) ArrayList(java.util.ArrayList) QueryType(ogc.schema.opengis.wfs.v_1_0_0.QueryType) GetFeatureType(ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType) Test(org.junit.Test)

Example 42 with GetFeatureType

use of net.opengis.wfs.v_1_1_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());
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryType(ogc.schema.opengis.wfs.v_1_0_0.QueryType) PropertyIsLikeType(ogc.schema.opengis.filter.v_1_0_0.PropertyIsLikeType) GetFeatureType(ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType) Test(org.junit.Test)

Example 43 with GetFeatureType

use of net.opengis.wfs.v_1_1_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);
}
Also used : SecureCxfClientFactory(org.codice.ddf.cxf.SecureCxfClientFactory) SpatialCapabilitiesType(ogc.schema.opengis.filter_capabilities.v_1_0_0.SpatialCapabilitiesType) FilterCapabilities(ogc.schema.opengis.filter_capabilities.v_1_0_0.FilterCapabilities) SpatialOperatorsType(ogc.schema.opengis.filter_capabilities.v_1_0_0.SpatialOperatorsType) List(java.util.List) ArrayList(java.util.ArrayList) GetFeatureType(ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType) GetCapabilitiesRequest(org.codice.ddf.spatial.ogc.wfs.v1_0_0.catalog.common.GetCapabilitiesRequest) FeatureTypeType(ogc.schema.opengis.wfs_capabilities.v_1_0_0.FeatureTypeType) FeatureTypeListType(ogc.schema.opengis.wfs_capabilities.v_1_0_0.FeatureTypeListType) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) BigInteger(java.math.BigInteger) Metacard(ddf.catalog.data.Metacard) XmlSchema(org.apache.ws.commons.schema.XmlSchema) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DescribeFeatureTypeRequest(org.codice.ddf.spatial.ogc.wfs.v1_0_0.catalog.common.DescribeFeatureTypeRequest) GeotoolsFilterAdapterImpl(ddf.catalog.filter.proxy.adapter.GeotoolsFilterAdapterImpl)

Example 44 with GetFeatureType

use of net.opengis.wfs.v_1_1_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;
}
Also used : Query(ddf.catalog.operation.Query) Wfs(org.codice.ddf.spatial.ogc.wfs.v1_0_0.catalog.common.Wfs) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) JAXBException(javax.xml.bind.JAXBException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) WebApplicationException(javax.ws.rs.WebApplicationException) SecurityServiceException(ddf.security.service.SecurityServiceException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) WfsFeatureCollection(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection) GetFeatureType(ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType)

Example 45 with GetFeatureType

use of net.opengis.wfs.v_1_1_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.");
    }
}
Also used : FilterType(ogc.schema.opengis.filter.v_1_0_0.FilterType) ContentType(ddf.catalog.data.ContentType) QName(javax.xml.namespace.QName) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) QueryType(ogc.schema.opengis.wfs.v_1_0_0.QueryType) GetFeatureType(ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType)

Aggregations

QueryImpl (ddf.catalog.operation.impl.QueryImpl)58 Test (org.junit.Test)57 Filter (org.opengis.filter.Filter)34 GetFeatureType (net.opengis.wfs.v_2_0_0.GetFeatureType)32 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)31 QueryType (net.opengis.wfs.v_2_0_0.QueryType)21 ArrayList (java.util.ArrayList)19 GetFeatureType (ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType)19 QueryType (net.opengis.wfs.v_1_1_0.QueryType)17 Matchers.containsString (org.hamcrest.Matchers.containsString)15 SortByImpl (ddf.catalog.filter.impl.SortByImpl)12 QueryType (ogc.schema.opengis.wfs.v_1_0_0.QueryType)12 SortBy (org.opengis.filter.sort.SortBy)12 Query (ddf.catalog.operation.Query)9 QName (javax.xml.namespace.QName)9 MetacardMapper (org.codice.ddf.spatial.ogc.wfs.catalog.mapper.MetacardMapper)8 Metacard (ddf.catalog.data.Metacard)5 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)5 JAXBElement (javax.xml.bind.JAXBElement)5 SortByType (net.opengis.filter.v_2_0_0.SortByType)5