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();
}
}
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());
}
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;
}
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;
}
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);
}
Aggregations