Skip to content

Commit

Permalink
Fixes #655 Request GetFeatureOfInterest is not validated with streami…
Browse files Browse the repository at this point in the history
…ng options

Add missing schema locations to validate the response. But the previous output was not invalid because the definition of schema locations in a XML document is optional and the user has to add the required schemas to the validation tool if they are not defined.
  • Loading branch information
Carsten Hollmann committed Jun 26, 2019
1 parent ad06359 commit 4bb4fae
Showing 1 changed file with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,25 @@
package org.n52.sos.encode.streaming.sos.v2;

import java.io.OutputStream;
import java.util.Collection;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.xml.stream.XMLStreamException;

import org.n52.sos.coding.CodingRepository;
import org.n52.sos.encode.EncodingValues;
import org.n52.sos.encode.XmlStreamWriter;
import org.n52.sos.encode.streaming.StreamingDataEncoder;
import org.n52.sos.exception.ows.NoApplicableCodeException;
import org.n52.sos.ogc.gml.AbstractFeature;
import org.n52.sos.ogc.gml.GmlConstants;
import org.n52.sos.ogc.om.OmConstants;
import org.n52.sos.ogc.om.features.FeatureCollection;
import org.n52.sos.ogc.om.features.SfConstants;
import org.n52.sos.ogc.om.features.samplingFeatures.AbstractSamplingFeature;
import org.n52.sos.ogc.ows.OwsExceptionReport;
import org.n52.sos.ogc.sos.Sos2Constants;
Expand All @@ -53,6 +60,8 @@
import org.n52.sos.service.Configurator;
import org.n52.sos.service.profile.Profile;
import org.n52.sos.util.CollectionHelper;
import org.n52.sos.util.SosHelper;
import org.n52.sos.util.XmlHelper;
import org.n52.sos.util.XmlOptionsHelper;
import org.n52.sos.w3c.SchemaLocation;
import org.n52.sos.w3c.W3CConstants;
Expand Down Expand Up @@ -137,13 +146,21 @@ protected GetFeatureOfInterestResponse getResponse() {
private void writeGetFeatureOfInterestResponseDoc(GetFeatureOfInterestResponse response, EncodingValues encodingValues)
throws XMLStreamException, OwsExceptionReport {
start(Sos2StreamingConstants.QN_GET_FEATURE_OF_INTEREST_RESPONSE);
namespace(W3CConstants.NS_XLINK_PREFIX, W3CConstants.NS_XLINK);
namespace(Sos2StreamingConstants.NS_SOS_PREFIX, Sos2StreamingConstants.NS_SOS_20);
namespace(SwesConstants.NS_SWES_PREFIX, SwesConstants.NS_SWES_20);
Map<String, String> namespaces = new LinkedHashMap<>();
namespaces.put(W3CConstants.NS_XLINK_PREFIX, W3CConstants.NS_XLINK);
namespaces.put(Sos2StreamingConstants.NS_SOS_PREFIX, Sos2StreamingConstants.NS_SOS_20);
namespaces.put(SwesConstants.NS_SWES_PREFIX, SwesConstants.NS_SWES_20);
namespaces.put(SfConstants.NS_SA_PREFIX, SfConstants.NS_SA);
namespaces.put(SfConstants.NS_SAMS_PREFIX, SfConstants.NS_SAMS);
namespaces.put(SfConstants.NS_SF_PREFIX, SfConstants.NS_SF);
namespaces.put(GmlConstants.NS_GML_PREFIX, GmlConstants.NS_GML_32);
for (Entry<String, String> namespace : namespaces.entrySet()) {
namespace(namespace.getKey(), namespace.getValue());
}
// get observation encoder
encodingValues.getAdditionalValues().put(HelperValues.DOCUMENT, null);
// write schemaLocation
schemaLocation(getSchemaLocation(encodingValues));
schemaLocation(getSchemaLocation(encodingValues, namespaces.values()));
writeNewLine();
if (response.isSetExtensions()) {
writeExtensions(response.getExtensions());
Expand All @@ -152,25 +169,41 @@ private void writeGetFeatureOfInterestResponseDoc(GetFeatureOfInterestResponse r
AbstractFeature feature = response.getAbstractFeature();
if (feature instanceof FeatureCollection) {
for (AbstractFeature f : (FeatureCollection) feature) {
writeFeatureMember(f, encodingValues);
if (f instanceof AbstractSamplingFeature && ((AbstractSamplingFeature) f).isSetGeometry()) {
writeFeatureMember(f, encodingValues);
} else {
writeReferencedFeatureMember(f);
}
writeNewLine();
}
} else if (feature instanceof AbstractSamplingFeature) {
writeFeatureMember(feature, encodingValues);
if (feature instanceof AbstractSamplingFeature && ((AbstractSamplingFeature) feature).isSetGeometry()) {
writeFeatureMember(feature, encodingValues);
} else {
writeReferencedFeatureMember(feature);
}
writeNewLine();
}
indent--;
end(Sos2StreamingConstants.QN_GET_FEATURE_OF_INTEREST_RESPONSE);
}

private Set<SchemaLocation> getSchemaLocation(EncodingValues encodingValue) {
private Set<SchemaLocation> getSchemaLocation(EncodingValues encodingValue, Collection<String> namespaces) {
Set<SchemaLocation> schemaLocations = Sets.newHashSet();
if (encodingValue.isSetEncoder()
&& CollectionHelper.isNotEmpty(encodingValue.getEncoder().getSchemaLocations())) {
schemaLocations.addAll(encodingValue.getEncoder().getSchemaLocations());
} else {
schemaLocations.add(Sos2Constants.SOS_GET_FEATURE_OF_INTEREST_SCHEMA_LOCATION);
}
for (String namespace : namespaces) {
schemaLocations.addAll(CodingRepository.getInstance().getSchemaLocation(namespace));
}
Profile activeProfile = getActiveProfile();
if (activeProfile.isSetEncodeFeatureOfInterestNamespace()) {
schemaLocations.addAll(CodingRepository.getInstance()
.getSchemaLocation(activeProfile.getEncodingNamespaceForFeatureOfInterest()));
}
return schemaLocations;
}

Expand All @@ -193,6 +226,14 @@ private void writeFeatureMember(AbstractFeature af, EncodingValues encodingValue
indent++;
}

private void writeReferencedFeatureMember(AbstractFeature af) throws XMLStreamException {
empty(Sos2StreamingConstants.QN_FEATURE_MEMBER);
addXlinkHrefAttr(af.getIdentifier());
if (af.isSetName()) {
addXlinkTitleAttr(af.getFirstName().getValue());
}
}

protected Profile getActiveProfile() {
return Configurator.getInstance().getProfileHandler().getActiveProfile();
}
Expand Down

0 comments on commit 4bb4fae

Please sign in to comment.