Search in sources :

Example 1 with WfsFeatureCollection

use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection in project ddf by codice.

the class FeatureCollectionConverterWfs10 method marshal.

@Override
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
    WfsFeatureCollection wfc = (WfsFeatureCollection) value;
    String schemaLoc = generateSchemaLocationFromMetacards(wfc.getFeatureMembers(), prefixToUriMapping);
    for (Entry<String, String> entry : prefixToUriMapping.entrySet()) {
        writer.addAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + entry.getKey(), entry.getValue());
    }
    writer.addAttribute(WfsConstants.ATTRIBUTE_SCHEMA_LOCATION, schemaLoc);
    Geometry allGeometry = getBounds(wfc.getFeatureMembers());
    if (!allGeometry.isEmpty()) {
        XmlNode.writeEnvelope(WfsConstants.GML_PREFIX + ":" + "boundedBy", context, writer, allGeometry.getEnvelopeInternal());
    }
    for (Metacard mc : wfc.getFeatureMembers()) {
        writer.startNode(WfsConstants.GML_PREFIX + ":" + featureMember);
        context.convertAnother(mc);
        writer.endNode();
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Metacard(ddf.catalog.data.Metacard) WfsFeatureCollection(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection)

Example 2 with WfsFeatureCollection

use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection in project ddf by codice.

the class TestGenericFeatureConverter method testUnmarshalFeatureCollectionUnsupportedProjectionXmlToObject.

@Test
public void testUnmarshalFeatureCollectionUnsupportedProjectionXmlToObject() {
    XStream xstream = new XStream(new WstxDriver());
    FeatureCollectionConverterWfs10 fcConverter = new FeatureCollectionConverterWfs10();
    Map<String, FeatureConverter> fcMap = new HashMap<>();
    GenericFeatureConverter converter = new GenericFeatureConverter("CUSTOM UNSUPPORTED PROJECTION");
    fcMap.put("video_data_set", converter);
    fcConverter.setFeatureConverterMap(fcMap);
    xstream.registerConverter(fcConverter);
    converter.setMetacardType(buildMetacardType());
    xstream.registerConverter(converter);
    xstream.registerConverter(new GmlGeometryConverter());
    xstream.alias("FeatureCollection", WfsFeatureCollection.class);
    InputStream is = TestGenericFeatureConverter.class.getResourceAsStream("/video_data_set_2.xml");
    WfsFeatureCollection wfc = (WfsFeatureCollection) xstream.fromXML(is);
    assertThat(wfc.getFeatureMembers(), hasSize(1));
    Metacard mc = wfc.getFeatureMembers().get(0);
    assertThat(mc.getId(), is("video_data_set.1"));
    assertThat(mc.getLocation(), nullValue());
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) Metacard(ddf.catalog.data.Metacard) GmlGeometryConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GmlGeometryConverter) HashMap(java.util.HashMap) GenericFeatureConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GenericFeatureConverter) XStream(com.thoughtworks.xstream.XStream) InputStream(java.io.InputStream) WfsFeatureCollection(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection) FeatureConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.FeatureConverter) GenericFeatureConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GenericFeatureConverter) Test(org.junit.Test)

Example 3 with WfsFeatureCollection

use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection in project ddf by codice.

the class FeatureCollectionMessageBodyReaderWfs10 method readFrom.

@Override
public WfsFeatureCollection readFrom(Class<WfsFeatureCollection> clazz, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream inStream) throws IOException, WebApplicationException {
    // Save original input stream for any exception message that might need to be
    // created
    String originalInputStream = IOUtils.toString(inStream, StandardCharsets.UTF_8.name());
    // Re-create the input stream (since it has already been read for potential
    // exception message creation)
    inStream = new ByteArrayInputStream(originalInputStream.getBytes(StandardCharsets.UTF_8.name()));
    WfsFeatureCollection featureCollection = null;
    try {
        featureCollection = (WfsFeatureCollection) xstream.fromXML(inStream);
    } catch (XStreamException e) {
        // If a ServiceExceptionReport is sent from the remote WFS site it will be sent with an
        // JAX-RS "OK" status, hence the ErrorResponse exception mapper will not fire.
        // Instead the ServiceExceptionReport will come here and be treated like a GetFeature
        // response, resulting in an XStreamException since ExceptionReport cannot be
        // unmarshalled. So this catch clause is responsible for catching that XStream
        // exception and creating a JAX-RS response containing the original stream
        // (with the ExceptionReport) and rethrowing it as a WebApplicationException,
        // which CXF will wrap as a ClientException that the WfsSource catches, converts
        // to a WfsException, and logs.
        LOGGER.debug("Exception unmarshalling", e);
        ByteArrayInputStream bis = new ByteArrayInputStream(originalInputStream.getBytes(StandardCharsets.UTF_8));
        ResponseBuilder responseBuilder = Response.ok(bis);
        responseBuilder.type("text/xml");
        Response response = responseBuilder.build();
        throw new WebApplicationException(e, response);
    } finally {
        IOUtils.closeQuietly(inStream);
    }
    return featureCollection;
}
Also used : Response(javax.ws.rs.core.Response) XStreamException(com.thoughtworks.xstream.XStreamException) WebApplicationException(javax.ws.rs.WebApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) WfsFeatureCollection(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 4 with WfsFeatureCollection

use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection 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 5 with WfsFeatureCollection

use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection in project ddf by codice.

the class TestGenericFeatureConverter method testMetacardCollectionToFeatureCollectionXml.

@Test
public void testMetacardCollectionToFeatureCollectionXml() {
    XStream xstream = new XStream(new EnhancedStaxDriver());
    xstream.setMode(XStream.NO_REFERENCES);
    xstream.registerConverter(new FeatureCollectionConverterWfs10());
    xstream.registerConverter(new GenericFeatureConverter());
    xstream.registerConverter(new GmlGeometryConverter());
    // Required the Implementing class. The interface would not work...
    xstream.alias("wfs:FeatureCollection", WfsFeatureCollection.class);
    Metacard mc = new SampleMetacard().getMetacard();
    WfsFeatureCollection wfc = new WfsFeatureCollection();
    wfc.getFeatureMembers().add(mc);
    MetacardImpl mc2 = new SampleMetacard().getMetacard();
    // Ignore the hack stuff, this was just to imitate having two different
    // "MetacardTypes"
    mc2.setType(new MetacardType() {

        @Override
        public String getName() {
            return "otherType";
        }

        @Override
        public Set<AttributeDescriptor> getAttributeDescriptors() {
            return BasicTypes.BASIC_METACARD.getAttributeDescriptors();
        }

        @Override
        public AttributeDescriptor getAttributeDescriptor(String arg0) {
            return BasicTypes.BASIC_METACARD.getAttributeDescriptor(arg0);
        }
    });
    wfc.getFeatureMembers().add(mc2);
    xstream.toXML(wfc);
}
Also used : Set(java.util.Set) GmlGeometryConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GmlGeometryConverter) XStream(com.thoughtworks.xstream.XStream) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MetacardType(ddf.catalog.data.MetacardType) FeatureMetacardType(org.codice.ddf.spatial.ogc.wfs.catalog.common.FeatureMetacardType) Metacard(ddf.catalog.data.Metacard) GenericFeatureConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GenericFeatureConverter) EnhancedStaxDriver(org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.EnhancedStaxDriver) WfsFeatureCollection(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection) Test(org.junit.Test)

Aggregations

WfsFeatureCollection (org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection)8 Metacard (ddf.catalog.data.Metacard)6 XStream (com.thoughtworks.xstream.XStream)4 GenericFeatureConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GenericFeatureConverter)4 GmlGeometryConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GmlGeometryConverter)4 Test (org.junit.Test)4 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 FeatureConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.FeatureConverter)3 WebApplicationException (javax.ws.rs.WebApplicationException)2 XStreamException (com.thoughtworks.xstream.XStreamException)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)1 MetacardType (ddf.catalog.data.MetacardType)1 Result (ddf.catalog.data.Result)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 ResultImpl (ddf.catalog.data.impl.ResultImpl)1 Query (ddf.catalog.operation.Query)1 QueryImpl (ddf.catalog.operation.impl.QueryImpl)1